diff options
author | Björn Stenberg <bjorn@haxx.se> | 2003-07-02 21:38:08 +0000 |
---|---|---|
committer | Björn Stenberg <bjorn@haxx.se> | 2003-07-02 21:38:08 +0000 |
commit | 973d7a9440c27d3a25e411bb87b18d906b46a478 (patch) | |
tree | 6c9419da4f3a27caa2415fde5b104253fd565f5d /apps/plugins | |
parent | 1a658f0c4ec009e9ee3703c1e05cfdd6c02f78b5 (diff) | |
download | rockbox-973d7a9440c27d3a25e411bb87b18d906b46a478.tar.gz rockbox-973d7a9440c27d3a25e411bb87b18d906b46a478.zip |
Snake is back! Written by Itai Shaked.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@3802 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/plugins')
-rw-r--r-- | apps/plugins/snake.c | 289 |
1 files changed, 289 insertions, 0 deletions
diff --git a/apps/plugins/snake.c b/apps/plugins/snake.c new file mode 100644 index 0000000000..7a302178e4 --- /dev/null +++ b/apps/plugins/snake.c | |||
@@ -0,0 +1,289 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2002 Itai Shaked | ||
11 | * | ||
12 | * All files in this archive are subject to the GNU General Public License. | ||
13 | * See the file COPYING in the source tree root for full license agreement. | ||
14 | * | ||
15 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
16 | * KIND, either express or implied. | ||
17 | * | ||
18 | ****************************************************************************/ | ||
19 | |||
20 | /* | ||
21 | Snake! | ||
22 | |||
23 | by Itai Shaked | ||
24 | |||
25 | ok, a little explanation - | ||
26 | board holds the snake and apple position - 1+ - snake body (the number | ||
27 | represents the age [1 is the snake's head]). | ||
28 | -1 is an apple, and 0 is a clear spot. | ||
29 | dir is the current direction of the snake - 0=up, 1=right, 2=down, 3=left; | ||
30 | |||
31 | */ | ||
32 | |||
33 | #include "plugin.h" | ||
34 | #ifdef HAVE_LCD_BITMAP | ||
35 | |||
36 | static int board[28][16],snakelength; | ||
37 | static unsigned int score,hiscore=0; | ||
38 | static short dir,frames,apple,level=1,dead=0; | ||
39 | static struct plugin_api* rb; | ||
40 | |||
41 | void die (void) | ||
42 | { | ||
43 | char pscore[5],hscore[17]; | ||
44 | rb->lcd_clear_display(); | ||
45 | rb->snprintf(pscore,sizeof(pscore),"%d",score); | ||
46 | rb->lcd_putsxy(3,12,"oops..."); | ||
47 | rb->lcd_putsxy(3,22,"Your score :"); | ||
48 | rb->lcd_putsxy(3,32, pscore); | ||
49 | if (score>hiscore) { | ||
50 | hiscore=score; | ||
51 | rb->lcd_putsxy(3,42,"New High Score!"); | ||
52 | } | ||
53 | else { | ||
54 | rb->snprintf(hscore,sizeof(hscore),"High Score: %d",hiscore); | ||
55 | rb->lcd_putsxy(3,42,hscore); | ||
56 | } | ||
57 | rb->lcd_update(); | ||
58 | rb->sleep(3*HZ); | ||
59 | dead=1; | ||
60 | } | ||
61 | |||
62 | void colission (short x, short y) | ||
63 | { | ||
64 | switch (board[x][y]) { | ||
65 | case 0: | ||
66 | break; | ||
67 | case -1: | ||
68 | snakelength+=2; | ||
69 | score+=level; | ||
70 | apple=0; | ||
71 | break; | ||
72 | default: | ||
73 | die(); | ||
74 | break; | ||
75 | } | ||
76 | if (x==28 || x<0 || y==16 || y<0) | ||
77 | die(); | ||
78 | } | ||
79 | |||
80 | void move_head (short x, short y) | ||
81 | { | ||
82 | switch (dir) { | ||
83 | case 0: | ||
84 | y-=1; | ||
85 | break; | ||
86 | case 1: | ||
87 | x+=1; | ||
88 | break; | ||
89 | case 2: | ||
90 | y+=1; | ||
91 | break; | ||
92 | case 3: | ||
93 | x-=1; | ||
94 | break; | ||
95 | } | ||
96 | colission (x,y); | ||
97 | if (dead) | ||
98 | return; | ||
99 | board[x][y]=1; | ||
100 | rb->lcd_fillrect(x*4,y*4,4,4); | ||
101 | } | ||
102 | |||
103 | void frame (void) | ||
104 | { | ||
105 | short x,y,head=0; | ||
106 | for (x=0; x<28; x++) { | ||
107 | for (y=0; y<16; y++) { | ||
108 | switch (board[x][y]) { | ||
109 | case 1: | ||
110 | if (!head) { | ||
111 | move_head(x,y); | ||
112 | if (dead) | ||
113 | return; | ||
114 | board[x][y]++; | ||
115 | head=1; | ||
116 | } | ||
117 | break; | ||
118 | case 0: | ||
119 | break; | ||
120 | case -1: | ||
121 | break; | ||
122 | default: | ||
123 | if (board[x][y]==snakelength) { | ||
124 | board[x][y]=0; | ||
125 | rb->lcd_clearrect(x*4,y*4,4,4); | ||
126 | } | ||
127 | else | ||
128 | board[x][y]++; | ||
129 | break; | ||
130 | } | ||
131 | } | ||
132 | } | ||
133 | rb->lcd_update(); | ||
134 | } | ||
135 | |||
136 | void redraw (void) | ||
137 | { | ||
138 | short x,y; | ||
139 | rb->lcd_clear_display(); | ||
140 | for (x=0; x<28; x++) { | ||
141 | for (y=0; y<16; y++) { | ||
142 | switch (board[x][y]) { | ||
143 | case -1: | ||
144 | rb->lcd_fillrect((x*4)+1,y*4,2,4); | ||
145 | rb->lcd_fillrect(x*4,(y*4)+1,4,2); | ||
146 | break; | ||
147 | case 0: | ||
148 | break; | ||
149 | default: | ||
150 | rb->lcd_fillrect(x*4,y*4,4,4); | ||
151 | break; | ||
152 | } | ||
153 | } | ||
154 | } | ||
155 | rb->lcd_update(); | ||
156 | } | ||
157 | |||
158 | void game_pause (void) { | ||
159 | rb->lcd_clear_display(); | ||
160 | rb->lcd_putsxy(3,12,"Game Paused"); | ||
161 | rb->lcd_putsxy(3,22,"[Play] to resume"); | ||
162 | rb->lcd_putsxy(3,32,"[Off] to quit"); | ||
163 | rb->lcd_update(); | ||
164 | while (1) { | ||
165 | switch (rb->button_get(true)) { | ||
166 | case BUTTON_OFF: | ||
167 | dead=1; | ||
168 | return; | ||
169 | case BUTTON_PLAY: | ||
170 | redraw(); | ||
171 | rb->sleep(HZ/2); | ||
172 | return; | ||
173 | } | ||
174 | } | ||
175 | } | ||
176 | |||
177 | |||
178 | void game (void) { | ||
179 | short x,y; | ||
180 | while (1) { | ||
181 | frame(); | ||
182 | if (dead) | ||
183 | return; | ||
184 | frames++; | ||
185 | if (frames==10) { | ||
186 | frames=0; | ||
187 | if (!apple) { | ||
188 | do { | ||
189 | x=rb->rand() % 28; | ||
190 | y=rb->rand() % 16; | ||
191 | } while (board[x][y]); | ||
192 | apple=1; | ||
193 | board[x][y]=-1; | ||
194 | rb->lcd_fillrect((x*4)+1,y*4,2,4); | ||
195 | rb->lcd_fillrect(x*4,(y*4)+1,4,2); | ||
196 | } | ||
197 | } | ||
198 | |||
199 | rb->sleep(HZ/level); | ||
200 | |||
201 | switch (rb->button_get(false)) { | ||
202 | case BUTTON_UP: | ||
203 | if (dir!=2) dir=0; | ||
204 | break; | ||
205 | case BUTTON_RIGHT: | ||
206 | if (dir!=3) dir=1; | ||
207 | break; | ||
208 | case BUTTON_DOWN: | ||
209 | if (dir!=0) dir=2; | ||
210 | break; | ||
211 | case BUTTON_LEFT: | ||
212 | if (dir!=1) dir=3; | ||
213 | break; | ||
214 | case BUTTON_OFF: | ||
215 | dead=1; | ||
216 | return; | ||
217 | case BUTTON_PLAY: | ||
218 | game_pause(); | ||
219 | break; | ||
220 | } | ||
221 | } | ||
222 | } | ||
223 | |||
224 | void game_init(void) { | ||
225 | short x,y; | ||
226 | char plevel[10],phscore[20]; | ||
227 | |||
228 | for (x=0; x<28; x++) { | ||
229 | for (y=0; y<16; y++) { | ||
230 | board[x][y]=0; | ||
231 | } | ||
232 | } | ||
233 | dead=0; | ||
234 | apple=0; | ||
235 | snakelength=4; | ||
236 | score=0; | ||
237 | board[11][7]=1; | ||
238 | |||
239 | |||
240 | rb->lcd_clear_display(); | ||
241 | rb->snprintf(plevel,sizeof(plevel),"Level - %d",level); | ||
242 | rb->snprintf(phscore,sizeof(phscore),"High Score: %d",hiscore); | ||
243 | rb->lcd_putsxy(3,2, plevel); | ||
244 | rb->lcd_putsxy(3,12, "(1 - slow, 9 - fast)"); | ||
245 | rb->lcd_putsxy(3,22, "[Off] to quit"); | ||
246 | rb->lcd_putsxy(3,32, "[Play] to start/pause"); | ||
247 | rb->lcd_putsxy(3,42, phscore); | ||
248 | rb->lcd_update(); | ||
249 | |||
250 | while (1) { | ||
251 | switch (rb->button_get(true)) { | ||
252 | case BUTTON_RIGHT: | ||
253 | case BUTTON_UP: | ||
254 | if (level<9) | ||
255 | level++; | ||
256 | break; | ||
257 | case BUTTON_LEFT: | ||
258 | case BUTTON_DOWN: | ||
259 | if (level>1) | ||
260 | level--; | ||
261 | break; | ||
262 | case BUTTON_OFF: | ||
263 | dead=1; | ||
264 | return; | ||
265 | break; | ||
266 | case BUTTON_PLAY: | ||
267 | return; | ||
268 | break; | ||
269 | } | ||
270 | rb->snprintf(plevel,sizeof(plevel),"Level - %d",level); | ||
271 | rb->lcd_putsxy(3,2, plevel); | ||
272 | rb->lcd_update(); | ||
273 | } | ||
274 | |||
275 | } | ||
276 | |||
277 | enum plugin_status plugin_start(struct plugin_api* api, void* parameter) | ||
278 | { | ||
279 | TEST_PLUGIN_API(api); | ||
280 | (void)(parameter); | ||
281 | rb = api; | ||
282 | |||
283 | game_init(); | ||
284 | rb->lcd_clear_display(); | ||
285 | game(); | ||
286 | return false; | ||
287 | } | ||
288 | |||
289 | #endif | ||