100分的最终作业范例

点击下载

运行文件为解压后文件夹内的Snake_console.exe

img

如果对你有帮助,希望可以打赏哦~(打赏按钮在下方)

下面为代码区

snake_main.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
#include "snake.h"

int main()
{
srand((unsigned int)time(0)); //生成随机数种子
int end = 1, result;
while (end) {
result = Menu();
switch (result)
{
case 1://开始游戏
InitMap();
while (MoveSnake());
break;
case 2://帮助信息
Help();
break;
case 3://关于信息
About();
break;
case 4://排行榜
Rank();
break;
case 0://结束程序
end = 0;
break;
}
}
}

代码与修改之前的相同,需要下载自行前往

snake.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
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
397
398
399
400
401
402
403
404
#include "snake.h"

// 全局变量
Snake snake; //定义蛇结构体变量
Food food; //定义食物结构体变量
int foodtype = 1; //定义食物的种类
char now_Dir = RIGHT; //当前蛇头方向
char direction = RIGHT; //预期蛇头方向
int grade[100] = { 0 };
int cycle = 1; //定义循环次数

int Menu()
{
GotoXY(40, 12); //定位光标的位置
printf("欢迎来到贪吃蛇小游戏");
GotoXY(43, 14);
printf("1. 开始游戏");
GotoXY(43, 16);
printf("2. 帮助");
GotoXY(43, 18);
printf("3. 关于");
GotoXY(43, 20);
printf("4. 排行榜");
GotoXY(43, 22);
printf("其他任意键退出游戏");
Hide(); //隐藏光标
char ch;
int result = 0;
ch = _getch();
switch (ch)
{
case '1': result = 1; break;
case '2': result = 2; break;
case '3': result = 3; break;
case '4': result = 4; break;
}
system("cls");
return result;
}

//光标定位函数
void GotoXY(int x, int y)
{
HANDLE hout;
COORD cor;
hout = GetStdHandle(STD_OUTPUT_HANDLE);
cor.X = x;
cor.Y = y;
SetConsoleCursorPosition(hout, cor);
}

//隐藏光标
void Hide()
{
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cor_info = { 1, 0 };
SetConsoleCursorInfo(hout, &cor_info);//隐藏光标

}

void About()
{
GotoXY(30, 12);
printf("杭州电子科技大学--程序设计综合实践作业");
GotoXY(43, 14);
printf("贪吃蛇-控制台游戏");
GotoXY(43, 16);
printf("按任意键返回上级菜单");
Hide(); //隐藏光标
char ch = _getch();
system("cls");
}

void Help()
{
GotoXY(40, 12);
printf("w 上 ");
GotoXY(40, 14);
printf("s 下 ");
GotoXY(40, 16);
printf("a 左 ");
GotoXY(40, 18);
printf("d 右 ");
GotoXY(45, 22);
printf("按任意键返回上级菜单 ");
char ch = _getch();
system("cls");
}

void InitMap()
{
Hide(); //隐藏光标
//设置蛇头位置在地图的中心
snake.snakeNode[0].x = MAP_WIDTH / 2 - 1;
snake.snakeNode[0].y = MAP_HEIGHT / 2 - 1;
GotoXY(snake.snakeNode[0].x, snake. snakeNode[0].y); //光标移动到蛇头位置
printf("@"); //蛇头
snake.length = 3; //初始长度
snake.speed = 250; //初始速度
now_Dir = RIGHT; // 当前蛇头的朝向

//显示蛇的身体
for (int i = 1; i < snake.length; i++) {
//设置蛇身的纵坐标位置和蛇头位置相同
snake.snakeNode[i].y = snake.snakeNode[i - 1].y;
snake.snakeNode[i].x = snake.snakeNode[i - 1].x - 1;
GotoXY(snake.snakeNode[i].x, snake.snakeNode[i].y);
printf("*");
}

//生成地图上下边界
for (int i = 0; i < MAP_WIDTH; i++) {
GotoXY(i, 0);
printf("-");
GotoXY(i, MAP_HEIGHT - 1);
printf("-");

}
for (int i = 1; i < MAP_HEIGHT - 1; i++) {
GotoXY(0, i);
printf("|");
GotoXY(MAP_WIDTH - 1, i);
printf("|");
}

//生成地图中的障碍物
for (int i = 6; i < MAP_WIDTH - 6; i++) {
GotoXY(i, 5);
printf("-");
GotoXY(i, MAP_HEIGHT - 6);
printf("-");
}
PrintFood(); //生成食物
GotoXY(50, 5);
printf("当前得分:0");
}


