游戏介绍
有一条蛇,在屏幕上爬,用上下左右键控制吃东西,吃得多了,到了一定积分,就能过关。越吃越长,不能碰墙,不能咬自己的尾巴,没了,哈哈。
这个蛇是条很有个性的蛇它会吃多少吐多少(如果要取消这个特性可以在代码中修改一行代码即可,我已经标注出来了)
话不多说直接上代码(代码中有详细注释)!

可以直接运行的:

//本来我是想用C语言实现的无奈水平有限,只能在C++下执行了,注意: 这里使用的字符集为Unicode

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <time.h>
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
#define MY_BUFSIZE 1024 //控制台窗口标题的缓冲区大小
#define INITIALTIME 200

//蛇的状态(上,下,左,右)
#define U 1
#define D 2
#define L 3
#define R 4

typedef struct Snake
{
int x; //节点的x坐标
int y; //节点的y坐标
struct Snake* next; //蛇身的下一个节点
}snake;

int score = 0, add = 0; //总得分与每次吃食物的得分
int HighScore = 0; //最高的得分
int status; //蛇的前进状态
int sleeptime = INITIALTIME; //蛇的时间间隔
int endgamestatus = 0; //游戏的结束的情况: 撞到墙,咬到自己,主动退出游戏

HANDLE hOut; //控制台句柄
snake* head, * food; //蛇头指针,食物指针
snake* q; //遍历蛇时用到的指针

void gotoxy(int x, int y); //设置光标位置
void color(int col); //更改文字颜色
void printsnake(); //字符画,蛇
void welcometogame(); //开始界面
void createMap(); //绘制地图
void scoreandtips(); //游戏右侧的得分和小提示
void initsnake(); //初始化蛇身(画出蛇身)
void createfood(); //创见并随机出击食物
bool biteself(); //判断是否咬到自己
void cantcrosswall(); //判断是否撞墙
void speedup(); //加速
void speedown(); //减速
void snakemove(); //控制蛇的前进方向
void keyboardControl(); //控制键盘按键
void Lostdraw(); //游戏结束界面
void endgame(); //游戏结束
void choose(); //游戏失败之后的选择
void File_out(); //从文件中读取最高分
void File_in(); //将最高分存入文件
void explation(); //游戏说明
HWND GetConsoleHwnd(); //获取当前句柄
int main(void)
{
//设置窗口
system(“mode con cols=100 lines=30”);
SetWindowLongPtr(GetConsoleHwnd(), GWL_STYLE, WS_VISIBLE | WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX);

printsnake();
welcometogame();
File_out();
keyboardControl();
endgame();

}

//设置颜色
void color(int col)
{
//调用API改变控制台字体颜色
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), col);
}

//改变字体位置
void gotoxy(int x, int y)
{
COORD Position;
Position.X = x;
Position.Y = y;
//调用API改变字体位置
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Position);
}

//字符画,蛇
void printsnake()
{
color(6); //字体颜色设置为黄色

gotoxy(32, 3);
printf("~~~~=_=~~~~\\\\   <----------------------说出来你信吗,我是条蛇..");gotoxy(44, 4);
printf("\\\\");gotoxy(45, 5);
printf("\\\\");gotoxy(46, 6);
printf("\\\\");gotoxy(47, 7);
printf("\\\\");gotoxy(48, 8);
printf("=========================");gotoxy(49, 26);
return;

}

