gddhy

_(:з」∠)_ 加载中...
  • 主页
  • 归档
  • 工具
  • 关于
所有文章 友链

gddhy

_(:з」∠)_ 加载中...

  • 主页
  • 归档
  • 工具
  • 关于

贪吃蛇

2023-03-07
字数统计:1.1k字 阅读时长≈6分

经典小游戏贪吃蛇,mpc程序

支持触屏+按键,使用手机CAPP 运行

冒泡开发实验系统自带贪吃蛇例程,可以在vmrp 中体验(仅支持按键)

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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
贪吃蛇
by eleqian

按键:
上下左右:改变方向
左软键:开始/暂停
右软键:退出/加速
触屏:
自己体验
*/

#include <base.h>
#include <exb.h>
#define sw SCRW
#define sh SCRH-50
#define s 300 //速度(定时器时长)
#define js 80 //加速速度
#define v 20 //蛇,食物像素大小

/*全局变量*/
int arr[50];
int linc, colc, xside, yside;
int food, last, slen, maxl, state,l,n;
int32 timer;
char txt[4];

int init()
{
setTextSize(1,36);
maxl = 50;

xside = (SCRW % 10) / 2;
if(0 == xside)
{
xside = 10;
}
xside = xside + 10;
yside = (sh % 10) / 2;
if(0 == yside)
{
yside = 10;
}
yside = yside + 10;

colc = (SCRW - xside * 2) / v;
linc = (sh - yside * 2) / v;
timer=timercreate();
inits();
return 0;
}

void inits()
{
int m;

m = (linc / 2) *colc + colc / 2;
arr[0]=m;
arr[1]= m + colc;
arr[2]= m + colc * 2;

slen = 3;
state = 0;
last = _UP;

dform();
dsnake(slen);
dfood();
xs();
}

void go(int s)
{
int j, l;

l = arr[slen - 1];
for(j = slen - 1; j > 0; j = j - 1)
{
arr[j]= arr[j - 1];
}
arr[0]= arr[0] + s;
if(0 != isself())
{
gameover();
}
eat(l);
dsnake(2);
}

void eat(int l)
{
if(arr[0] == food)
{
if(slen < maxl)
{
arr[slen]=l;
slen = slen + 1;
xs();
}
else
{
dclub(l, 0, 0, 0);
}
dfood();

}
else
{
dclub(l, 0, 0, 0);
}
}

int isself()
{
int i, j, r;

i = arr[0];
r = 0;

for(j = 1; j < slen; j = j + 1)
{
if(arr[j] == i)
{
r = 1;
j = slen;
}
}
return r;
}

int isate()
{
int i, r;
r = 0;
for(i = 0; i < slen; i = i + 1)
{
if(arr[i] == food)
{
r = 1;
i = slen;
}
}
return r;
}

void dform()
{
cls(0, 0, 0);
drect(xside - 2, yside, 2, sh-yside*2, 200, 200, 200);
drect(SCRW - xside, yside, 2, sh-yside*2, 200, 200, 200);
drect(xside, yside - 2, sw-xside*2, 2, 200, 200, 200);
drect(xside, sh - yside, sw-xside*2, 2, 200, 200, 200);
ref(0, 0, SCRW, SCRH);
}

void dfood()
{
sand(getuptime());
do
{
food = rand() % (colc *linc);
}
while(0 != isate());

dclub(food, 255, 0, 0);
}

void dsnake(int dl)
{
int i;

dclub(arr[0], 0, 255, 255);

for(i = 1; i < dl; i = i + 1)
{
dclub(arr[i], 0, 255, 0);
}
}


void dclub(int i, int r, int g, int b)
{
int x, y;

x = v *(i % colc) + xside;
y = v *(i / colc) + yside;
drect(x, y, v, v, r / 2, g / 2, b / 2);
drect(x + 1, y + 1, v-2, v-2, r, g, b);
ref(x, y, v, v);
}

void gameover()
{
stop();
inits();
}

