DOS简易版C语言贪吃蛇

时间:2021-08-05 07:12:08

本文实例为大家分享了C语言实现贪吃蛇的具体代码,供大家参考,具体内容如下

?
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
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <windows.h>
 
 
 
#define WALL_LENGTH 22
 
#define LEFT 0x4b
#define RIGHT 0x4d
#define DOWN 0x50
#define UP 0x48
 
struct Snakes{
 int x;
 int y;
 struct Snakes *prev;
 struct Snakes *next;
};
 
struct Food{
 int x;
 int y;
};
 
 
struct Snakes *header;
struct Snakes *tailer;
struct Food *food;
 
int wall[WALL_LENGTH][WALL_LENGTH];
int direction = RIGHT;
 
 
 
/**/
void init();
void draw();
void move();
void doMove(int x1, int y1);
void eat();
 
void keydown();
 
void foods();
int isOver();
int isDrawSnake(int x, int y);
int isDrawFood(int x, int y);
 
 
int main(){
 init();
 
 while(1){
 
 
 
 if(isOver()){
 break;
 }
 
 
 
 move();
 eat();
 draw();
  _sleep( 100 );
 keydown();
 
 
 
 }
 
 printf("GAME OVER!");
 
 system("pause");
}
 
 
void init(){
 int y, x;
 
 for(y=0; y < WALL_LENGTH; y++){
 for(x=0; x < WALL_LENGTH; x++){
 if(y == 0 || y == WALL_LENGTH - 1 || x == 0 || x == WALL_LENGTH - 1){
  wall[y][x] = 1;
 }
 }
 }
 
 
 header=(struct Snakes *)malloc(sizeof(struct Snakes));
 header->x=10;
 header->y=10;
 header->prev=NULL;
 
 
 tailer=(struct Snakes *)malloc(sizeof(struct Snakes));
 tailer->x=9;
 tailer->y=10;
 tailer->next=NULL;
 tailer->prev=header;
 
 header->next=tailer;
 
 foods();
 
}
 
void draw(){
 int y, x;
 
 system("cls");
 for(y=0; y < WALL_LENGTH; y++){
 for(x=0; x < WALL_LENGTH; x++){
 
 if(wall[y][x] == 1){
 printf("[]");
 }else if(isDrawSnake(x, y)){
 printf("[]");
 }else if(isDrawFood(x, y)){
 printf("()");
 }else{
 printf(" ");
 
 }
 }
 printf("\n");
 }
 
}
 
void move(){
 
 switch(direction){
 case LEFT :
 doMove(-1, 0);
 break;
 case RIGHT :
 doMove(1, 0);
 break;
 case UP :
 doMove(0, -1);
 break;
 case DOWN :
 doMove(0, 1);
 break;
 }
 
}
 
void doMove(int x1, int y1){
 
 struct Snakes *temp_tailer = tailer->prev;
 
 tailer->x=header->x + x1;
 tailer->y=header->y + y1;
 
 tailer->next=header;
 tailer->prev->next = NULL;
 tailer->prev = NULL;
 
 header->prev=tailer;
 
 header = tailer;
 tailer = temp_tailer;
 
}
 
void eat(){
 
 if(header->x == food->x && header->y == food->y){
 int x1=0, y1 =0;
 struct Snakes *temp_tailer = tailer;
 tailer=(struct Snakes *)malloc(sizeof(struct Snakes));
 
 switch(direction){
 case LEFT :
 x1 = -1;
 y1 = 0;
 break;
 
 case RIGHT :
 x1 = 1;
 y1 = 0;
 break;
 
 case UP :
 x1 = 0;
 y1 = -1;
 break;
 
 case DOWN :
 x1 = 0;
 y1 = 1;
 break;
 }
 
 tailer->x=temp_tailer->x + x1;
 tailer->y=temp_tailer->y + y1;
 tailer->next=NULL;
 tailer->prev=temp_tailer;
 
 temp_tailer->next = tailer;
 foods();
 }
 
}
 
 
void foods(){
 
 int y,x;
 struct Snakes *temp = header;
 
 _sleep(20);
 srand((unsigned)time(NULL));
 y=rand()%WALL_LENGTH;
 x=rand()%WALL_LENGTH;
 
 if(y == 0 || y == WALL_LENGTH - 1 || x == 0 || x == WALL_LENGTH - 1 ){
 return foods();
 }
 
 do{
 if(temp->x == x && temp->y == y){
 return foods();
 }
 temp = temp->next;
 }while(temp != NULL);
 
 
 
 if(food == NULL){
 food=(struct Food *)malloc(sizeof(struct Food));
 }
 food->x = x;
 food->y = y;
 
}
 
void keydown(){
 char keycode;
 if(_kbhit()&&(keycode =_getch())) { 
  switch(keycode) {
 case LEFT:
 if(RIGHT!=direction) {
  direction=LEFT;
 // move();
 // draw();
 }
 break;
 
 case RIGHT:
 if(LEFT!=direction) {
  direction=RIGHT;
 // move();
 // draw();
 }
 break;
 
 case UP:
 if(DOWN!=direction) {
  direction=UP;
 // move();
 // draw();
 }
 break;
 
 case DOWN:
 if(UP!=direction){
  direction=DOWN;
 // move();
 // draw();
 }
 break;
 
  }
 }
 
}
 
 
int isDrawSnake(int x, int y){
 struct Snakes *temp = header;
 do{
 if(temp->x == x && temp->y == y){
 return 1;
 }
 temp = temp->next;
 }while(temp != NULL);
 return 0;
}
 
int isDrawFood(int x, int y){
 if(food->x == x && food->y == y){
 return 1;
 }
 return 0;
}
 
int isOver(){
 int x1=0, y1 =0;
 switch(direction){
 case LEFT :
 x1 = -1;
 y1 = 0;
 break;
 
 case RIGHT :
 x1 = 1;
 y1 = 0;
 break;
 
 case UP :
 x1 = 0;
 y1 = -1;
 break;
 
 case DOWN :
 x1 = 0;
 y1 = 1;
 break;
 }
 
 if(header->x + x1 <= 0 || header->x + x1 >= WALL_LENGTH - 1
 || header->y + y1 <= 0 || header->y + y1 >= WALL_LENGTH - 1){
 
 return 1;
 }
 return 0;
 
}

好久没写过C语言了,随便写个贪吃蛇玩一玩,BUG不少,当记录了。

DOS简易版C语言贪吃蛇

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/xxb2008/article/details/40747291