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@29:bdc4138b5171, 2019-05-07 (annotated)
- Committer:
- lewisgw
- Date:
- Tue May 07 17:21:00 2019 +0000
- Revision:
- 29:bdc4138b5171
- Parent:
- 26:4253656c0755
Final Submission. I have read and agreed with the Statement of Academic Integrity.
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 | 29:bdc4138b5171 | 33 | // Toggle the state of the coin every 3 loop iterations. This will be used to |
lewisgw | 29:bdc4138b5171 | 34 | // print different coin sprites so it looks like it is 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 | 29:bdc4138b5171 | 43 | // Set the coin coords based on input values. |
lewisgw | 26:4253656c0755 | 44 | if (rand_y > 40) { |
lewisgw | 26:4253656c0755 | 45 | _y = 15; // Set the coin on the top platforms. |
lewisgw | 15:876c047a6ec9 | 46 | } else { |
lewisgw | 26:4253656c0755 | 47 | _y = 33; // Set coin on the bottom platforms. |
lewisgw | 15:876c047a6ec9 | 48 | } |
lewisgw | 15:876c047a6ec9 | 49 | _x = rand_x; |
lewisgw | 26:4253656c0755 | 50 | if (_x < 3 || _x > 78) { // Ensures the coin does not generate off-screen. |
lewisgw | 26:4253656c0755 | 51 | _x = 50; // Default value if the coin generates off-screen. |
lewisgw | 15:876c047a6ec9 | 52 | } |
lewisgw | 15:876c047a6ec9 | 53 | } |
lewisgw | 15:876c047a6ec9 | 54 | |
lewisgw | 15:876c047a6ec9 | 55 | int * Coin::get_coin_sprite() { |
lewisgw | 26:4253656c0755 | 56 | // Return different coin sprites. |
lewisgw | 15:876c047a6ec9 | 57 | if (_rotate_coin) { |
lewisgw | 15:876c047a6ec9 | 58 | return *coin_front; |
lewisgw | 15:876c047a6ec9 | 59 | } else { |
lewisgw | 15:876c047a6ec9 | 60 | return *coin_side; |
lewisgw | 15:876c047a6ec9 | 61 | } |
lewisgw | 15:876c047a6ec9 | 62 | } |
lewisgw | 15:876c047a6ec9 | 63 | |
lewisgw | 15:876c047a6ec9 | 64 | int Coin::get_coin_x() { |
lewisgw | 15:876c047a6ec9 | 65 | return _x; |
lewisgw | 15:876c047a6ec9 | 66 | } |
lewisgw | 15:876c047a6ec9 | 67 | |
lewisgw | 15:876c047a6ec9 | 68 | int Coin::get_coin_y() { |
lewisgw | 15:876c047a6ec9 | 69 | return _y; |
lewisgw | 16:331be5c7ed80 | 70 | } |
lewisgw | 21:20478f086bc2 | 71 | |
lewisgw | 21:20478f086bc2 | 72 |