void PrintFood()
{
foodtype = rand() % 3;
if (foodtype == 0) {
int flag = 1;
while (flag) {
flag = 0;

//生成食物的随机坐标
food.x = rand() % (MAP_WIDTH - 2) + 1;
food.y = rand() % (MAP_HEIGHT - 2) + 1;
//循环判断食物的位置能否而后蛇的位置重叠,如果重叠则重新设置食物的位置
for (int k = 0; k <= snake.length - 1; k++) {
if (snake.snakeNode[k].x == food.x && snake.snakeNode[k].y == food.y) {
flag = 1;
break;
}
}
if (food.x >= 6 && food.x < MAP_WIDTH - 6 && (food.y == 5 || food.y == MAP_HEIGHT - 6)) {
flag = 1;
}
}
GotoXY(food.x, food.y);
printf("o"); //此处的食物用o来表示
}

else if (foodtype == 1) {
int flag = 1;
while (flag) {
flag = 0;

//生成食物的随机坐标
food.x = rand() % (MAP_WIDTH - 3) + 1;
food.y = rand() % (MAP_HEIGHT - 2) + 1;
//循环判断食物的位置能否而后蛇的位置重叠,如果重叠则重新设置食物的位置
for (int k = 0; k <= snake.length - 1; k++) {
if (snake.snakeNode[k].x == food.x && snake.snakeNode[k].y == food.y) {
flag = 1;
break;
}
}
if (food.x >= 6 && food.x < MAP_WIDTH - 6 && (food.y == 5 || food.y == MAP_HEIGHT - 6)) {
flag = 1;
}
}
GotoXY(food.x, food.y);
printf("$"); //此处的食物用$来表示
}

else if (foodtype == 2) {
int flag = 1;
while (flag) {
flag = 0;

//生成食物的随机坐标
food.x = rand() % (MAP_WIDTH - 3) + 1;
food.y = rand() % (MAP_HEIGHT - 2) + 1;
//循环判断食物的位置能否而后蛇的位置重叠,如果重叠则重新设置食物的位置
for (int k = 0; k <= snake.length - 1; k++) {
if (snake.snakeNode[k].x == food.x && snake.snakeNode[k].y == food.y) {
flag = 1;
break;
}
}
if (food.x >= 6 && food.x < MAP_WIDTH - 6 && (food.y == 5 || food.y == MAP_HEIGHT - 6)) {
flag = 1;
}
}
GotoXY(food.x, food.y);
printf("¥"); //此处的食物用¥来表示
}
}


int MoveSnake()
{
Snakenode temp;
int flag = 0;
int mm = 0; // 用于存储代码
temp = snake.snakeNode[snake.length - 1]; //记录蛇尾
for (int i = snake.length - 1; i >= 1; i--)
snake.snakeNode[i] = snake.snakeNode[i - 1];
GotoXY(snake.snakeNode[1].x, snake.snakeNode[1].y);
printf("*");

//响应键盘的操作
if (_kbhit()) {
direction = _getch();
switch (direction)
{
case UP:
if (now_Dir != DOWN)
now_Dir = direction;
break;
case DOWN:
if (now_Dir != UP)
now_Dir = direction;
break;
case RIGHT:
if (now_Dir != LEFT)
now_Dir = direction;
break;
case LEFT:
if (now_Dir != RIGHT)
now_Dir = direction;
break;
}
}

switch (now_Dir)
{
case UP:snake.snakeNode[0].y--; break; //上移
case DOWN:snake.snakeNode[0].y++; break; //下移
case LEFT:snake.snakeNode[0].x--; break; //左移
case RIGHT:snake.snakeNode[0].x++; break; //右移
}

//打印蛇头
GotoXY(snake.snakeNode[0].x, snake.snakeNode[0].y);
printf("@");
//判断是否吃到食物,如果蛇头的位置和食物的位置相同表示吃到食物
if (snake.snakeNode[0].x == food.x && snake.snakeNode[0].y == food.y) {
for (int i = foodtype + 1; i >= 0; i--) {
snake.length++; //吃到食物蛇长加一
flag = 1; //1表示吃到食物
snake.snakeNode[snake.length - 1] = temp; // 吃到食物,蛇尾加一节
}



}
//输出蛇此时的状态
//没吃到食物的时候,在原来的蛇尾打印一个空格,去掉原来的蛇尾
if (!flag) {
GotoXY(temp.x, temp.y);
printf(" ");
}
else {
PrintFood();
GotoXY(50, 5);
printf("当前得分:%d", snake.length - 3); //打印得分,得分为蛇长减3
}

//判断是否死亡
if (!IsCorrect()) {
grade[cycle] = snake.length - 3;
system("cls");
GotoXY(45, 12);
printf("这就输了???,真垃圾啊!");
GotoXY(45, 14);
printf("最终得分:%d", snake.length - 3);
GotoXY(45, 16);
printf("游戏结束!");
GotoXY(45, 18);
printf("这是第 %d 次进行的游戏", cycle);
cycle++;
GotoXY(45, 20);
printf("按任意键返回主菜单");
char c = _getch();
system("cls");
return 0;
}

//吃了自己之后的操作
if (zican()) {

for (int i = zican(); i <= snake.length; i++) {
GotoXY(snake.snakeNode[i].x, snake.snakeNode[i].y);
printf(" ");
}
snake.length = zican();
GotoXY(50, 5);
printf("当前得分:%d", snake.length - 3); //打印得分,得分为蛇长减3

}

//调整速度
SpeedControl();
Sleep(snake.speed);
return 1;
}

