Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Coin/Coin.cpp@26:4253656c0755, 2019-05-03 (annotated)
- Committer:
- lewisgw
- Date:
- Fri May 03 09:33:34 2019 +0000
- Revision:
- 26:4253656c0755
- Parent:
- 21:20478f086bc2
- Child:
- 29:bdc4138b5171
Now a dynamic fire at the bottom of the screen that gets higher as the player collects more coins. It will kill the skater if they collide.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lewisgw | 15:876c047a6ec9 | 1 | #include "Coin.h" |
lewisgw | 15:876c047a6ec9 | 2 | |
lewisgw | 15:876c047a6ec9 | 3 | // Define sprite arrays. |
lewisgw | 15:876c047a6ec9 | 4 | int coin_front[5][5] = { |
lewisgw | 15:876c047a6ec9 | 5 | { 0,0,1,0,0 }, |
lewisgw | 15:876c047a6ec9 | 6 | { 0,1,0,1,0 }, |
lewisgw | 15:876c047a6ec9 | 7 | { 1,0,1,0,1 }, |
lewisgw | 15:876c047a6ec9 | 8 | { 0,1,0,1,0 }, |
lewisgw | 15:876c047a6ec9 | 9 | { 0,0,1,0,0 }, |
lewisgw | 15:876c047a6ec9 | 10 | }; |
lewisgw | 15:876c047a6ec9 | 11 | |
lewisgw | 15:876c047a6ec9 | 12 | int coin_side[5][5] = { |
lewisgw | 15:876c047a6ec9 | 13 | { 0,0,1,0,0 }, |
lewisgw | 15:876c047a6ec9 | 14 | { 0,0,1,0,0 }, |
lewisgw | 15:876c047a6ec9 | 15 | { 0,0,1,0,0 }, |
lewisgw | 15:876c047a6ec9 | 16 | { 0,0,1,0,0 }, |
lewisgw | 15:876c047a6ec9 | 17 | { 0,0,1,0,0 }, |
lewisgw | 15:876c047a6ec9 | 18 | }; |
lewisgw | 15:876c047a6ec9 | 19 | |
lewisgw | 15:876c047a6ec9 | 20 | // Constructor and destructor. |
lewisgw | 15:876c047a6ec9 | 21 | Coin::Coin() {} |
lewisgw | 15:876c047a6ec9 | 22 | |
lewisgw | 15:876c047a6ec9 | 23 | Coin::~Coin() {} |
lewisgw | 15:876c047a6ec9 | 24 | |
lewisgw | 15:876c047a6ec9 | 25 | void Coin::init() { |
lewisgw | 26:4253656c0755 | 26 | // Starting position of the coin. |
lewisgw | 16:331be5c7ed80 | 27 | _x = 20; |
lewisgw | 15:876c047a6ec9 | 28 | _y = 33; |
lewisgw | 15:876c047a6ec9 | 29 | _coin_counter = 0; |
lewisgw | 15:876c047a6ec9 | 30 | } |
lewisgw | 15:876c047a6ec9 | 31 | |
lewisgw | 15:876c047a6ec9 | 32 | void Coin::generate_coin() { |
lewisgw | 21:20478f086bc2 | 33 | // Toggle the state of the coin every 3 loop iterations so it looks like it is |
lewisgw | 21:20478f086bc2 | 34 | // rotating. |
lewisgw | 15:876c047a6ec9 | 35 | if (_coin_counter == 2) { |
lewisgw | 15:876c047a6ec9 | 36 | _coin_counter = 0; |
lewisgw | 15:876c047a6ec9 | 37 | _rotate_coin = !_rotate_coin; |
lewisgw | 15:876c047a6ec9 | 38 | } |
lewisgw | 15:876c047a6ec9 | 39 | _coin_counter++; |
lewisgw | 15:876c047a6ec9 | 40 | } |
lewisgw | 15:876c047a6ec9 | 41 | |
lewisgw | 21:20478f086bc2 | 42 | void Coin::set_coin(int rand_x, int rand_y) { |
lewisgw | 26:4253656c0755 | 43 | if (rand_y > 40) { |
lewisgw | 26:4253656c0755 | 44 | _y = 15; // Set the coin on the top platforms. |
lewisgw | 15:876c047a6ec9 | 45 | } else { |
lewisgw | 26:4253656c0755 | 46 | _y = 33; // Set coin on the bottom platforms. |
lewisgw | 15:876c047a6ec9 | 47 | } |
lewisgw | 15:876c047a6ec9 | 48 | _x = rand_x; |
lewisgw | 26:4253656c0755 | 49 | if (_x < 3 || _x > 78) { // Ensures the coin does not generate off-screen. |
lewisgw | 26:4253656c0755 | 50 | _x = 50; // Default value if the coin generates off-screen. |
lewisgw | 15:876c047a6ec9 | 51 | } |
lewisgw | 15:876c047a6ec9 | 52 | } |
lewisgw | 15:876c047a6ec9 | 53 | |
lewisgw | 15:876c047a6ec9 | 54 | int * Coin::get_coin_sprite() { |
lewisgw | 26:4253656c0755 | 55 | // Return different coin sprites. |
lewisgw | 15:876c047a6ec9 | 56 | if (_rotate_coin) { |
lewisgw | 15:876c047a6ec9 | 57 | return *coin_front; |
lewisgw | 15:876c047a6ec9 | 58 | } else { |
lewisgw | 15:876c047a6ec9 | 59 | return *coin_side; |
lewisgw | 15:876c047a6ec9 | 60 | } |
lewisgw | 15:876c047a6ec9 | 61 | } |
lewisgw | 15:876c047a6ec9 | 62 | |
lewisgw | 15:876c047a6ec9 | 63 | int Coin::get_coin_x() { |
lewisgw | 15:876c047a6ec9 | 64 | return _x; |
lewisgw | 15:876c047a6ec9 | 65 | } |
lewisgw | 15:876c047a6ec9 | 66 | |
lewisgw | 15:876c047a6ec9 | 67 | int Coin::get_coin_y() { |
lewisgw | 15:876c047a6ec9 | 68 | return _y; |
lewisgw | 16:331be5c7ed80 | 69 | } |
lewisgw | 21:20478f086bc2 | 70 | |
lewisgw | 21:20478f086bc2 | 71 |