void cd(int k)
{
if(k == _LEFT)
{
if(last != _RIGHT)
{
last = k;
}
}
if(k == _RIGHT)
{
if(last != _LEFT)
{
last = k;
}
}
if(k == _UP)
{
if(last != _DOWN)
{
last = k;
}
}
if(k == _DOWN)
{
if(last != _UP)
{
last = k;
}
}

if(last == k)
{
stop();
_timer(0);
start();
}
}

int event(int t, int a, int b)
{
if(t==MS_DOWN)
{
if(state)
{
if(a>0&&a<sw&&b>0&&b<sh)
{ fx(a,b);}
if(a>sw/2&&a<sw&&b>sh&&b<SCRH)
{ event(KY_DOWN,18,0);}
}
}

if(t==MS_UP)
{
if(a>0&&a<sw&&b>sh&&b<SCRH)
{
if(a<sw/2)
{ event(KY_UP,17,0);}
else
{ event(KY_UP,18,0);}
}
}

if(t == KY_DOWN)
{
if(state == 1)
{
if(a==18)
timersettime(timer,js);
cd(a);
}
}

if(t == KY_UP)
{
if(a == _SLEFT)
{
if(0 == state)
{
start();
}
else
{
stop();
}
xs();
}
if(a == _SRIGHT)
{
if(state==1)
timersettime(timer,s);
else
{
timerdel(timer);
exit();
}
}
}
return 0;
}


//触屏方向控制

void fx(int x,int y)
{
l=last;
if(x<sw/2)
if(y<sh/2) cd(l==14||l==15?12:14);
else cd(l==14||l==15?13:14);
if(x>=sw/2)
if(y<sh/2) cd(l==14||l==15?12:15);
else cd(l==14||l==15?13:15);
}

//底部按键显示
void xs()
{
drect(0,sh,sw,50,0,0,0);
dtext(state==0?"开始":"暂停",5,sh+5,200,200,200,0,1);
dtext(state==0?"退出":"加速",sw-75,sh+5,200,200,200,0,1);
sprintf(txt,"%d",slen-3);
dtext(txt,sw/2,sh+5,200,200,200,0,1);
ref(0,sh,sw,50);
}

void start()
{
timerstart(timer, s, 0, "_timer", TRUE);
state = 1;
}

void stop()
{
timerstop(timer);
state = 0;
}

void _timer(int32 data)
{
if(last == _LEFT)
{
if(0 == arr[0] % colc)
{
gameover();
}
else
{
go( - 1);
}
return ;
}
if(last == _DOWN)
{
if(arr[0] >= (linc - 1) *colc)
{
gameover();
}
else
{
go(colc);
}
return ;
}
if(last == _RIGHT)
{
if((colc - 1) == arr[0] % colc)
{
gameover();
}
else
{
go(1);
}
return ;
}
if(last == _UP)
{
if(arr[0] < colc)
{
gameover();
}
else
{
go( - colc);
}
return ;
}
}


int pause()
{
return 0;
}
int resume()
{
return 0;
}

若安卓手机分辨率太大导致显示蛇过小可以在init()中使用setscrsize()限制显示分辨率

1
setscrsize(240,SCRH*240/SCRW);
赏

谢谢你请我吃糖果

微信

扫一扫,分享到微信

微信分享二维码
打开小米隐藏开关
山东电信资费公示
目录,不存在的…
留言已关闭
© gddhy
Hexo Theme Yilia by Litten
  • 所有文章
  • 友链

tag:

  • 软件分享
  • Android
  • Hexo
  • game
  • Html
  • Java
  • mtk
  • MIUI
  • 旧机博物馆
  • git
  • Termux
  • 原神
  • Win
  • 安卓学习笔记

    缺失模块

  • Luminous' Home
  • 影子博客
  • 四次元领域
  • 初之音
  • Mr.Pumpkin
  • JUSEBLOG
  • ZhaoQuinn 's Blog