文末会将源代码公布,仅供大家一起学习,感谢网络上的各种学习资源及B站UP主们,有了你们的分享,我们才能更好更快地学习进步!!!
效果如下图所展示:
/*
贪吃蛇
snake.cpp
涉及内容:结构体、控制语句、循环、+easyx图形库
*/
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<mmsystem.h> //播放音乐
#pragma comment(lib,"winmm.lib") //播放音乐
#define WIN_WIDTH 640
#define WIN_HEIGHT 480
#define MAX_SNAKE 100 //蛇的最大节数
enum DIR //蛇的方向
{
UP,
DOWN,
LEFT,
RIGHT,
};
struct Snake_tlg //蛇的结构体
{
int num;
int dir;//蛇的方向
int size;//蛇的宽度和高度
int score;
POINT coor[MAX_SNAKE];
/*
typedef struct tagPOINT
{
LONG x;
LONG y;
}POINT;
*/
}snake;
struct Food_tlg
{
POINT fd;
int flag;
DWORD color;
}food;
void GameInit()
{
srand(GetTickCount());
//初始化蛇
snake.num = 3;
snake.dir = RIGHT;
snake.score = 0;
snake.size = 10;
snake.coor[2].x = 0;
snake.coor[2].y = 0;
snake.coor[1].x = snake.size;
snake.coor[1].y = 0;
snake.coor[0].x = 2 * snake.size;
snake.coor[0].y = 0;
//初始化食物
food.fd.x = rand() % (WIN_WIDTH/10)*10;
food.fd.y = rand() % (WIN_HEIGHT/10)*10;
food.flag = 1;
food.color = RGB(rand() % 256, rand() % 256, rand() % 256);
mciSendString("open BANG.mp3 alias a", 0, 0, 0); //播放音乐
mciSendString("play a repeat", 0, 0, 0); //播放音乐
}
void GameDraw()
{
//设置背景颜色
setbkcolor(RGB(105,160,141));
cleardevice();
//画蛇
for (int i = 0; i < snake.num; i++)
{
setlinecolor(BLACK);
setfillcolor(YELLOW);
fillrectangle(snake.coor[i].x, snake.coor[i].y, snake.coor[i].x+snake.size, snake.coor[i].y+snake.size);
printf("%d%d\n", snake.coor[i].x, snake.coor[i].x);
}
//画食物
if (food.flag == 1)
{
setfillcolor(food.color);
fillellipse(food.fd.x, food.fd.y, food.fd.x + 10, food.fd.y + 10);
}
//显示分数
char temp[20] = "";
sprintf(temp, "分数: %d", snake.score);
setbkmode(TRANSPARENT);
outtextxy(20, 20, temp);
}
void SnakeMove()
{
for (int i = snake.num - 1; i > 0; i--) //从最后一节开始,每一节蛇都等于前一节蛇的上一次坐标
{
snake.coor[i].x = snake.coor[i - 1].x;
snake.coor[i].y = snake.coor[i - 1].y;
}
switch (snake.dir)
{
case UP:
snake.coor[0].y -= 10;
if (snake.coor[0].y + 10 <= 0)
{
snake.coor[0].y = WIN_HEIGHT;
}
break;
case DOWN:
snake.coor[0].y += 10;
if (snake.coor[0].y - 10 >= WIN_HEIGHT)
{
snake.coor[0].y = 0;
}
break;
case LEFT:
snake.coor[0].x -= 10;
if (snake.coor[0].x + 10 <= 0)
{
snake.coor[0].x = WIN_WIDTH;
}
break;
case RIGHT:
snake.coor[0].x += 10;
if (snake.coor[0].x - 10 >= WIN_WIDTH)
{
snake.coor[0].x = 0;
}
break;
default:
break;
}
}
void KeyControl()
{
//使用win32API获取键盘信息
if (GetAsyncKeyState(VK_UP)&& snake.dir!=DOWN)
{
snake.dir = UP;
}
if (GetAsyncKeyState(VK_DOWN) && snake.dir != UP)
{
snake.dir = DOWN;
}
if (GetAsyncKeyState(VK_LEFT) && snake.dir != RIGHT)
{
snake.dir = LEFT;
}
if (GetAsyncKeyState(VK_RIGHT) && snake.dir != LEFT)
{
snake.dir = RIGHT;
}
}
void EatFood()
{
if (snake.coor[0].x == food.fd.x&&snake.coor[0].y == food.fd.y&&food.flag ==1)
{
snake.num++;
snake.score += 10;
food.flag = 0;
}
if (food.flag == 0)
{
food.fd.x = rand() % (WIN_WIDTH / 10) * 10;
food.fd.y = rand() % (WIN_HEIGHT / 10) * 10;
food.flag = 1;
food.color = RGB(rand() % 256, rand() % 256, rand() % 256);
}
}
void DontEatSelf()
{
for (int i = 4; i < snake.num; i++)
{
if (snake.coor[0].x == snake.coor[i].x&&snake.coor[0].y == snake.coor[i].y)
{
outtextxy(200, 200, "Game Over!");
_getch();
exit(666);
}
}
}
int main()
{
initgraph(WIN_WIDTH, WIN_HEIGHT, SHOWCONSOLE);//初始化一个图形窗口
GameInit();
DWORD t1, t2;
t1 = t2 = GetTickCount();
BeginBatchDraw();
while (1)
{
if (t2 - t1 > 100)
{
SnakeMove();
t1 = t2;
}
t2 = GetTickCount();
GameDraw();
EatFood();
DontEatSelf();
FlushBatchDraw();
KeyControl();
}
getchar();
closegraph();
return 0;
}