Laila Al Badwawi 200906179 SpaceInvaders I declare this my own independent work and understand the university rules on plagiarism.

Dependencies:   mbed

Committer:
fy14lkaa
Date:
Thu May 09 14:23:44 2019 +0000
Revision:
150:bd02678bfdb1
Parent:
149:bd0f37008f5a
finished the games and the last formatting

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