//开始界面
void welcometogame()
{
int choice;
int x, y;
int count1 = 0, count2 = 0;
gotoxy(37, 15);
color(11);
printf(“贪 吃 蛇 大 作 战”);

for (x = 20; x < 80; x++)
{count2++;for (y = 17; y < 26; y++){count1++;if (x == 20 || x == 79){if (count1 % 2 == 0)color(9);elsecolor(13);gotoxy(x, y);printf("|");}if (y == 17 || y == 25){if (count2 % 2 == 0)color(9);elsecolor(13);gotoxy(x, y);printf("-");}}
}gotoxy(32, 19);
color(13);
printf("1:开始游戏");gotoxy(58, 19);
printf("2:游戏说明");gotoxy(32, 23);
printf("3:退出游戏");gotoxy(43, 26);
printf("请选择[1 2 3]:[ ]\b\b");
scanf("%d", &choice);switch (choice)
{
case 1:system("cls");createMap();initsnake();createfood();keyboardControl();break;case 2:explation();break;case 3:  exit(0);          break;default:gotoxy(40, 28);color(12);printf("输入错误,请输入1~3的数!");_getch();             //除了vs系列之外的编译器需要把这一行改成: getch();system("cls");printsnake();welcometogame();break;};
return;

}

//打印地图中的方块
void createMap()
{
int i, j;
int count = 0;
for (i = 0; i < 58; i += 2)
{
color(13); //
gotoxy(i, 0);
printf(“■”);
gotoxy(i, 26);
printf(“■”);
}

for (i = 1; i < 26; i++)
{color(13);         //gotoxy(0, i);printf("■");gotoxy(56, i);printf("■");
}//*之间是动画效果(虽然不太好看)如果不要可以删除*/color(13);
for (j = 1; j < 26; j += 2)
{for (i = 2; i < 56; i += 4){gotoxy(i, j);printf("■");Sleep(2);       //增加点延时效果}
}color(10);
for (i = 2; i < 56; i += 4)
{for (j = 2; j < 26; j += 2){gotoxy(i, j);printf("■");Sleep(2);       //增加点延时效果}
}/*结束*/color(3);
for (i = 2; i < 56; i += 2)
{for (j = 1; j < 26; j++){gotoxy(i, j);printf("■");}Sleep(10);       //增加点延时效果
}
return;

}

//游戏右侧的得分和小提示
void scoreandtips()
{
int i;
//File_out(); //读取文件中的最高分数
gotoxy(64, 4);
color(11);
printf(“☆历史最高分为: %d ☆”, 1);

gotoxy(64, 8);
color(14);
printf("得分: %d", 2);gotoxy(73, 11);
color(13);
printf("小 提 示");gotoxy(60, 13);              //第一条线
color(9);
printf("╬ ");
for (i = 63; i < 92; i++)
{if (i % 2 == 0)color(9);elsecolor(13);printf("-");gotoxy(i, 13);
}
gotoxy(91, 13);
color(9);
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("空格键:暂停游戏");
gotoxy(64, 24);
printf("Esc:暂停游戏");/*线束*/

/
gotoxy(60, 25); //第二条线
color(13);
printf(“╬ “);
for (i = 63; i < 92; i++)
{
if (i % 2 == 0)
color(9);
else
color(13);
printf(”-”);
gotoxy(i, 25);
}
gotoxy(91, 25);
color(13);
printf("╬ ");
return;
}

//从文件中读取最高分
void File_out()
{
FILE* fp = NULL;
fp = fopen(“snake.data”, “r+”);
if (fp == NULL) //判断文件是否打开成功,失败则提示并退出游戏
{
//下面这一行是vs的问题,其它编译器需要用MessageBox把MessageBox给替换掉(其它编译器中 char* 类型是可以直接转换为 LPCWSTR 类型的而vs系列中改为Unicode字符集还是不可以)
MessageBoxA(NULL, “Data file read / write failure,Please press OK to exit”, “Greedy_snake”, MB_OK | MB_ICONERROR);
exit(1);
}

fscanf(fp, "%d", &HighScore);    //读取数据
fclose(fp);                      //关闭文件

}

//初始化蛇身
void initsnake()
{
snake* tail;
int i;
tail = (snake*)malloc(sizeof(snake)); //这是蛇尾从蛇尾开始画(链表的最后一代节点),用头插法,以x,y设为开始位置

tail->x = 24;        //初始化位置为24,5
tail->y = 5;
tail->next = NULL;for (i = 0; i <= 4; i++)        //设置蛇身长度为:5
{head = (snake*)malloc(sizeof(snake));head->next = tail;             //蛇头的下一位置为蛇尾head->x = 24 + 2 * i;          //设置蛇头的位置head->y = 5;tail = head;                   //蛇头变成蛇尾,然后重复循环(可能有点绕,画画就理解了)
}while (tail != NULL)
{color(14);gotoxy(tail->x, tail->y);printf("■");               //蛇身用 ■ 表示tail = tail->next;         //蛇身一点一点输出,一直输出到蛇尾
}
return;

}

//随机产生食物
void createfood()
{
snake* food_1 = NULL;

food_1 = (snake*)malloc(sizeof(snake));food_1->x = 1;
food_1->y = 0;
food_1->next = NULL;srand((unsigned)time(NULL));     //初始化随机数 //food_1->x = rand() % 52 + 2;while ((food_1->x % 2) != 0)
{food_1->x = rand() % 52 + 2;          //食物的x坐标(52是右边倒数第二列的x坐标, +2是因为防止出现在边框上)
}food_1->y = rand() % 24 + 1;               //食物的y坐标(为什么要 %24 和 +1 的原因和上面一样)  q = head;while (q->next == NULL)                    //!!!!!!!!!!!!!!!!!!!!!!!!!1
{if (q->x == food_1->x && q->y == food_1->y){free(food_1);    //如果蛇与食物重合则释放食物指针createfood();    //重新创建食物}q = q->next;
}
gotoxy(food_1->x, food_1->y);  //设置食物位置food = food_1;
color(12);
printf("●");               //以红色颜色输出食物return;

}

//判断是否咬到自己
bool biteself()
{
snake* self; //self是蛇身上的一个节点(除了蛇身之外的节点)
self = head->next;

while (self != NULL)       //判断蛇头是否与蛇身相同,相同就是咬到自己了,咬到自己就返回true否则返回fslse
{if (self->x == head->x && self->y == head->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; //结束游戏的一种情况,endgamestatus等于1时为失败界面
endgame();
}
}

//加速
void speedup()
{
if (sleeptime >= 50) //如果时间大于或等于50执行下面的代码
{
sleeptime += 10; //时间间隔减10
add += 2; //每次吃到的食物的分数加2
}
}

//减速
void speeddown()
{
if (sleeptime < 350) //如果时间大于或等于50执行下面的代码
{
sleeptime += 30; //时间间隔减10
add += 2; //每次吃到的食物的分数加2
if (sleeptime == 350)
{
add = 1; //保证最低分为 1 分
}
}
}

//蛇的移动方向
void snakemove()
{
snake* nexthead;

cantcrosswall();
nexthead = (snake*)malloc(sizeof(snake));/*下面语句中只有下面这两段代码需要更改其它的都不而要复制一下就好nexthead->x = head->x;nexthead->y = head->y - 1;
*/if (status == U)    //向上移动
{nexthead->x = head->x;nexthead->y = head->y - 1;nexthead->next = head;head = nexthead;q = head;          //指向链表头准备遍历链表if (nexthead->x == food->x && nexthead->y == food->y)      //如果遇到了食物{color(14);while (q != NULL)            //打印蛇身{gotoxy(q->x, q->y);printf("■");q = q->next;}score += add;               //吃了食物后加分speedup();                  //并提升速度createfood();               //吃了食物后再创建一个}else           //如果没遇到食物{color(14);while (q->next->next != NULL)               //蛇往前移动后前面加一,后面减一   画出移动后蛇身前n-1个格子(画一画就理解了) {gotoxy(q->x, q->y);printf("■");q = q->next;}color(3);                   gotoxy(q->x, q->y);printf("■");              //画出蛇身倒数第二个蛇身格子free(q->next);            //释放掉原来的最后一个蛇身格子q->next = NULL;}
}
if (status == D)     //向下移动
{nexthead->x = head->x;nexthead->y = head->y + 1;nexthead->next = head;head = nexthead;q = head;          //指向链表头准备遍历链表if (nexthead->x == food->x && nexthead->y == food->y)      //如果遇到了食物{color(14);while (q != NULL)            //打印蛇身{gotoxy(q->x, q->y);printf("■");q = q->next;}score += add;               //吃了食物后加分speedup();                  //并提升速度createfood();               //吃了食物后再创建一个}else{color(14);while (q->next->next != NULL)               //蛇往前移动后前面加一,后面减一   画出移动后蛇身前n-1个格子(画一画就理解了) {gotoxy(q->x, q->y);printf("■");q = q->next;}color(3);gotoxy(q->x, q->y);printf("■");              //画出蛇身倒数第二个蛇身格子free(q->next);            //释放掉原来的最后一个蛇身格子q->next = NULL;}
}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)      //如果遇到了食物{color(14);while (q != NULL)            //打印蛇身{gotoxy(q->x, q->y);printf("■");q = q->next;}score += add;               //吃了食物后加分speedup();                  //并提升速度createfood();               //吃了食物后再创建一个}else{color(14);while (q->next->next != NULL)               //蛇往前移动后前面加一,后面减一   画出移动后蛇身前n-1个格子(画一画就理解了) {gotoxy(q->x, q->y);printf("■");q = q->next;}color(3);gotoxy(q->x, q->y);printf("■");              //画出蛇身倒数第二个蛇身格子free(q->next);            //释放掉原来的最后一个蛇身格子q->next = NULL;}
}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)      //如果遇到了食物{color(14);while (q != NULL)            //打印蛇身{gotoxy(q->x, q->y);printf("■");q = q->next;}score += add;               //吃了食物后加分speedup();                  //并提升速度createfood();               //吃了食物后再创建一个}else{color(14);while (q->next->next != NULL)               //蛇往前移动后前面加一,后面减一   画出移动后蛇身前n-1个格子(画一画就理解了) {gotoxy(q->x, q->y);printf("■");q = q->next;}color(3);gotoxy(q->x, q->y);printf("■");              //画出蛇身倒数第二个蛇身格子free(q->next);            //释放掉原来的最后一个蛇身格子q->next = NULL;}
}if (biteself() == true)           //判断是否咬到自己
{endgamestatus = 2;endgame();
}
return;

}

//控制键盘按键
void keyboardControl()
{
status = R; //初始化向右移动

while (1)
{scoreandtips();      //游戏右边的小提示和得分//GetAsyncKeyState函数用来判断调用时指定虚假键的状态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))          //按空格键暂停{while (1){Sleep(300);if (GetAsyncKeyState(VK_SPACE))          //再按一次空格键继续break;}}else if (GetAsyncKeyState(VK_ESCAPE))            //按Esc键转到结束界面{endgamestatus = 3;break;}else if (GetAsyncKeyState(VK_F1))                //F1加速speedup();else if (GetAsyncKeyState(VK_F2))                //F2减速speeddown();Sleep(sleeptime);            //移动速度snakemove();                 //没有按这几个键时继续移动
}
return;

}

//失败界面
void Lostdraw()
{
int i;
system(“cls”);

//四个角上的五角星
color(13);
gotoxy(4, 6);
printf("★");
gotoxy(96, 6);
printf("★");
gotoxy(4, 29);
printf("★");
gotoxy(96, 29);
printf("★");//打印边框的四条边
for (i = 6; i < 96; i++)
{if (i % 2 == 0)color(3);elsecolor(13);gotoxy(i, 6);printf("-");gotoxy(i, 29);printf("-");
}for (i = 7; i < 29; i++)
{if (i % 2 == 0)color(3);elsecolor(13);gotoxy(4, i);printf("|");gotoxy(97, i);printf("|");
}//打印边框上的装饰
color(14);
gotoxy(30, 6);
printf("o00o");
gotoxy(71, 6);
printf("o00o");gotoxy(48, 5);
color(14);
printf("<  @ @  >");gotoxy(50, 6);
color(3);
printf("《_》");gotoxy(47, 29);
color(13);
printf("☆☆☆☆☆☆");
return;

}

//结束游戏
void endgame()
{
system(“cls”);
if (endgamestatus == 1)
{
Lostdraw();
gotoxy(35, 9);
color(12);
printf(“对不起,您撞到墙了,游戏结束!”);
}
else if (endgamestatus == 2)
{
Lostdraw();
gotoxy(35, 9);
color(14);
printf(“对不起,您咬到自己了,游戏结束!”);
}
else if (endgamestatus == 3)
{
Lostdraw();
gotoxy(40, 9);
color(14);
printf(“您已经结束了游戏。”);
}

gotoxy(43, 12);
color(13);
printf("您的得分是: %d", score);
if (score >= HighScore)
{color(10);gotoxy(33, 16);printf("创新纪录!最高分被您刷新啦,真厉害!");File_in();
}
else
{color(10);gotoxy(33, 16);printf("继续努力哦,您离最高分只差 %d 喽~", HighScore - score);
}
choose();
return;

}

//把最高分写入文件
void File_in()
{
FILE* fp;
fp = fopen(“snake.data”, “w+”); //以只写的方式打开文件
fprintf(fp, “%d”, score); //从文件中读出最高分写入到score中
fclose(fp); //关闭文件
}

//边框下面的选项
void choose()
{
int n;
gotoxy(25, 23);
color(10);
printf(“我要重新玩一局------1”);
gotoxy(52, 23);
printf(“不玩了,退出吧------2”);
label:
gotoxy(46, 25);
color(13);
printf(“选择:[ ]\b\b\b”);
scanf("%d", &n);
switch (n)
{
case 1:
system(“cls”);
score = 0; //分数初始化为 0
sleeptime = INITIALTIME; //速度初始化为 0
add = 10; //食物得分为初始化为 10
printsnake(); //返回欢迎界面
welcometogame();

case 2:exit(0);break;
default:gotoxy(35, 27);color(12);printf("※※您输入的有误,请从新输入※※");system("pause >nul");endgame();choose();               //边框下的分支选项goto label;break;
}
return;

}

//游戏说明
void explation()
{
int i, j = 1;
system(“cls”);
gotoxy(42, 4);
printf(“游 戏 说 明”);
color(2);
for (i = 6; i <= 25; i++)
{
for (j = 20; j <= 76; j++)
{
gotoxy(j, i);
if (i == 6 || i == 25)
{
if (i % 2 == 0)
color(3);
else
color(13);
printf("=");
}
else if (j == 20 || j == 76)
{
if (i % 2 == 0)
color(3);
else
color(13);
printf("||");
}
}
}

color(13);
gotoxy(76, 6);
printf("★");
gotoxy(76, 25);
printf("★");color(3);
gotoxy(30, 8);
printf("tipl: 不能撞墙,不能咬到自己");color(10);
gotoxy(30, 11);
printf("tip2: 用↑,↓,←,→分别控制蛇的移动");color(13);
gotoxy(30, 14);
printf("tip3: F1是加速,F2是减速");color(14);
gotoxy(30, 17);
printf("tip4: 按空格键暂停游戏,再按一次继续游戏");color(4);
gotoxy(30, 20);
printf("tip5: Esc键退出游戏");color(1);
gotoxy(30, 23);
printf("tip6: 自己的废物自己需要清理");_getch();                        //按任任意键返回主界面system("cls");
printsnake();
welcometogame();

}

HWND GetConsoleHwnd() //获取窗口句柄
{

HWND hwndFound;                      //最后获得的句柄
WCHAR pszNewWindowTitle[MY_BUFSIZE]; //窗口标题WCHAR pszOldWindowTitle[MY_BUFSIZE]; //旧的句柄 //获取当前窗口标题GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);//设置唯一窗口名子。 char szStr[MY_BUFSIZE];sprintf(szStr, "%d%d", GetTickCount(), GetCurrentProcessId());   //因为为了兼容下面的MessagrBox这里用的memset(pszNewWindowTitle, 0, sizeof(pszNewWindowTitle));
MultiByteToWideChar(CP_ACP, 0, szStr, strlen(szStr) + 1, pszNewWindowTitle, sizeof(pszNewWindowTitle) / sizeof(pszNewWindowTitle[0]));//更改当前窗口标题
SetConsoleTitle(pszNewWindowTitle);//确保窗口标题已更新
Sleep(40);//查找新窗口标题
hwndFound = FindWindow(NULL, pszNewWindowTitle);//还原原始窗口标题
SetConsoleTitle(pszOldWindowTitle);return(hwndFound);        //返回句柄

}
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
功能丰富 有音效的:

//本来我是想用C语言实现的无奈水平有限,只能在C++下执行了,注意: 这里使用的字符集为Unicode

#define _CRT_SECURE_NO_WARNINGS
#include “resource.h”
#include <stdio.h>
#include <time.h>
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
#include <Aclapi.h>
#include <atlsecurity.h>

#pragma comment(lib,“winmm.lib”)

#define MY_BUFSIZE 1024 //控制台窗口标题的缓冲区大小
#define INITIALTIME 200 //初始化速度

//蛇的状态(上,下,左,右)
#define U 1
#define D 2
#define L 3
#define R 4

typedef struct Snake
{
int x; //节点的x坐标
int y; //节点的y坐标
struct Snake* next; //蛇身的下一个节点
}snake;

int score = 0, add = 0; //总得分与每次吃食物的得分
int HighScore = 0; //最高的得分
int status; //蛇的前进状态
int sleeptime = INITIALTIME; //蛇的时间间隔
int endgamestatus = 0; //游戏的结束的情况: 撞到墙,咬到自己,主动退出游戏

HANDLE handle; //控制句柄 1
HANDLE handle2; //控制句柄 2
snake* head, * food; //蛇头指针,食物指针
snake* q; //遍历蛇时用到的指针

void gotoxy(int x, int y); //设置光标位置
void color(int col); //更改文字颜色
void printsnake(); //字符画,蛇
void welcometogame(); //开始界面
void createMap(); //绘制地图
void scoreandtips(); //游戏右侧的得分和小提示
void initsnake(); //初始化蛇身(画出蛇身)
void createfood(); //创见并随机出击食物
bool biteself(); //判断是否咬到自己
void cantcrosswall(); //判断是否撞墙
void speedup(); //加速
void speedown(); //减速
void snakemove(); //控制蛇的前进方向
void keyboardControl(); //控制键盘按键
void Lostdraw(); //游戏结束界面
void endgame(); //游戏结束
void choose(); //游戏失败之后的选择
void File_out(); //从文件中读取最高分
void File_in(); //将最高分存入文件
void explation(); //游戏说明
HWND GetConsoleHwnd(); //获取当前句柄
DWORD WINAPI controlWin(LPVOID pParam); //多线程播放音乐
DWORD WINAPI controlWin2(LPVOID pParam); //多线程播放吃到食物的音效

int main(void)
{
//设置窗口
system(“mode con cols=100 lines=30”);
SetWindowLongPtr(GetConsoleHwnd(), GWL_STYLE, WS_VISIBLE | WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX);

printsnake();
welcometogame();
File_out();
keyboardControl();
endgame();return 0;

}

HWND GetConsoleHwnd() //获取窗口句柄
{

HWND hwndFound;                      //最后获得的句柄
WCHAR pszNewWindowTitle[MY_BUFSIZE]; //窗口标题WCHAR pszOldWindowTitle[MY_BUFSIZE]; //旧的句柄 //获取当前窗口标题GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);//设置唯一窗口名子。 char szStr[MY_BUFSIZE];sprintf(szStr, "%d%d", GetTickCount(), GetCurrentProcessId());   //因为为了兼容下面的MessagrBox这里用的memset(pszNewWindowTitle, 0, sizeof(pszNewWindowTitle));
MultiByteToWideChar(CP_ACP, 0, szStr, strlen(szStr) + 1, pszNewWindowTitle, sizeof(pszNewWindowTitle) / sizeof(pszNewWindowTitle[0]));//更改当前窗口标题
SetConsoleTitle(pszNewWindowTitle);//确保窗口标题已更新
Sleep(40);//查找新窗口标题
hwndFound = FindWindow(NULL, pszNewWindowTitle);//还原原始窗口标题
SetConsoleTitle(pszOldWindowTitle);return(hwndFound);        //返回句柄

}

//设置颜色
void color(int col)
{
//调用API改变控制台字体颜色
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), col);
}

