ELEC2645 (2018/19) / Mbed 2 deprecated el17m2h_public

Dependencies:   mbed

Committer:
el17m2h
Date:
Thu May 09 08:59:39 2019 +0000
Revision:
30:863565e9859f
Parent:
29:15e9640646b7
Child:
31:5c4acae51026
Changed the size of the enemy and updated the new values in the floors cpp. I also added the test code for the doodler's movement and called it on the main cpp.

Who changed what in which revision?

UserRevisionLine numberNew 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 30:863565e9859f 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 29:15e9640646b7 31 {
el17m2h 11:2041290b5a74 32 // when they leave the screen they will re-appear in random x-coordinate so that 10 floors are always on screen
el17m2h 29:15e9640646b7 33 //rectangle (1-82 & 9 - 48 )
el17m2h 29:15e9640646b7 34 _doodler_pos_x = doodler_pos_x;
el17m2h 29:15e9640646b7 35 _doodler_pos_y = doodler_pos_y;
el17m2h 30:863565e9859f 36 if (_position.y > 45 ) { // the place decision bool is updated every time a floor re-appears
el17m2h 29:15e9640646b7 37 _position.y = 9;
el17m2h 29:15e9640646b7 38 _position.x = 1 + (rand()% 70);
el17m2h 30:863565e9859f 39 place = rand()% 6;
el17m2h 30:863565e9859f 40 if (place == 2) { // 1/6 chance of placing a ghost
el17m2h 29:15e9640646b7 41 eny.update(_position);
el17m2h 29:15e9640646b7 42 put_enemy = true;
el17m2h 29:15e9640646b7 43 } else{
el17m2h 29:15e9640646b7 44 eny.erase();
el17m2h 29:15e9640646b7 45 put_enemy = false;
el17m2h 26:d16a5b1e0ace 46 }
el17m2h 20:a359092079b0 47 }
el17m2h 30:863565e9859f 48 if (_doodler_pos_y < 13) { // shift floors once doodler reaches this value on the screen
el17m2h 30:863565e9859f 49 // I chose this value because testing with the doodler's jump, it allows the doodler to keep jumping on a floor
el17m2h 30:863565e9859f 50 // without it moving down and dissapearing
el17m2h 29:15e9640646b7 51 _position.y += 1;
el17m2h 29:15e9640646b7 52 }
el17m2h 29:15e9640646b7 53 check_enemy(_bullet_pos_x, _bullet_pos_y);
el17m2h 29:15e9640646b7 54 }
el17m2h 29:15e9640646b7 55
el17m2h 29:15e9640646b7 56 void Floors::check_enemy(float _bullet_pos_x, float _bullet_pos_y){
el17m2h 29:15e9640646b7 57 enemy_position = eny.get_position();
el17m2h 29:15e9640646b7 58 if (put_enemy == true){
el17m2h 29:15e9640646b7 59 eny.update(_position);
el17m2h 29:15e9640646b7 60 }
el17m2h 29:15e9640646b7 61 if ( // to check if the doodler has collided with the ghost
el17m2h 29:15e9640646b7 62 (_doodler_pos_x + 6 <= enemy_position.x + 12) &&
el17m2h 29:15e9640646b7 63 (_doodler_pos_x + 6 >= enemy_position.x) &&
el17m2h 29:15e9640646b7 64 (_doodler_pos_y <= enemy_position.y + 15) &&
el17m2h 29:15e9640646b7 65 (_doodler_pos_y >= enemy_position.y)
el17m2h 29:15e9640646b7 66 ){
el17m2h 29:15e9640646b7 67 eny.erase(); // should end the game
el17m2h 29:15e9640646b7 68 }
el17m2h 29:15e9640646b7 69 if ( // to check if the bullet has collided with the ghost
el17m2h 29:15e9640646b7 70 (_bullet_pos_x <= enemy_position.x + 12) &&
el17m2h 29:15e9640646b7 71 (_bullet_pos_x >= enemy_position.x) &&
el17m2h 29:15e9640646b7 72 (_bullet_pos_y <= enemy_position.y + 15) &&
el17m2h 29:15e9640646b7 73 (_bullet_pos_y >= enemy_position.y)
el17m2h 29:15e9640646b7 74 ){
el17m2h 29:15e9640646b7 75 eny.erase();
el17m2h 15:4efa04a6a376 76 }
el17m2h 9:5e53bca2a4c2 77 }
el17m2h 9:5e53bca2a4c2 78
el17m2h 29:15e9640646b7 79 Vector2D Floors::get_position()
el17m2h 29:15e9640646b7 80 {
el17m2h 14:529f798adae4 81 Vector2D p = {_position.x,_position.y};
el17m2h 9:5e53bca2a4c2 82 return p;
el17m2h 5:8814d6de77d0 83 }
el17m2h 5:8814d6de77d0 84
el17m2h 29:15e9640646b7 85 void Floors::set_position(Vector2D pos)
el17m2h 29:15e9640646b7 86 {
el17m2h 14:529f798adae4 87 _position.x = pos.x;
el17m2h 14:529f798adae4 88 _position.y = pos.y;
el17m2h 21:6b16ca9834e6 89 }