封面展示
内部展示
完整代码
#define _CRT_SECURE_NO_WARNINGS
#include<easyx.h>
#include<stdio.h>
#include<mmsystem.h>
#pragma comment (lib,"winmm.lib")
#define width 40//宽有40个格子
#define height 30//长有40个格子
#define size 25//每个正方形格子的边长为20
int snack[width][height] = { 0 };//蛇
char direction='w';//初始方向向上
int foodx, foody;//食物刷新位置
int isover = 0;//游戏是否结束
int result = 0;//得分
int speed = 5;//速度
int maxscore;//历史最高分
char title1[] = "开始游戏",title2[] = "退出游戏",title3[] = "查看历史最高分",title4[] = "游戏帮助",title5[]="选择难度模式";//主页文字
int x = width * size / 2, y = height * size / 2;//提前用于文字居中
void getmaxscore();//获得历史最高分
void Remembermaxscore();//记住最高分
void menu();//菜单
void choose();//选择指令
void choosespeed();//选择蛇的移动速度
void Look_maxscore();//查看最高分,重新打印封面
void BGM();//这么刺激的游戏,没有点音乐可不行
void game();//游戏实现
void gamehelp();//游戏帮助
void gamemap();//绘制游戏地图
void Initialize();//绘制初始蛇以及食物
void draw();//每次蛇不同,重新绘制
void movespeed();//控制蛇的移动速度
void operate();//控制蛇的操作
void move();//蛇的运动
int main()
{
getmaxscore();
initgraph(width * size, height * size);//地图尺寸
choose();
return 0;
}
void getmaxscore()
{
FILE* fp = fopen("历史最高分.txt", "r");
fscanf(fp, "%d", &maxscore);
fclose(fp);
}
void Remembermaxscore()
{
if (result > maxscore)
{
FILE* fp = fopen("历史最高分.txt", "w");
fprintf(fp, "%d", result);
fclose(fp);
}
}
void menu()
{
IMAGE img;
loadimage(&img, "./贪吃蛇封面.jpg", width * size, height * size);
putimage(0, 0, &img);
setfillcolor(YELLOW);
setlinecolor(BLACK);
setbkmode(TRANSPARENT);
settextstyle(20, 0, "黑体");
settextcolor(BLACK);
fillrectangle(x - 100, y - 60, x + 100, y - 20);//打印四个按钮,按钮居中
outtextxy(x - 100 + (200 - textwidth(title1))/2, y - 60 + (40 - textheight(title1))/2, title1);
fillrectangle(x - 100, y - 20, x + 100, y + 20);
outtextxy(x - 100 + (200 - textwidth(title2)) / 2, y - 20 + (40 - textheight(title2)) / 2, title2);
fillrectangle(x - 100, y + 20, x + 100, y + 60);
outtextxy(x - 100 + (200 - textwidth(title3)) / 2, y + 20 + (40 - textheight(title3)) / 2, title3);
fillrectangle(x - 100, y + 60, x + 100, y + 100);
outtextxy(x - 100 + (200 - textwidth(title4)) / 2, y + 60 + (40 - textheight(title4)) / 2, title4);
fillrectangle(x - 100, y + 100, x + 100, y + 140);
outtextxy(x - 100 + (200 - textwidth(title5)) / 2, y + 100 + (40 - textheight(title5)) / 2, title5);
}
void choose()
{
menu();
int f = 0;
while (1)
{
ExMessage msg;
if (f) break;
while (peekmessage(&msg, EX_MOUSE))
{
switch (msg.message)
{
case WM_LBUTTONDOWN:
if (msg.x > x - 100 && msg.x<x + 100 && msg.y>y - 60 && msg.y < y - 20)
{
game();
menu();
}
else if (msg.x > x - 100 && msg.x<x + 100 && msg.y>y - 20 && msg.y < y + 20)
f = 1;
else if (msg.x > x - 100 && msg.x<x + 100 && msg.y>y + 20 && msg.y < y + 60)
{
Look_maxscore();
menu();
}
else if (msg.x > x - 100 && msg.x<x + 100 && msg.y>y + 60 && msg.y < y + 100)
{
gamehelp();
menu();
}
else if (msg.x > x - 100 && msg.x<x + 100 && msg.y>y + 100 && msg.y < y + 140)
{
choosespeed();
menu();
}
break;
default:
break;
}
}
}
}
void choosespeed()
{
IMAGE img;
loadimage(&img, "./01.jpg", width * size, height * size);
putimage(0, 0, &img);
char back[] = "<<===";
settextstyle(30, 0, "宋体");
setfillcolor(BROWN);
fillrectangle(10, 10, 100, 50);
outtextxy(10 + (90 - textwidth(back)) / 2, 10 + (40 - textheight(back)) / 2, back);
setfillcolor(GREEN);
fillrectangle(200, 305, 400, 445);
outtextxy(200 + (200 - textwidth("老年人模式")) / 2, 305 + (140 - textheight("老年人模式")) / 2, "老年人模式");
setfillcolor(YELLOW);
fillrectangle(420, 305, 620, 445);
outtextxy(420 + (200 - textwidth("儿童模式")) / 2, 305 + (140 - textheight("儿童模式")) / 2, "儿童模式");
setfillcolor(RED);
fillrectangle(640, 305, 840, 445);
outtextxy(640 + (200 - textwidth("大学生模式")) / 2, 305 + (140 - textheight("大学生模式")) / 2, "大学生模式");
int f = 0;
while (1)
{
ExMessage msg;
while (peekmessage(&msg, EX_MOUSE))
{
switch (msg.message)
{
case WM_LBUTTONDOWN:
if (msg.x > 200 && msg.x < 400 && msg.y>305 && msg.y < 445)
{
speed = 10;
}
else if (msg.x > 420 && msg.x < 620 && msg.y>305 && msg.y < 445)
{
speed = 5;
}
else if (msg.x > 640 && msg.x < 840 && msg.y>305 && msg.y < 445)
{
speed = 3;
}
else if (msg.x > 10 && msg.x < 100 && msg.y>10 && msg.y < 50)
{
f = 1;
}
break;
default:
break;
}
}
if (f) break;
}
}
void Look_maxscore()
{
IMAGE img;
loadimage(&img, "./01.jpg", width * size, height * size);
putimage(0, 0, &img);
char score[100];
char back[] = "<<===";
settextstyle(30, 0, "宋体");
sprintf_s(score, "历史最高分:%d", maxscore);
outtextxy(width*size/2-120, height*size/2, score);
setfillcolor(BROWN);
fillrectangle(10, 10, 100, 50);
outtextxy(10 + (90 - textwidth(back)) / 2, 10 + (40 - textheight(back)) / 2,back);
int f = 0;
while (1)
{
ExMessage msg;
if (f) break;
while(peekmessage(&msg, EX_MOUSE))
{
switch (msg.message)
{
case WM_LBUTTONDOWN:
if (msg.x > 10 && msg.x < 100 && msg.y>10 && msg.y < 50)
f = 1;
break;
default:
break;
}
}
}
}
void gamehelp()
{
IMAGE img;
loadimage(&img, "./01.jpg", width * size, height * size);
putimage(0, 0, &img);
char back[] = "<<===";
settextstyle(30, 0, "宋体");
setfillcolor(BROWN);
fillrectangle(10, 10, 100, 50);
outtextxy(10 + (90 - textwidth(back)) / 2, 10 + (40 - textheight(back)) / 2, back);
outtextxy(50, 100, "帮助:");
outtextxy(200, 200, "按键操作:");
outtextxy(200, 250, "w:向上");
outtextxy(200, 300, "s:向下");
outtextxy(200, 350, "a:向左");
outtextxy(200, 400, "d:向右");
outtextxy(200, 450, "尽量吃更多的食物获得更高的分数。");
outtextxy(200, 500, "注意:蛇头不能撞上地图边缘和自己身体!");
int f = 0;
while (1)
{
ExMessage msg;
if (f) break;
while (peekmessage(&msg, EX_MOUSE))
{
switch (msg.message)
{
case WM_LBUTTONDOWN:
if (msg.x > 10 && msg.x < 100 && msg.y>10 && msg.y < 50)
f = 1;
break;
default:
break;
}
}
}
}
void BGM()
{
mciSendString("open ./level.mp3 alias s1", 0, 0, 0);
mciSendString("play s1 repeat", 0, 0, 0);
}
void game()
{
gamemap();
Initialize();
BGM();
while (1)
{
draw();
movespeed();
operate();
}
}
void gamemap()
{
setbkmode(BLUE);
cleardevice();
BeginBatchDraw();
setlinecolor(WHITE);//网格颜色
for (int i = size; i < width * size; i += size)//打印网格
line(i, 0, i, height * size);
for (int i = size; i < height * size; i += size)
line(0, i, width * size,i);
}
void Initialize()
{
snack[width / 2][height / 2] = 1;//初始蛇头
for (int i = 1; i <= 4; i++)
snack[width / 2][height / 2 + i] = i + 1;//初始蛇身
foodx = rand() % (width - 2) + 1;//随机刷出食物位置
foody = rand() % (height - 2) + 1;
}
void draw()
{
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
if (snack[i][j] != 0)
setfillcolor(HSVtoRGB(snack[i][j] * 10, 0.9, 1));
else
setfillcolor(LIGHTGRAY);
fillrectangle(i * size, j * size, (i + 1) * size, (j + 1) * size);
}
}
setfillcolor(LIGHTMAGENTA);
fillrectangle(foodx * size, foody * size, (foodx + 1) * size, (foody + 1) * size);
if (isover)
{
Remembermaxscore();
char a[100];
sprintf_s(a, "最后得分:%d", result);
settextcolor(RED);
settextstyle(40, 0, "宋体");
setbkmode(TRANSPARENT);
outtextxy(200, 150, "游戏结束");
outtextxy(200, 200, a);
}
FlushBatchDraw();
}
void operate()
{
if (GetAsyncKeyState('W'))
{
if (direction == 's')
direction = 's';
else direction = 'w';
}
if (GetAsyncKeyState('S'))
{
if (direction == 'w')
direction = 'w';
else direction = 's';
}
if (GetAsyncKeyState('A'))
{
if (direction == 'd')
direction = 'd';
else direction = 'a';
}
if (GetAsyncKeyState('D'))
{
if (direction == 'a')
direction = 'a';
else direction = 'd';
}
}
void movespeed()
{
if (isover) return;
static int wait = 1;
wait++;
if (wait == speed)
{
move();
wait = 1;
}
}
void move()
{
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++)
if (snack[i][j] != 0) snack[i][j]++;
int oldHeadX, oldHeadY, oldTailX, oldTailY;
int tailsnacks = 0;
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
if (tailsnacks < snack[i][j]) {
tailsnacks = snack[i][j];
oldTailX = i;
oldTailY = j;
}
if (snack[i][j] == 2) {
oldHeadX = i;
oldHeadY = j;
}
}
}
int newHeadX = oldHeadX, newHeadY = oldHeadY;
switch (direction) {
case'a':
newHeadX -= 1;
break;
case 's':
newHeadY += 1;
break;
case 'd':
newHeadX += 1;
break;
case 'w':
newHeadY -= 1;
break;
}
if (newHeadX >= width || newHeadX < 0 || newHeadY >= height || newHeadY < 0 || snack[newHeadX][newHeadY] != 0) {
isover = 1;//判断是否失败
return;
}
snack[newHeadX][newHeadY] = 1;
if (newHeadX == foodx && newHeadY == foody) {
result++;//重新刷新食物
foodx = rand() % (width - 2) + 1;
foody = rand() % (height - 2) + 1;
}
else
snack[oldTailX][oldTailY] = 0;
}
音乐和背景图大家可以自定义添加