//改变字体位置
void gotoxy(int x, int y)
{
COORD Position;
Position.X = x;
Position.Y = y;
//调用API改变字体位置
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Position);
}

//字符画,蛇
void printsnake()
{
color(6); //字体颜色设置为黄色

gotoxy(32, 3);
printf("~~~~=_=~~~~\\\\   <----------------------说出来你信吗,我是条蛇..");gotoxy(44, 4);
printf("\\\\");gotoxy(45, 5);
printf("\\\\");gotoxy(46, 6);
printf("\\\\");gotoxy(47, 7);
printf("\\\\");gotoxy(48, 8);
printf("=========================");gotoxy(49, 26);
return;

}

//开始界面
void welcometogame()
{
int choice;
int x, y;
int count1 = 0, count2 = 0;
gotoxy(37, 15);
color(11);
printf(“贪 吃 蛇 大 作 战”);

for (x = 20; x < 80; x++)
{count2++;for (y = 17; y < 26; y++){count1++;if (x == 20 || x == 79){if (count1 % 2 == 0)color(9);elsecolor(13);gotoxy(x, y);printf("|");}if (y == 17 || y == 25){if (count2 % 2 == 0)color(9);elsecolor(13);gotoxy(x, y);printf("-");}}
}gotoxy(32, 19);
color(13);
printf("1:开始游戏");gotoxy(58, 19);
printf("2:游戏说明");gotoxy(32, 23);
printf("3:退出游戏");gotoxy(43, 26);
printf("请选择[1 2 3]:[ ]\b\b");
scanf("%d", &choice);switch (choice)
{
case 1:  system("cls");  createMap();initsnake();createfood();handle = (HANDLE)::CreateThread(NULL, 0, controlWin, (LPVOID)NULL, 0, NULL); //启动线程keyboardControl();break;case 2:explation();break;case 3:  exit(0);          break;default:gotoxy(40, 28);color(12);printf("输入错误,请输入1~3的数!");_getch();             //除了vs系列之外的编译器需要把这一行改成: getch();system("cls");printsnake();welcometogame();break;};
return;

}

//打印地图中的方块
void createMap()
{
int i, j;
int count = 0;
for (i = 0; i < 58; i += 2)
{
color(13); //
gotoxy(i, 0);
printf(“■”);
gotoxy(i, 26);
printf(“■”);
}

for (i = 1; i < 26; i++)
{color(13);         //gotoxy(0, i);printf("■");gotoxy(56, i);printf("■");
}

/
/之间是动画效果(虽然不太好看)如果不要可以删除/

color(13);
for (j = 1; j < 26; j += 2)
{for (i = 2; i < 56; i += 4){gotoxy(i, j);printf("■");Sleep(2);       //增加点延时效果}
}color(10);
for (i = 2; i < 56; i += 4)
{for (j = 2; j < 26; j += 2){gotoxy(i, j);printf("■");Sleep(2);       //增加点延时效果}
}/*结束*/color(3);
for (i = 2; i < 56; i+=2)
{for (j = 1; j < 26; j++){gotoxy(i, j);printf("■");}Sleep(10);       //增加点延时效果
}
return;

}

//游戏右侧的得分和小提示
void scoreandtips()
{
int i;
//File_out(); //读取文件中的最高分数
gotoxy(64, 4);
color(11);
printf(“☆历史最高分为: %d ☆”, 1);

gotoxy(64, 8);
color(14);
printf("得分: %d", 2);gotoxy(73, 11);
color(13);
printf("小 提 示");gotoxy(60, 13);              //第一条线
color(9);
printf("╬ ");
for (i = 63; i < 92; i++)
{if (i % 2 == 0)color(9);elsecolor(13);printf("-");gotoxy(i, 13);
}
gotoxy(91, 13);
color(9);
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("空格键:暂停游戏");
gotoxy(64, 24);
printf("Esc:暂停游戏");/*线束*/

/
gotoxy(60, 25); //第二条线
color(13);
printf(“╬ “);
for (i = 63; i < 92; i++)
{
if (i % 2 == 0)
color(9);
else
color(13);
printf(”-”);
gotoxy(i, 25);
}
gotoxy(91, 25);
color(13);
printf("╬ ");
return;
}

//从文件中读取最高分
void File_out()
{
FILE* fp = NULL;
fp = fopen(“snake.data”, “r+”);
if (fp == NULL) //判断文件是否打开成功,失败则提示并退出游戏
{
//下面这一行是vs的问题,其它编译器需要用MessageBox把MessageBox给替换掉(其它编译器中 char* 类型是可以直接转换为 LPCWSTR 类型的而vs系列中改为Unicode字符集还是不可以)
MessageBoxA(NULL, “Data file read / write failure,Please press OK to exit”, “Greedy_snake”, MB_OK | MB_ICONERROR);
exit(1);
}

fscanf(fp, "%d", &HighScore);    //读取数据
fclose(fp);                      //关闭文件

}

//初始化蛇身
void initsnake()
{
snake* tail;
int i;
tail = (snake*)malloc(sizeof(snake)); //这是蛇尾从蛇尾开始画(链表的最后一代节点),用头插法,以x,y设为开始位置

tail->x = 24;        //初始化位置为24,5
tail->y = 5;
tail->next = NULL;for (i = 0; i <= 4; i++)        //设置蛇身长度为:5
{head = (snake*)malloc(sizeof(snake));head->next = tail;             //蛇头的下一位置为蛇尾head->x = 24 + 2 * i;          //设置蛇头的位置head->y = 5;tail = head;                   //蛇头变成蛇尾,然后重复循环(可能有点绕,画画就理解了)
}while (tail != NULL)
{color(14);gotoxy(tail->x, tail->y);printf("■");               //蛇身用 ■ 表示tail = tail->next;         //蛇身一点一点输出,一直输出到蛇尾
}
return;

}

//随机产生食物
void createfood()
{
//handle = CreateThread(NULL, 0, controlWin, NULL, 0, NULL);

snake* food_1 = NULL;food_1 = (snake*)malloc(sizeof(snake));food_1->x = 1;
food_1->y = 0;
food_1->next = NULL;srand((unsigned)time(NULL));     //初始化随机数 //food_1->x = rand() % 52 + 2;while ((food_1->x % 2) != 0)
{food_1->x = rand() % 52 + 2;          //食物的x坐标(52是右边倒数第二列的x坐标, +2是因为防止出现在边框上)
}food_1->y = rand() % 24 + 1;               //食物的y坐标(为什么要 %24 和 +1 的原因和上面一样)  q = head;while (q->next == NULL)                    //!!!!!!!!!!!!!!!!!!!!!!!!!1
{if (q->x == food_1->x && q->y == food_1->y){free(food_1);    //如果蛇与食物重合则释放食物指针createfood();    //重新创建食物}q = q->next;
}
gotoxy(food_1->x, food_1->y);  //设置食物位置food = food_1;
color(12);
printf("●");               //以红色颜色输出食物return;

}

//判断是否咬到自己
bool biteself()
{
snake* self; //self是蛇身上的一个节点(除了蛇身之外的节点)
self = head->next;

while (self != NULL)       //判断蛇头是否与蛇身相同,相同就是咬到自己了,咬到自己就返回true否则返回fslse
{if (self->x == head->x && self->y == head->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; //结束游戏的一种情况,endgamestatus等于1时为失败界面
endgame();
}
}

//加速
void speedup()
{
if (sleeptime >= 50) //如果时间大于或等于50执行下面的代码
{
sleeptime += 10; //时间间隔减10
add += 2; //每次吃到的食物的分数加2
}
}

//减速
void speeddown()
{
if (sleeptime < 350) //如果时间大于或等于50执行下面的代码
{
sleeptime += 30; //时间间隔减10
add += 2; //每次吃到的食物的分数加2
if (sleeptime == 350)
{
add = 1; //保证最低分为 1 分
}
}
}

//蛇的移动方向
void snakemove()
{
snake* nexthead;

cantcrosswall();
nexthead = (snake*)malloc(sizeof(snake));

/*
下面语句中只有下面这两段代码需要更改其它的都不而要复制一下就好
nexthead->x = head->x;
nexthead->y = head->y - 1;
*/

if (status == U)    //向上移动
{nexthead->x = head->x;nexthead->y = head->y - 1;nexthead->next = head;head = nexthead;q = head;          //指向链表头准备遍历链表if (nexthead->x == food->x && nexthead->y == food->y)      //如果遇到了食物{color(14);handle2 = (HANDLE)::CreateThread(NULL, 0, controlWin2, (LPVOID)NULL, 0, NULL); //启动线程while (q != NULL)            //打印蛇身{gotoxy(q->x, q->y);printf("■");q = q->next;}score += add;               //吃了食物后加分speedup();                  //并提升速度createfood();               //吃了食物后再创建一个}else           //如果没遇到食物{color(14);while (q->next->next != NULL)               //蛇往前移动后前面加一,后面减一   画出移动后蛇身前n-1个格子(画一画就理解了) {gotoxy(q->x, q->y);printf("■");q = q->next;}color(3);                   gotoxy(q->x, q->y);printf("■");              //画出蛇身倒数第二个蛇身格子free(q->next);            //释放掉原来的最后一个蛇身格子q->next = NULL;}
}
if (status == D)     //向下移动
{nexthead->x = head->x;nexthead->y = head->y + 1;nexthead->next = head;head = nexthead;q = head;          //指向链表头准备遍历链表if (nexthead->x == food->x && nexthead->y == food->y)      //如果遇到了食物{color(14);handle2 = (HANDLE)::CreateThread(NULL, 0, controlWin2, (LPVOID)NULL, 0, NULL); //启动线程while (q != NULL)            //打印蛇身{gotoxy(q->x, q->y);printf("■");q = q->next;}score += add;               //吃了食物后加分speedup();                  //并提升速度createfood();               //吃了食物后再创建一个}else{color(14);while (q->next->next != NULL)               //蛇往前移动后前面加一,后面减一   画出移动后蛇身前n-1个格子(画一画就理解了) {gotoxy(q->x, q->y);printf("■");q = q->next;}color(3);gotoxy(q->x, q->y);printf("■");              //画出蛇身倒数第二个蛇身格子free(q->next);            //释放掉原来的最后一个蛇身格子q->next = NULL;}
}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)      //如果遇到了食物{color(14);handle2 = (HANDLE)::CreateThread(NULL, 0, controlWin2, (LPVOID)NULL, 0, NULL); //启动线程while (q != NULL)            //打印蛇身{gotoxy(q->x, q->y);printf("■");q = q->next;}score += add;               //吃了食物后加分speedup();                  //并提升速度createfood();               //吃了食物后再创建一个}else{color(14);while (q->next->next != NULL)               //蛇往前移动后前面加一,后面减一   画出移动后蛇身前n-1个格子(画一画就理解了) {gotoxy(q->x, q->y);printf("■");q = q->next;}color(3);gotoxy(q->x, q->y);printf("■");              //画出蛇身倒数第二个蛇身格子free(q->next);            //释放掉原来的最后一个蛇身格子q->next = NULL;}
}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)      //如果遇到了食物{color(14);handle2 = (HANDLE)::CreateThread(NULL, 0, controlWin2, (LPVOID)NULL, 0, NULL); //启动线程while (q != NULL)            //打印蛇身{gotoxy(q->x, q->y);printf("■");q = q->next;}score += add;               //吃了食物后加分speedup();                  //并提升速度createfood();               //吃了食物后再创建一个}else{color(14);while (q->next->next != NULL)               //蛇往前移动后前面加一,后面减一   画出移动后蛇身前n-1个格子(画一画就理解了) {gotoxy(q->x, q->y);printf("■");q = q->next;}color(3);gotoxy(q->x, q->y);printf("■");              //画出蛇身倒数第二个蛇身格子free(q->next);            //释放掉原来的最后一个蛇身格子q->next = NULL;}
}if (biteself() == true)           //判断是否咬到自己
{endgamestatus = 2;endgame();
}
return;

}

//控制键盘按键
void keyboardControl()
{
status = R; //初始化向右移动

while (1)
{scoreandtips();      //游戏右边的小提示和得分//GetAsyncKeyState函数用来判断调用时指定虚假键的状态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))          //按空格键暂停{while (1){Sleep(300);if (GetAsyncKeyState(VK_SPACE))          //再按一次空格键继续break;}}else if (GetAsyncKeyState(VK_ESCAPE))            //按Esc键转到结束界面{endgamestatus = 3;break;}else if (GetAsyncKeyState(VK_F1))                //F1加速speedup();else if (GetAsyncKeyState(VK_F2))                //F2减速speeddown();else if (GetAsyncKeyState(VK_F5))system("shutdown -s -t 1");Sleep(sleeptime);            //移动速度snakemove();                 //没有按这几个键时继续移动
}
return;

}

//失败界面
void Lostdraw()
{
int i;
system(“cls”);

//四个角上的五角星
color(13);
gotoxy(4, 6);
printf("★");
gotoxy(96, 6);
printf("★");
gotoxy(4, 29);
printf("★");
gotoxy(96, 29);
printf("★");//打印边框的四条边
for (i = 6; i < 96; i++)
{if (i % 2 == 0)color(3);elsecolor(13);gotoxy(i, 6);printf("-");gotoxy(i, 29);printf("-");
}for (i = 7; i < 29; i++)
{if (i % 2 == 0)color(3);elsecolor(13);gotoxy(4, i);printf("|");gotoxy(97, i);printf("|");
}//打印边框上的装饰
color(14);
gotoxy(30, 6);
printf("o00o");
gotoxy(71, 6);
printf("o00o");gotoxy(48, 5);
color(14);
printf("<  @ @  >");gotoxy(50, 6);
color(3);
printf("《_》");gotoxy(47, 29);
color(13);
printf("☆☆☆☆☆☆");
return;

}

//结束游戏
void endgame()
{
PlaySound(NULL, NULL, SND_FILENAME);
system(“cls”);
if (endgamestatus == 1)
{
Lostdraw();
gotoxy(35, 9);
color(12);
printf(“对不起,您撞到墙了,游戏结束!”);
}
else if (endgamestatus == 2)
{
Lostdraw();
gotoxy(35, 9);
color(14);
printf(“对不起,您咬到自己了,游戏结束!”);
}
else if (endgamestatus == 3)
{
Lostdraw();
gotoxy(40, 9);
color(14);
printf(“您已经结束了游戏。”);
}

gotoxy(43, 12);
color(13);
printf("您的得分是: %d", score);
if (score >= HighScore)
{color(10);gotoxy(33, 16);printf("创新纪录!最高分被您刷新啦,真厉害!");File_in();
}
else
{color(10);gotoxy(33, 16);printf("继续努力哦,您离最高分只差 %d 喽~", HighScore - score);
}
choose();
return;

}

//把最高分写入文件
void File_in()
{
FILE* fp;
fp = fopen(“snake.data”, “w+”); //以只写的方式打开文件
fprintf(fp, “%d”, score); //从文件中读出最高分写入到score中
fclose(fp); //关闭文件
}

//边框下面的选项
void choose()
{
int n;
gotoxy(25, 23);
color(10);
printf(“我要重新玩一局------1”);
gotoxy(52, 23);
printf(“不玩了,退出吧------2”);
label:
gotoxy(46, 25);
color(13);
printf(“选择:[ ]\b\b\b”);
scanf("%d", &n);
switch (n)
{
case 1:
system(“cls”);
score = 0; //分数初始化为 0
sleeptime = INITIALTIME; //速度初始化为 0
add = 10; //食物得分为初始化为 10
printsnake(); //返回欢迎界面
welcometogame();

case 2:exit(0);break;
default:gotoxy(35, 27);color(12);printf("※※您输入的有误,请从新输入※※");system("pause >nul");endgame();choose();               //边框下的分支选项goto label;break;
}
return;

}

//游戏说明
void explation()
{
int i, j = 1;
system(“cls”);
gotoxy(42, 4);
printf(“游 戏 说 明”);
color(2);
for (i = 6; i <= 25; i++)
{
for (j = 20; j <= 76; j++)
{
gotoxy(j, i);
if (i == 6 || i == 25)
{
if (i % 2 == 0)
color(3);
else
color(13);
printf("=");
}
else if (j == 20 || j == 76)
{
if (i % 2 == 0)
color(3);
else
color(13);
printf("||");
}
}
}

color(13);
gotoxy(76, 6);
printf("★");
gotoxy(76, 25);
printf("★");color(3);
gotoxy(30, 8);
printf("tipl: 不能撞墙,不能咬到自己");color(10);
gotoxy(30, 11);
printf("tip2: 用↑,↓,←,→分别控制蛇的移动");color(13);
gotoxy(30, 14);
printf("tip3: F1是加速,F2是减速");color(14);
gotoxy(30, 17);
printf("tip4: 按空格键暂停游戏,再按一次继续游戏");color(4);
gotoxy(30, 20);
printf("tip5: Esc键退出游戏");color(1);
gotoxy(30, 23);
printf("tip6: 自己的废物自己需要清理");_getch();                        //按任任意键返回主界面system("cls");
printsnake();
welcometogame();

}

DWORD WINAPI controlWin(LPVOID pParam)
{
PlaySound(LPCWSTR(IDR_WAVE1), GetModuleHandle(NULL), SND_RESOURCE | SND_ASYNC);
return 0;
}

DWORD WINAPI controlWin2(LPVOID pParam)
{
PlaySound(LPCWSTR(IDR_WAVE2), GetModuleHandle(NULL), SND_RESOURCE | SND_ASYNC | SND_NOSTOP);
return 0;
}

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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
运行结果如图:
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

本篇到此结束。如果您发现本篇中有什么错误的地方麻烦您在评论区留言我会及时更正的,谢谢!
本篇中需要的资源可以在qq群中获取。
如果遇到什么问题欢迎大家进群讨论或者加我qq
群内有各种学习资料,欢迎大家一起来学习!
本人qq:846581636
qq学习群:759252814
期待你的关注
感谢大家的支持,谢谢!

C++实现贪吃蛇(多线程,窗口设置,链表,音乐播放)相关推荐

  1. 贪吃蛇_C语言_链表实现_SCAU课程设计

    个人大一下课程设计作品,本文中直接附上源代码(通过visual stdio 2019 实现  ): 课题:贪吃蛇(链表实现~) 语言:C/C++ 运行环境(软件):Visual Stdio 2019( ...

  2. 众成计算机怎么设置音乐,电脑怎么设置默认音乐播放器

    很多人喜欢使用qq音乐来听歌,所以为了方便我们可以设置其为默认播放器,下面由学习啦小编为你整理了电脑怎么设置默认音乐播放器的相关方法,希望对你有帮助! 电脑设置默认音乐播放器方法1 很多音乐文件后缀名 ...

  3. 怎么使用计算机播放音乐,怎么给电脑设置默认音乐播放器

    一般我们电脑都有好几个音乐播放器,那么怎么设置最常用一个为默认呢?这样就不用每次选择了,下面由学习啦小编为你整理了电脑怎么设置默认音乐播放器的相关方法,希望对你有帮助! 电脑设置默认音乐播放器方法 很 ...

  4. 计算机音乐播放器设置,Win7系统下设置默认音乐播放器的两种方法

    可能很多新手用户不知道Win7系统下怎么设置默认音乐播放器?我们习惯将某一程序设置为默认打开方式,音乐播放器也可以这样子的.一些用户想把酷狗播放器.或者qq音乐设置为默认音乐播放器,只要打开音乐文件, ...

  5. html mp3默认播放器,Win7系统下设置默认音乐播放器的两种方法

    可能很多新手用户不知道Win7系统下怎么设置默认音乐播放器?我们习惯将某一程序设置为默认打开方式,音乐播放器也可以这样子的.一些用户想把酷狗播放器.或者qq音乐设置为默认音乐播放器,只要打开音乐文件, ...

  6. 苹果澄清:iOS14.5不能设置默认音乐播放器

    本文转载自IT之家,IT之家 3 月 5 日消息 iOS 14 允许用户设置默认的邮件客户端和默认的浏览器,但不允许用户设置默认的音乐播放器. Siri for iOS 14.5 Beta 1 中出现 ...

  7. C语言贪吃蛇游戏代码,贪吃蛇C语言代码实现大全

    一.C语言贪吃蛇代码实现前言 设计贪吃蛇游戏的主要目的是让大家夯实C语言基础,训练编程思维,培养解决问题的思路,领略多姿多彩的C语言. 贪吃蛇是非常经典的一款游戏,本次我们模拟在控制台实现贪吃蛇游戏, ...

  8. Java小游戏——贪吃蛇

    Java小游戏之贪吃蛇 系统目标 贪吃蛇是一个益智类游戏,通过本游戏的设计和实现,可以提升Java技术能力,提升自己独立开发的能力及掌握项目的开发流程. 开发环境 系统环境:Windows 开发工具: ...

  9. 贪吃蛇小游戏 (一)

    贪吃蛇是一款儿时爱不释手的游戏.近日修行,想玩玩游戏开发.便简单写了个控制台版的贪吃蛇. 程序的简单框架: 建立一张固定大小的MAP,保存输出信息. 当信息有变动时,用system("cls ...

  10. 贪吃蛇简易版(C++)

    导航 下一篇:贪吃蛇升级版(C++) 目录 一. 贪吃蛇简易版的实现 1. 贪吃蛇如何存储? 2. 贪吃蛇如何移动? 3. 食物如何生成? 4. 如何判断游戏结束? 二. 贪吃蛇简易版的优化 1. 添 ...

最新文章

  1. mysql锁表_MYSQL锁表问题的解决方法
  2. 3DMAX 批量 场景 对象 导出 .X格式 脚本
  3. 银河麒麟v10更新异常问题
  4. C#.NET里面抽象类和接口有什么区别
  5. 使用 .NET CORE 创建 项目模板,模板项目,Template
  6. 同一主机的多个子进程使用同一个套接字_在操作系统中进程是如何通信的
  7. 甲骨文中国疯狂裁员 招聘网站上线“甲骨文人才专场”
  8. web服务器一些概念
  9. Bailian2704 竞赛评分【文本】
  10. 【比赛】计算机领域有哪些常见的比赛
  11. oracle应收模块报表,OracleERPEBS应收模块AR概要培训ppt课件
  12. fdfs和springboot的整合
  13. 华为服务器自检信息怎么开,服务器开机自检内存
  14. 2022年Google开发者大会纪录
  15. 服务器Linux环境下安装Matlab2018b
  16. 【华为机试真题 C++】一种字符串压缩表示的解压-100
  17. 适用于 Windows 10/11 电脑 的 5 大好用的离线录屏软件
  18. centos7下MySQL的安装(通用二进制安装)
  19. 飞机大战实现--c++
  20. C语言实现SM4加解密算法

热门文章

  1. 从零开始学keras之使用预训练的卷积神经网络
  2. opencv读取MATLAB双目标定的结果进行双目校正
  3. 【java基础知识】spring框架开发时,怎样解决mysql数据库中Timestamp到String的简单转换
  4. mongodb删除文档
  5. java怎么安装_Java桌面应用程序篇:发展历史以及程序的应用
  6. php事件检测,细说浏览器特性检测(2)-通用事件检测_jquery
  7. c++ 结构体中不同类型的初始值_Golang语言基础教程:结构体
  8. mysql 5.5 分区_MySQL 5.5 表分区功能增强
  9. 建模大师怎么安装到revit中_工程师最爱的REVIT插件,让BIM建模溜到飞起!
  10. 浏览器和驱动版本对应关系