-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgamerunning.cpp
More file actions
109 lines (89 loc) · 2.35 KB
/
Copy pathgamerunning.cpp
File metadata and controls
109 lines (89 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "gamerunning.h"
#include "gameover.h"
#include <Gamebuino-Meta.h>
#include <cassert>
namespace {
void process_keys(Snake& snake)
{
if (gb.buttons.timeHeld(BUTTON_UP) > 0) {
snake.up();
}
if (gb.buttons.timeHeld(BUTTON_DOWN) > 0) {
snake.down();
}
if (gb.buttons.timeHeld(BUTTON_LEFT) > 0) {
snake.left();
}
if (gb.buttons.timeHeld(BUTTON_RIGHT) > 0) {
snake.right();
}
}
uint16_t fruitSound[] = { 0x0005, 0x148, 0x158, 0x168, 0x0000 };
uint16_t moveSound[] = { 0x0005, 0x118, 0x128, 0x0000 };
uint16_t gameOverSound[] = { 0x0005, 0x188, 0x186, 0x180, 0x17E,
0x17C, 0x17A, 0x178, 0x0000 };
}
GameRunning::GameRunning()
: space(0, 0, 10, 7)
{
auto& captured_score = score;
snake.on_move([&captured_score]() {
captured_score += 1;
gb.sound.play(moveSound);
});
auto& captured_fruit_collection = fruitCollection;
auto& captured_snake = snake;
auto& captured_lights = lights;
snake.on_eat([&captured_fruit_collection,
&captured_snake,
&captured_score,
&captured_lights](const Fruit& fruit) {
captured_fruit_collection.remove_fruit(fruit.position);
captured_snake.grow();
const int score = (50 * fruit.life) / fruit.max_life();
captured_score += score;
gb.sound.play(fruitSound);
captured_lights.go();
});
snake.on_self_collision([this]() {
self_collision();
});
snake.on_out_of_bounds([this]() {
out_of_bounds();
});
}
void GameRunning::update()
{
process_keys(snake);
dsp.begin();
snake.update(1, space, fruitCollection);
fruitGenerator.update(1, fruitCollection, space, snake);
fruitCollection.update(1);
lights.update();
fruitCollection.display();
snake.display(dsp);
score.display();
}
void GameRunning::self_collision()
{
game_over();
}
void GameRunning::out_of_bounds()
{
game_over();
}
void GameRunning::game_over()
{
gameOver = true;
gb.sound.play(gameOverSound);
}
bool GameRunning::finished()
{
return gameOver;
}
std::unique_ptr<GameState> GameRunning::new_state()
{
assert(gameOver);
gameOver = false;
return std::unique_ptr<GameState>(new GameOver(score));
}