Skip to content

Commit a65ffbd

Browse files
committed
snake: add variable resolution
1 parent 1c73f89 commit a65ffbd

2 files changed

Lines changed: 46 additions & 25 deletions

File tree

main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,14 @@ void program(void);
7777
void buttontest(void);
7878
void showbmp(void);
7979
void dumpir(void);
80-
void snake(void);
80+
void snakemenu(void);
8181

8282
static const struct menuitem main_menu[] = {
8383
{ .label = "Browse program", .cb = program, },
8484
{ .label = "Button test", .cb = buttontest, },
8585
{ .label = "Show BMP", .cb = showbmp, },
8686
{ .label = "Dump IR data", .cb = dumpir, },
87-
{ .label = "Snake", .cb = snake, },
87+
{ .label = "Snake", .cb = snakemenu, },
8888
};
8989

9090
void __noreturn

snake.c

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "events.h"
2626
#include "buttons.h"
2727
#include "display.h"
28+
#include "menu.h"
2829

2930
enum events {
3031
EV_UP = 1,
@@ -65,10 +66,10 @@ struct pos {
6566
};
6667

6768
static void
68-
random_pos(struct pos *p)
69+
random_pos(struct pos *p, unsigned int res)
6970
{
70-
p->x = (rand() % 44) * 5 + 10;
71-
p->y = (rand() % 44) * 5 + 10;
71+
p->x = (rand() % (240/res - 2)) * res + res;
72+
p->y = (rand() % (240/res - 2)) * res + res;
7273
}
7374

7475
static void
@@ -87,7 +88,7 @@ lost(unsigned int score)
8788
}
8889

8990
void
90-
snake(void)
91+
snake(unsigned int res)
9192
{
9293
struct pos path[256];
9394
struct pos point;
@@ -96,8 +97,8 @@ snake(void)
9697
enum direction ndir = DIR_DOWN;
9798
unsigned int len = 5;
9899
unsigned int i;
99-
uint8_t currentX = 30;
100-
uint8_t currentY = 30;
100+
uint8_t currentX = 6*res;
101+
uint8_t currentY = 6*res;
101102

102103
/* initialize path */
103104
for (i = 0; i < len; i++) {
@@ -111,17 +112,17 @@ snake(void)
111112
srand(4);
112113

113114
/* print border */
114-
dp_fill(0, 0, 240, 5, 0xCB0);
115-
dp_fill(0, 235, 240, 5, 0xCB0);
116-
dp_fill(0, 5, 5, 230, 0xCB0);
117-
dp_fill(235, 5, 5, 230, 0xCB0);
115+
dp_fill(0, 0, 240, res, 0xCB0);
116+
dp_fill(0, 240-res, 240, res, 0xCB0);
117+
dp_fill(0, res, res, 240 - 2*res, 0xCB0);
118+
dp_fill(240-res, res, res, 240 - 2*res, 0xCB0);
118119
/* clear middle part */
119-
dp_fill(5, 5, 230, 230, 0x000);
120+
dp_fill(res, res, 240 - 2*res, 240 - 2*res, 0x000);
120121

121122
/* first point */
122-
point.x = 200;
123-
point.y = 200;
124-
dp_fill(point.x, point.y, 5, 5, 0xF00);
123+
point.x = 240 - 8*res;
124+
point.y = 240 - 8*res;
125+
dp_fill(point.x, point.y, res, res, 0xF00);
125126

126127
buttons_config(snake_buttons);
127128

@@ -153,18 +154,18 @@ snake(void)
153154

154155
cdir = ndir;
155156
switch (cdir) {
156-
case DIR_DOWN: currentY += 5; break;
157-
case DIR_UP: currentY -= 5; break;
158-
case DIR_LEFT: currentX -= 5; break;
159-
case DIR_RIGHT: currentX += 5; break;
157+
case DIR_DOWN: currentY += res; break;
158+
case DIR_UP: currentY -= res; break;
159+
case DIR_LEFT: currentX -= res; break;
160+
case DIR_RIGHT: currentX += res; break;
160161
}
161162

162163
/* did we eat a point? */
163164
if (currentX == point.x && currentY == point.y) {
164165
if (len < (ARRAY_SIZE(path) - 1))
165166
len += 1;
166167
/* generate new point */
167-
random_pos(&point);
168+
random_pos(&point, res);
168169
}
169170

170171
/* calculate tail index */
@@ -174,10 +175,10 @@ snake(void)
174175
k = i - len;
175176

176177
/* clear tail point */
177-
dp_fill(path[k].x, path[k].y, 5, 5, 0x000);
178+
dp_fill(path[k].x, path[k].y, res, res, 0x000);
178179

179180
/* paint new head */
180-
dp_fill(currentX, currentY, 5, 5, 0xCB0);
181+
dp_fill(currentX, currentY, res, res, 0xCB0);
181182

182183
/* did we hit ourself? */
183184
while (1) {
@@ -192,7 +193,7 @@ snake(void)
192193
}
193194

194195
/* did we hit the wall? */
195-
if (currentX == 0 || currentX == 235 || currentY == 0 || currentY == 235) {
196+
if (currentX == 0 || currentX == 240-res || currentY == 0 || currentY == 240-res) {
196197
ticker_stop(&tick);
197198
lost(len - 5);
198199
return;
@@ -201,11 +202,31 @@ snake(void)
201202
/* paint red dot last since it might
202203
* be inside the snake
203204
*/
204-
dp_fill(point.x, point.y, 5, 5, 0xF00);
205+
dp_fill(point.x, point.y, res, res, 0xF00);
205206

206207
/* record this point */
207208
path[i].x = currentX;
208209
path[i].y = currentY;
209210
i = (i + 1) % ARRAY_SIZE(path);
210211
}
211212
}
213+
214+
static void snake4(void) { snake(4); }
215+
static void snake5(void) { snake(5); }
216+
static void snake6(void) { snake(6); }
217+
static void snake8(void) { snake(8); }
218+
static void snake10(void) { snake(10); }
219+
220+
void
221+
snakemenu(void)
222+
{
223+
static const struct menuitem snakemenu[] = {
224+
{ .label = "4", .cb = snake4, },
225+
{ .label = "5", .cb = snake5, },
226+
{ .label = "6", .cb = snake6, },
227+
{ .label = "8", .cb = snake8, },
228+
{ .label = "10",.cb = snake10, },
229+
};
230+
231+
menu(snakemenu, ARRAY_SIZE(snakemenu), 0xCB0, 0x000);
232+
}

0 commit comments

Comments
 (0)