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
Fork of el17dg by
game/game.cpp@31:becb8f6bf7b7, 2019-04-23 (annotated)
- Committer:
- Noximilien
- Date:
- Tue Apr 23 18:18:57 2019 +0000
- Revision:
- 31:becb8f6bf7b7
- Parent:
- 30:d454d0cb72bc
- Child:
- 32:5403bb974294
Have changed comments structure as well as the content. Finished intro music. Have cleaned a code in some places to make it readable.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Noximilien | 3:10918b0f7a7d | 1 | #include "mbed.h" |
| Noximilien | 3:10918b0f7a7d | 2 | #include "N5110.h" |
| Noximilien | 3:10918b0f7a7d | 3 | #include "Gamepad.h" |
| Noximilien | 3:10918b0f7a7d | 4 | |
| Noximilien | 3:10918b0f7a7d | 5 | #include "models.h" |
| Noximilien | 4:02c63aaa2df9 | 6 | #include "main.h" |
| Noximilien | 4:02c63aaa2df9 | 7 | #include "game.h" |
| Noximilien | 21:0eb394495b8a | 8 | #include "geometry.h" |
| Noximilien | 21:0eb394495b8a | 9 | #include "gameobject.h" |
| Noximilien | 21:0eb394495b8a | 10 | |
| Noximilien | 21:0eb394495b8a | 11 | #include "enemies.h" |
| Noximilien | 22:4dc3c95f2146 | 12 | #include "constants.h" |
| Noximilien | 23:240bc00ef25b | 13 | #include "stars.h" |
| Noximilien | 23:240bc00ef25b | 14 | #include "player.h" |
| Noximilien | 26:676874c42883 | 15 | #include "hud.h" |
| Noximilien | 20:557e84189a57 | 16 | |
| Noximilien | 31:becb8f6bf7b7 | 17 | const int increase_difficulty = 50; |
| Noximilien | 7:42376925945c | 18 | |
| Noximilien | 31:becb8f6bf7b7 | 19 | int GameGlobals::game_score = 0; |
| Noximilien | 31:becb8f6bf7b7 | 20 | int GameGlobals::score_count_for_difficulty = 0; |
| Noximilien | 31:becb8f6bf7b7 | 21 | int GameGlobals::player_lifes = 3; |
| Noximilien | 31:becb8f6bf7b7 | 22 | int GameGlobals::high_score = 0; |
| Noximilien | 31:becb8f6bf7b7 | 23 | bool GameGlobals::is_shield_on = false; |
| Noximilien | 11:cf2ba52e8b7e | 24 | |
| Noximilien | 21:0eb394495b8a | 25 | Enemies enemies; |
| Noximilien | 23:240bc00ef25b | 26 | Stars stars; |
| Noximilien | 23:240bc00ef25b | 27 | Player playerShip; |
| Noximilien | 23:240bc00ef25b | 28 | GameObject gameOverLogo; |
| Noximilien | 23:240bc00ef25b | 29 | GameObject youDied; |
| Noximilien | 30:d454d0cb72bc | 30 | CircleBounds circleBounds; |
| Noximilien | 26:676874c42883 | 31 | Hud hud; |
| Noximilien | 12:bfe3a3deaac3 | 32 | |
| Noximilien | 30:d454d0cb72bc | 33 | /** |
| Noximilien | 29:579e00b7f118 | 34 | * This is the main function of game.cpp, where the actual gameplay happens. |
| Noximilien | 29:579e00b7f118 | 35 | * Here all other functions are activeated, and when the player dies, it |
| Noximilien | 29:579e00b7f118 | 36 | * returns back to main menu "main.cpp". |
| Noximilien | 29:579e00b7f118 | 37 | */ |
| Noximilien | 29:579e00b7f118 | 38 | bool Game::updateAndDraw() { |
| Noximilien | 29:579e00b7f118 | 39 | if (game_over) { |
| Noximilien | 29:579e00b7f118 | 40 | printf("start game \n"); |
| Noximilien | 29:579e00b7f118 | 41 | startNewGame(); |
| Noximilien | 29:579e00b7f118 | 42 | } |
| Noximilien | 31:becb8f6bf7b7 | 43 | if (gamepad.check_event(gamepad.X_PRESSED) && !GameGlobals::is_shield_on){ |
| Noximilien | 29:579e00b7f118 | 44 | // Checking the button second time to prevent double blast. |
| Noximilien | 29:579e00b7f118 | 45 | gamepad.check_event(gamepad.X_PRESSED); |
| Noximilien | 29:579e00b7f118 | 46 | playerShip.fireNewBlast(); |
| Noximilien | 29:579e00b7f118 | 47 | gamepad.tone(200,0.1); |
| Noximilien | 29:579e00b7f118 | 48 | } |
| Noximilien | 29:579e00b7f118 | 49 | playerShip.playerShipMovement(); |
| Noximilien | 29:579e00b7f118 | 50 | stars.starsSpawnDelay(); |
| Noximilien | 29:579e00b7f118 | 51 | increaseGameDifficultyAndEnemySpawnDelay(); |
| Noximilien | 29:579e00b7f118 | 52 | hud.displayLifes(); |
| Noximilien | 29:579e00b7f118 | 53 | playerShip.updateAndDrawBlasts(); |
| Noximilien | 29:579e00b7f118 | 54 | stars.updateAndDrawSmallStars(); |
| Noximilien | 29:579e00b7f118 | 55 | stars.updateAndDrawMediumStars(); |
| Noximilien | 29:579e00b7f118 | 56 | enemies.updateAndDrawEnemies(); |
| Noximilien | 29:579e00b7f118 | 57 | collideEnemiesAndBlasts(); |
| Noximilien | 29:579e00b7f118 | 58 | collideEnemiesBlastsAndPlayer(); |
| Noximilien | 29:579e00b7f118 | 59 | collideEnemiesAndPlayer(); |
| Noximilien | 29:579e00b7f118 | 60 | enemies.updateAndDrawEnemyBlasts(); |
| Noximilien | 29:579e00b7f118 | 61 | hud.drawScore(); |
| Noximilien | 29:579e00b7f118 | 62 | |
| Noximilien | 30:d454d0cb72bc | 63 | return returnToMenu(); |
| Noximilien | 29:579e00b7f118 | 64 | } |
| Noximilien | 29:579e00b7f118 | 65 | |
| Noximilien | 30:d454d0cb72bc | 66 | /** |
| Noximilien | 22:4dc3c95f2146 | 67 | * This function checks whether the requirments for the collision of the two objects, |
| Noximilien | 22:4dc3c95f2146 | 68 | * are met. When those requirments are met the collision of two objects function will |
| Noximilien | 22:4dc3c95f2146 | 69 | * be checking wheter the boundaries of the objects colide. If they do, the blast |
| Noximilien | 22:4dc3c95f2146 | 70 | * becomes inactive, in game score increases and enemy dies. |
| Noximilien | 22:4dc3c95f2146 | 71 | */ |
| Noximilien | 21:0eb394495b8a | 72 | void Game::collideEnemiesAndBlasts() { |
| Noximilien | 21:0eb394495b8a | 73 | for (int i = 0; i < max_enemies; ++i) { |
| Noximilien | 23:240bc00ef25b | 74 | for (int j = 0; j < max_player_blasts; ++j) { |
| Noximilien | 21:0eb394495b8a | 75 | Enemy& enemy = enemies.enemies[i]; |
| Noximilien | 31:becb8f6bf7b7 | 76 | GameObject& blast = playerShip.blasts[j]; |
| Noximilien | 21:0eb394495b8a | 77 | if (enemy.active && !enemy.dead && blast.active) { |
| Noximilien | 30:d454d0cb72bc | 78 | bool collision = circleBounds.circleCollideTwoObjects( |
| Noximilien | 21:0eb394495b8a | 79 | enemy.pos, enemies.enemy_bounds, |
| Noximilien | 31:becb8f6bf7b7 | 80 | blast.pos, playerShip.blast_bounds |
| Noximilien | 12:bfe3a3deaac3 | 81 | ); |
| Noximilien | 12:bfe3a3deaac3 | 82 | if (collision) { |
| Noximilien | 21:0eb394495b8a | 83 | enemy.die(); |
| Noximilien | 29:579e00b7f118 | 84 | printf("enemy got hit and dies from blast"); |
| Noximilien | 31:becb8f6bf7b7 | 85 | GameGlobals::game_score += 30; |
| Noximilien | 31:becb8f6bf7b7 | 86 | GameGlobals::score_count_for_difficulty +=30; |
| Noximilien | 21:0eb394495b8a | 87 | blast.active = false; |
| Noximilien | 12:bfe3a3deaac3 | 88 | } |
| Noximilien | 12:bfe3a3deaac3 | 89 | } |
| Noximilien | 12:bfe3a3deaac3 | 90 | } |
| Noximilien | 12:bfe3a3deaac3 | 91 | } |
| Noximilien | 12:bfe3a3deaac3 | 92 | } |
| Noximilien | 5:2b9181bc5c89 | 93 | |
| Noximilien | 30:d454d0cb72bc | 94 | /** |
| Noximilien | 23:240bc00ef25b | 95 | * This code does the same work as the one before but with two other objects. |
| Noximilien | 22:4dc3c95f2146 | 96 | * It checks whether the requirments for the collision of the two objects, |
| Noximilien | 22:4dc3c95f2146 | 97 | * are met. When those requirments are met the collision of two objects function will |
| Noximilien | 22:4dc3c95f2146 | 98 | * be checking wheter the boundaries of the objects colide. If they do, the blast |
| Noximilien | 22:4dc3c95f2146 | 99 | * becomes inactive, in game score increases and enemy dies. |
| Noximilien | 22:4dc3c95f2146 | 100 | */ |
| Noximilien | 21:0eb394495b8a | 101 | void Game::collideEnemiesBlastsAndPlayer() { |
| Noximilien | 23:240bc00ef25b | 102 | for (int i = 0; i < max_enemies; ++i) { |
| Noximilien | 21:0eb394495b8a | 103 | GameObject& blast = enemies.enemy_blasts[i]; |
| Noximilien | 21:0eb394495b8a | 104 | if (blast.active) { |
| Noximilien | 30:d454d0cb72bc | 105 | bool collision = circleBounds.circleCollideTwoObjects( |
| Noximilien | 31:becb8f6bf7b7 | 106 | playerShip.player.pos, playerShip.player_bounds, |
| Noximilien | 21:0eb394495b8a | 107 | blast.pos, enemies.enemy_blast_bounds |
| Noximilien | 21:0eb394495b8a | 108 | ); |
| Noximilien | 21:0eb394495b8a | 109 | if (collision) { |
| Noximilien | 31:becb8f6bf7b7 | 110 | if (!GameGlobals::is_shield_on){ |
| Noximilien | 30:d454d0cb72bc | 111 | gamepad.tone(423,0.4); |
| Noximilien | 31:becb8f6bf7b7 | 112 | GameGlobals::player_lifes -= 1; |
| Noximilien | 31:becb8f6bf7b7 | 113 | printf("lost a life from blast. left: %i \n", GameGlobals::player_lifes); |
| Noximilien | 30:d454d0cb72bc | 114 | blast.active = false; |
| Noximilien | 30:d454d0cb72bc | 115 | }else{ |
| Noximilien | 30:d454d0cb72bc | 116 | blast.active = false; |
| Noximilien | 30:d454d0cb72bc | 117 | gamepad.tone(700,0.6); |
| Noximilien | 30:d454d0cb72bc | 118 | } |
| Noximilien | 18:6becc9f9de5e | 119 | } |
| Noximilien | 18:6becc9f9de5e | 120 | } |
| Noximilien | 18:6becc9f9de5e | 121 | } |
| Noximilien | 18:6becc9f9de5e | 122 | } |
| Noximilien | 26:676874c42883 | 123 | |
| Noximilien | 30:d454d0cb72bc | 124 | /** |
| Noximilien | 23:240bc00ef25b | 125 | * This code does the same work as the one before but with two other object. |
| Noximilien | 23:240bc00ef25b | 126 | * of enemy ship and the player's ship |
| Noximilien | 22:4dc3c95f2146 | 127 | */ |
| Noximilien | 23:240bc00ef25b | 128 | void Game::collideEnemiesAndPlayer() { |
| Noximilien | 27:f05f4e738ba9 | 129 | for (int i = 0; i < max_enemies; ++i) { |
| Noximilien | 23:240bc00ef25b | 130 | Enemy& enemy = enemies.enemies[i]; |
| Noximilien | 23:240bc00ef25b | 131 | if (enemy.active && !enemy.dead) { |
| Noximilien | 30:d454d0cb72bc | 132 | bool collision = circleBounds.circleCollideTwoObjects( |
| Noximilien | 31:becb8f6bf7b7 | 133 | playerShip.player.pos, playerShip.player_bounds, |
| Noximilien | 23:240bc00ef25b | 134 | enemy.pos, enemies.enemy_bounds |
| Noximilien | 23:240bc00ef25b | 135 | ); |
| Noximilien | 23:240bc00ef25b | 136 | if (collision) { |
| Noximilien | 31:becb8f6bf7b7 | 137 | GameGlobals::player_lifes -= 1; |
| Noximilien | 31:becb8f6bf7b7 | 138 | printf("lost a life from enemy col. left: %i \n", GameGlobals::player_lifes); |
| Noximilien | 23:240bc00ef25b | 139 | enemy.die(); |
| Noximilien | 29:579e00b7f118 | 140 | printf("enemy got hit from collsion and dies"); |
| Noximilien | 23:240bc00ef25b | 141 | } |
| Noximilien | 23:240bc00ef25b | 142 | } |
| Noximilien | 23:240bc00ef25b | 143 | } |
| Noximilien | 13:5c3dc6e827c2 | 144 | } |
| Noximilien | 13:5c3dc6e827c2 | 145 | |
| Noximilien | 30:d454d0cb72bc | 146 | /** |
| Noximilien | 26:676874c42883 | 147 | * This function resets all the values to their intial states when the game is |
| Noximilien | 26:676874c42883 | 148 | * first began when the player dies and wants to restart the game. |
| Noximilien | 23:240bc00ef25b | 149 | * It does not reset the values when the game is paused. |
| Noximilien | 23:240bc00ef25b | 150 | */ |
| Noximilien | 21:0eb394495b8a | 151 | void Game::startNewGame() { |
| Noximilien | 31:becb8f6bf7b7 | 152 | low_frequency_music_counter = 0; |
| Noximilien | 31:becb8f6bf7b7 | 153 | high_frequency_music_counter = 0; |
| Noximilien | 23:240bc00ef25b | 154 | gameOverLogo.pos.x = game_area_x - 29; // 0 - the sprite length |
| Noximilien | 23:240bc00ef25b | 155 | gameOverLogo.pos.y = game_area_y; |
| Noximilien | 23:240bc00ef25b | 156 | youDied.pos.x = game_area_width; |
| Noximilien | 23:240bc00ef25b | 157 | youDied.pos.y = game_area_y; |
| Noximilien | 21:0eb394495b8a | 158 | game_over = false; |
| Noximilien | 31:becb8f6bf7b7 | 159 | playerShip.player.pos.x = 0; //player was defined in player.h |
| Noximilien | 31:becb8f6bf7b7 | 160 | playerShip.player.pos.y = 24; |
| Noximilien | 31:becb8f6bf7b7 | 161 | stars.stars_delay = 0; |
| Noximilien | 26:676874c42883 | 162 | enemy_ship_delay_max = 40; |
| Noximilien | 26:676874c42883 | 163 | enemy_ship_delay_counter = enemy_ship_delay_max; |
| Noximilien | 31:becb8f6bf7b7 | 164 | GameGlobals::is_shield_on = false; |
| Noximilien | 31:becb8f6bf7b7 | 165 | GameGlobals::game_score = 0; |
| Noximilien | 31:becb8f6bf7b7 | 166 | GameGlobals::score_count_for_difficulty = 0; |
| Noximilien | 31:becb8f6bf7b7 | 167 | GameGlobals::player_lifes = 3; |
| Noximilien | 31:becb8f6bf7b7 | 168 | hud.resetRedLed(); |
| Noximilien | 26:676874c42883 | 169 | enemy_blast_speed = 3; |
| Noximilien | 26:676874c42883 | 170 | enemy_speed = 1; |
| Noximilien | 21:0eb394495b8a | 171 | for (int i = 0; i < max_enemies; ++i) { |
| Noximilien | 21:0eb394495b8a | 172 | enemies.enemies[i].active = false; |
| Noximilien | 13:5c3dc6e827c2 | 173 | } |
| Noximilien | 23:240bc00ef25b | 174 | for (int i = 0; i < max_player_blasts; ++i) { |
| Noximilien | 31:becb8f6bf7b7 | 175 | playerShip.blasts[i].active = false; |
| Noximilien | 13:5c3dc6e827c2 | 176 | } |
| Noximilien | 21:0eb394495b8a | 177 | for (int i = 0; i < max_enemy_blasts; ++i) { |
| Noximilien | 21:0eb394495b8a | 178 | enemies.enemy_blasts[i].active = false; |
| Noximilien | 13:5c3dc6e827c2 | 179 | } |
| Noximilien | 29:579e00b7f118 | 180 | //Reset start and Y button event to avoid errors |
| Noximilien | 29:579e00b7f118 | 181 | gamepad.check_event(gamepad.START_PRESSED); |
| Noximilien | 29:579e00b7f118 | 182 | gamepad.check_event(gamepad.Y_PRESSED); |
| Noximilien | 13:5c3dc6e827c2 | 183 | } |
| Noximilien | 13:5c3dc6e827c2 | 184 | |
| Noximilien | 30:d454d0cb72bc | 185 | /** |
| Noximilien | 23:240bc00ef25b | 186 | * A game over function that shows the sprites of "game over" and "you died". |
| Noximilien | 23:240bc00ef25b | 187 | * Allows to reset the game to play again. |
| Noximilien | 23:240bc00ef25b | 188 | */ |
| Noximilien | 23:240bc00ef25b | 189 | void Game::gameOver() { |
| Noximilien | 30:d454d0cb72bc | 190 | drawGameOver(); |
| Noximilien | 30:d454d0cb72bc | 191 | lcd.normalMode(); |
| Noximilien | 26:676874c42883 | 192 | char buffer[32]; |
| Noximilien | 31:becb8f6bf7b7 | 193 | sprintf(buffer,"Your Score %i", GameGlobals::game_score); |
| Noximilien | 24:0570cb4b92d7 | 194 | lcd.printString(buffer,0,3); |
| Noximilien | 23:240bc00ef25b | 195 | wait(1); |
| Noximilien | 23:240bc00ef25b | 196 | lcd.printString("Press Y",0,4); |
| Noximilien | 23:240bc00ef25b | 197 | lcd.printString("to restart",0,5); |
| Noximilien | 23:240bc00ef25b | 198 | lcd.refresh(); |
| Noximilien | 29:579e00b7f118 | 199 | gamepad.check_event(gamepad.START_PRESSED); |
| Noximilien | 29:579e00b7f118 | 200 | while (!gamepad.check_event(gamepad.Y_PRESSED)){///////////////////////////////// |
| Noximilien | 29:579e00b7f118 | 201 | musicGameOver(); |
| Noximilien | 29:579e00b7f118 | 202 | ledsGameOver(); |
| Noximilien | 23:240bc00ef25b | 203 | } |
| Noximilien | 23:240bc00ef25b | 204 | } |
| Noximilien | 23:240bc00ef25b | 205 | |
| Noximilien | 30:d454d0cb72bc | 206 | /** |
| Noximilien | 26:676874c42883 | 207 | * A function tbat delays enemy spawn on low game score. |
| Noximilien | 26:676874c42883 | 208 | * It decreases the enemy spawn delay as in game score increases. |
| Noximilien | 29:579e00b7f118 | 209 | * The enemy spawn delay is located in game.cpp because the game difficulty |
| Noximilien | 29:579e00b7f118 | 210 | * depends on the on how fast enemy appears. |
| Noximilien | 26:676874c42883 | 211 | */ |
| Noximilien | 26:676874c42883 | 212 | void Game::increaseGameDifficultyAndEnemySpawnDelay(){ |
| Noximilien | 26:676874c42883 | 213 | if (enemy_ship_delay_counter <= 0){ |
| Noximilien | 26:676874c42883 | 214 | enemies.spawnNewEnemy(); |
| Noximilien | 26:676874c42883 | 215 | enemy_ship_delay_counter = enemy_ship_delay_max; |
| Noximilien | 26:676874c42883 | 216 | } |
| Noximilien | 26:676874c42883 | 217 | else { enemy_ship_delay_counter -= 1;} |
| Noximilien | 26:676874c42883 | 218 | |
| Noximilien | 31:becb8f6bf7b7 | 219 | if (enemy_ship_delay_max >= 4 && GameGlobals::score_count_for_difficulty >= increase_difficulty){ |
| Noximilien | 29:579e00b7f118 | 220 | //decrease enemy delay spawn. |
| Noximilien | 26:676874c42883 | 221 | enemy_ship_delay_max -= 3; |
| Noximilien | 26:676874c42883 | 222 | if (enemy_ship_delay_max <= 20 && enemy_ship_delay_max >= 15){ |
| Noximilien | 26:676874c42883 | 223 | enemy_blast_speed += 1; |
| Noximilien | 26:676874c42883 | 224 | enemy_speed += 1; |
| Noximilien | 26:676874c42883 | 225 | } |
| Noximilien | 31:becb8f6bf7b7 | 226 | GameGlobals::score_count_for_difficulty = 0; |
| Noximilien | 26:676874c42883 | 227 | } |
| Noximilien | 31:becb8f6bf7b7 | 228 | if (GameGlobals::game_score >= 500){ |
| Noximilien | 31:becb8f6bf7b7 | 229 | lcd.inverseMode(); |
| Noximilien | 31:becb8f6bf7b7 | 230 | } |
| Noximilien | 26:676874c42883 | 231 | } |
| Noximilien | 26:676874c42883 | 232 | |
| Noximilien | 30:d454d0cb72bc | 233 | bool Game::forceShildActivate(){ |
| Noximilien | 30:d454d0cb72bc | 234 | if (gamepad.check_event(gamepad.R_PRESSED)){ |
| Noximilien | 31:becb8f6bf7b7 | 235 | GameGlobals::is_shield_on = !GameGlobals::is_shield_on; |
| Noximilien | 30:d454d0cb72bc | 236 | } |
| Noximilien | 31:becb8f6bf7b7 | 237 | return GameGlobals::is_shield_on; |
| Noximilien | 30:d454d0cb72bc | 238 | } |
| Noximilien | 30:d454d0cb72bc | 239 | |
| Noximilien | 30:d454d0cb72bc | 240 | /** |
| Noximilien | 27:f05f4e738ba9 | 241 | * This is a single line function to set the player lifes to 0 when the. |
| Noximilien | 27:f05f4e738ba9 | 242 | * game is over. |
| Noximilien | 27:f05f4e738ba9 | 243 | */ |
| Noximilien | 27:f05f4e738ba9 | 244 | bool Game::checkForGameOver() { |
| Noximilien | 31:becb8f6bf7b7 | 245 | return GameGlobals::player_lifes == 0; |
| Noximilien | 28:35af3843de8f | 246 | } |
| Noximilien | 29:579e00b7f118 | 247 | |
| Noximilien | 30:d454d0cb72bc | 248 | /** |
| Noximilien | 29:579e00b7f118 | 249 | * A separate function that draws game over. */ |
| Noximilien | 28:35af3843de8f | 250 | void Game::drawGameOver(){ |
| Noximilien | 28:35af3843de8f | 251 | for (int i = 0; i < 42; i++){ |
| Noximilien | 29:579e00b7f118 | 252 | musicGameOver(); |
| Noximilien | 28:35af3843de8f | 253 | lcd.clear(); |
| Noximilien | 29:579e00b7f118 | 254 | gameOverLogo.pos.x += 1; |
| Noximilien | 29:579e00b7f118 | 255 | youDied.pos.x -= 1; |
| Noximilien | 29:579e00b7f118 | 256 | drawSprite(gameOverLogo.pos, game_over_sprite); |
| Noximilien | 29:579e00b7f118 | 257 | drawSprite(youDied.pos, you_died_sprite); |
| Noximilien | 28:35af3843de8f | 258 | lcd.refresh(); |
| Noximilien | 28:35af3843de8f | 259 | wait(0.1); |
| Noximilien | 28:35af3843de8f | 260 | } |
| Noximilien | 29:579e00b7f118 | 261 | } |
| Noximilien | 29:579e00b7f118 | 262 | |
| Noximilien | 29:579e00b7f118 | 263 | void Game::ledsGameOver(){ |
| Noximilien | 29:579e00b7f118 | 264 | gamepad.led(1,(float)led_state); |
| Noximilien | 29:579e00b7f118 | 265 | gamepad.led(2,(float)!led_state); |
| Noximilien | 29:579e00b7f118 | 266 | gamepad.led(3,(float)led_state); |
| Noximilien | 29:579e00b7f118 | 267 | gamepad.led(4,(float)!led_state); |
| Noximilien | 29:579e00b7f118 | 268 | gamepad.led(5,(float)led_state); |
| Noximilien | 29:579e00b7f118 | 269 | gamepad.led(6,(float)!led_state); |
| Noximilien | 29:579e00b7f118 | 270 | wait(0.5); |
| Noximilien | 29:579e00b7f118 | 271 | led_state = !led_state; |
| Noximilien | 29:579e00b7f118 | 272 | } |
| Noximilien | 29:579e00b7f118 | 273 | |
| Noximilien | 29:579e00b7f118 | 274 | void Game::musicGameOver(){ |
| Noximilien | 29:579e00b7f118 | 275 | lowFrequencyPartMusic(); |
| Noximilien | 29:579e00b7f118 | 276 | highFrequencyPartMusic(); |
| Noximilien | 29:579e00b7f118 | 277 | high_frequency_music_counter ++; |
| Noximilien | 30:d454d0cb72bc | 278 | //low_frequency_music_counter ++; //comment out this for epic game over beat. |
| Noximilien | 29:579e00b7f118 | 279 | printMusicCountersTest(); |
| Noximilien | 29:579e00b7f118 | 280 | } |
| Noximilien | 29:579e00b7f118 | 281 | |
| Noximilien | 29:579e00b7f118 | 282 | void Game::lowFrequencyPartMusic(){ |
| Noximilien | 30:d454d0cb72bc | 283 | // Low frequency |
| Noximilien | 29:579e00b7f118 | 284 | if (low_frequency_music_counter == 0){ gamepad.tone(60,3);} |
| Noximilien | 29:579e00b7f118 | 285 | else if (low_frequency_music_counter == 3){gamepad.tone(90,3);} |
| Noximilien | 29:579e00b7f118 | 286 | else if (low_frequency_music_counter == 6){gamepad.tone(60,3);} |
| Noximilien | 29:579e00b7f118 | 287 | else if (low_frequency_music_counter == 9){gamepad.tone(80,3);} |
| Noximilien | 29:579e00b7f118 | 288 | else if (low_frequency_music_counter == 12){gamepad.tone(70,2);} |
| Noximilien | 29:579e00b7f118 | 289 | else if (low_frequency_music_counter == 14){gamepad.tone(60,2);} |
| Noximilien | 29:579e00b7f118 | 290 | else if (low_frequency_music_counter == 16){gamepad.tone(70,3);} |
| Noximilien | 29:579e00b7f118 | 291 | else if (low_frequency_music_counter == 19){gamepad.tone(50,1);} |
| Noximilien | 29:579e00b7f118 | 292 | else if (low_frequency_music_counter == 20){gamepad.tone(40,3);} |
| Noximilien | 29:579e00b7f118 | 293 | else if (low_frequency_music_counter== 23){ |
| Noximilien | 29:579e00b7f118 | 294 | gamepad.tone(60,2); |
| Noximilien | 29:579e00b7f118 | 295 | low_frequency_music_counter = 0; |
| Noximilien | 29:579e00b7f118 | 296 | } |
| Noximilien | 29:579e00b7f118 | 297 | } |
| Noximilien | 29:579e00b7f118 | 298 | |
| Noximilien | 29:579e00b7f118 | 299 | void Game::highFrequencyPartMusic(){ |
| Noximilien | 29:579e00b7f118 | 300 | // High frequency |
| Noximilien | 29:579e00b7f118 | 301 | if ( high_frequency_music_counter == 0){ gamepad.tone(300,0.1);} |
| Noximilien | 29:579e00b7f118 | 302 | else if (high_frequency_music_counter == 3){gamepad.tone(250,0.1);} |
| Noximilien | 29:579e00b7f118 | 303 | else if (high_frequency_music_counter == 6){gamepad.tone(230,0.2);} |
| Noximilien | 29:579e00b7f118 | 304 | else if (high_frequency_music_counter == 9){gamepad.tone(250,0.1);} |
| Noximilien | 29:579e00b7f118 | 305 | else if ( high_frequency_music_counter == 12){gamepad.tone(250,0.2);} |
| Noximilien | 29:579e00b7f118 | 306 | else if ( high_frequency_music_counter == 14){gamepad.tone(220,0.1);} |
| Noximilien | 29:579e00b7f118 | 307 | else if ( high_frequency_music_counter == 16){gamepad.tone(210,0.3);} |
| Noximilien | 29:579e00b7f118 | 308 | else if ( high_frequency_music_counter == 19){gamepad.tone(200,1);} |
| Noximilien | 29:579e00b7f118 | 309 | else if ( high_frequency_music_counter == 21){gamepad.tone(250,1);} |
| Noximilien | 29:579e00b7f118 | 310 | else if ( high_frequency_music_counter == 22){ |
| Noximilien | 29:579e00b7f118 | 311 | gamepad.tone(200,1); |
| Noximilien | 29:579e00b7f118 | 312 | high_frequency_music_counter = 0; |
| Noximilien | 29:579e00b7f118 | 313 | } |
| Noximilien | 29:579e00b7f118 | 314 | } |
| Noximilien | 29:579e00b7f118 | 315 | |
| Noximilien | 30:d454d0cb72bc | 316 | /** This small statment checks whether the pause button was pressed or not. |
| Noximilien | 30:d454d0cb72bc | 317 | * If it was pressed, then the game will go back to main menu and will save |
| Noximilien | 30:d454d0cb72bc | 318 | * the current status of the object until the game is continued. |
| Noximilien | 30:d454d0cb72bc | 319 | */ |
| Noximilien | 30:d454d0cb72bc | 320 | bool Game::returnToMenu(){ |
| Noximilien | 30:d454d0cb72bc | 321 | bool want_to_pause = false; |
| Noximilien | 30:d454d0cb72bc | 322 | game_over = checkForGameOver(); |
| Noximilien | 30:d454d0cb72bc | 323 | if (game_over){ |
| Noximilien | 30:d454d0cb72bc | 324 | printf("game over happened\n"); |
| Noximilien | 30:d454d0cb72bc | 325 | gameOver(); |
| Noximilien | 30:d454d0cb72bc | 326 | want_to_pause = true; |
| Noximilien | 30:d454d0cb72bc | 327 | } |
| Noximilien | 30:d454d0cb72bc | 328 | if (gamepad.check_event(gamepad.START_PRESSED)){ |
| Noximilien | 30:d454d0cb72bc | 329 | printf("game paused\n"); |
| Noximilien | 30:d454d0cb72bc | 330 | gamepad.check_event(gamepad.START_PRESSED); |
| Noximilien | 30:d454d0cb72bc | 331 | want_to_pause = true; |
| Noximilien | 30:d454d0cb72bc | 332 | } |
| Noximilien | 30:d454d0cb72bc | 333 | return want_to_pause; |
| Noximilien | 30:d454d0cb72bc | 334 | } |
| Noximilien | 30:d454d0cb72bc | 335 | |
| Noximilien | 29:579e00b7f118 | 336 | void Game::printMusicCountersTest(){ |
| Noximilien | 29:579e00b7f118 | 337 | printf("Low frequency counter value:: %i\n", low_frequency_music_counter); |
| Noximilien | 29:579e00b7f118 | 338 | printf("high frequency counter value:: %i\n", high_frequency_music_counter); |
| Noximilien | 26:676874c42883 | 339 | } |
