forked from benanderman/spork_console
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspace_invaders.cpp
More file actions
234 lines (199 loc) · 6.86 KB
/
Copy pathspace_invaders.cpp
File metadata and controls
234 lines (199 loc) · 6.86 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include "arduino.h"
#include "space_invaders.h"
#include "graphics.h"
// Invaders are drawn with SPACING pixels between each one.
// Pixel position of invader (r,c) = (inv_x + c*SPACING, inv_y + r*SPACING)
#define INV_COLS 4
#define INV_ROWS 2
#define SPACING 2
bool SpaceInvaders::run(Display& disp, Controller *controllers, uint8_t controller_count) {
SpaceInvaders game = SpaceInvaders(disp, controllers, controller_count);
return game.play();
}
MenuOption SpaceInvaders::menuOption() {
static const char PROGMEM graphic[] =
"__________"
"_3_3_3_3__"
"__________"
"_3_3_3_3__"
"__________"
"____2_____"
"__________"
"__________"
"____1_____"
"__________";
return MenuOption(graphic, SpaceInvaders::setPalette, SpaceInvaders::run);
}
void SpaceInvaders::setPalette(Display &disp) {
disp.palette[0] = RGB(0, 0, 0);
disp.palette[1] = RGB(4, 16, 4); // player
disp.palette[2] = RGB(4, 16, 16); // player bullet
disp.palette[3] = RGB(16, 4, 4); // invaders
disp.palette[4] = RGB(16, 12, 0); // enemy bullet
disp.palette[5] = RGB(16, 16, 16); // end game flash
}
bool SpaceInvaders::play() {
SpaceInvaders::setPalette(disp);
randomSeed(millis());
bool invaders[INV_ROWS][INV_COLS];
for (int r = 0; r < INV_ROWS; r++)
for (int c = 0; c < INV_COLS; c++)
invaders[r][c] = true;
// inv_x / inv_y = pixel position of the top-left invader (col 0, row 0)
int8_t inv_x = 1;
int8_t inv_y = 0;
int8_t inv_dir = 1;
int inv_count = INV_ROWS * INV_COLS;
player_x = disp.width / 2;
prev_fire = false;
int8_t player_y = disp.height - 1;
int8_t bullet_x = 0, bullet_y = -1;
bool bullet_active = false;
int8_t ebullet_x = 0, ebullet_y = -1;
bool ebullet_active = false;
unsigned long last_player_move = millis();
unsigned long last_bullet_move = millis();
unsigned long last_ebullet_move = millis();
unsigned long last_inv_move = millis();
unsigned long last_enemy_fire = millis();
int inv_speed = 700;
const int bullet_speed = 100;
const int player_speed = 80;
int enemy_fire_interval = 2500;
bool alive = true;
while (alive) {
unsigned long now = millis();
// Player input
if (now > last_player_move + player_speed) {
bool fire = false;
if (handle_input(fire)) return true;
if (fire && !bullet_active) {
bullet_x = player_x;
bullet_y = player_y - 1;
bullet_active = true;
}
last_player_move = now;
}
// Move player bullet upward
if (bullet_active && now > last_bullet_move + bullet_speed) {
bullet_y--;
if (bullet_y < 0) {
bullet_active = false;
} else {
// Check if the bullet pixel lands on a living invader
int8_t rel_x = bullet_x - inv_x;
int8_t rel_y = bullet_y - inv_y;
if (rel_x >= 0 && rel_x % SPACING == 0 &&
rel_y >= 0 && rel_y % SPACING == 0) {
int col = rel_x / SPACING;
int row = rel_y / SPACING;
if (col < INV_COLS && row < INV_ROWS && invaders[row][col]) {
invaders[row][col] = false;
bullet_active = false;
inv_count--;
}
}
}
last_bullet_move = now;
}
// Move enemy bullet downward
if (ebullet_active && now > last_ebullet_move + bullet_speed) {
ebullet_y++;
if (ebullet_y > player_y) {
ebullet_active = false;
} else if (ebullet_x == player_x && ebullet_y == player_y) {
alive = false;
}
last_ebullet_move = now;
}
// Enemy fires from the bottom-most alive invader in a random column
if (!ebullet_active && now > last_enemy_fire + enemy_fire_interval) {
int col = random(INV_COLS);
for (int r = INV_ROWS - 1; r >= 0; r--) {
if (invaders[r][col]) {
ebullet_x = inv_x + col * SPACING;
ebullet_y = inv_y + r * SPACING + 1;
ebullet_active = (ebullet_x >= 0 && ebullet_x < disp.width);
break;
}
}
last_enemy_fire = now;
}
// Move invader group
if (now > last_inv_move + inv_speed) {
// Find leftmost and rightmost alive column indices
int8_t leftmost = INV_COLS, rightmost = -1;
for (int c = 0; c < INV_COLS; c++) {
for (int r = 0; r < INV_ROWS; r++) {
if (invaders[r][c]) {
if (c < leftmost) leftmost = c;
if (c > rightmost) rightmost = c;
break;
}
}
}
int8_t left_px = inv_x + leftmost * SPACING;
int8_t right_px = inv_x + rightmost * SPACING;
bool hit_edge = (inv_dir > 0 && right_px >= disp.width - 1) ||
(inv_dir < 0 && left_px <= 0);
if (hit_edge) {
inv_dir = -inv_dir;
inv_y++;
// Check if bottom row reached the player
for (int r = INV_ROWS - 1; r >= 0 && alive; r--) {
for (int c = 0; c < INV_COLS && alive; c++) {
if (invaders[r][c] && inv_y + r * SPACING >= player_y)
alive = false;
}
}
} else {
inv_x += inv_dir;
}
last_inv_move = now;
}
// New wave when all invaders cleared
if (inv_count <= 0) {
inv_speed = max(250, inv_speed - 50);
enemy_fire_interval = max(800, enemy_fire_interval - 200);
for (int r = 0; r < INV_ROWS; r++)
for (int c = 0; c < INV_COLS; c++)
invaders[r][c] = true;
inv_count = INV_ROWS * INV_COLS;
inv_x = 1;
inv_y = 0;
inv_dir = 1;
bullet_active = false;
ebullet_active = false;
}
// Draw
disp.clear_all();
for (int r = 0; r < INV_ROWS; r++)
for (int c = 0; c < INV_COLS; c++)
if (invaders[r][c])
disp.set_pixel(inv_x + c * SPACING, inv_y + r * SPACING, 3);
disp.set_pixel(player_x, player_y, 1);
if (bullet_active) disp.set_pixel(bullet_x, bullet_y, 2);
if (ebullet_active) disp.set_pixel(ebullet_x, ebullet_y, 4);
disp.refresh();
delay(1);
}
return Graphics::end_game(disp, controllers, controller_count, 5, 0);
}
bool SpaceInvaders::handle_input(bool &fire) {
Controller::update_state(controllers, controller_count);
bool left = false, right = false, shoot = false, start = false;
for (int i = 0; i < controller_count; i++) {
if (controllers[i].is_connected()) {
left = left || controllers[i][Controller::Button::left];
right = right || controllers[i][Controller::Button::right];
shoot = shoot || controllers[i][Controller::Button::a]
|| controllers[i][Controller::Button::b];
start = start || controllers[i][Controller::Button::start];
}
}
if (left) player_x = max((int8_t)0, (int8_t)(player_x - 1));
if (right) player_x = min((int8_t)(disp.width - 1), (int8_t)(player_x + 1));
fire = shoot && !prev_fire;
prev_fire = shoot;
return start;
}