-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtetris.cpp
More file actions
210 lines (157 loc) · 4.28 KB
/
tetris.cpp
File metadata and controls
210 lines (157 loc) · 4.28 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <Arduboy.h>
#include "shapes.h"
#include "grid.h"
#include "input_handler.h"
using namespace std;
static Arduboy arduboy;
static shape_actor_t current_actor;
static Grid grid;
static InputHandler input_handler;
static unsigned long time_started;
static unsigned long last_update;
static const unsigned long kDroppingBlockSpeed = 12.0;
static const unsigned long kFallingBlockSpeed = 1.0;
static const unsigned int kBlockDropScoreVal = 5;
static const unsigned int kLineClearScoreVal = 10;
static unsigned long block_speed = kFallingBlockSpeed;
static unsigned int current_score = 0;
bool game_over = false;
tetromino_t* random_tetromino()
{
static const unsigned int num_tetrominos = 7;
tetromino_t *tetrominos[num_tetrominos] = {
&SquareBlock,
&LeftLBlock,
&RightLBlock,
&LongBlock,
&MountainBlock,
&ZBlock,
&SBlock,
};
long random_idx = random(num_tetrominos);
return tetrominos[random_idx];
}
void restart_game()
{
current_score = 0;
game_over = false;
grid.reset();
current_actor.position = { 5, 0 };
current_actor.tetromino = random_tetromino();
}
void next_block()
{
grid.commit_actor(current_actor);
unsigned int lines_cleared = grid.clear_lines();
current_score += kBlockDropScoreVal;
current_score += lines_cleared * kLineClearScoreVal;
current_actor.position = { 5, 0 };
current_actor.tetromino = random_tetromino();
current_actor.rotation = 0;
if (grid.actor_collides(current_actor)) {
game_over = true;
}
}
void move_current_block_down()
{
shape_actor_t ghost_actor(current_actor);
ghost_actor.position.y += 1;
if (grid.actor_collides(ghost_actor)) {
next_block();
} else {
current_actor.position = ghost_actor.position;
}
}
void button_handler(uint8_t button, bool down)
{
if (down && game_over) {
restart_game();
return;
}
shape_actor_t ghost_actor(current_actor);
if (down && button == RIGHT_BUTTON) {
ghost_actor.position.x += 1;
}
if (down && button == LEFT_BUTTON) {
ghost_actor.position.x -= 1;
}
if (down && button == A_BUTTON) {
ghost_actor.rotation -= 1;
}
if (down && button == B_BUTTON) {
ghost_actor.rotation += 1;
}
if (button == DOWN_BUTTON) {
if (down) {
block_speed = kDroppingBlockSpeed;
} else {
block_speed = kFallingBlockSpeed;
}
}
if (!grid.actor_collides(ghost_actor)) {
current_actor.position = ghost_actor.position;
current_actor.rotation = ghost_actor.rotation;
}
}
void draw_hud(int16_t xpos, int16_t ypos)
{
const unsigned int kLineHeight = 8;
arduboy.setTextSize(1);
arduboy.setCursor(xpos, ypos);
arduboy.print("Ardutris");
ypos += kLineHeight + 8;
arduboy.setCursor(xpos, ypos);
const char *scoreText = "SCORE";
arduboy.print(scoreText);
ypos += kLineHeight;
arduboy.setCursor(xpos, ypos);
char scoreString[16];
sprintf(scoreString, "%u", current_score);
arduboy.print(scoreString);
if (game_over) {
ypos += kLineHeight * 2;
arduboy.setCursor(xpos, ypos);
arduboy.print("GAME OVER");
}
}
void update(unsigned long dt)
{
input_handler.handle_input(arduboy);
if (!game_over) {
unsigned long now = millis();
static unsigned long last_movement = 0;
if ( (now - last_movement) > (1000 / block_speed) ) {
// Every second, move interactive actor down by one block
move_current_block_down();
last_movement = now;
}
}
}
void draw(unsigned long dt)
{
arduboy.clear();
arduboy.fillScreen(BLACK);
Grid display_grid;
display_grid.copy(grid);
// Put current actor onto the display grid
display_grid.commit_actor(current_actor);
display_grid.draw(arduboy, 20, 2);
draw_hud(65, 5);
arduboy.display();
}
void setup()
{
randomSeed(analogRead(0));
arduboy.begin();
arduboy.setFrameRate(60);
time_started = millis();
last_update = time_started;
input_handler.button_handler = button_handler;
restart_game();
}
void loop()
{
unsigned long now = millis();
update(now - last_update);
draw(now - last_update);
}