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@29:15e9640646b7, 2019-05-08 (annotated)
- Committer:
- el17m2h
- Date:
- Wed May 08 21:11:35 2019 +0000
- Revision:
- 29:15e9640646b7
- Parent:
- 26:d16a5b1e0ace
- Child:
- 30:863565e9859f
Changed the enemy file so that it has functions to make it update its position or make it disappear from the screen. The floors h file includes the enemy, and depending on its position it updates it or not.
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 | 22:0d2ac98a8b48 | 13 | // x-coordinate initially random: 2-30 or 40-71 so doodler falls to bottom floor |
| el17m2h | 22:0d2ac98a8b48 | 14 | int decide = rand() % 2; |
| el17m2h | 29:15e9640646b7 | 15 | if (decide == 0) { // right |
| el17m2h | 29:15e9640646b7 | 16 | _position.x = 2 + (rand()% 28); |
| el17m2h | 22:0d2ac98a8b48 | 17 | } else { // left |
| el17m2h | 29:15e9640646b7 | 18 | _position.x = 40 + (rand()% 31); |
| el17m2h | 22:0d2ac98a8b48 | 19 | } |
| el17m2h | 14:529f798adae4 | 20 | _position.y = y; |
| el17m2h | 29:15e9640646b7 | 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 | 1:0001cb3eb053 | 27 | } |
| el17m2h | 1:0001cb3eb053 | 28 | |
| el17m2h | 29:15e9640646b7 | 29 | void Floors::update(float doodler_pos_x, float doodler_pos_y, float _bullet_pos_x, float _bullet_pos_y) |
| el17m2h | 29:15e9640646b7 | 30 | { |
| el17m2h | 11:2041290b5a74 | 31 | // when they leave the screen they will re-appear in random x-coordinate so that 10 floors are always on screen |
| el17m2h | 29:15e9640646b7 | 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 | 29:15e9640646b7 | 35 | if (_position.y > 45 ) { |
| el17m2h | 29:15e9640646b7 | 36 | _position.y = 9; |
| el17m2h | 29:15e9640646b7 | 37 | _position.x = 1 + (rand()% 70); |
| el17m2h | 26:d16a5b1e0ace | 38 | place = rand()% 8; |
| el17m2h | 29:15e9640646b7 | 39 | if (place == 2) { // 1/8 chance of placing a ghost |
| el17m2h | 29:15e9640646b7 | 40 | eny.update(_position); |
| el17m2h | 29:15e9640646b7 | 41 | put_enemy = true; |
| el17m2h | 29:15e9640646b7 | 42 | } else{ |
| el17m2h | 29:15e9640646b7 | 43 | eny.erase(); |
| el17m2h | 29:15e9640646b7 | 44 | put_enemy = false; |
| el17m2h | 26:d16a5b1e0ace | 45 | } |
| el17m2h | 20:a359092079b0 | 46 | } |
| el17m2h | 29:15e9640646b7 | 47 | if (_doodler_pos_y < 15) { // shift floors once doodler reaches over halfway the screen |
| el17m2h | 29:15e9640646b7 | 48 | _position.y += 1; |
| el17m2h | 29:15e9640646b7 | 49 | } |
| el17m2h | 29:15e9640646b7 | 50 | check_enemy(_bullet_pos_x, _bullet_pos_y); |
| el17m2h | 29:15e9640646b7 | 51 | } |
| el17m2h | 29:15e9640646b7 | 52 | |
| el17m2h | 29:15e9640646b7 | 53 | void Floors::check_enemy(float _bullet_pos_x, float _bullet_pos_y){ |
| el17m2h | 29:15e9640646b7 | 54 | enemy_position = eny.get_position(); |
| el17m2h | 29:15e9640646b7 | 55 | if (put_enemy == true){ |
| el17m2h | 29:15e9640646b7 | 56 | eny.update(_position); |
| el17m2h | 29:15e9640646b7 | 57 | } |
| el17m2h | 29:15e9640646b7 | 58 | if ( // to check if the doodler has collided with the ghost |
| el17m2h | 29:15e9640646b7 | 59 | (_doodler_pos_x + 6 <= enemy_position.x + 12) && |
| el17m2h | 29:15e9640646b7 | 60 | (_doodler_pos_x + 6 >= enemy_position.x) && |
| el17m2h | 29:15e9640646b7 | 61 | (_doodler_pos_y <= enemy_position.y + 15) && |
| el17m2h | 29:15e9640646b7 | 62 | (_doodler_pos_y >= enemy_position.y) |
| el17m2h | 29:15e9640646b7 | 63 | ){ |
| el17m2h | 29:15e9640646b7 | 64 | eny.erase(); // should end the game |
| el17m2h | 29:15e9640646b7 | 65 | } |
| el17m2h | 29:15e9640646b7 | 66 | if ( // to check if the bullet has collided with the ghost |
| el17m2h | 29:15e9640646b7 | 67 | (_bullet_pos_x <= enemy_position.x + 12) && |
| el17m2h | 29:15e9640646b7 | 68 | (_bullet_pos_x >= enemy_position.x) && |
| el17m2h | 29:15e9640646b7 | 69 | (_bullet_pos_y <= enemy_position.y + 15) && |
| el17m2h | 29:15e9640646b7 | 70 | (_bullet_pos_y >= enemy_position.y) |
| el17m2h | 29:15e9640646b7 | 71 | ){ |
| el17m2h | 29:15e9640646b7 | 72 | eny.erase(); |
| el17m2h | 15:4efa04a6a376 | 73 | } |
| el17m2h | 9:5e53bca2a4c2 | 74 | } |
| el17m2h | 9:5e53bca2a4c2 | 75 | |
| el17m2h | 29:15e9640646b7 | 76 | Vector2D Floors::get_position() |
| el17m2h | 29:15e9640646b7 | 77 | { |
| el17m2h | 14:529f798adae4 | 78 | Vector2D p = {_position.x,_position.y}; |
| el17m2h | 9:5e53bca2a4c2 | 79 | return p; |
| el17m2h | 5:8814d6de77d0 | 80 | } |
| el17m2h | 5:8814d6de77d0 | 81 | |
| el17m2h | 29:15e9640646b7 | 82 | void Floors::set_position(Vector2D pos) |
| el17m2h | 29:15e9640646b7 | 83 | { |
| el17m2h | 14:529f798adae4 | 84 | _position.x = pos.x; |
| el17m2h | 14:529f798adae4 | 85 | _position.y = pos.y; |
| el17m2h | 21:6b16ca9834e6 | 86 | } |