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