int IsCorrect()
{
if (snake.snakeNode[0].x <= 0 || snake.snakeNode[0].y <= 0 || snake.snakeNode[0].x >= MAP_WIDTH - 1 || snake.snakeNode[0].y >= MAP_HEIGHT - 1){
return 0;
}

if (snake.snakeNode[0].x >= 6 && snake.snakeNode[0].x < MAP_WIDTH - 6 && (snake.snakeNode[0].y == 5 || snake.snakeNode[0].y == MAP_HEIGHT - 6)) {
return 0;
}
return 1;
}

int zican() //吃自己简称自残
{
for (int i = 1; i < snake.length; i++) {//判断是否重合
if (snake.snakeNode[0].x == snake.snakeNode[i].x && snake.snakeNode[0].y == snake.snakeNode[i].y) {
return i;
}
}
return 0;
}

void SpeedControl()
{
switch (snake.length)
{
case 6:
snake.speed = 200;
break;
case 9:
snake.speed = 180;
break;
case 12:
snake.speed = 160;
break;
case 15:
snake.speed = 140;
break;
case 18:
snake.speed = 120;
break;
case 21:
snake.speed = 100;
break;
case 24:
snake.speed = 80;
break;
case 27:
snake.speed = 60;
break;
case 30:
snake.speed = 40;
break;
default:
break;
}
}

void Rank()
{
int i;
int temp;
//排序
for(i = 2; i < cycle; i++){
for (int j = 1; j <= i; j++) {
if (grade[j] < grade[i]) {
temp = grade[j];
grade[j] = grade[i];
grade[i] = temp;
}
}
}

i = 1;
//输出结果
while (i < cycle) {
GotoXY(45, (5 + 2 * i));
printf("第 %d 名 分数为 %d", i, grade[i]);
i++;
}

char ch = _getch();
system("cls");
}

此处由于修改代码部分过多,所以提供下载方式

点击下载

snake.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
#pragma once
#include<stdio.h>
#include<Windows.h>
#include<conio.h>//控制台的一些文件
#include<time.h>

//宏定义
#define MAP_HEIGHT 20 // 地图高度
#define MAP_WIDTH 40 // 地图的宽度
#define UP 'w' // 定义上移键
#define DOWN 's' // 定义下移键
#define LEFT 'a' // 定义左移键
#define RIGHT 'd' // 定义右移键

//结构体定义
typedef struct
{
int x; // x坐标的位置
int y; // y坐标的位置
}Food, Snakenode;

typedef struct //定义蛇的结构体
{
Snakenode snakeNode[1000]; //表示蛇最多有1000个节点
int length; //蛇的长度
int speed; //蛇移动的速度
}Snake;

void GotoXY(int, int); //光标定位函数
void Hide(); //隐藏光标函数
int Menu(); //主菜单函数
void Help(); //帮助信息
void About(); //关于信息
void InitMap(); //地图初始化
void PrintFood(); //生成食物
int MoveSnake(); //蛇的移动
int IsCorrect(); //自撞或撞墙检测
void SpeedControl(); //速度控制
int zican(); //判断是否啃到自己
void Rank(); //显示最高分的情况

该代码区有小部分的修改

下载资源如下

点击下载