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
Diff: Floors/Floors.cpp
- Revision:
- 29:15e9640646b7
- Parent:
- 26:d16a5b1e0ace
- Child:
- 30:863565e9859f
--- a/Floors/Floors.cpp Wed May 08 18:02:11 2019 +0000
+++ b/Floors/Floors.cpp Wed May 08 21:11:35 2019 +0000
@@ -1,49 +1,86 @@
#include "Floors.h"
-Floors::Floors(){
+Floors::Floors()
+{
}
-Floors::~Floors(){
+Floors::~Floors()
+{
}
-void Floors::init(int y, int width, int height){
+void Floors::init(int y, int width, int height)
+{
_height = height;
_width = width;
// x-coordinate initially random: 2-30 or 40-71 so doodler falls to bottom floor
int decide = rand() % 2;
- if (decide == 0){ // right
- _position.x = 2 + (rand()% 28);
+ if (decide == 0) { // right
+ _position.x = 2 + (rand()% 28);
} else { // left
- _position.x = 40 + (rand()% 31);
+ _position.x = 40 + (rand()% 31);
}
_position.y = y;
+
}
-void Floors::draw(N5110 &lcd){
+void Floors::draw(N5110 &lcd)
+{
lcd.drawRect(_position.x, _position.y, _width, _height, FILL_BLACK);
}
-void Floors::update(float doodler_pos_y){
+void Floors::update(float doodler_pos_x, float doodler_pos_y, float _bullet_pos_x, float _bullet_pos_y)
+{
// when they leave the screen they will re-appear in random x-coordinate so that 10 floors are always on screen
- //rectangle (1-82 & 9 - 48 )
- if (_position.y > 45 ){
- _position.y = 9;
- _position.x = 1 + (rand()% 70);
+//rectangle (1-82 & 9 - 48 )
+ _doodler_pos_x = doodler_pos_x;
+ _doodler_pos_y = doodler_pos_y;
+ if (_position.y > 45 ) {
+ _position.y = 9;
+ _position.x = 1 + (rand()% 70);
place = rand()% 8;
- if (place == 2){ // 1/8 chance of placing an enemy
- _eny.init(_position.x, _position.y);
+ if (place == 2) { // 1/8 chance of placing a ghost
+ eny.update(_position);
+ put_enemy = true;
+ } else{
+ eny.erase();
+ put_enemy = false;
}
}
- if (doodler_pos_y < 25){ // shift floors once doodler reaches over halfway the screen
- _position.y += 1;
- _eny.update(_position.x, _position.y);
+ if (_doodler_pos_y < 15) { // shift floors once doodler reaches over halfway the screen
+ _position.y += 1;
+ }
+ check_enemy(_bullet_pos_x, _bullet_pos_y);
+}
+
+void Floors::check_enemy(float _bullet_pos_x, float _bullet_pos_y){
+ enemy_position = eny.get_position();
+ if (put_enemy == true){
+ eny.update(_position);
+ }
+ if ( // to check if the doodler has collided with the ghost
+ (_doodler_pos_x + 6 <= enemy_position.x + 12) &&
+ (_doodler_pos_x + 6 >= enemy_position.x) &&
+ (_doodler_pos_y <= enemy_position.y + 15) &&
+ (_doodler_pos_y >= enemy_position.y)
+ ){
+ eny.erase(); // should end the game
+ }
+ if ( // to check if the bullet has collided with the ghost
+ (_bullet_pos_x <= enemy_position.x + 12) &&
+ (_bullet_pos_x >= enemy_position.x) &&
+ (_bullet_pos_y <= enemy_position.y + 15) &&
+ (_bullet_pos_y >= enemy_position.y)
+ ){
+ eny.erase();
}
}
-Vector2D Floors::get_position(){
+Vector2D Floors::get_position()
+{
Vector2D p = {_position.x,_position.y};
return p;
}
-void Floors::set_position(Vector2D pos){
+void Floors::set_position(Vector2D pos)
+{
_position.x = pos.x;
_position.y = pos.y;
}