ELEC2645 (2018/19) / Mbed 2 deprecated fy14lkaa

Dependencies:   mbed

Committer:
fy14lkaa
Date:
Thu May 09 11:57:29 2019 +0000
Revision:
147:c008c0a32d23
Parent:
146:5014ef83d4e6
Child:
148:83e5a4dedffd
formatting the SpaceEngine.cpp.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
fy14lkaa 120:53e47c1e58f3 1 #include"SpaceEngine.h"
fy14lkaa 12:45b1249b3d9a 2
fy14lkaa 145:e060e890c725 3 SpaceEngine::SpaceEngine() //constructor of class SpaceEngine
fy14lkaa 12:45b1249b3d9a 4 {
fy14lkaa 12:45b1249b3d9a 5 }
fy14lkaa 145:e060e890c725 6 SpaceEngine::~SpaceEngine() ////Destructor of class SpaceEngine
fy14lkaa 12:45b1249b3d9a 7 {
fy14lkaa 12:45b1249b3d9a 8 }
fy14lkaa 12:45b1249b3d9a 9
fy14lkaa 12:45b1249b3d9a 10
fy14lkaa 122:d1fd8cbe6633 11
fy14lkaa 122:d1fd8cbe6633 12 void SpaceEngine::init(int x_spaceship,int y_spaceship, int x_bullet, int y_bullet,int fired_bullet, int x_alien,int y_alien, int speed_alien, int speed_bullet, int speed_spaceship)
fy14lkaa 57:45c5de9cefdf 13 {
fy14lkaa 57:45c5de9cefdf 14
fy14lkaa 147:c008c0a32d23 15 _x_spaceship=x_spaceship; //represents the x-cooridante of the spaceship.
fy14lkaa 147:c008c0a32d23 16 _y_spaceship=y_spaceship; //represents the y-cooridante of the spaceship.
fy14lkaa 147:c008c0a32d23 17 _speed_spaceship=speed_spaceship; // speed of the spaceship.
fy14lkaa 147:c008c0a32d23 18 _x_bullet=x_bullet; //represents the x-cooridante of the spaceship.
fy14lkaa 147:c008c0a32d23 19 _y_bullet=y_bullet; //represents the y-cooridante of the spaceship.
fy14lkaa 147:c008c0a32d23 20 _fired_bullet= fired_bullet; // spaceship fired the alien by relaseing the bullet.
fy14lkaa 147:c008c0a32d23 21 _x_alien= x_alien; //represents the x-cooridante of the alien.
fy14lkaa 147:c008c0a32d23 22 _y_alien= y_alien; //represents the y-cooridante of the spaceship.
fy14lkaa 147:c008c0a32d23 23 _speed_alien= speed_alien; // speed of the alien.
fy14lkaa 147:c008c0a32d23 24 _speed_bullet=speed_bullet; // speed of the bullet.
fy14lkaa 147:c008c0a32d23 25
fy14lkaa 147:c008c0a32d23 26 _bullet.init(_x_bullet, _y_bullet, speed_bullet,fired_bullet); //initalise the object bullet.
fy14lkaa 147:c008c0a32d23 27 _alien.init (_x_alien, _y_alien,_speed_alien); //initalise the object alien.
fy14lkaa 147:c008c0a32d23 28 _spaceship.init( _x_spaceship, _y_spaceship, _speed_spaceship); //initalise the object spaceship.
fy14lkaa 147:c008c0a32d23 29 _alien_killed = 0; // set the killed alien at 0.
fy14lkaa 123:d68eb9023d88 30 }
fy14lkaa 147:c008c0a32d23 31
fy14lkaa 146:5014ef83d4e6 32 // void function for Gamepad library to read the position of the objects.
fy14lkaa 124:77f379153715 33 void SpaceEngine::read_input(Gamepad &pad)
fy14lkaa 12:45b1249b3d9a 34 {
fy14lkaa 147:c008c0a32d23 35
fy14lkaa 12:45b1249b3d9a 36 _d = pad.get_direction();
fy14lkaa 13:9d6ee753eca6 37 _mag = pad.get_mag();
fy14lkaa 12:45b1249b3d9a 38 }
fy14lkaa 146:5014ef83d4e6 39 /**this function draw the objects on the screen
fy14lkaa 146:5014ef83d4e6 40 *by using the (N5110 &lcd) libraries
fy14lkaa 146:5014ef83d4e6 41 * if statment to check if the alien is alive so the lcd can draw it on the screen
fy14lkaa 146:5014ef83d4e6 42 * else if statment will check if the alien dead so it should disappear
fy14lkaa 146:5014ef83d4e6 43 *also it draws the spaceship
fy14lkaa 146:5014ef83d4e6 44 */
fy14lkaa 147:c008c0a32d23 45
fy14lkaa 124:77f379153715 46 void SpaceEngine::draw(N5110 &lcd)
fy14lkaa 124:77f379153715 47 {
fy14lkaa 124:77f379153715 48 _bullet.draw(lcd);
fy14lkaa 136:58f393968aa3 49
fy14lkaa 133:eed60548d170 50 if (_alien.isAlive() == true) {
fy14lkaa 133:eed60548d170 51 _alien.draw(lcd);
fy14lkaa 133:eed60548d170 52 } else { // alien is dead
fy14lkaa 147:c008c0a32d23 53 int y_pos = (rand() % 30) + 10; // random number 10 to 39
fy14lkaa 147:c008c0a32d23 54 _alien.init(70,y_pos,1);
fy14lkaa 133:eed60548d170 55 wait(0.2);
fy14lkaa 133:eed60548d170 56 }
fy14lkaa 136:58f393968aa3 57
fy14lkaa 124:77f379153715 58 _spaceship.draw(lcd);
fy14lkaa 133:eed60548d170 59 lcd.printString("alien_killed",0,0);
fy14lkaa 133:eed60548d170 60 char kills[14];
fy14lkaa 133:eed60548d170 61 sprintf(kills,"%2d",_alien_killed);
fy14lkaa 133:eed60548d170 62 lcd.printString(kills,70,0);
fy14lkaa 137:fe80c0f2da9d 63
fy14lkaa 56:c8fc0a1f4132 64 }
fy14lkaa 147:c008c0a32d23 65
fy14lkaa 147:c008c0a32d23 66 /*
fy14lkaa 147:c008c0a32d23 67 *this void function for reading the the positions of the inputs(objects)by(Gamepad &pad)libraries in class SpaceEngine to detect the collisions.
fy14lkaa 147:c008c0a32d23 68 *this inculde if statment that shows when joystic's direction to the east the bullt will fired.
fy14lkaa 147:c008c0a32d23 69 */
fy14lkaa 125:b83378770171 70 void SpaceEngine::update(Gamepad &pad)
fy14lkaa 70:7f0b330ff40b 71 {
fy14lkaa 125:b83378770171 72 _bullet.update(_d,_mag);
fy14lkaa 125:b83378770171 73 _alien.update(_d,_mag);
fy14lkaa 125:b83378770171 74 _spaceship.update(_d,_mag);
fy14lkaa 137:fe80c0f2da9d 75 check_space_collision(pad);
fy14lkaa 133:eed60548d170 76 _y_alien = _alien.get_pos_y();
fy14lkaa 133:eed60548d170 77 _x_alien = _alien.get_pos_x();
fy14lkaa 133:eed60548d170 78 _y_bullet = _bullet.get_pos_y();
fy14lkaa 133:eed60548d170 79 _x_bullet = _bullet.get_pos_x();
fy14lkaa 133:eed60548d170 80 if(_d==E) {
fy14lkaa 125:b83378770171 81 _fired_bullet=1;
fy14lkaa 147:c008c0a32d23 82
fy14lkaa 147:c008c0a32d23 83 _bullet.set_pos(_spaceship.get_pos_x()+33, _spaceship.get_pos_y());
fy14lkaa 147:c008c0a32d23 84 }
fy14lkaa 133:eed60548d170 85 }
fy14lkaa 147:c008c0a32d23 86
fy14lkaa 147:c008c0a32d23 87
fy14lkaa 147:c008c0a32d23 88
fy14lkaa 147:c008c0a32d23 89 /*void function that checks the collision between the objects
fy14lkaa 147:c008c0a32d23 90 *if statment is to detect if x-coordinate of the bullet is larger or equal 65
fy14lkaa 147:c008c0a32d23 91 *and less than or equal to the 68 and if y-cooridante of the bullet is larger
fy14lkaa 147:c008c0a32d23 92 *than y-corrdindate of the alien-5 and y_alien+15 is larger or equal y_bullet
fy14lkaa 147:c008c0a32d23 93 *then the alien gets killed.
fy14lkaa 147:c008c0a32d23 94 */
fy14lkaa 147:c008c0a32d23 95
fy14lkaa 137:fe80c0f2da9d 96 void SpaceEngine::check_space_collision(Gamepad &pad) {
fy14lkaa 133:eed60548d170 97
fy14lkaa 136:58f393968aa3 98 if(_x_bullet >= 65 && _x_bullet <= 68 && _y_bullet >= _y_alien-5 && _y_bullet <= _y_alien+15) {
fy14lkaa 136:58f393968aa3 99 _alien.setAlive(false);
fy14lkaa 136:58f393968aa3 100 _alien_killed++;
fy14lkaa 136:58f393968aa3 101 }
fy14lkaa 137:fe80c0f2da9d 102
fy14lkaa 147:c008c0a32d23 103 }