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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include "../../../inc/MarlinConfigPre.h"
#if ENABLED(MARLIN_SNAKE)
#include "game.h"
#define SNAKE_BOX 4
#define HEADER_H (MENU_FONT_ASCENT - 2)
#define SNAKE_WH (SNAKE_BOX + 1)
#define IDEAL_L 2
#define IDEAL_R (LCD_PIXEL_WIDTH - 1 - 2)
#define IDEAL_T (HEADER_H + 2)
#define IDEAL_B (LCD_PIXEL_HEIGHT - 1 - 2)
#define IDEAL_W (IDEAL_R - (IDEAL_L) + 1)
#define IDEAL_H (IDEAL_B - (IDEAL_T) + 1)
#define GAME_W int((IDEAL_W) / (SNAKE_WH))
#define GAME_H int((IDEAL_H) / (SNAKE_WH))
#define BOARD_W ((SNAKE_WH) * (GAME_W) + 1)
#define BOARD_H ((SNAKE_WH) * (GAME_H) + 1)
#define BOARD_L ((LCD_PIXEL_WIDTH - (BOARD_W) + 1) / 2)
#define BOARD_R (BOARD_L + BOARD_W - 1)
#define BOARD_T (((LCD_PIXEL_HEIGHT + IDEAL_T) - (BOARD_H)) / 2)
#define BOARD_B (BOARD_T + BOARD_H - 1)
#define GAMEX(X) (BOARD_L + ((X) * (SNAKE_WH)))
#define GAMEY(Y) (BOARD_T + ((Y) * (SNAKE_WH)))
#if SNAKE_BOX > 2
#define FOOD_WH SNAKE_BOX
#else
#define FOOD_WH 2
#endif
#if SNAKE_BOX < 1
#define SNAKE_SIZ 1
#else
#define SNAKE_SIZ SNAKE_BOX
#endif
constexpr fixed_t snakev = FTOP(0.20);
snake_data_t &sdat = marlin_game_data.snake;
// Remove the first pixel from the tail.
// If needed, shift out the first segment.
void shorten_tail() {
pos_t &p = sdat.snake_tail[0], &q = sdat.snake_tail[1];
bool shift = false;
if (p.x == q.x) {
// Vertical line
p.y += (q.y > p.y) ? 1 : -1;
shift = p.y == q.y;
}
else {
// Horizontal line
p.x += (q.x > p.x) ? 1 : -1;
shift = p.x == q.x;
}
if (shift) {
sdat.head_ind--;
LOOP_LE_N(i, sdat.head_ind)
sdat.snake_tail[i] = sdat.snake_tail[i + 1];
}
}
// The food is on a line
inline bool food_on_line() {
LOOP_L_N(n, sdat.head_ind) {
pos_t &p = sdat.snake_tail[n], &q = sdat.snake_tail[n + 1];
if (p.x == q.x) {
if ((sdat.foodx == p.x - 1 || sdat.foodx == p.x) && WITHIN(sdat.foody, _MIN(p.y, q.y), _MAX(p.y, q.y)))
return true;
}
else if ((sdat.foody == p.y - 1 || sdat.foody == p.y) && WITHIN(sdat.foodx, _MIN(p.x, q.x), _MAX(p.x, q.x)))
return true;
}
return false;
}
// Add a new food blob
void food_reset() {
do {
sdat.foodx = random(0, GAME_W);
sdat.foody = random(0, GAME_H);
} while (food_on_line());
}
// Turn the snake cw or ccw
inline void turn_snake(const bool cw) {
sdat.snake_dir += cw ? 1 : -1;
sdat.snake_dir &= 0x03;
sdat.head_ind++;
sdat.snake_tail[sdat.head_ind].x = FTOB(sdat.snakex);
sdat.snake_tail[sdat.head_ind].y = FTOB(sdat.snakey);
}
// Reset the snake for a new game
void snake_reset() {
// Init the head and velocity
sdat.snakex = BTOF(1);
sdat.snakey = BTOF(GAME_H / 2);
//snakev = FTOP(0.25);
// Init the tail with a cw turn
sdat.snake_dir = 0;
sdat.head_ind = 0;
sdat.snake_tail[0].x = 0;
sdat.snake_tail[0].y = GAME_H / 2;
turn_snake(true);
// Clear food flag
sdat.food_cnt = 5;
// Clear the controls
ui.encoderPosition = 0;
sdat.old_encoder = 0;
}
// Check if head segment overlaps another
bool snake_overlap() {
// 4 lines must exist before a collision is possible
if (sdat.head_ind < 4) return false;
// Is the last segment crossing any others?
const pos_t &h1 = sdat.snake_tail[sdat.head_ind - 1], &h2 = sdat.snake_tail[sdat.head_ind];
// VERTICAL head segment?
if (h1.x == h2.x) {
// Loop from oldest to segment two away from head
LOOP_L_N(n, sdat.head_ind - 2) {
// Segment p to q
const pos_t &p = sdat.snake_tail[n], &q = sdat.snake_tail[n + 1];
if (p.x != q.x) {
// Crossing horizontal segment
if (WITHIN(h1.x, _MIN(p.x, q.x), _MAX(p.x, q.x)) && (h1.y <= p.y) == (h2.y >= p.y)) return true;
} // Overlapping vertical segment
else if (h1.x == p.x && _MIN(h1.y, h2.y) <= _MAX(p.y, q.y) && _MAX(h1.y, h2.y) >= _MIN(p.y, q.y)) return true;
}
}
else {
// Loop from oldest to segment two away from head
LOOP_L_N(n, sdat.head_ind - 2) {
// Segment p to q
const pos_t &p = sdat.snake_tail[n], &q = sdat.snake_tail[n + 1];
if (p.y != q.y) {
// Crossing vertical segment
if (WITHIN(h1.y, _MIN(p.y, q.y), _MAX(p.y, q.y)) && (h1.x <= p.x) == (h2.x >= p.x)) return true;
} // Overlapping horizontal segment
else if (h1.y == p.y && _MIN(h1.x, h2.x) <= _MAX(p.x, q.x) && _MAX(h1.x, h2.x) >= _MIN(p.x, q.x)) return true;
}
}
return false;
}
void SnakeGame::game_screen() {
// Run the snake logic
if (game_frame()) do { // Run logic twice for finer resolution
// Move the snake's head one unit in the current direction
const int8_t oldx = FTOB(sdat.snakex), oldy = FTOB(sdat.snakey);
switch (sdat.snake_dir) {
case 0: sdat.snakey -= snakev; break;
case 1: sdat.snakex += snakev; break;
case 2: sdat.snakey += snakev; break;
case 3: sdat.snakex -= snakev; break;
}
const int8_t x = FTOB(sdat.snakex), y = FTOB(sdat.snakey);
// If movement took place...
if (oldx != x || oldy != y) {
if (!WITHIN(x, 0, GAME_W - 1) || !WITHIN(y, 0, GAME_H - 1)) {
game_state = 0; // Game Over
_BUZZ(400, 40); // Bzzzt!
break; // ...out of do-while
}
sdat.snake_tail[sdat.head_ind].x = x;
sdat.snake_tail[sdat.head_ind].y = y;
// Change snake direction if set
const int8_t enc = int8_t(ui.encoderPosition), diff = enc - sdat.old_encoder;
if (diff) {
sdat.old_encoder = enc;
turn_snake(diff > 0);
}
if (sdat.food_cnt) --sdat.food_cnt; else shorten_tail();
// Did the snake collide with itself or go out of bounds?
if (snake_overlap()) {
game_state = 0; // Game Over
_BUZZ(400, 40); // Bzzzt!
}
// Is the snake at the food?
else if (x == sdat.foodx && y == sdat.foody) {
_BUZZ(5, 220);
_BUZZ(5, 280);
score++;
sdat.food_cnt = 2;
food_reset();
}
}
} while(0);
u8g.setColorIndex(1);
// Draw Score
if (PAGE_UNDER(HEADER_H)) lcd_put_int(0, HEADER_H - 1, score);
// DRAW THE PLAYFIELD BORDER
u8g.drawFrame(BOARD_L - 2, BOARD_T - 2, BOARD_R - BOARD_L + 4, BOARD_B - BOARD_T + 4);
// Draw the snake (tail)
#if SNAKE_WH < 2
// At this scale just draw a line
LOOP_L_N(n, sdat.head_ind) {
const pos_t &p = sdat.snake_tail[n], &q = sdat.snake_tail[n + 1];
if (p.x == q.x) {
const int8_t y1 = GAMEY(_MIN(p.y, q.y)), y2 = GAMEY(_MAX(p.y, q.y));
if (PAGE_CONTAINS(y1, y2))
u8g.drawVLine(GAMEX(p.x), y1, y2 - y1 + 1);
}
else if (PAGE_CONTAINS(GAMEY(p.y), GAMEY(p.y))) {
const int8_t x1 = GAMEX(_MIN(p.x, q.x)), x2 = GAMEX(_MAX(p.x, q.x));
u8g.drawHLine(x1, GAMEY(p.y), x2 - x1 + 1);
}
}
#elif SNAKE_WH == 2
// At this scale draw two lines
LOOP_L_N(n, sdat.head_ind) {
const pos_t &p = sdat.snake_tail[n], &q = sdat.snake_tail[n + 1];
if (p.x == q.x) {
const int8_t y1 = GAMEY(_MIN(p.y, q.y)), y2 = GAMEY(_MAX(p.y, q.y));
if (PAGE_CONTAINS(y1, y2 + 1))
u8g.drawFrame(GAMEX(p.x), y1, 2, y2 - y1 + 1 + 1);
}
else {
const int8_t py = GAMEY(p.y);
if (PAGE_CONTAINS(py, py + 1)) {
const int8_t x1 = GAMEX(_MIN(p.x, q.x)), x2 = GAMEX(_MAX(p.x, q.x));
u8g.drawFrame(x1, py, x2 - x1 + 1 + 1, 2);
}
}
}
#else
// Draw a series of boxes
LOOP_L_N(n, sdat.head_ind) {
const pos_t &p = sdat.snake_tail[n], &q = sdat.snake_tail[n + 1];
if (p.x == q.x) {
const int8_t y1 = _MIN(p.y, q.y), y2 = _MAX(p.y, q.y);
if (PAGE_CONTAINS(GAMEY(y1), GAMEY(y2) + SNAKE_SIZ - 1)) {
for (int8_t i = y1; i <= y2; ++i) {
const int8_t y = GAMEY(i);
if (PAGE_CONTAINS(y, y + SNAKE_SIZ - 1))
u8g.drawBox(GAMEX(p.x), y, SNAKE_SIZ, SNAKE_SIZ);
}
}
}
else {
const int8_t py = GAMEY(p.y);
if (PAGE_CONTAINS(py, py + SNAKE_SIZ - 1)) {
const int8_t x1 = _MIN(p.x, q.x), x2 = _MAX(p.x, q.x);
for (int8_t i = x1; i <= x2; ++i)
u8g.drawBox(GAMEX(i), py, SNAKE_SIZ, SNAKE_SIZ);
}
}
}
#endif
// Draw food
const int8_t fy = GAMEY(sdat.foody);
if (PAGE_CONTAINS(fy, fy + FOOD_WH - 1)) {
const int8_t fx = GAMEX(sdat.foodx);
u8g.drawFrame(fx, fy, FOOD_WH, FOOD_WH);
if (FOOD_WH == 5) u8g.drawPixel(fx + 2, fy + 2);
}
// Draw GAME OVER
if (!game_state) draw_game_over();
// A click always exits this game
if (ui.use_click()) exit_game();
}
void SnakeGame::enter_game() {
init_game(1, game_screen); // 1 = Game running
snake_reset();
food_reset();
}
#endif // MARLIN_SNAKE
|