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
SpaceEngine/SpaceEngine.cpp@146:5014ef83d4e6, 2019-05-09 (annotated)
- Committer:
- fy14lkaa
- Date:
- Thu May 09 10:35:00 2019 +0000
- Revision:
- 146:5014ef83d4e6
- Parent:
- 145:e060e890c725
- Child:
- 147:c008c0a32d23
added more comments for SpaceEngine.cpp
Who changed what in which revision?
| User | Revision | Line number | New 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 | 146:5014ef83d4e6 | 11 | //void function to initalise all the parameters of SpaceEngine Class. |
| fy14lkaa | 122:d1fd8cbe6633 | 12 | |
| fy14lkaa | 122:d1fd8cbe6633 | 13 | 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 | 14 | { |
| fy14lkaa | 57:45c5de9cefdf | 15 | |
| fy14lkaa | 146:5014ef83d4e6 | 16 | _x_spaceship=x_spaceship; // represents the x-cooridante of the spaceship. |
| fy14lkaa | 146:5014ef83d4e6 | 17 | _y_spaceship=y_spaceship; //represents the y-cooridante of the spaceship. |
| fy14lkaa | 146:5014ef83d4e6 | 18 | _speed_spaceship=speed_spaceship; // speed of the spaceship. |
| fy14lkaa | 146:5014ef83d4e6 | 19 | _x_bullet=x_bullet; //the x-cooridante of the bullet. |
| fy14lkaa | 146:5014ef83d4e6 | 20 | _y_bullet=y_bullet; //the y-cooridante of the bullet. |
| fy14lkaa | 146:5014ef83d4e6 | 21 | _fired_bullet= fired_bullet; //spaceship fired the alien by relaseing the bullet. |
| fy14lkaa | 146:5014ef83d4e6 | 22 | _x_alien= x_alien; // x-cooridante of the alien. |
| fy14lkaa | 146:5014ef83d4e6 | 23 | _y_alien= y_alien; //y-cooridante of the alien. |
| fy14lkaa | 146:5014ef83d4e6 | 24 | _speed_alien= speed_alien; // the speed of the alien. |
| fy14lkaa | 146:5014ef83d4e6 | 25 | _speed_bullet=speed_bullet; // the speed of the bullet |
| fy14lkaa | 145:e060e890c725 | 26 | |
| fy14lkaa | 146:5014ef83d4e6 | 27 | _bullet.init(_x_bullet, _y_bullet, speed_bullet,fired_bullet); //initalise bullet. |
| fy14lkaa | 146:5014ef83d4e6 | 28 | _alien.init (_x_alien, _y_alien,_speed_alien); //initalise alien |
| fy14lkaa | 146:5014ef83d4e6 | 29 | _spaceship.init( _x_spaceship, _y_spaceship, _speed_spaceship); //initalise spaceship |
| fy14lkaa | 146:5014ef83d4e6 | 30 | _alien_killed = 0; // set the killed alien at 0. |
| fy14lkaa | 123:d68eb9023d88 | 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 | 146:5014ef83d4e6 | 35 | |
| fy14lkaa | 12:45b1249b3d9a | 36 | _d = pad.get_direction(); |
| fy14lkaa | 13:9d6ee753eca6 | 37 | _mag = pad.get_mag(); |
| fy14lkaa | 124:77f379153715 | 38 | |
| fy14lkaa | 12:45b1249b3d9a | 39 | } |
| fy14lkaa | 12:45b1249b3d9a | 40 | |
| fy14lkaa | 146:5014ef83d4e6 | 41 | /**this function draw the objects on the screen |
| fy14lkaa | 146:5014ef83d4e6 | 42 | *by using the (N5110 &lcd) libraries |
| fy14lkaa | 146:5014ef83d4e6 | 43 | * if statment to check if the alien is alive so the lcd can draw it on the screen |
| fy14lkaa | 146:5014ef83d4e6 | 44 | * else if statment will check if the alien dead so it should disappear |
| fy14lkaa | 146:5014ef83d4e6 | 45 | *also it draws the spaceship |
| fy14lkaa | 146:5014ef83d4e6 | 46 | */ |
| fy14lkaa | 124:77f379153715 | 47 | void SpaceEngine::draw(N5110 &lcd) |
| fy14lkaa | 124:77f379153715 | 48 | { |
| fy14lkaa | 124:77f379153715 | 49 | _bullet.draw(lcd); |
| fy14lkaa | 136:58f393968aa3 | 50 | |
| fy14lkaa | 133:eed60548d170 | 51 | if (_alien.isAlive() == true) { |
| fy14lkaa | 133:eed60548d170 | 52 | _alien.draw(lcd); |
| fy14lkaa | 133:eed60548d170 | 53 | } else { // alien is dead |
| fy14lkaa | 146:5014ef83d4e6 | 54 | int y_pos = (rand() % 30) + 10; // random number 10 to 39. |
| fy14lkaa | 146:5014ef83d4e6 | 55 | _alien.init(70,y_pos,1); //initalise alien. |
| fy14lkaa | 133:eed60548d170 | 56 | wait(0.2); |
| fy14lkaa | 133:eed60548d170 | 57 | } |
| fy14lkaa | 136:58f393968aa3 | 58 | |
| fy14lkaa | 124:77f379153715 | 59 | _spaceship.draw(lcd); |
| fy14lkaa | 133:eed60548d170 | 60 | lcd.printString("alien_killed",0,0); |
| fy14lkaa | 133:eed60548d170 | 61 | char kills[14]; |
| fy14lkaa | 133:eed60548d170 | 62 | sprintf(kills,"%2d",_alien_killed); |
| fy14lkaa | 133:eed60548d170 | 63 | lcd.printString(kills,70,0); |
| fy14lkaa | 137:fe80c0f2da9d | 64 | |
| fy14lkaa | 56:c8fc0a1f4132 | 65 | } |
| fy14lkaa | 146:5014ef83d4e6 | 66 | /* |
| fy14lkaa | 146:5014ef83d4e6 | 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 | 146:5014ef83d4e6 | 68 | *this inculde if statment that shows when joystic's direction to the east the bullt will fired. |
| fy14lkaa | 146:5014ef83d4e6 | 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 | 137:fe80c0f2da9d | 82 | } |
| fy14lkaa | 146:5014ef83d4e6 | 83 | _bullet.set_pos(_spaceship.get_pos_x()+33, _spaceship.get_pos_y()); //set the position of the bullet where the x= x_spaceship+x_bullet but the y are same |
| fy14lkaa | 133:eed60548d170 | 84 | } |
| fy14lkaa | 137:fe80c0f2da9d | 85 | |
| fy14lkaa | 137:fe80c0f2da9d | 86 | |
| fy14lkaa | 137:fe80c0f2da9d | 87 | |
| fy14lkaa | 137:fe80c0f2da9d | 88 | void SpaceEngine::check_space_collision(Gamepad &pad) { |
| fy14lkaa | 133:eed60548d170 | 89 | |
| fy14lkaa | 136:58f393968aa3 | 90 | if(_x_bullet >= 65 && _x_bullet <= 68 && _y_bullet >= _y_alien-5 && _y_bullet <= _y_alien+15) { |
| fy14lkaa | 136:58f393968aa3 | 91 | _alien.setAlive(false); |
| fy14lkaa | 136:58f393968aa3 | 92 | _alien_killed++; |
| fy14lkaa | 136:58f393968aa3 | 93 | } |
| fy14lkaa | 137:fe80c0f2da9d | 94 | |
| fy14lkaa | 133:eed60548d170 | 95 | } |