<>代码:

代码有点长,
#include<stdio.h> #include<Windows.h> #include<time.h> #include<stdlib.h> #
include<conio.h> #define U 1 #define D 2 #define L 3 #define R 4 struct snake {
int x; int y; snake* next; }; int score = 0, add = 10; int HighScore = 0; int
status= 0, Sleeptime = 200; snake* head, * food; snake* q; int endgamestatus = 0
; HANDLE Hout; /* 函 数 声 明 */ int color(int c); //1 void gotoxy(int x, int y);
//2 void printsnake(); //3 void welcome_to_game(); //4 void createMap(); //5
void score_and_tips(); //6 void File_out(); //7 void initsnake(); //8 void
createfood(); //9 bool bitself(); //10 void cantcrosswall(); //11 void speedup()
; //12 void speeddown(); //13 void snakemove(); //14 void keyboardControl();
//15 void Lostdraw(); //16 void endgame(); //17 void file_in(); //18 void choose
(); //19 void explation(); //20 //主类型 int main() { printsnake(); welcome_to_game
(); return 0; } /* 子 函 数 */ //改变颜色 int color(int c) { SetConsoleTextAttribute(
GetStdHandle(STD_OUTPUT_HANDLE), c); return 0; } /*获得控制台的坐标位置*/ void gotoxy(int
x, int y) { COORD c; c.X = x; c.Y = y; SetConsoleCursorPosition(GetStdHandle(
STD_OUTPUT_HANDLE), c); } //打印贪吃蛇 void printsnake() { gotoxy(35, 1); color(6);
printf("/^\\/^\\"); gotoxy(34, 2); printf("|__| o|"); gotoxy(32, 2); color(2);
printf("_"); gotoxy(30, 3); color(2); printf("/ \\_/ \\"); gotoxy(37, 3); color(
6); printf("\\_/"); gotoxy(30, 4); color(2); printf("|__________/\\"); gotoxy(20
, 3); color(12); printf("\\/"); gotoxy(21, 4); color(12); printf("\\________
__________"); gotoxy(30, 4); color(2); printf("|"); gotoxy(41, 4); color(2);
printf("/\\"); gotoxy(30, 5); printf("\\______ \\"); gotoxy(36, 7); printf("/ /"
); gotoxy(35, 8); printf("/ /"); gotoxy(34, 9); printf("/ / \\"); gotoxy(33, 10)
; printf("/ / \\ \\"); gotoxy(32, 11); printf("/ / \\ \\"); gotoxy(31, 12);
printf("/ / ----------- ~- \\ \\"); gotoxy(31, 13); printf("\\ \\_________-~ ~-
\\ \\"); gotoxy(32, 14); printf("\\ -~-----~- ~----- \\"); gotoxy(33, 15);
printf("\\_______________- ~ ~-________________/"); printf("\n"); } //游戏欢迎界面
void welcome_to_game() { int n; int i, j = 1; color(11); gotoxy(43, 18); printf(
"贪 吃 蛇 大 作 战"); color(14); for (i = 20; i <= 26; ++i) { for (j = 27; j <= 74; ++
j) { gotoxy(j, i); if (i == 20 || i == 26) printf("-"); if (j == 28 || j == 73)
printf("|"); } } color(12); gotoxy(35, 22); printf("1.开始游戏"); gotoxy(55, 22);
printf("2.游戏说明"); gotoxy(35, 24); printf("3.退出游戏"); color(3); gotoxy(29, 27);
printf("请选择:[1,2,3]:[ ]\b\b"); color(14); scanf_s("%d", &n); switch (n) { case 1
:system("cls"); createMap(); initsnake(); createfood(); File_out();
keyboardControl(); break; case 2: explation(); break; case 3: exit(0); break; }
} //创建游戏地图 void createMap() { int i, j; for (i = 0; i < 58; i += 2) { gotoxy(i,
0); color(5); printf("□"); gotoxy(i, 26); printf("□"); } for (i = 1; i < 26; ++i
) { gotoxy(0, i); printf("□"); gotoxy(56, i); printf("□"); } for (i = 2; i < 56;
i+= 2) { for (j = 1; j < 26; j++) { gotoxy(i, j); color(3); printf("■\n\n"); }
} } //分数和操作提示 void score_and_tips() { File_out(); color(11); color(11); gotoxy(
64, 4); printf("☆最高纪录☆:%d", HighScore); color(14); gotoxy(64, 8); printf("得分:%d"
, score); color(13); gotoxy(73, 11); printf("小 提 示:"); color(6); gotoxy(60, 13);
printf("╬------------------------------╬"); gotoxy(60, 25); printf(
"╬------------------------------╬"); color(3); gotoxy(64, 14); printf(
"每个食物得分:%d分",add); gotoxy(64, 16); printf("不能穿墙,不能咬到自己"); gotoxy(64, 18); printf
("用↑↓←→分别控制蛇的移动"); gotoxy(64, 20); printf("F1为加速,F2为减速"); gotoxy(64, 22); printf
("space:暂停游戏"); gotoxy(64, 24); printf("ESC:退出游戏"); } //读入最高分 void File_out() {
FILE* fp; fopen_s(&fp, "save.txt", "a+"); fscanf_s(fp, "%d", &HighScore); fclose
(fp); } //绘制蛇身 void initsnake() { snake* tail; int i; tail = (snake*)malloc(
sizeof(snake)); tail->x = 24; tail->y = 5; tail->next = NULL; for (i = 1; i <= 4
; ++i) { head = (snake*)malloc(sizeof(snake)); head->next = tail; head->x = 24 +
2 * i; head->y = 5; tail = head; } while (tail != NULL) { gotoxy(tail->x, tail->
y); color(14); printf("☆"); tail = tail->next; } gotoxy(30, 30); } //绘制食物 void
createfood() { snake* food_1; food_1 = (snake*)malloc(sizeof(snake)); food_1->x
= 1; while ((food_1->x % 2) != 0) { srand((unsigned)time(0)); food_1->x = rand()
% 52 + 2; } food_1->y = rand() % 24 + 1; q = head; while (q != NULL) { if (q->x
== food_1->y && q->y == food_1->y) { free(food_1); createfood(); return; } q = q
->next; } gotoxy(food_1->x, food_1->y); food = food_1; color(12); printf("●"); }
//判断是否咬到自己 bool bitself() { snake* self; self = head->next; while (self != NULL)
{ if (head->x == self->x && head->y == self->y) return true; self = self->next;
} return false; } //判断是否撞墙 void cantcrosswall() { if (head->x == 0 || head->x ==
56 || head->y == 0 || head->y == 26) { endgamestatus = 1; endgame(); } } //蛇加速
void speedup() { if (Sleeptime >= 50) { Sleeptime -= 10; add += 2; } } //蛇减速
void speeddown() { if (Sleeptime < 350) { Sleeptime += 30; add -= 2; } if (
Sleeptime== 350) add = 1; } //控制方向 void snakemove() { snake* nexthead; nexthead
= (snake*)malloc(sizeof(snake)); cantcrosswall(); if (status == U) { nexthead->x
= head->x; nexthead->y = head->y - 1; nexthead->next = head; head = nexthead; q
= head; if (head->x == food->x && head->y == food->y) { while (q != NULL) {
gotoxy(q->x, q->y); color(14); printf("☆"); q = q->next; } score += add;
createfood(); speedup(); } else { while (q->next->next != NULL) { gotoxy(q->x, q
->y); color(14); printf("☆"); q = q->next; } gotoxy(q->next->x, q->next->y);
color(3); printf("■"); free(q->next); q->next = NULL; } } else if (status == D)
{ nexthead->x = head->x; nexthead->y = head->y + 1; nexthead->next = head; head
= nexthead; q = head; if (head->x == food->x && head->y == food->y) { while (q
!= NULL) { gotoxy(q->x, q->y); color(14); printf("☆"); q = q->next; } score +=
add; createfood(); speedup(); } else { while (q->next->next != NULL) { gotoxy(q-
>x, q->y); color(14); printf("☆"); q = q->next; } gotoxy(q->next->x, q->next->y)
; color(3); printf("■"); free(q->next); q->next = NULL; } } else if (status == L
) { nexthead->x = head->x - 2; nexthead->y = head->y; nexthead->next = head;
head= nexthead; q = head; if (nexthead->x == food->x && nexthead->y == food->y)
{ while (q != NULL) { gotoxy(q->x, q->y); color(14); printf("☆"); q = q->next; }
score+= add; createfood(); speedup(); } else { while (q->next->next != NULL) {
gotoxy(q->x, q->y); color(14); printf("☆"); q = q->next; } gotoxy(q->next->x, q-
>next->y); color(3); printf("■"); free(q->next); q->next = NULL; } } else if (
status== R) { nexthead->x = head->x + 2; nexthead->y = head->y; nexthead->next =
head; head = nexthead; q = head; if (nexthead->x == food->x && nexthead->y ==
food->y) { while (q != NULL) { gotoxy(q->x, q->y); color(14); printf("☆"); q = q
->next; } score += add; createfood(); speedup(); } else { do { gotoxy(q->x, q->y
); color(14); printf("☆"); q = q->next; } while (q->next->next != NULL); gotoxy(
q->next->x, q->next->y); color(3); printf("■"); free(q->next); q->next = NULL; }
} if (bitself()) { endgamestatus = 2; endgame(); } } //用键盘控制蛇的走向 void
keyboardControl() { status = R; while (1) { score_and_tips(); if (
GetAsyncKeyState(VK_UP) && status != D) { status = U; } else if (
GetAsyncKeyState(VK_DOWN) && status != U) { status = D; } else if (
GetAsyncKeyState(VK_LEFT) && status != R) { status = L; } else if (
GetAsyncKeyState(VK_RIGHT) && status != L) { status = R; } if (GetAsyncKeyState(
VK_SPACE)) { system("pause"); if (GetAsyncKeyState(VK_SPACE)) { continue; } }
else if (GetAsyncKeyState(VK_ESCAPE)) { endgamestatus = 3; endgame(); break; }
else if (GetAsyncKeyState(VK_F1)) { speedup(); } else if (GetAsyncKeyState(VK_F2
)) { speeddown(); } Sleep(Sleeptime); snakemove(); } } //失败页面 void Lostdraw() {
system("cls"); int i; gotoxy(46, 2); color(6); printf("\\\\\\|///"); gotoxy(43,
3); printf("\\\\"); color(15); gotoxy(47, 3); printf(".-.-"); color(6); gotoxy(
54, 3); printf("//"); color(14); gotoxy(44, 4); printf("<"); color(15); gotoxy(
47, 4); printf(".@.@"); color(14); gotoxy(54, 4); printf(">"); color(11); gotoxy
(17, 5); printf("+-----------------"); color(14); printf("o00o"); color(11);
printf("---------"); color(14); printf("<_>"); color(11); printf("---------");
color(14); printf("o00o"); color(11); printf("-----------------+"); gotoxy(17,
19); color(11); printf("+----------------------------"); color(14); printf("
☆☆☆ "); color(11); printf("----------------------------+"); for (int i = 6; i <
19; i++) { gotoxy(17, i); printf("|"); gotoxy(82, i); printf("|"); } gotoxy(30,
30); } //游戏结束界面 void endgame() { system("cls"); if (endgamestatus == 1) {
Lostdraw(); color(12); gotoxy(35, 9); printf("对不起,您撞到了墙,游戏结束。"); } else if (
endgamestatus== 2) { Lostdraw(); color(12); gotoxy(35, 9); printf(
"对不起,您咬到了自己,游戏结束。"); } else if (endgamestatus == 3) { Lostdraw(); color(12);
gotoxy(40, 9); printf("您已结束了游戏。"); } color(13); gotoxy(43, 12); printf(
"您的得分是:%d\n", score); if (score >= HighScore) { color(10); gotoxy(33, 16);
printf("创纪录啦!最高分被您刷新了,真棒!!!"); file_in(); } else { color(10); gotoxy(33, 16);
printf("继续努力吧!你离最高分还差%d分。",HighScore - score); } choose(); } //将最高分存储进文件中 void
file_in() { FILE* fp; fopen_s(&fp, "save.txt", "w+"); fprintf(fp, "%d", score);
fclose(fp); } //边框下方的分支选项 void choose() { int n; color(12); gotoxy(25, 22);
printf("我要重新玩一局-----1"); gotoxy(52, 22); printf("不玩了,退出吧-----2"); color(15);
gotoxy(46, 25); printf("选择:"); scanf_s("%d", &n); switch (n) { case 1: system(
"cls"); score = 0; Sleeptime = 200; printsnake(); welcome_to_game(); break; case
2: exit(0); break; default: color(12); printf("※※您的输入有误,请重新输入。※※"); gotoxy(51,
25); system("pause >nul"); endgame(); choose(); break; } } //游戏说明界面 void
explation() { int i, j = 1; system("cls"); color(13); gotoxy(44, 3); printf(
"游戏说明"); color(2); for (i = 20; i <= 75; ++i) { for (j = 6; j <= 22; ++j) {
gotoxy(i, j); if (j == 6 || j == 22) { printf("="); } if ((i == 20 || i == 74)
&& j != 6 && j != 22) { printf("||"); } } } color(3); gotoxy(30, 8); printf(
"tip1:不能穿墙,不能咬到自己。"); color(10); gotoxy(30, 11); printf("tip2:用↑↓←→分别控制蛇的移动");
color(14); gotoxy(30, 14); printf("tip3:F1为加速,F2为减速。"); color(11); gotoxy(30, 17
); printf("tip4:按space空格键暂停,再按空格键继续。"); color(4); gotoxy(30, 20); printf(
"ESC:退出游戏。Space:暂停游戏。"); _getch(); system("cls"); printsnake(); welcome_to_game(
); }
<>运行效果:

<>1.欢迎界面

<>2.游戏界面:

<>3.失败界面:

<>4.游戏说明界面:

技术
©2019-2020 Toolsou All rights reserved,
uni-app中使用 async + await 实现异步请求同步化Dart中的Isolate十分钟掌握Pytorch搭建神经网络的流程利用python对monkey日志完成自动化分析二进制模2除法(CRC循环冗余检验)vue实现pc端的自适应,rem适配希尔排序Unity面试经验(两天面六家,四个offer,济南)C++实现《走迷宫》小游戏VR、AR和MR这些技术的区别