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
Floors/Floors.cpp@33:de130e274391, 2019-05-09 (annotated)
- Committer:
- el17m2h
- Date:
- Thu May 09 11:39:59 2019 +0000
- Revision:
- 33:de130e274391
- Parent:
- 31:5c4acae51026
- Child:
- 34:a9b14a4ccd46
Changed trivial functions to one-line structure.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| el17m2h | 1:0001cb3eb053 | 1 | #include "Floors.h" |
| el17m2h | 29:15e9640646b7 | 2 | Floors::Floors() |
| el17m2h | 29:15e9640646b7 | 3 | { |
| el17m2h | 1:0001cb3eb053 | 4 | } |
| el17m2h | 29:15e9640646b7 | 5 | Floors::~Floors() |
| el17m2h | 29:15e9640646b7 | 6 | { |
| el17m2h | 1:0001cb3eb053 | 7 | } |
| el17m2h | 3:116913e97fd7 | 8 | |
| el17m2h | 29:15e9640646b7 | 9 | void Floors::init(int y, int width, int height) |
| el17m2h | 29:15e9640646b7 | 10 | { |
| el17m2h | 1:0001cb3eb053 | 11 | _height = height; |
| el17m2h | 1:0001cb3eb053 | 12 | _width = width; |
| el17m2h | 31:5c4acae51026 | 13 | end_game = false; // the read decision should be false initially so that the game is not ended as soon as it starts |
| el17m2h | 31:5c4acae51026 | 14 | int decide = rand() % 2; // x-coordinate initially random: 2-30 or 40-71 so doodler falls to bottom floor |
| el17m2h | 31:5c4acae51026 | 15 | if (decide == 0) { // right |
| el17m2h | 29:15e9640646b7 | 16 | _position.x = 2 + (rand()% 28); |
| el17m2h | 31:5c4acae51026 | 17 | } else { // left |
| el17m2h | 29:15e9640646b7 | 18 | _position.x = 40 + (rand()% 31); |
| el17m2h | 22:0d2ac98a8b48 | 19 | } |
| el17m2h | 14:529f798adae4 | 20 | _position.y = y; |
| el17m2h | 31:5c4acae51026 | 21 | |
| el17m2h | 1:0001cb3eb053 | 22 | } |
| el17m2h | 1:0001cb3eb053 | 23 | |
| el17m2h | 29:15e9640646b7 | 24 | void Floors::draw(N5110 &lcd) |
| el17m2h | 29:15e9640646b7 | 25 | { |
| el17m2h | 14:529f798adae4 | 26 | lcd.drawRect(_position.x, _position.y, _width, _height, FILL_BLACK); |
| el17m2h | 31:5c4acae51026 | 27 | eny.draw(lcd); // draws enemy |
| el17m2h | 1:0001cb3eb053 | 28 | } |
| el17m2h | 1:0001cb3eb053 | 29 | |
| el17m2h | 29:15e9640646b7 | 30 | void Floors::update(float doodler_pos_x, float doodler_pos_y, float _bullet_pos_x, float _bullet_pos_y) |
| el17m2h | 31:5c4acae51026 | 31 | { // when they leave the screen they will re-appear in random x-coordinate so that 10 floors are always on screen |
| el17m2h | 31:5c4acae51026 | 32 | // rectangle (1-82 & 9 - 48 ) |
| el17m2h | 29:15e9640646b7 | 33 | _doodler_pos_x = doodler_pos_x; |
| el17m2h | 29:15e9640646b7 | 34 | _doodler_pos_y = doodler_pos_y; |
| el17m2h | 31:5c4acae51026 | 35 | if (_position.y > 45 ) { // the place decision bool is updated every time a floor re-appears |
| el17m2h | 29:15e9640646b7 | 36 | _position.y = 9; |
| el17m2h | 31:5c4acae51026 | 37 | _position.x = 1 + (rand()% 69); |
| el17m2h | 30:863565e9859f | 38 | place = rand()% 6; |
| el17m2h | 31:5c4acae51026 | 39 | if (place == 2) { // 1/6 chance of placing a ghost |
| el17m2h | 29:15e9640646b7 | 40 | eny.update(_position); |
| el17m2h | 29:15e9640646b7 | 41 | put_enemy = true; |
| el17m2h | 31:5c4acae51026 | 42 | } else { |
| el17m2h | 29:15e9640646b7 | 43 | eny.erase(); |
| el17m2h | 29:15e9640646b7 | 44 | put_enemy = false; |
| el17m2h | 26:d16a5b1e0ace | 45 | } |
| el17m2h | 20:a359092079b0 | 46 | } |
| el17m2h | 33:de130e274391 | 47 | if (_doodler_pos_y < 16) { // shift floors once doodler reaches this value on the screen |
| el17m2h | 31:5c4acae51026 | 48 | // I chose this value because testing with the doodler's jump, it allows the doodler to keep jumping on a floor |
| el17m2h | 31:5c4acae51026 | 49 | // without it moving down and dissapearing |
| el17m2h | 29:15e9640646b7 | 50 | _position.y += 1; |
| el17m2h | 29:15e9640646b7 | 51 | } |
| el17m2h | 29:15e9640646b7 | 52 | check_enemy(_bullet_pos_x, _bullet_pos_y); |
| el17m2h | 29:15e9640646b7 | 53 | } |
| el17m2h | 29:15e9640646b7 | 54 | |
| el17m2h | 31:5c4acae51026 | 55 | void Floors::check_enemy(float _bullet_pos_x, float _bullet_pos_y) |
| el17m2h | 31:5c4acae51026 | 56 | { |
| el17m2h | 29:15e9640646b7 | 57 | enemy_position = eny.get_position(); |
| el17m2h | 31:5c4acae51026 | 58 | if (put_enemy == true) { |
| el17m2h | 29:15e9640646b7 | 59 | eny.update(_position); |
| el17m2h | 29:15e9640646b7 | 60 | } |
| el17m2h | 31:5c4acae51026 | 61 | if ( // to check if the doodler has collided with the ghost |
| el17m2h | 31:5c4acae51026 | 62 | (_doodler_pos_x + 6 <= enemy_position.x + 12) && |
| el17m2h | 31:5c4acae51026 | 63 | (_doodler_pos_x + 6 >= enemy_position.x) && |
| el17m2h | 31:5c4acae51026 | 64 | (_doodler_pos_y <= enemy_position.y + 11) && |
| el17m2h | 31:5c4acae51026 | 65 | (_doodler_pos_y >= enemy_position.y) |
| el17m2h | 31:5c4acae51026 | 66 | ) { |
| el17m2h | 31:5c4acae51026 | 67 | end_game = true; |
| el17m2h | 29:15e9640646b7 | 68 | } |
| el17m2h | 31:5c4acae51026 | 69 | if ( // to check if the bullet has collided with the ghost |
| el17m2h | 31:5c4acae51026 | 70 | (_bullet_pos_x <= enemy_position.x + 12) && |
| el17m2h | 31:5c4acae51026 | 71 | (_bullet_pos_x >= enemy_position.x) && |
| el17m2h | 31:5c4acae51026 | 72 | (_bullet_pos_y <= enemy_position.y + 11) && |
| el17m2h | 31:5c4acae51026 | 73 | (_bullet_pos_y >= enemy_position.y) |
| el17m2h | 31:5c4acae51026 | 74 | ) { |
| el17m2h | 29:15e9640646b7 | 75 | eny.erase(); |
| el17m2h | 15:4efa04a6a376 | 76 | } |
| el17m2h | 9:5e53bca2a4c2 | 77 | } |
| el17m2h | 9:5e53bca2a4c2 | 78 | |
| el17m2h | 33:de130e274391 | 79 | Vector2D Floors::get_position() { Vector2D p = {_position.x,_position.y}; return p; } |
| el17m2h | 5:8814d6de77d0 | 80 | |
| el17m2h | 33:de130e274391 | 81 | void Floors::set_position(Vector2D pos) { _position.x = pos.x; _position.y = pos.y; } |
| el17m2h | 31:5c4acae51026 | 82 | |
| el17m2h | 31:5c4acae51026 | 83 | // The get_end_game function can be called in the engine to check if the game should end or not (if the doodler has collided |
| el17m2h | 31:5c4acae51026 | 84 | // with the enemy) |
| el17m2h | 33:de130e274391 | 85 | bool Floors::get_end_game() { return end_game; } |