diff options
author | Björn Stenberg <bjorn@haxx.se> | 2002-09-24 17:47:09 +0000 |
---|---|---|
committer | Björn Stenberg <bjorn@haxx.se> | 2002-09-24 17:47:09 +0000 |
commit | 614272c45f6235b01685d3aa48b5a154f9362c10 (patch) | |
tree | 289c5be2933d47c6fc36830132db1f1e0a95ddad /apps/recorder | |
parent | b1b8bd4dd159e90f9e197c8870953da079162639 (diff) | |
download | rockbox-614272c45f6235b01685d3aa48b5a154f9362c10.tar.gz rockbox-614272c45f6235b01685d3aa48b5a154f9362c10.zip |
One snake game is enough
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@2402 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/recorder')
-rw-r--r-- | apps/recorder/snake.c | 266 |
1 files changed, 0 insertions, 266 deletions
diff --git a/apps/recorder/snake.c b/apps/recorder/snake.c deleted file mode 100644 index 736d08dbd7..0000000000 --- a/apps/recorder/snake.c +++ /dev/null | |||
@@ -1,266 +0,0 @@ | |||
1 | /* | ||
2 | Snake! | ||
3 | |||
4 | by Itai Shaked | ||
5 | |||
6 | ok, a little explanation - | ||
7 | board holds the snake and apple position - 1+ - snake body (the number | ||
8 | represents the age [1 is the snake's head]). | ||
9 | -1 is an apple, and 0 is a clear spot. | ||
10 | dir 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 | #include "lang.h" | ||
24 | |||
25 | int board[28][16],snakelength; | ||
26 | unsigned int score,hiscore=0; | ||
27 | short dir,frames,apple,level=1,dead=0; | ||
28 | |||
29 | int rand_num (int range) { | ||
30 | return current_tick % range; | ||
31 | } | ||
32 | |||
33 | void 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,str(LANG_SNAKE_SCORE),0); | ||
39 | lcd_putsxy(3,32, pscore,0); | ||
40 | if (score>hiscore) { | ||
41 | hiscore=score; | ||
42 | lcd_putsxy(3,42,str(LANG_SNAKE_HISCORE_NEW),0); | ||
43 | } | ||
44 | else { | ||
45 | snprintf(hscore,sizeof(hscore),str(LANG_SNAKE_HISCORE),hiscore); | ||
46 | lcd_putsxy(3,42,hscore,0); | ||
47 | } | ||
48 | lcd_update(); | ||
49 | sleep(HZ); | ||
50 | dead=1; | ||
51 | } | ||
52 | |||
53 | void 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 | |||
70 | void 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 | |||
91 | void 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 | |||
121 | void 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 | |||
142 | void game_pause (void) { | ||
143 | lcd_clear_display(); | ||
144 | lcd_putsxy(3,12,str(LANG_SNAKE_PAUSE),0); | ||
145 | lcd_putsxy(3,22,str(LANG_SNAKE_RESUME),0); | ||
146 | lcd_putsxy(3,32,str(LANG_SNAKE_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 | |||
162 | void 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 | |||
207 | void 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),str(LANG_SNAKE_LEVEL),level); | ||
225 | snprintf(phscore,sizeof(phscore),str(LANG_SNAKE_HISCORE),hiscore); | ||
226 | lcd_putsxy(3,2, plevel,0); | ||
227 | lcd_putsxy(3,12, str(LANG_SNAKE_RANGE),0); | ||
228 | lcd_putsxy(3,22, str(LANG_SNAKE_QUIT),0); | ||
229 | lcd_putsxy(3,32, str(LANG_SNAKE_START),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),str(LANG_SNAKE_LEVEL),level); | ||
254 | lcd_putsxy(3,2, plevel,0); | ||
255 | lcd_update(); | ||
256 | } | ||
257 | |||
258 | } | ||
259 | |||
260 | bool snake(void) { | ||
261 | game_init(); | ||
262 | lcd_clear_display(); | ||
263 | game(); | ||
264 | return false; | ||
265 | } | ||
266 | |||