easyx C语言贪吃蛇

easyx C语言贪吃蛇,第1张

本程序使用VS2022编写,预先安装好easyx图形库

tip:记得设置字符集使用多字节字符集

在b站上学习并自己加以修改

在网络上下载了一些声音素材

源码程序与素材下载

链接: https://pan.baidu.com/s/1YisBwq2iiWTq2p-J7fVzKw?pwd=wnsa 提取码: wnsa

#include 
#include 
#include 
#include 
#include 
#pragma comment(lib,"Winmm.lib")
#define SNAKE_NUM 500  
int score = 0;
struct Snake
{
	int size;
	int dir;
	int speed;
	POINT coor[SNAKE_NUM];
}snake;
struct Food
{
	int x;
	int y;
	int r;
	bool flag;
	DWORD color; 
}food;
enum Dir
{
	UP,
	DOWN,
	LEFT,
	RIGHT,
};
void GamaInit()
{	
	initgraph(640, 480);
	SetWindowText(GetHWnd(), "贪吃snake by 话说");
	mciSendString("play src/bgm.mp3 repeat", 0, 0, 0);
	snake.size = 3;
	snake.speed = 10;
	snake.dir=RIGHT;
	for (int i = 0; i < snake.size ; i++)
	{
    snake.coor[i].x = 40 - 10*i;
	snake.coor[i].y = 10;
	} 
	food.x = rand() % 640; 
	food.y = rand() % 480;
	food.color = RGB(rand()%256, rand() % 256, rand() % 256);
	food.r = rand() % 10 + 5;
	food.flag = true;
}
void GameDraw()
{
	//BeginBatchDraw();
	setbkcolor(RGB(30, 30, 30));
	cleardevice();
	setfillcolor(RGB(rand() % 256, rand() % 256, rand() % 256));
	for (int i = 0; i < snake.size; i++)
	{
		solidcircle(snake.coor[i].x, snake.coor[i].y, 5);
	}
	if (food.flag)
	{
		solidcircle(food.x, food.y, food.r);
	}
	//EndBatchDraw();
}
void snakeMove()
{
	for (int i = snake.size - 1; i > 0 ; i--)
	{
		snake.coor[i] = snake.coor[i - 1];
	}
	switch (snake.dir)
	{
	case UP: snake.coor[0].y -= snake.speed;
		if (snake.coor[0].y+10<=0)
		{
			snake.coor[0].y = 480;
		}
		break;
	case DOWN: snake.coor[0].y += snake.speed;
		if (snake.coor[0].y - 10 >= 480)
		{
			snake.coor[0].y = 0;
		}
		break;
	case LEFT: snake.coor[0].x -= snake.speed;
		if (snake.coor[0].x + 10 <= 0)
		{
			snake.coor[0].x = 640;
		}
		break;
	case RIGHT: snake.coor[0].x += snake.speed;
		if (snake.coor[0].x - 10 >= 640)
		{
			snake.coor[0].x = 0;
		}
		break;
	}
}
void keyContorl()
{
	if (_kbhit())
	{
		switch (_getch())
		{
		case 'w':
		case 'W':
		case 72:	if (snake.dir != DOWN)
		{
			snake.dir = UP;
		}
			break;
		case 's':
		case 'S':
		case 80:	if (snake.dir != UP)
		{
			snake.dir = DOWN;
		}
			break;
		case 'a':
		case 'A':
		case 75:	if (snake.dir != RIGHT)
		{
			snake.dir = LEFT;
		}
			break;
		case 'd':
		case 'D':
		case 77:	if (snake.dir != LEFT)
		{
			snake.dir = RIGHT;
		}
			break;
		case ' ':
			while (1)
			{
				if (_getch() == ' ')
					return;
			}
			break;
		case 't':
		case 'T': 
			snake.speed += 2;
			if (snake.speed >= 20)snake.speed = 20;
			   break;
		case 'y':
		case 'Y':  
			snake.speed -= 2;
			if (snake.speed <= 5)snake.speed = 5;
				break;
		}
	}
}
void Eatfood()
{
	if (food.flag&&snake.coor[0].x>=food.x-food.r&& snake.coor[0].x <= food.x + food.r&& snake.coor[0].y >= food.y - food.r&& snake.coor[0].y <= food.y + food.r)
	{
		mciSendString("play src/eat.mp3", 0, 0, 0);
		food.flag = false;
		snake.size++; score++;
	}
	if (!food.flag)
	{
		food.x = rand() % 640;
		food.y = rand() % 480;
		food.color = RGB(rand() % 256, rand() % 256, rand() % 256);
		food.r = rand() % 10 + 5;
		food.flag = true;
	}
}
void score_write(int score)
{
	TCHAR s[30];
	TCHAR sp[30];
	_stprintf_s(s, _T("%d"), score);//需要先把数字进行转换
	_stprintf_s(sp, _T("%d"),snake.speed);
	//BeginBatchDraw();
	setbkmode(TRANSPARENT);
	settextcolor(RGB(255, 255, 255));
	settextstyle(30, 0, "楷体");
	outtextxy(5, 450, "你的分数:");
	outtextxy(150, 450, s);
	outtextxy(180, 450, "速度:");
	outtextxy(250, 450, sp);
	//EndBatchDraw();
}
void Gameover()
{
	for (int i = 1; i < snake.size; i++)
	{
		if (snake.coor[0].x > snake.coor[i].x - 4 && snake.coor[0].x < snake.coor[i].x + 4  && snake.coor[0].y > snake.coor[i].y - 4 && snake.coor[0].y < snake.coor[i].y + 4)
		{
			mciSendString("close src/bgm.mp3", 0, 0, 0);
			mciSendString("play src/dead.mp3", 0, 0, 0);
			while (1)
			{	if(score<=15)
				outtextxy(200, 240, "牛马 按空格继续");
				else if(score<30)
				outtextxy(200, 240, "666 按空格继续");
				else
				outtextxy(200, 240, "驭蛇大师 按空格继续");
				if (_getch() == ' ')
				{
					snake.size = 3;
					score = 0;
					GamaInit();
					return; 
				}
					
			}
		}
	}
}
int main()
{
	GamaInit();
	while (1)
	{	
		snakeMove();
		BeginBatchDraw();
		GameDraw();
		score_write(score);
		EndBatchDraw();
		keyContorl();
		Eatfood();
		Gameover();
		Sleep(50);
	}
	return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/674311.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-19
下一篇 2022-04-19

发表评论

登录后才能评论

评论列表(0条)

保存