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