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
Diff: GameEngine/GameEngine.cpp
- Revision:
- 9:f0213e632379
- Parent:
- 8:1e4182ebb063
- Child:
- 10:3958fb08696d
diff -r 1e4182ebb063 -r f0213e632379 GameEngine/GameEngine.cpp
--- a/GameEngine/GameEngine.cpp Thu May 21 14:16:51 2020 +0000
+++ b/GameEngine/GameEngine.cpp Fri May 22 11:08:40 2020 +0000
@@ -12,6 +12,7 @@
void GameEngine::init(int head_size, int head_speed)
{
+ _game_over = false;
_score = 0;
_mouse = 0;
_head_size = head_size;
@@ -37,8 +38,10 @@
void GameEngine::draw(N5110 &lcd)
{
lcd.drawRect(0, 0, WIDTH, HEIGHT, FILL_TRANSPARENT);
- spawn_mouse(lcd);
head.draw(lcd);
+ if (_mouse < 1) {
+ spawn_mouse(lcd);
+ };
}
void GameEngine::spawn_mouse(N5110 &lcd)
@@ -48,10 +51,9 @@
int _mouse_x = rand() %WIDTH;
int _mouse_y = rand() %HEIGHT;
//check if mouse count is less than one, we only want one mouse spawned at a time
- if (_mouse < 1) {
- lcd.drawRect(_mouse_x, _mouse_y, 2, 2, FILL_BLACK);
- _mouse++;
- }
+ lcd.drawRect(_mouse_x, _mouse_y, 2, 2, FILL_BLACK);
+ _mouse = 1;
+
}
void GameEngine::check_mouse_eaten(Gamepad &pad)
@@ -62,7 +64,7 @@
if(headX == _mouse_x && headY == _mouse_y) {
_score = _score + 10;
- _mouse--;
+ _mouse = 0;
pad.tone(750.0, 0.1);
}
}
@@ -74,23 +76,33 @@
int headY = headPos.y;
if (headX < 0+2 || headX > WIDTH-3) {
+ _game_over = true;
game_over(lcd, pad);
}
if (headY < 0+2 || headY > HEIGHT-3) {
+ _game_over = true;
game_over(lcd, pad);
}
}
void GameEngine::game_over(N5110 &lcd, Gamepad &pad)
{
- lcd.clear();
+
+
pad.tone(NOTE_E5, 0.1);
+ wait(0.1);
pad.tone(NOTE_D5, 0.1);
+ wait(0.1);
pad.tone(NOTE_C5, 0.1);
- lcd.printString(" GAME OVER! ", 0, 1);
+ while(_game_over == true){
+ lcd.clear();
+ lcd.printString(" GAME OVER! ", 5, 0);
char buffer[14];
sprintf(buffer, "%2d", _score);
- lcd.printString(buffer, 3, 3);
+ lcd.printString(" You scored: ", 2, 2);
+ lcd.printString(buffer, 8, 4);
+ lcd.refresh();
+ };
}