ELEC2645 (2018/19) / Mbed 2 deprecated fy14lkaa

Dependencies:   mbed

Committer:
fy14lkaa
Date:
Sun May 05 15:16:06 2019 +0000
Revision:
123:d68eb9023d88
Parent:
122:d1fd8cbe6633
Child:
124:77f379153715
added more new parameters in void init functions into 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 120:53e47c1e58f3 3 SpaceEngine::SpaceEngine()
fy14lkaa 12:45b1249b3d9a 4 {
fy14lkaa 12:45b1249b3d9a 5 }
fy14lkaa 120:53e47c1e58f3 6 SpaceEngine::~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 122:d1fd8cbe6633 15 _x_spaceship=x_spaceship;
fy14lkaa 122:d1fd8cbe6633 16 _y_spaceship=y_spaceship;
fy14lkaa 122:d1fd8cbe6633 17 _x_bullet=x_bullet;
fy14lkaa 122:d1fd8cbe6633 18 _y_bullet=y_bullet;
fy14lkaa 122:d1fd8cbe6633 19 _fired_bullet= fired_bullet;
fy14lkaa 122:d1fd8cbe6633 20 _x_alien= x_alien;
fy14lkaa 122:d1fd8cbe6633 21 _y_alien= y_alien;
fy14lkaa 122:d1fd8cbe6633 22 _speed_alien= speed_alien;
fy14lkaa 122:d1fd8cbe6633 23 _speed_bullet=speed_bullet;
fy14lkaa 122:d1fd8cbe6633 24 _speed_spaceship=speed_spaceship;
fy14lkaa 123:d68eb9023d88 25 _bullet.init(_x_bullet, _y_bullet, speed_bullet,fired_bullet);
fy14lkaa 123:d68eb9023d88 26 _alien.init (_x_alien, _y_alien,_speed_alien);
fy14lkaa 123:d68eb9023d88 27 _spaceship.init( _x_spaceship, _y_spaceship, _speed_spaceship);
fy14lkaa 123:d68eb9023d88 28 }
fy14lkaa 12:45b1249b3d9a 29
fy14lkaa 12:45b1249b3d9a 30 void SpaceInvadersEngine::read_input(Gamepad &pad)
fy14lkaa 12:45b1249b3d9a 31 {
fy14lkaa 12:45b1249b3d9a 32 _d = pad.get_direction();
fy14lkaa 13:9d6ee753eca6 33 _mag = pad.get_mag();
fy14lkaa 12:45b1249b3d9a 34 }
fy14lkaa 12:45b1249b3d9a 35
fy14lkaa 57:45c5de9cefdf 36 void SpaceInvadersEngine::draw(N5110 &lcd)
fy14lkaa 12:45b1249b3d9a 37 {
fy14lkaa 12:45b1249b3d9a 38 // draw the elements in the LCD buffer
fy14lkaa 12:45b1249b3d9a 39 // pitch
fy14lkaa 57:45c5de9cefdf 40
fy14lkaa 12:45b1249b3d9a 41 //score
fy14lkaa 57:45c5de9cefdf 42 print_scores(lcd);
fy14lkaa 92:8a1b14488ca5 43 alien.draw(lcd);
fy14lkaa 92:8a1b14488ca5 44 space_ship1.draw(lcd);
fy14lkaa 92:8a1b14488ca5 45 bullet1.draw(lcd);
fy14lkaa 56:c8fc0a1f4132 46 }
fy14lkaa 57:45c5de9cefdf 47
fy14lkaa 60:c5f97e0a14eb 48 void SpaceInvadersEngine::update(Gamepad &pad)
fy14lkaa 60:c5f97e0a14eb 49 {
fy14lkaa 62:0bd9b22ad5ed 50 check_goal(pad);
fy14lkaa 92:8a1b14488ca5 51 bullet1.update();
fy14lkaa 92:8a1b14488ca5 52 space_ship1.update(_d,_mag);
fy14lkaa 60:c5f97e0a14eb 53
fy14lkaa 92:8a1b14488ca5 54 alien.update();
fy14lkaa 66:0123bd3dcbc6 55
fy14lkaa 66:0123bd3dcbc6 56 check_Alien_collision(pad);
fy14lkaa 92:8a1b14488ca5 57 checkspace_ship_collisions(pad);
fy14lkaa 66:0123bd3dcbc6 58
fy14lkaa 57:45c5de9cefdf 59
fy14lkaa 66:0123bd3dcbc6 60 }
fy14lkaa 92:8a1b14488ca5 61 /*void SpaceInvadersEngine::check_Alien_collision(Gamepad &pad)
fy14lkaa 66:0123bd3dcbc6 62 {
fy14lkaa 57:45c5de9cefdf 63
fy14lkaa 66:0123bd3dcbc6 64 // read current bullet attributes
fy14lkaa 92:8a1b14488ca5 65 Vector2D bullet_pos = bullet.get_pos();
fy14lkaa 92:8a1b14488ca5 66 Vector2D bullet_velocity = bullet1.get_velocity();
fy14lkaa 66:0123bd3dcbc6 67 //check if the bullet hit the top of the alien
fy14lkaa 57:45c5de9cefdf 68
fy14lkaa 68:175190a03cbd 69 // check if the bullet hits top of the Alien
fy14lkaa 67:4299b64ad5fc 70 if (bullet_pos.y <= 1) { // 1 due to 1 pixel boundary
fy14lkaa 67:4299b64ad5fc 71 bullet_pos.y = 1; // bounce off ceiling without going off screen
fy14lkaa 67:4299b64ad5fc 72 bullet_velocity.y = -bullet_velocity.y;
fy14lkaa 67:4299b64ad5fc 73 // audio feedback
fy14lkaa 67:4299b64ad5fc 74 pad.tone(750.0,0.1);
fy14lkaa 67:4299b64ad5fc 75 }
fy14lkaa 67:4299b64ad5fc 76
fy14lkaa 68:175190a03cbd 77 // check if the bullet hits bottom of the Alien
fy14lkaa 92:8a1b14488ca5 78 else if (bullet_pos.y + bullet1_size >= (HEIGHT-1) ) { // bottom pixel is 47
fy14lkaa 68:175190a03cbd 79 // hit bottom
fy14lkaa 92:8a1b14488ca5 80 bullet_pos.y = (HEIGHT-1) - bullet1_size; // stops bullet going off screen
fy14lkaa 68:175190a03cbd 81 bullet_velocity.y = -bullet_velocity.y;
fy14lkaa 68:175190a03cbd 82 // audio feedback
fy14lkaa 68:175190a03cbd 83 pad.tone(750.0,0.1);
fy14lkaa 68:175190a03cbd 84 }
fy14lkaa 68:175190a03cbd 85
fy14lkaa 68:175190a03cbd 86 // update bullet parameters
fy14lkaa 92:8a1b14488ca5 87 bullet1.set_velocity(bullet_velocity);
fy14lkaa 92:8a1b14488ca5 88 bullet1.set_pos(bullet_pos);
fy14lkaa 66:0123bd3dcbc6 89 }
fy14lkaa 92:8a1b14488ca5 90 */
fy14lkaa 92:8a1b14488ca5 91 /*void SpaceInvadersEngine::checkspace_ship_collisions(Gamepad &pad)
fy14lkaa 92:8a1b14488ca5 92 {
fy14lkaa 92:8a1b14488ca5 93 // read current bullet attributes
fy14lkaa 92:8a1b14488ca5 94 Vector2D bullet_pos = bullet1.get_pos();
fy14lkaa 92:8a1b14488ca5 95 Vector2D bullet_velocity = bullet1.get_velocity();
fy14lkaa 54:095eae44895b 96
fy14lkaa 92:8a1b14488ca5 97
fy14lkaa 72:938d2d65199d 98 / check p1 first
fy14lkaa 92:8a1b14488ca5 99 Vector2D p1_pos = space_ship1.get_pos();
fy14lkaa 72:938d2d65199d 100
fy14lkaa 72:938d2d65199d 101 // see if bullet has hit the paddle by checking for overlaps
fy14lkaa 72:938d2d65199d 102 if (
fy14lkaa 72:938d2d65199d 103 (bullet_pos.y >= space_ship.y) && //top
fy14lkaa 72:938d2d65199d 104 (bullet_pos.y <= space_ship_pos.y + _paddle_height) && //bottom
fy14lkaa 92:8a1b14488ca5 105 (bullet_pos.x >= space_ship1x) && //left
fy14lkaa 92:8a1b14488ca5 106 (bullet_pos.x <= space_ship1x + space_ship1_width) //right
fy14lkaa 72:938d2d65199d 107 ) {
fy14lkaa 72:938d2d65199d 108 // if it has, fix position and reflect x velocity
fy14lkaa 92:8a1b14488ca5 109 bullet_pos.x = _p1x + space_ship1_width;
fy14lkaa 72:938d2d65199d 110 bullet_velocity.x = -bullet_velocity.x;
fy14lkaa 72:938d2d65199d 111 // audio feedback
fy14lkaa 72:938d2d65199d 112 pad.tone(1000.0,0.1);
fy14lkaa 72:938d2d65199d 113 }
fy14lkaa 72:938d2d65199d 114
fy14lkaa 70:7f0b330ff40b 115 }
fy14lkaa 92:8a1b14488ca5 116 */
fy14lkaa 92:8a1b14488ca5 117 /*void SpaceInvadersEngin::check_goal(Gamepad &pad)
fy14lkaa 70:7f0b330ff40b 118 {
fy14lkaa 92:8a1b14488ca5 119 Vector2D bullet_pos = bullet1.get_pos();
fy14lkaa 70:7f0b330ff40b 120 // spaceship has scored
fy14lkaa 92:8a1b14488ca5 121 if (bullet_pos.x + bullet1_size < 0) {
fy14lkaa 70:7f0b330ff40b 122 _sace_ship.add_score();
fy14lkaa 92:8a1b14488ca5 123 bullet1.init(bullet1_size,_speed);
fy14lkaa 70:7f0b330ff40b 124 pad.tone(1500.0,0.5);
fy14lkaa 70:7f0b330ff40b 125 pad.leds_on();
fy14lkaa 70:7f0b330ff40b 126 wait(0.5);
fy14lkaa 70:7f0b330ff40b 127 pad.leds_off();
fy14lkaa 70:7f0b330ff40b 128 }
fy14lkaa 56:c8fc0a1f4132 129
fy14lkaa 71:1f6b665fc047 130 void SpaceInvadersEngin::print_scores(N5110 &lcd) {
fy14lkaa 71:1f6b665fc047 131 // get scores from the spaceship
fy14lkaa 92:8a1b14488ca5 132 int space_ship_score = space_ship1.get_score();
fy14lkaa 57:45c5de9cefdf 133
fy14lkaa 68:175190a03cbd 134
fy14lkaa 71:1f6b665fc047 135 // print to LCD i
fy14lkaa 71:1f6b665fc047 136 char buffer1[14];
fy14lkaa 71:1f6b665fc047 137 sprintf(buffer1,"%2d",space_ship_score);
fy14lkaa 71:1f6b665fc047 138 lcd.printString(buffer1,WIDTH/2 - 20,1); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits
fy14lkaa 68:175190a03cbd 139
fy14lkaa 71:1f6b665fc047 140 }
fy14lkaa 71:1f6b665fc047 141
fy14lkaa 92:8a1b14488ca5 142 */