本文實例為大家分享了C語言實現貪吃蛇小游戲的具體代碼,供大家參考,具體內容如下
實現功能
蛇最開始三節,向右移動。用戶可以通過按上下左右來控制蛇的移動,食物隨機產生,蛇吃到食物后蛇的身體會變長。蛇撞墻或者撞到自己身體后,游戲結束。
怎么實現
要實現一個貪吃蛇小游戲,首先要想清楚游戲里有什么,怎樣實現功能。
很明顯游戲中只有兩樣東西,蛇和食物。
所以要建立蛇和食物信息,然后將蛇和食物進行初始化,在將蛇和食物畫出來。
實現的功能有:
1. 蛇的移動
2. 按鍵控制蛇的移動
3. 食物的產生
4. 蛇吃食物后蛇身體變長
5. 游戲的結束
用結構體建立蛇和食物的信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
struct COOR{ //位置,x,y坐標 int x; int y; }; struct SNAKE{ //蛇的基礎信息 int size; //節數 int speed; //運動速度 char dir; //運動方向 struct COOR xy[MAX]; //位置 }snakes; struct FOOD{ //食物信息 struct COOR fooddir; //食物位置 int flag; //判斷食物是否被吃掉,1未被吃掉,0被吃掉 }food; |
實現功能的函數:
蛇:
1
2
3
4
|
void snakeInit(){ //初始化蛇的信息 void drawSnake(){ //畫蛇 void moveSnake(){ //蛇的移動 void coorSnake(){ //按鍵控制蛇的運動方向 |
食物:
1
2
|
void initFood(){//初始化食物的信息 void drawFood(){//畫食物 |
其它:
1
2
|
int gameOver(){ //游戲結束情況 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坐標 int x; int y; }; struct SNAKE{ //蛇的基礎信息 int size; //節數 int speed; //運動速度 char dir; //運動方向 struct COOR xy[MAX]; //位置 }snakes; struct FOOD{ //食物信息 struct COOR fooddir; //食物位置 int flag; //判斷食物是否被吃掉,1未被吃掉,0被吃掉 }food; void snakeInit(){ //初始化蛇的信息 snakes.size = 3; //開始節數 snakes.dir = RIGHT; //開始運動方向 snakes.speed = 10; int i = 0; for (; i < snakes.size; i++){ //每一節書的位置,注意將第一節作為頭 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--){ //蛇身跟著舌頭運動 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(){ //按鍵控制蛇的運動方向 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; //食物位置隨機 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(){ //游戲結束情況 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); //設置窗口大小 setbkcolor(GREEN); //設置窗口顏色 } int main(){ srand ((unsigned long ) time (NULL)); //生成隨機數 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; } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/weixin_57023347/article/details/117289955