本文实例为大家分享了C语言实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下
IDE用的是 VS2019
先看效果
代码全览
game.h
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
|
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#define PLATFORM 1 //运行的系统 1为win 0为linux
#define MAPWIDTH 15 //地图宽度,包括墙
#define MAPHEIGHT 15 //地图高度,包括墙
#define SNAKELENGTH (MAPHEIGHT - 2) * (MAPWIDTH - 2)
//结构体声明
struct Body
{
int isExist;
int x;
int y;
};
struct Food {
int x;
int y;
};
void game();
void initWall( char wall[MAPHEIGHT][MAPWIDTH], int mapWidth, int mapHeight);
void displayMap( int mapWidth, int mapHeight, struct Body snake[], int snakelength, struct Food food);
void clearScreen();
void inputProcess( char * pinput);
void initSnake( struct Body snake[SNAKELENGTH], int length);
void generateFood( struct Food* food, struct Body snake[]);
int isWall( int x, int y);
int isSnake( int x, int y, struct Body snake[], int lengh);
void control( char input, struct Body snake[]);
void generateFood( struct Food* food, struct Body snake[]);
int isFood( int x, int y, struct Food* food);
int isEat( struct Body snake[], struct Food* pfood);
void bodyMove( struct Body snake[], int * bodyLength);
int isInBody( struct Body snake[], int lengh);
|
GameStart.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
|
#include "game.h"
void displayMenu() {
printf ( "########################\n" );
printf ( "###### 贪吃蛇游戏 #######\n" );
printf ( "########################\n" );
printf ( "------------------------\n" );
printf ( " 1.开始游戏 \n" );
printf ( " 0.退出游戏 \n" );
printf ( "------------------------\n" );
printf ( "请输入选项:>" );
char ch;
scanf ( "%c" , &ch);
getchar ();
switch (ch)
{
case '1' : {
game();
break ;
}
case '0' : {
exit (0);
break ;
}
default :
printf ( "输入错误,请重新输入:>" );
break ;
}
}
int main( void ) {
while (1) {
clearScreen();
displayMenu();
clearScreen();
}
return 0;
}
|
game.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
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
|
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
//游戏逻辑
void game() {
//分数
int score = 0;
//游戏状态 0为胜利 1为咬到蛇身 2为撞到墙上
int gameState = 0;
//输入状态
char input = 0;
//墙
char wall[MAPHEIGHT][MAPWIDTH];
//创建蛇结构体数组
struct Body snake[SNAKELENGTH];
//创建食物结构体
struct Food food = { 5,5 };
//初始化蛇
initSnake(snake, SNAKELENGTH);
//初始化墙
initWall(wall, MAPWIDTH, MAPHEIGHT);
//生成食物
generateFood(&food, snake);
while (1)
{
//清屏
clearScreen();
control(input, snake);
//显示地图
displayMap(MAPWIDTH, MAPHEIGHT, snake, SNAKELENGTH, food);
printf ( "得分:%d\n" , score);
//printf("food:%d %d\n", food.x, food.y);
//printf("snake:%d %d", snake[0].x, snake[0].y);
//处理输入
inputProcess(&input);
//撞到蛇身,游戏失败
if (isInBody(snake, SNAKELENGTH)) {
gameState = 1;
break ;
}
//撞到墙上,游戏失败
if (isWall(snake[0].x, snake[0].y)) {
gameState = 2;
break ;
}
//吃到食物加分,蛇身加一
if (isEat(snake, &food)) {
score++;
snake[score].isExist = 1;
snake[score].x = snake[score - 1].x;
snake[score].y = snake[score - 1].y;
if (score == SNAKELENGTH - 1) {
//游戏胜利
gameState = 0;
break ;
}
generateFood(&food, snake);
}
//蛇身移动
bodyMove(snake, &score);
}
//胜负显示
switch (gameState)
{
case 1: {
printf ( "咬到蛇身,游戏结束!\n" );
break ;
}
case 2: {
printf ( "撞到墙上,游戏结束!\n" );
break ;
}
case 0: {
printf ( "游戏胜利!\n" );
break ;
}
default :
break ;
}
printf ( "按回车键退出" );
getchar ();
}
//清除屏幕
void clearScreen() {
if (PLATFORM) {
system ( "cls" );
}
else {
system ( "clear" );
}
printf ( "\033c" );
}
//输入处理
void inputProcess( char * pinput) {
int t = ( int ) time (NULL);
while (1) {
if (_kbhit()) {
switch (getch())
{
case 'w' : {
if (*pinput != 's' ) {
*pinput = 'w' ;
}
break ;
}
case 's' :
{
if (*pinput != 'w' ) {
*pinput = 's' ;
}
break ;
}
case 'a' : {
if (*pinput != 'd' ) {
*pinput = 'a' ;
}
break ;
}
case 'd' : {
if (*pinput != 'a' ) {
*pinput = 'd' ;
}
break ;
}
/* case ' ': {
*pinput = ' ';
break;
}*/
default :
break ;
}
}
if (( int ) time (NULL) - t == 1) {
//printf("%c\n", *pinput);
//一秒一帧
break ;
}
/*if (*pinput == ' ') {
continue;
}*/
}
}
//初始化墙
//'#'墙
//' '空
void initWall( char wall[MAPHEIGHT][MAPWIDTH], int mapWidth, int mapHeight) {
for ( int i = 0; i < mapHeight; i++) {
for ( int j = 0; j < mapWidth; j++) {
if (i == 0 || i == mapHeight - 1) {
wall[i][j] = '#' ;
}
else if (j == 0 || j == MAPWIDTH - 1) {
wall[i][j] = '#' ;
}
else {
wall[i][j] = ' ' ;
}
}
}
}
//初始化蛇状态,位置
void initSnake( struct Body snake[SNAKELENGTH], int length) {
for ( int i = 0; i < length; i++) {
if (i == 0)
{
snake[i].x = MAPWIDTH / 2;
snake[i].y = MAPHEIGHT / 2; //蛇出生位置,即蛇头初始位置
snake[i].isExist = 1;
}
else {
snake[i].isExist = 0;
snake[i].x = 0;
snake[i].y = 0;
}
}
}
//生成食物
void generateFood( struct Food* food, struct Body snake[]) {
int x;
int y;
srand ((unsigned int ) time (NULL));
do {
x = ( rand () % MAPHEIGHT) + 1;
y = ( rand () % MAPWIDTH) + 1;
} while (isSnake(x, y, snake, SNAKELENGTH) || isWall(x, y));
(*food).y = y;
(*food).x = x;
}
//判断是否是墙
int isWall( int x, int y) {
if (y <= 1 || y >= MAPHEIGHT || x <= 1 || x >= MAPWIDTH) {
return 1;
}
return 0;
}
//判断是否是蛇
int isSnake( int x, int y, struct Body snake[], int lengh) {
for ( int i = 0; i < lengh; i++) {
if (snake[i].isExist == 1 && snake[i].x == x && snake[i].y == y) {
return 1;
}
}
return 0;
}
//判断是否撞到蛇身
int isInBody( struct Body snake[], int lengh) {
for ( int i = 1; i < lengh; i++) {
if (snake[i].isExist == 1 && snake[i].x == snake[0].x && snake[i].y == snake[0].y) {
return 1;
}
}
return 0;
}
//判断是否是食物
int isFood( int x, int y, struct Food* food) {
if ((*food).x == x && (*food).y == y) {
return 1;
}
return 0;
}
//显示游戏地图
void displayMap( int mapWidth, int mapHeight, struct Body snake[], int snakelength, struct Food food) {
int x;
int y;
for ( int i = 0; i < mapHeight; i++) {
y = i + 1;
for ( int j = 0; j < mapWidth; j++) {
x = j + 1;
if (isWall(x, y)) {
printf ( "# " );
}
else if (isSnake(x, y, snake, snakelength)) {
if (snake[0].x == x && snake[0].y == y) {
printf ( "@ " ); //蛇头
}
else {
printf ( "* " ); //蛇身
}
}
else if (isFood(x, y, &food)) {
printf ( "+ " );
}
else {
printf ( " " );
}
}
printf ( "\n" );
}
}
//方向控制
void control( char input, struct Body snake[]) {
switch (input) {
case 'w' : {
snake[0].y -= 1;
break ;
}
case 'a' : {
snake[0].x -= 1;
break ;
}
case 's' : {
snake[0].y += 1;
break ;
}
case 'd' : {
snake[0].x += 1;
break ;
}
}
}
//判断是否吃到食物
int isEat( struct Body snake[], struct Food* pfood) {
if (isFood(snake[0].x, snake[0].y, pfood)) {
return 1;
}
return 0;
}
//移动蛇身
void bodyMove( struct Body snake[], int * bodyLength) {
if (*bodyLength) {
for ( int i = *bodyLength; i >= 1; i--) {
snake[i].x = snake[i - 1].x;
snake[i].y = snake[i - 1].y;
}
}
}
|
相关思路有空再写。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_44684288/article/details/120588218