summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--apps/games_menu.c6
-rw-r--r--apps/recorder/snake.c266
2 files changed, 270 insertions, 2 deletions
diff --git a/apps/games_menu.c b/apps/games_menu.c
index 49f491753f..dd0c671be4 100644
--- a/apps/games_menu.c
+++ b/apps/games_menu.c
@@ -35,6 +35,7 @@
35#include "sokoban.h" 35#include "sokoban.h"
36#include "wormlet.h" 36#include "wormlet.h"
37extern Menu tetris(void); 37extern Menu tetris(void);
38extern Menu snake(void);
38 39
39Menu games_menu(void) 40Menu games_menu(void)
40{ 41{
@@ -42,9 +43,10 @@ Menu games_menu(void)
42 Menu result; 43 Menu result;
43 44
44 struct menu_items items[] = { 45 struct menu_items items[] = {
45 { "Tetris", tetris }, 46 { "Tetris", tetris },
46 { "Sokoban", sokoban }, 47 { "Sokoban", sokoban },
47 { "Wormlet", wormlet } 48 { "Wormlet", wormlet },
49 { "Snake", snake }
48 }; 50 };
49 51
50 m=menu_init( items, sizeof items / sizeof(struct menu_items) ); 52 m=menu_init( items, sizeof items / sizeof(struct menu_items) );
diff --git a/apps/recorder/snake.c b/apps/recorder/snake.c
new file mode 100644
index 0000000000..b0e42d8fd0
--- /dev/null
+++ b/apps/recorder/snake.c
@@ -0,0 +1,266 @@
1/*
2Snake!
3
4by Itai Shaked
5
6ok, a little explanation -
7board holds the snake and apple position - 1+ - snake body (the number
8represents the age [1 is the snake's head]).
9-1 is an apple, and 0 is a clear spot.
10dir is the current direction of the snake - 0=up, 1=right, 2=down, 3=left;
11
12(i'll write more later)
13*/
14
15#include "sprintf.h"
16
17
18#include "config.h"
19#include "lcd.h"
20#include "button.h"
21#include "kernel.h"
22#include "menu.h"
23
24
25int board[28][16],snakelength;
26unsigned int score,hiscore=0;
27short dir,frames,apple,level=1,dead=0;
28
29int rand_num (int range) {
30 return current_tick % range;
31}
32
33void die (void) {
34 char pscore[5],hscore[17];
35 lcd_clear_display();
36 snprintf(pscore,sizeof(pscore),"%d",score);
37 lcd_putsxy(3,12,"oops...",0);
38 lcd_putsxy(3,22,"Your Score :",0);
39 lcd_putsxy(3,32, pscore,0);
40 if (score>hiscore) {
41 hiscore=score;
42 lcd_putsxy(3,42,"New High Score!",0);
43 }
44 else {
45 snprintf(hscore,sizeof(hscore),"High Score %d",hiscore);
46 lcd_putsxy(3,42,hscore,0);
47 }
48 lcd_update();
49 sleep(HZ);
50 dead=1;
51}
52
53void colission (short x, short y) {
54 switch (board[x][y]) {
55 case 0:
56 break;
57 case -1:
58 snakelength+=2;
59 score+=level;
60 apple=0;
61 break;
62 default:
63 die();
64 break;
65 }
66 if (x==28 || x<0 || y==16 || y<0)
67 die();
68}
69
70void move_head (short x, short y) {
71 switch (dir) {
72 case 0:
73 y-=1;
74 break;
75 case 1:
76 x+=1;
77 break;
78 case 2:
79 y+=1;
80 break;
81 case 3:
82 x-=1;
83 break;
84 }
85 colission (x,y);
86 if (dead) return;
87 board[x][y]=1;
88 lcd_fillrect(x*4,y*4,4,4);
89}
90
91void frame (void) {
92 short x,y,head=0;
93 for (x=0; x<28; x++) {
94 for (y=0; y<16; y++) {
95 switch (board[x][y]) {
96 case 1:
97 if (!head) {
98 move_head(x,y);
99 if (dead) return;
100 board[x][y]++;
101 head=1;
102 }
103 break;
104 case 0:
105 break;
106 case -1:
107 break;
108 default:
109 if (board[x][y]==snakelength) {
110 board[x][y]=0;
111 lcd_clearrect(x*4,y*4,4,4);
112 }
113 else
114 board[x][y]++;
115 }
116 }
117 }
118 lcd_update();
119}
120
121void redraw (void) {
122 short x,y;
123 lcd_clear_display();
124 for (x=0; x<28; x++) {
125 for (y=0; y<16; y++) {
126 switch (board[x][y]) {
127 case -1:
128 lcd_fillrect((x*4)+1,y*4,2,4);
129 lcd_fillrect(x*4,(y*4)+1,4,2);
130 break;
131 case 0:
132 break;
133 default:
134 lcd_fillrect(x*4,y*4,4,4);
135 break;
136 }
137 }
138 }
139 lcd_update();
140}
141
142void game_pause (void) {
143 lcd_clear_display();
144 lcd_putsxy(3,12,"Game Paused",0);
145 lcd_putsxy(3,22,"[play] to resume",0);
146 lcd_putsxy(3,32,"[off] to quit",0);
147 lcd_update();
148 while (1) {
149 switch (button_get(true)) {
150 case BUTTON_OFF:
151 dead=1;
152 return;
153 case BUTTON_PLAY:
154 redraw();
155 sleep(HZ/2);
156 return;
157 }
158 }
159}
160
161
162void game (void) {
163 short x,y;
164 while (1) {
165 frame();
166 if (dead) return;
167 frames++;
168 if (frames==10) {
169 frames=0;
170 if (!apple) {
171 do {
172 x=rand_num(28);
173 y=rand_num(16);
174 } while (board[x][y]);
175 apple=1;
176 board[x][y]=-1;
177 lcd_fillrect((x*4)+1,y*4,2,4);
178 lcd_fillrect(x*4,(y*4)+1,4,2);
179 }
180 }
181
182 sleep(HZ/level);
183
184 switch (button_get(false)) {
185 case BUTTON_UP:
186 if (dir!=2) dir=0;
187 break;
188 case BUTTON_RIGHT:
189 if (dir!=3) dir=1;
190 break;
191 case BUTTON_DOWN:
192 if (dir!=0) dir=2;
193 break;
194 case BUTTON_LEFT:
195 if (dir!=1) dir=3;
196 break;
197 case BUTTON_OFF:
198 dead=1;
199 return;
200 case BUTTON_PLAY:
201 game_pause();
202 break;
203 }
204 }
205}
206
207void game_init(void) {
208 short x,y;
209 char plevel[10],phscore[20];
210
211 for (x=0; x<28; x++) {
212 for (y=0; y<16; y++) {
213 board[x][y]=0;
214 }
215 }
216 dead=0;
217 apple=0;
218 snakelength=4;
219 score=0;
220 board[11][7]=1;
221
222
223 lcd_clear_display();
224 snprintf(plevel,sizeof(plevel),"Level - %d",level);
225 snprintf(phscore,sizeof(phscore),"High Score - %d",hiscore);
226 lcd_putsxy(3,2, plevel,0);
227 lcd_putsxy(3,12, "(1 - slow, 9 - fast)",0);
228 lcd_putsxy(3,22, "[off] to quit",0);
229 lcd_putsxy(3,32, "[play] to start/pause",0);
230 lcd_putsxy(3,42, phscore,0);
231 lcd_update();
232
233 while (1) {
234 switch (button_get(true)) {
235 case BUTTON_RIGHT:
236 case BUTTON_UP:
237 if (level<9)
238 level++;
239 break;
240 case BUTTON_LEFT:
241 case BUTTON_DOWN:
242 if (level>1)
243 level--;
244 break;
245 case BUTTON_OFF:
246 dead=1;
247 return;
248 break;
249 case BUTTON_PLAY:
250 return;
251 break;
252 }
253 snprintf(plevel,sizeof(plevel),"Level - %d",level);
254 lcd_putsxy(3,2, plevel,0);
255 lcd_update();
256 }
257
258}
259
260Menu snake(void) {
261 game_init();
262 lcd_clear_display();
263 game();
264 return MENU_OK;
265}
266