![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Matis Requis 201241242
Dependencies: mbed
Tempest Game
Game Screen
The board is made of 12 columns. The Hero stays at the top of the column
Game Controls
To control the hero spaceship point the joystick to the column you want the hero to go to.
Press the A button to shoot a bullet in the column you are currently in.
TempestEngine/TempestEngine.cpp@12:1f1907ebebeb, 2020-05-27 (annotated)
- Committer:
- MatisRequis
- Date:
- Wed May 27 05:53:03 2020 +0000
- Revision:
- 12:1f1907ebebeb
- Parent:
- 10:2ae9d4145410
Final Submission. I have read and agreed with Statement of Academic Integrity
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MatisRequis | 2:d59a92e65bd9 | 1 | #include "TempestEngine.h" |
MatisRequis | 2:d59a92e65bd9 | 2 | |
MatisRequis | 2:d59a92e65bd9 | 3 | TempestEngine::TempestEngine() { |
MatisRequis | 2:d59a92e65bd9 | 4 | |
MatisRequis | 2:d59a92e65bd9 | 5 | } |
MatisRequis | 2:d59a92e65bd9 | 6 | |
MatisRequis | 2:d59a92e65bd9 | 7 | TempestEngine::~TempestEngine() { |
MatisRequis | 2:d59a92e65bd9 | 8 | |
MatisRequis | 2:d59a92e65bd9 | 9 | } |
MatisRequis | 2:d59a92e65bd9 | 10 | |
MatisRequis | 10:2ae9d4145410 | 11 | //initialises all the speeds, score, lives, and the hero |
MatisRequis | 4:8e3ba8d6d915 | 12 | void TempestEngine::init() { |
MatisRequis | 4:8e3ba8d6d915 | 13 | _hero.init(0); |
MatisRequis | 9:759b419fec3b | 14 | _alienspeed = 6; |
MatisRequis | 10:2ae9d4145410 | 15 | _score = 0; |
MatisRequis | 10:2ae9d4145410 | 16 | _herolives = 3; |
MatisRequis | 12:1f1907ebebeb | 17 | _shooting_speed = 5; |
MatisRequis | 2:d59a92e65bd9 | 18 | } |
MatisRequis | 2:d59a92e65bd9 | 19 | |
MatisRequis | 10:2ae9d4145410 | 20 | //draws all the elemnts |
MatisRequis | 2:d59a92e65bd9 | 21 | void TempestEngine::draw(N5110 &lcd) { |
MatisRequis | 3:54132cf073d7 | 22 | _board.draw(lcd); |
MatisRequis | 4:8e3ba8d6d915 | 23 | |
MatisRequis | 4:8e3ba8d6d915 | 24 | //draw hero ship |
MatisRequis | 4:8e3ba8d6d915 | 25 | _hero.draw(lcd); |
MatisRequis | 2:d59a92e65bd9 | 26 | |
MatisRequis | 2:d59a92e65bd9 | 27 | //draw bullets |
MatisRequis | 6:037dfa5064a1 | 28 | draw_bullets(lcd); |
MatisRequis | 2:d59a92e65bd9 | 29 | |
MatisRequis | 9:759b419fec3b | 30 | draw_aliens(lcd); |
MatisRequis | 10:2ae9d4145410 | 31 | display_score(lcd); |
MatisRequis | 10:2ae9d4145410 | 32 | display_lives(lcd); |
MatisRequis | 10:2ae9d4145410 | 33 | } |
MatisRequis | 9:759b419fec3b | 34 | |
MatisRequis | 10:2ae9d4145410 | 35 | //reads the input from the gamepad |
MatisRequis | 4:8e3ba8d6d915 | 36 | void TempestEngine::read_input(Gamepad &pad) { |
MatisRequis | 4:8e3ba8d6d915 | 37 | _d = pad.get_angle(); |
MatisRequis | 7:94bc3e21d664 | 38 | _a = pad.A_pressed(); |
MatisRequis | 4:8e3ba8d6d915 | 39 | } |
MatisRequis | 4:8e3ba8d6d915 | 40 | |
MatisRequis | 4:8e3ba8d6d915 | 41 | |
MatisRequis | 10:2ae9d4145410 | 42 | //updates all the objects and members |
MatisRequis | 4:8e3ba8d6d915 | 43 | void TempestEngine::update() { |
MatisRequis | 10:2ae9d4145410 | 44 | _hero.update(_d); |
MatisRequis | 10:2ae9d4145410 | 45 | create_aliens(); |
MatisRequis | 10:2ae9d4145410 | 46 | update_aliens(); |
MatisRequis | 6:037dfa5064a1 | 47 | create_bullets(); |
MatisRequis | 9:759b419fec3b | 48 | update_bullets(); |
MatisRequis | 9:759b419fec3b | 49 | |
MatisRequis | 10:2ae9d4145410 | 50 | check_alien_bullet_colision(); |
MatisRequis | 10:2ae9d4145410 | 51 | game_over(); |
MatisRequis | 10:2ae9d4145410 | 52 | } |
MatisRequis | 10:2ae9d4145410 | 53 | |
MatisRequis | 10:2ae9d4145410 | 54 | //sets the gamover flag to 1 when there are no lives left |
MatisRequis | 10:2ae9d4145410 | 55 | int TempestEngine::game_over() { |
MatisRequis | 10:2ae9d4145410 | 56 | if (_herolives == 0) { |
MatisRequis | 10:2ae9d4145410 | 57 | return 1; |
MatisRequis | 10:2ae9d4145410 | 58 | } else { |
MatisRequis | 10:2ae9d4145410 | 59 | return 0; |
MatisRequis | 10:2ae9d4145410 | 60 | } |
MatisRequis | 10:2ae9d4145410 | 61 | } |
MatisRequis | 10:2ae9d4145410 | 62 | |
MatisRequis | 10:2ae9d4145410 | 63 | //displays the amount of lives with three hearts |
MatisRequis | 10:2ae9d4145410 | 64 | void TempestEngine::display_lives(N5110 &lcd) { |
MatisRequis | 10:2ae9d4145410 | 65 | |
MatisRequis | 10:2ae9d4145410 | 66 | //heart sprite made with an array |
MatisRequis | 10:2ae9d4145410 | 67 | int heart_sprite[9][9] = { {0,1,1,0,0,0,1,1,0}, {1,1,1,1,0,1,1,1,1}, {1,1,1,1,1,1,1,1,1}, {1,1,1,1,1,1,1,1,1}, {1,1,1,1,1,1,1,1,1}, {0,1,1,1,1,1,1,1,0}, {0,0,1,1,1,1,1,0,0}, {0,0,0,1,1,1,0,0,0}, {0,0,0,0,1,0,0,0,0} }; |
MatisRequis | 10:2ae9d4145410 | 68 | |
MatisRequis | 10:2ae9d4145410 | 69 | //displays the amount of hearts corresponding to the amount of lives left |
MatisRequis | 10:2ae9d4145410 | 70 | if (_herolives == 3) { |
MatisRequis | 10:2ae9d4145410 | 71 | lcd.drawSprite(6, 34, 9, 9, (int*)heart_sprite); |
MatisRequis | 10:2ae9d4145410 | 72 | lcd.drawSprite(6, 18, 9, 9, (int*)heart_sprite); |
MatisRequis | 10:2ae9d4145410 | 73 | lcd.drawSprite(6, 3, 9, 9, (int*)heart_sprite); |
MatisRequis | 10:2ae9d4145410 | 74 | |
MatisRequis | 10:2ae9d4145410 | 75 | } else if (_herolives == 2) { |
MatisRequis | 10:2ae9d4145410 | 76 | lcd.drawSprite(6, 34, 9, 9, (int*)heart_sprite); |
MatisRequis | 10:2ae9d4145410 | 77 | lcd.drawSprite(6, 18, 9, 9, (int*)heart_sprite); |
MatisRequis | 10:2ae9d4145410 | 78 | |
MatisRequis | 10:2ae9d4145410 | 79 | } else if (_herolives == 1) { |
MatisRequis | 10:2ae9d4145410 | 80 | lcd.drawSprite(6, 34, 9, 9, (int*)heart_sprite); |
MatisRequis | 10:2ae9d4145410 | 81 | |
MatisRequis | 10:2ae9d4145410 | 82 | } |
MatisRequis | 10:2ae9d4145410 | 83 | } |
MatisRequis | 10:2ae9d4145410 | 84 | |
MatisRequis | 10:2ae9d4145410 | 85 | //displays the current score |
MatisRequis | 10:2ae9d4145410 | 86 | void TempestEngine::display_score(N5110 &lcd) { |
MatisRequis | 10:2ae9d4145410 | 87 | char buffer[2]; |
MatisRequis | 10:2ae9d4145410 | 88 | sprintf(buffer, "%2d", _score); |
MatisRequis | 10:2ae9d4145410 | 89 | lcd.printString(buffer,71,3); |
MatisRequis | 6:037dfa5064a1 | 90 | } |
MatisRequis | 6:037dfa5064a1 | 91 | |
MatisRequis | 9:759b419fec3b | 92 | |
MatisRequis | 10:2ae9d4145410 | 93 | void TempestEngine::check_alien_bullet_colision() { |
MatisRequis | 10:2ae9d4145410 | 94 | |
MatisRequis | 10:2ae9d4145410 | 95 | //iterates throught the alien vector |
MatisRequis | 10:2ae9d4145410 | 96 | for (int i = 0; i< alien_vect.size(); i++) { |
MatisRequis | 10:2ae9d4145410 | 97 | |
MatisRequis | 10:2ae9d4145410 | 98 | //gets the current alien column and position in the column |
MatisRequis | 10:2ae9d4145410 | 99 | Vector2D alien_pos = alien_vect[i].getcolumnpos(); |
MatisRequis | 10:2ae9d4145410 | 100 | |
MatisRequis | 10:2ae9d4145410 | 101 | //iterates through the bullet vector |
MatisRequis | 10:2ae9d4145410 | 102 | for (int j = 0; j< bullet_vect.size(); j++) { |
MatisRequis | 10:2ae9d4145410 | 103 | |
MatisRequis | 10:2ae9d4145410 | 104 | //gets the current bullet position |
MatisRequis | 10:2ae9d4145410 | 105 | Vector2D bullet_pos = bullet_vect[j].getcolumnpos(); |
MatisRequis | 10:2ae9d4145410 | 106 | |
MatisRequis | 10:2ae9d4145410 | 107 | //removes the bullet and the alien if they are on the same position |
MatisRequis | 10:2ae9d4145410 | 108 | if ( (alien_pos.x == bullet_pos.x) && (alien_pos.y <= bullet_pos.y) ) { |
MatisRequis | 10:2ae9d4145410 | 109 | alien_vect.erase(alien_vect.begin() + i); |
MatisRequis | 10:2ae9d4145410 | 110 | bullet_vect.erase(bullet_vect.begin() + j); |
MatisRequis | 10:2ae9d4145410 | 111 | _score++; |
MatisRequis | 10:2ae9d4145410 | 112 | } |
MatisRequis | 10:2ae9d4145410 | 113 | } |
MatisRequis | 10:2ae9d4145410 | 114 | } |
MatisRequis | 10:2ae9d4145410 | 115 | } |
MatisRequis | 9:759b419fec3b | 116 | |
MatisRequis | 9:759b419fec3b | 117 | |
MatisRequis | 9:759b419fec3b | 118 | |
MatisRequis | 9:759b419fec3b | 119 | |
MatisRequis | 9:759b419fec3b | 120 | ////////////////////////////////////////////////////////////BULLET BEHAVIOR///////////////////////////// |
MatisRequis | 6:037dfa5064a1 | 121 | void TempestEngine::create_bullets() { |
MatisRequis | 10:2ae9d4145410 | 122 | |
MatisRequis | 10:2ae9d4145410 | 123 | //bullet countdown timer to prevent spam of bullets |
MatisRequis | 10:2ae9d4145410 | 124 | if (_bullet_countdown <= 0) { |
MatisRequis | 10:2ae9d4145410 | 125 | |
MatisRequis | 10:2ae9d4145410 | 126 | //checks if A button is pressed |
MatisRequis | 7:94bc3e21d664 | 127 | if (_a) { |
MatisRequis | 10:2ae9d4145410 | 128 | |
MatisRequis | 10:2ae9d4145410 | 129 | //creates new bullet object and adds it to the bullet vector. |
MatisRequis | 6:037dfa5064a1 | 130 | Bullet new_bullet; |
MatisRequis | 6:037dfa5064a1 | 131 | |
MatisRequis | 6:037dfa5064a1 | 132 | new_bullet.init(_hero.get_column()); |
MatisRequis | 6:037dfa5064a1 | 133 | |
MatisRequis | 6:037dfa5064a1 | 134 | bullet_vect.push_back(new_bullet); |
MatisRequis | 6:037dfa5064a1 | 135 | |
MatisRequis | 10:2ae9d4145410 | 136 | //reset antispam counter |
MatisRequis | 10:2ae9d4145410 | 137 | _bullet_countdown = _shooting_speed; |
MatisRequis | 6:037dfa5064a1 | 138 | } |
MatisRequis | 8:5feb1913bc92 | 139 | } |
MatisRequis | 10:2ae9d4145410 | 140 | _bullet_countdown--; |
MatisRequis | 6:037dfa5064a1 | 141 | } |
MatisRequis | 6:037dfa5064a1 | 142 | |
MatisRequis | 7:94bc3e21d664 | 143 | |
MatisRequis | 6:037dfa5064a1 | 144 | //draws all the bullets |
MatisRequis | 6:037dfa5064a1 | 145 | void TempestEngine::draw_bullets(N5110 &lcd) { |
MatisRequis | 6:037dfa5064a1 | 146 | //iterates through the bullet objects |
MatisRequis | 9:759b419fec3b | 147 | for (int i = 0; i< bullet_vect.size(); i++) { |
MatisRequis | 6:037dfa5064a1 | 148 | //draws current bullet |
MatisRequis | 6:037dfa5064a1 | 149 | bullet_vect[i].draw(lcd); |
MatisRequis | 9:759b419fec3b | 150 | } |
MatisRequis | 9:759b419fec3b | 151 | } |
MatisRequis | 9:759b419fec3b | 152 | |
MatisRequis | 9:759b419fec3b | 153 | |
MatisRequis | 9:759b419fec3b | 154 | void TempestEngine::update_bullets() { |
MatisRequis | 10:2ae9d4145410 | 155 | |
MatisRequis | 10:2ae9d4145410 | 156 | //iterates through the bullets |
MatisRequis | 9:759b419fec3b | 157 | for (int i =0; i< bullet_vect.size(); i++) { |
MatisRequis | 9:759b419fec3b | 158 | bullet_vect[i].update(); |
MatisRequis | 6:037dfa5064a1 | 159 | |
MatisRequis | 6:037dfa5064a1 | 160 | //checks if bullet needs to be deleted |
MatisRequis | 6:037dfa5064a1 | 161 | if (bullet_vect[i].checkdelete()) { |
MatisRequis | 10:2ae9d4145410 | 162 | //removes the bullet from the vector |
MatisRequis | 6:037dfa5064a1 | 163 | bullet_vect.erase(bullet_vect.begin() + i); |
MatisRequis | 6:037dfa5064a1 | 164 | } |
MatisRequis | 9:759b419fec3b | 165 | } |
MatisRequis | 9:759b419fec3b | 166 | } |
MatisRequis | 9:759b419fec3b | 167 | |
MatisRequis | 9:759b419fec3b | 168 | |
MatisRequis | 9:759b419fec3b | 169 | |
MatisRequis | 9:759b419fec3b | 170 | |
MatisRequis | 9:759b419fec3b | 171 | ///////////////////////////////////////////////////////////////ALIEN BEHAVIOR////////////////////////////// |
MatisRequis | 9:759b419fec3b | 172 | |
MatisRequis | 9:759b419fec3b | 173 | void TempestEngine::create_aliens() { |
MatisRequis | 10:2ae9d4145410 | 174 | //alien countdown timer to limit the amount of aliens on screen |
MatisRequis | 9:759b419fec3b | 175 | if (_alien_timer <= 0) { |
MatisRequis | 9:759b419fec3b | 176 | |
MatisRequis | 10:2ae9d4145410 | 177 | //creates new alien object and adds it to the alien vector. |
MatisRequis | 9:759b419fec3b | 178 | Alien new_alien; |
MatisRequis | 9:759b419fec3b | 179 | |
MatisRequis | 9:759b419fec3b | 180 | new_alien.init((rand() % 12), _alienspeed); |
MatisRequis | 9:759b419fec3b | 181 | |
MatisRequis | 9:759b419fec3b | 182 | alien_vect.push_back(new_alien); |
MatisRequis | 10:2ae9d4145410 | 183 | |
MatisRequis | 10:2ae9d4145410 | 184 | //randomises the alien spawn time |
MatisRequis | 10:2ae9d4145410 | 185 | _alien_timer = (rand() % 20) + 15; |
MatisRequis | 9:759b419fec3b | 186 | |
MatisRequis | 9:759b419fec3b | 187 | } |
MatisRequis | 9:759b419fec3b | 188 | _alien_timer--; |
MatisRequis | 9:759b419fec3b | 189 | } |
MatisRequis | 9:759b419fec3b | 190 | |
MatisRequis | 9:759b419fec3b | 191 | |
MatisRequis | 9:759b419fec3b | 192 | //draws all the aliens |
MatisRequis | 9:759b419fec3b | 193 | void TempestEngine::draw_aliens(N5110 &lcd) { |
MatisRequis | 9:759b419fec3b | 194 | //iterates through the alien objects |
MatisRequis | 9:759b419fec3b | 195 | for (int i = 0; i< alien_vect.size(); i++) { |
MatisRequis | 9:759b419fec3b | 196 | //draws current alien |
MatisRequis | 9:759b419fec3b | 197 | alien_vect[i].draw(lcd); |
MatisRequis | 6:037dfa5064a1 | 198 | } |
MatisRequis | 9:759b419fec3b | 199 | } |
MatisRequis | 9:759b419fec3b | 200 | |
MatisRequis | 9:759b419fec3b | 201 | |
MatisRequis | 9:759b419fec3b | 202 | void TempestEngine::update_aliens() { |
MatisRequis | 10:2ae9d4145410 | 203 | |
MatisRequis | 10:2ae9d4145410 | 204 | //iterates through the all the aliens and updates them |
MatisRequis | 9:759b419fec3b | 205 | for (int i =0; i< alien_vect.size(); i++) { |
MatisRequis | 9:759b419fec3b | 206 | alien_vect[i].update(); |
MatisRequis | 9:759b419fec3b | 207 | |
MatisRequis | 10:2ae9d4145410 | 208 | //checks if alien needs to be deleted at the top of the board |
MatisRequis | 9:759b419fec3b | 209 | if (alien_vect[i].checkdelete()) { |
MatisRequis | 10:2ae9d4145410 | 210 | |
MatisRequis | 10:2ae9d4145410 | 211 | //removes the alien from the vector and removes a life |
MatisRequis | 9:759b419fec3b | 212 | alien_vect.erase(alien_vect.begin() + i); |
MatisRequis | 10:2ae9d4145410 | 213 | _herolives--; |
MatisRequis | 9:759b419fec3b | 214 | } |
MatisRequis | 9:759b419fec3b | 215 | } |
MatisRequis | 10:2ae9d4145410 | 216 | } |
MatisRequis | 10:2ae9d4145410 | 217 |