本文實(shí)例為大家分享了C語言實(shí)現(xiàn)貪吃蛇小游戲的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)功能
蛇最開始三節(jié),向右移動。用戶可以通過按上下左右來控制蛇的移動,食物隨機(jī)產(chǎn)生,蛇吃到食物后蛇的身體會變長。蛇撞墻或者撞到自己身體后,游戲結(jié)束。
怎么實(shí)現(xiàn)
要實(shí)現(xiàn)一個貪吃蛇小游戲,首先要想清楚游戲里有什么,怎樣實(shí)現(xiàn)功能。
很明顯游戲中只有兩樣?xùn)|西,蛇和食物。
所以要建立蛇和食物信息,然后將蛇和食物進(jìn)行初始化,在將蛇和食物畫出來。
實(shí)現(xiàn)的功能有:
1. 蛇的移動
2. 按鍵控制蛇的移動
3. 食物的產(chǎn)生
4. 蛇吃食物后蛇身體變長
5. 游戲的結(jié)束
用結(jié)構(gòu)體建立蛇和食物的信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
struct COOR{ //位置,x,y坐標(biāo) int x; int y; }; struct SNAKE{ //蛇的基礎(chǔ)信息 int size; //節(jié)數(shù) int speed; //運(yùn)動速度 char dir; //運(yùn)動方向 struct COOR xy[MAX]; //位置 }snakes; struct FOOD{ //食物信息 struct COOR fooddir; //食物位置 int flag; //判斷食物是否被吃掉,1未被吃掉,0被吃掉 }food; |
實(shí)現(xiàn)功能的函數(shù):
蛇:
1
2
3
4
|
void snakeInit(){ //初始化蛇的信息 void drawSnake(){ //畫蛇 void moveSnake(){ //蛇的移動 void coorSnake(){ //按鍵控制蛇的運(yùn)動方向 |
食物:
1
2
|
void initFood(){//初始化食物的信息 void drawFood(){//畫食物 |
其它:
1
2
|
int gameOver(){ //游戲結(jié)束情況 void gameInit(){ //初始化窗口范圍 |
代碼
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
|
#include<stdio.h> #include<stdlib.h> #include<time.h> #include<graphics.h> #include<conio.h> #include<Windows.h> #define MAX 200 HWND hwnd = NULL; enum DIR{ //枚舉移動方向 UP, DOWN, LEFT, RIGHT, }; struct COOR{ //位置,x,y坐標(biāo) int x; int y; }; struct SNAKE{ //蛇的基礎(chǔ)信息 int size; //節(jié)數(shù) int speed; //運(yùn)動速度 char dir; //運(yùn)動方向 struct COOR xy[MAX]; //位置 }snakes; struct FOOD{ //食物信息 struct COOR fooddir; //食物位置 int flag; //判斷食物是否被吃掉,1未被吃掉,0被吃掉 }food; void snakeInit(){ //初始化蛇的信息 snakes.size = 3; //開始節(jié)數(shù) snakes.dir = RIGHT; //開始運(yùn)動方向 snakes.speed = 10; int i = 0; for (; i < snakes.size; i++){ //每一節(jié)書的位置,注意將第一節(jié)作為頭 snakes.xy[i].x = 40 - 10 * i; snakes.xy[i].y = 10; } } void drawSnake(){ //畫蛇 int i = 0; for (; i < snakes.size; i++){ setlinecolor(BLACK); //畫線的顏色 setfillcolor(RED); //填充色 //fillrectangle(snakes.xy[i].x, snakes.xy[i].y, snakes.xy[i].x + 10, snakes.xy[i].y+10);//矩形 fillcircle(snakes.xy[i].x, snakes.xy[i].y, 5); //圓形 } } void moveSnake(){ //蛇的移動 //snakes.xy[0].x++; int i = 0; for (i = snakes.size-1; i >0; i--){ //蛇身跟著舌頭運(yùn)動 snakes.xy[i].x = snakes.xy[i-1].x; snakes.xy[i].y = snakes.xy[i-1].y; } switch (snakes.dir){ case UP: snakes.xy[0].y-=snakes.speed; break ; case DOWN: snakes.xy[0].y+=snakes.speed; break ; case LEFT: snakes.xy[0].x-=snakes.speed; break ; case RIGHT: snakes.xy[0].x+=snakes.speed; break ; default : break ; } } void coorSnake(){ //按鍵控制蛇的運(yùn)動方向 if (_kbhit()){ //等待獲取按鍵 char c = _getch(); //獲得按鍵 switch (c){ case 72: case 'w' : if (snakes.dir != DOWN){ snakes.dir = UP; } break ; case 80: case 's' : if (snakes.dir != UP){ snakes.dir = DOWN; } break ; case 75: case 'a' : if (snakes.dir != RIGHT){ snakes.dir = LEFT; } break ; case 77: case 'd' : if (snakes.dir != LEFT){ snakes.dir = RIGHT; } break ; default : break ; } } } void initFood(){ //初始化食物的信息 food.flag = 1; while (1){ START: food.fooddir.x = rand () % 63 * 10; //食物位置隨機(jī) food.fooddir.y = rand () % 47 * 10; for ( int i = 0; i < snakes.size; i++){ //防止食物生成在蛇身上。 if (food.fooddir.x == snakes.xy[i].x&&food.fooddir.y == snakes.xy[i].y){ goto START; } else { break ; } } break ; } } void drawFood(){ //畫食物 //food.fooddir.x = 100; //food.fooddir.y = 200; setlinecolor(BLACK); setfillcolor(RED); fillcircle(food.fooddir.x, food.fooddir.y, 5); } void eatFood(){ //蛇吃食物 if (snakes.xy[0].x - food.fooddir.x <= 5 && snakes.xy[0].y - food.fooddir.y <= 5 \ && food.fooddir.x - snakes.xy[0].x <= 5 && food.fooddir.y - snakes.xy[0].y <= 5 && food.flag == 1){ food.flag = 0; snakes.size++; } } int gameOver(){ //游戲結(jié)束情況 if (snakes.xy[0].x < 5 || snakes.xy[0].y <= 0 || snakes.xy[0].x > 635 || snakes.xy[0].y > 478){ MessageBox(hwnd, "GAME OVER!" , "你撞墻了!" , MB_OK); return 1; } for ( int i = 1; i < snakes.size; i++){ if (snakes.xy[0].x == snakes.xy[i].x&&snakes.xy[0].y == snakes.xy[i].y){ MessageBox(hwnd, "GAME OVER!" , "你撞了自己" ,MB_OK); return 1; } } return 0; } void gameInit(){ hwnd=initgraph(640, 480); //設(shè)置窗口大小 setbkcolor(GREEN); //設(shè)置窗口顏色 } int main(){ srand ((unsigned long ) time (NULL)); //生成隨機(jī)數(shù) gameInit(); cleardevice(); //刷新窗口 snakeInit(); initFood(); while (1){ cleardevice(); if (food.flag == 0){ initFood(); } drawFood(); drawSnake(); coorSnake(); eatFood(); moveSnake(); //eatFood(); if (gameOver()){ break ; } //stopGame(); Sleep(100); } getchar (); //防止閃屏 closegraph(); system ( "pause" ); return 0; } |
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/weixin_57023347/article/details/117289955