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
Diff: game/game.cpp
- Revision:
- 30:d454d0cb72bc
- Parent:
- 29:579e00b7f118
- Child:
- 31:becb8f6bf7b7
--- a/game/game.cpp Mon Apr 15 12:59:51 2019 +0000
+++ b/game/game.cpp Tue Apr 16 21:16:33 2019 +0000
@@ -16,6 +16,7 @@
// The speed of every counter is 1Hz
bool game_over = true;
+bool is_shield_on = false;
int game_score;
int high_score = 0;
int score_count_for_difficulty;
@@ -35,20 +36,21 @@
Player playerShip;
GameObject gameOverLogo;
GameObject youDied;
+CircleBounds circleBounds;
Hud hud;
-/**@brief
+/**
* This is the main function of game.cpp, where the actual gameplay happens.
* Here all other functions are activeated, and when the player dies, it
* returns back to main menu "main.cpp".
*/
bool Game::updateAndDraw() {
- printf("update \n");
+ //printf("update \n");
if (game_over) {
printf("start game \n");
startNewGame();
}
- if (gamepad.check_event(gamepad.X_PRESSED)){
+ if (gamepad.check_event(gamepad.X_PRESSED) && !is_shield_on){
// Checking the button second time to prevent double blast.
gamepad.check_event(gamepad.X_PRESSED);
playerShip.fireNewBlast();
@@ -68,27 +70,10 @@
enemies.updateAndDrawEnemyBlasts();
hud.drawScore();
- /**@brief
- * This small statment checks whether the pause button was pressed or not.
- * If it was pressed, then the game will go back to main menu and will save
- * the current status of the object until the game is continued.
- */
- bool want_to_pause = false;
- game_over = checkForGameOver();
- if (game_over){
- printf("game over happened\n");
- gameOver();
- want_to_pause = true;
- }
- if (gamepad.check_event(gamepad.START_PRESSED)){
- printf("pausing the game\n");
- gamepad.check_event(gamepad.START_PRESSED);
- want_to_pause = true;
- }
- return want_to_pause;
+ return returnToMenu();
}
-/**@brief
+/**
* This function checks whether the requirments for the collision of the two objects,
* are met. When those requirments are met the collision of two objects function will
* be checking wheter the boundaries of the objects colide. If they do, the blast
@@ -100,7 +85,7 @@
Enemy& enemy = enemies.enemies[i];
GameObject& blast = blasts[j];
if (enemy.active && !enemy.dead && blast.active) {
- bool collision = circleCollideTwoObjects(
+ bool collision = circleBounds.circleCollideTwoObjects(
enemy.pos, enemies.enemy_bounds,
blast.pos, blast_bounds
);
@@ -116,7 +101,7 @@
}
}
-/**@brief
+/**
* This code does the same work as the one before but with two other objects.
* It checks whether the requirments for the collision of the two objects,
* are met. When those requirments are met the collision of two objects function will
@@ -127,21 +112,26 @@
for (int i = 0; i < max_enemies; ++i) {
GameObject& blast = enemies.enemy_blasts[i];
if (blast.active) {
- bool collision = circleCollideTwoObjects(
+ bool collision = circleBounds.circleCollideTwoObjects(
player.pos, player_bounds,
blast.pos, enemies.enemy_blast_bounds
);
if (collision) {
- gamepad.tone(423,0.4);
- player_lifes -= 1;
- printf("lost a life from blst. left: %i \n", player_lifes);
- blast.active = false;
+ if (!is_shield_on){
+ gamepad.tone(423,0.4);
+ player_lifes -= 1;
+ //printf("lost a life from blast. left: %i \n", player_lifes);
+ blast.active = false;
+ }else{
+ blast.active = false;
+ gamepad.tone(700,0.6);
+ }
}
}
}
}
-/**@brief
+/**
* This code does the same work as the one before but with two other object.
* of enemy ship and the player's ship
*/
@@ -149,7 +139,7 @@
for (int i = 0; i < max_enemies; ++i) {
Enemy& enemy = enemies.enemies[i];
if (enemy.active && !enemy.dead) {
- bool collision = circleCollideTwoObjects(
+ bool collision = circleBounds.circleCollideTwoObjects(
player.pos, player_bounds,
enemy.pos, enemies.enemy_bounds
);
@@ -163,7 +153,7 @@
}
}
-/**@brief
+/**
* This function resets all the values to their intial states when the game is
* first began when the player dies and wants to restart the game.
* It does not reset the values when the game is paused.
@@ -174,6 +164,7 @@
youDied.pos.x = game_area_width;
youDied.pos.y = game_area_y;
game_over = false;
+ is_shield_on = false;
player.pos.x = 0; //player was defined in player.h
player.pos.y = 24;
stars_delay = 0;
@@ -200,12 +191,13 @@
gamepad.check_event(gamepad.Y_PRESSED);
}
-/**@brief
+/**
* A game over function that shows the sprites of "game over" and "you died".
* Allows to reset the game to play again.
*/
void Game::gameOver() {
- drawGameOver();
+ drawGameOver();
+ lcd.normalMode();
char buffer[32];
sprintf(buffer,"Your Score %i",game_score);
lcd.printString(buffer,0,3);
@@ -220,7 +212,7 @@
}
}
-/**@brief
+/**
* A function tbat delays enemy spawn on low game score.
* It decreases the enemy spawn delay as in game score increases.
* The enemy spawn delay is located in game.cpp because the game difficulty
@@ -242,9 +234,17 @@
}
score_count_for_difficulty = 0;
}
+ if (game_score >= 500){lcd.inverseMode();}
}
-/**@brief
+bool Game::forceShildActivate(){
+ if (gamepad.check_event(gamepad.R_PRESSED)){
+ is_shield_on = !is_shield_on;
+ }
+ return is_shield_on;
+}
+
+/**
* This is a single line function to set the player lifes to 0 when the.
* game is over.
*/
@@ -252,7 +252,7 @@
return player_lifes == 0;
}
-/**@brief
+/**
* A separate function that draws game over. */
void Game::drawGameOver(){
for (int i = 0; i < 42; i++){
@@ -282,13 +282,12 @@
lowFrequencyPartMusic();
highFrequencyPartMusic();
high_frequency_music_counter ++;
- low_frequency_music_counter ++; //remove this for epic game over beat.
+ //low_frequency_music_counter ++; //comment out this for epic game over beat.
printMusicCountersTest();
}
void Game::lowFrequencyPartMusic(){
- // Low frequency //
- //60:1, 50:1, 40:1, 30:1, 40:0.5, 50:0.5, 40:0.5, 30:0.5, 40:1, 30:2 = 9s;
+ // Low frequency
if (low_frequency_music_counter == 0){ gamepad.tone(60,3);}
else if (low_frequency_music_counter == 3){gamepad.tone(90,3);}
else if (low_frequency_music_counter == 6){gamepad.tone(60,3);}
@@ -321,6 +320,26 @@
}
}
+/** This small statment checks whether the pause button was pressed or not.
+ * If it was pressed, then the game will go back to main menu and will save
+ * the current status of the object until the game is continued.
+ */
+bool Game::returnToMenu(){
+ bool want_to_pause = false;
+ game_over = checkForGameOver();
+ if (game_over){
+ printf("game over happened\n");
+ gameOver();
+ want_to_pause = true;
+ }
+ if (gamepad.check_event(gamepad.START_PRESSED)){
+ printf("game paused\n");
+ gamepad.check_event(gamepad.START_PRESSED);
+ want_to_pause = true;
+ }
+ return want_to_pause;
+}
+
void Game::printMusicCountersTest(){
printf("Low frequency counter value:: %i\n", low_frequency_music_counter);
printf("high frequency counter value:: %i\n", high_frequency_music_counter);
