C语言实战 - 贪吃蛇(图形界面)

时间:2025-04-15 14:33:45
  • 由于本人精力有限,暂时先把素材和代码放上,等以后有空再补教程。

目录

效果预览

准备工作

EasyX图形库

音频素材

代码编写

文件

文件

文件


效果预览

先来看一下最终成品效果

贪吃蛇图形界面

准备工作

EasyX图形库

这个贪吃蛇项目是基于EasyX图形库写的,所以需要安装一个easy图像库,官方下载地址如下:

EasyX Graphics Library for C++/

音频素材

****资源下载地址:贪吃蛇素材文件(图片+音频)-C文档类资源-****文库

如果遇到非会员下载不了的情况可以去百度网盘下载

PS:设置的永久有效,如果失效还请联系作者

链接:/s/105A1ONEPL8KgvNuZFd9EcA?pwd=wx93 
提取码:wx93 
--来自百度网盘

代码编写

  • 文件


//头文件
#pragma once
#include<>
#include<>
#include<>
#include<>
#include<>
#include<>
#include<>
#include<>//包含多媒体设备接口头文件
#pragma comment(lib,"")//加载静态库

//类型定义
typedef struct snake_coor {//蛇的坐标
	int x;
	int y;
}COOR;
typedef enum dir {//蛇的方向
	up, down, left, right,stop
}DIR;
typedef  struct snake_information {//蛇的信息
	COOR coor[1000]={0};
	int length=0;
	int state=0;
	DIR direction;
}INF;
typedef struct food_coor {//食物坐标
	int x=0;
	int y=0;
}FOOD;

//函数声明
void button(int x, int y, int len, int wid, 
const char* text,int chg1,int chg2);				//按钮函数
int menu();											//菜单界面
void background();									//游戏背景
void snake_Initialization();						//初始化蛇
void snkae_key();									//按键控制
void snake_move();									//蛇的移动
void snake_draw();									//蛇的绘制
void food_draw();									//画出食物
void food_coor();									//食物坐标
void judge();										//判断函数
void game();										//游戏函数
  • 文件

#include""
INF snake;				//蛇的定义
FOOD food;				//定义食物

void button(int x,int y,int len,int wid,const char* text,int chg1,int chg2)
{
	//图形部分
	setlinecolor(TRANSPARENT);
	setfillcolor(YELLOW);
	setlinecolor(BROWN);
	fillroundrect(x, y, x + len, y + wid,20,20);

	//文字部分
	char text_trans[50];			
	strcpy_s(text_trans, text);
	setbkmode(TRANSPARENT);
	settextstyle(30, 0, "华文行楷");
	settextcolor(BLACK);
	int x_end = x+chg1;
	int y_end = y+chg2;
	outtextxy(x_end,y_end, text_trans);
}
int menu()//菜单
{
	//播放背景音乐
	mciSendString("play ./menu_bgm.mp3 repeat", 0, 0, 0);			
	cleardevice();
	//右边区域
	HRGN words_area = CreateRectRgn(680, 0, 900, 580);
	setcliprgn(words_area);
	IMAGE pic_left;
	loadimage(&pic_left, "./background_other.jpg", 0, 0);
	putimage(680, 0, &pic_left);
	RECT words{ 700, 150, 850, 400 };
	DeleteObject(words_area);
	//左边区域
	HRGN menu_area = CreateRectRgn(0, 0, 680, 580);
	setcliprgn(menu_area);
	IMAGE background_menu;
	loadimage(&background_menu, "./background_menu.jpg", 0, 0);
	putimage(0, 0, &background_menu);	
	button(260, 80, 160, 50, "开始游戏",25,10);
	button(260, 200, 160, 50, "退出游戏", 25, 10);
	//鼠标选择
	MOUSEMSG click;
	while (1)
	{
		if (MouseHit())
		{
			click = GetMouseMsg();										//获取鼠标信息
			if ( == WM_LBUTTONDOWN)							//按下左键时
			{
			//第一个按钮
			if ( >= 260 &&  <= 420 &&  >= 80 &&  <= 130)		
				{
				mciSendString("stop ./menu_bgm.mp3",0, 0, 0);			//停止菜单背景音乐
				mciSendString("play click_sound.mp3", 0, 0, 0);			//点击音效
				mciSendString("play ./game_bgm.mp3 repeat", 0, 0, 0);	//播放游戏背景音乐
				return 1;
				}
			//第二个按钮
			if ( >= 260 &&  <= 420 &&  >= 200 &&  <= 250)		
				{
				mciSendString("stop ./menu_bgm.mp3", 0, 0, 0);			//停止菜单背景音乐
				mciSendString("play click_sound.mp3", 0, 0, 0);			//点击音效				
				return 0;
				}
			}
		}
	}
	return 0;
}
void background()
{
	cleardevice();
	//右边图片
	HRGN words_area = CreateRectRgn(680, 0, 900, 580);
	setcliprgn(words_area);
	IMAGE pic_left;
	loadimage(&pic_left, "./background_right.jpg", 0, 0);
	putimage(680, 0, &pic_left);
	//右边文字
	settextstyle(18, 0, "宋体");
	settextcolor(BLACK);
	setbkmode(TRANSPARENT);
	outtextxy(690, 500, "退出:Esc    暂停:Space");
	//得分区域
	settextstyle(40, 0, "微软雅黑"); 
	settextcolor(BLUE);
	setbkmode(TRANSPARENT); 
	char grade[50]; 
	sprintf_s(grade ,"%d" , - 3);
	outtextxy(790, 344, grade); 
	//释放空间
	DeleteObject(words_area); 
	//游戏区域
	HRGN game_area = CreateRectRgn(0, 0, 680, 580);
	setcliprgn(game_area);
	IMAGE background_menu;
	loadimage(&background_menu, "./background_left.jpg", 0, 0);
	putimage(0, 0, &background_menu);
}
void snake_Initialization()
{
	//初始化长度
	 = 3;
	//初始化蛇头
	[0].x = 320;
	[0].y = 250;
	//初始化蛇身
	[1].x = 310;
	[1].y = 250;
	//初始化蛇尾
	[2].x = 300;
	[2].y = 250;
	//初始化食物坐标
	 = (rand() % 69) * 10;
	 = (rand() % 56) * 10;
	//初始化方向
	 = right;
}
void snake_draw()
{
	int i = 0, j = 0;
	for (i = 0; i < ; i++)
	{
			//线条颜色
			setlinecolor(YELLOW);
			//蛇身颜色
			if (i!=0&&i!=-1)
			{
				setfillcolor(RGB(51,255,88));			
				fillrectangle([i].x, [i].y, [i].x + 10, [i].y + 10);
			}
			//蛇头颜色
			if (i == 0)
			{
				setfillcolor(RGB(31,100,255));				
				fillrectangle([i].x, [i].y, [i].x + 10, [i].y + 10);
			}
			//蛇尾颜色
			if (i ==  - 1)
			{
				setfillcolor(RGB(159,255,213)); 			
				fillrectangle([i].x, [i].y, [i].x + 10, [i].y + 10);
			}				
	}
}
void snkae_key()
{
	char get;
	//获取键盘内容
	printf("2");
	if (_kbhit())
	{
		printf("3");
		get = _getch();
		printf("1");
		if ( (get == 'W' || get =='w') &&  != down)
		{
			 = up;			//上 --- W
		}
		if ( (get == 'A' || get == 'a') &&  != right)
		{
			 = left;			//左 --- A 
		}
		if ((get == 'S' || get == 's') &&  != up)
		{
			 = down;			//下 --- S
		}
		if ((get == 'D' || get == 'd') &&  != left)
		{
			 = right;		//右 --- D
		}
		if (get == ' ')						
		{
			system("pause");			    //暂停 - Space
		}
		if (get == 27)
		{
			exit(0);						//退出 - Esc
		}
		if (get == 72 &&  != down)
		{
			 = up;			//上					
		}
		if (get == 80 &&  != up)
		{
			 = down;			//下					
		}
		if (get == 75 &&  != right)
		{
			 = left;			//左					
		}
		if (get == 77 &&  != left)
		{
			 = right;		//右					
		}

	}
}
void snake_move()
{
	//蛇身坐标前移
	for (int i =  - 2; i >= 0; i--)
	{
		[i + 1].x = [i].x;
		[i + 1].y = [i].y;
	}
	//蛇头坐标移动
	if (==up)//上
	{
		[0].y -= 10;		
	}
	if ( == down)//下
	{
		[0].y += 10;
	}
	if ( == left)//左
	{
		[0].x -= 10;
	}
	if ( == right)//右
	{
		[0].x += 10;
	}	
	//画出新的蛇
	snake_draw();
}
void food_draw()
{
		setfillcolor(RGB(255,0,0));			
		solidrectangle(, ,  + 10,  + 10);
}
void food_coor()
{
		int x, y;
		x = ((rand() % 66) +1) * 10; 
		y = ((rand() % 56) +1) * 10; 
		//食物不能与蛇身重合
		for (int i = 0;( x==[i].x  && y == [i].y); i++)
		{
			x = ((rand() % 66) + 1) * 10;
			y = ((rand() % 56) + 1) * 10;
		}
		 = x;
		 = y;
}
void judge()
{
		HWND result= GetHWnd();
		int out;
		char grade[50];
		//判断吃否吃到食物
		if ([0].x == && [0].y == )
		{
			mciSendString("open ./eat_sound.mp3", 0, 0, 0);	//食物音效
			mciSendString("play eat_sound.mp3", 0, 0, 0);	//食物音效
			food_coor();			
			++;
		}
		//判断是否撞墙
		if ( == up && [0].y <=10 )
		{
			mciSendString("play death_sound.mp3", 0, 0, 0);			//播死亡音效
			mciSendString("stop ./game_bgm.mp3", 0, 0, 0);			//停止游戏背景音乐
			sprintf_s(grade, "分数:%d",  - 3);			
			out = MessageBox(result, grade, "游戏结束",MB_DEFBUTTON1);
			 = 1;
		}
		if ( == down && [0].y >=560  )
		{
			mciSendString("play death_sound.mp3", 0, 0, 0);			//播死亡音效 
			mciSendString("stop ./game_bgm.mp3", 0, 0, 0);			//停止游戏背景音乐
			sprintf_s(grade, "分数:%d",  - 3);
			out = MessageBox(result, grade, "游戏结束", MB_DEFBUTTON1);
			 = 1;
		}
		if ( == left && [0].x<=10  )
		{
			mciSendString("play death_sound.mp3", 0, 0, 0);			//播死亡音效
			mciSendString("stop ./game_bgm.mp3", 0, 0, 0);			//停止游戏背景音乐
			sprintf_s(grade, "分数:%d",  - 3);
			out = MessageBox(result, grade, "游戏结束", MB_DEFBUTTON1);
			 = 1;
		}
		if ( == right && [0].x>=660 )
		{
			mciSendString("play death_sound.mp3", 0, 0, 0);			//播死亡音效
			mciSendString("stop ./game_bgm.mp3", 0, 0, 0);			//停止游戏背景音乐
			sprintf_s(grade, "分数:%d",  - 3);
			out = MessageBox(result, grade, "游戏结束", MB_DEFBUTTON1);
			 = 1;
		}
		//判断是否撞到蛇身
		for (int i = 1; i < ; i++)
		{
		if ([0].x == [i].x && [0].y == [i].y)
			{
			mciSendString("play ./death_sound.mp3", 0, 0, 0);		//播死亡音效
			mciSendString("stop ./game_bgm.mp3", 0, 0, 0);			//停止游戏背景音乐	
			sprintf_s(grade, "分数:%d",  - 3);
			out = MessageBox(result, grade, "游戏结束", MB_DEFBUTTON1);
			 = 1;
			}
		}
}
void game()
{	
	int m, n;
	snake_Initialization();			//初始化蛇
	while ( != 1)		//进入循环
	{								//
	BeginBatchDraw();				//开始批量绘图
	background();					//游戏背景
	food_draw();					//画食物
	snake_move();					//蛇移动
	m = 180 - ( - 3);	//控制速度变化
	n = (m >= 10) ? (m) : (10);		//控制速度变化
	Sleep(n);						//休眠 - 控制速度
	EndBatchDraw();					//结束批量绘图
	snkae_key();					//按键检测
	judge();						//进行判断
	}								//

}
  • 文件

#include""

extern INF snake;	
int main()
{
	//打开音乐
	mciSendString("open ./click_sound.mp3", 0, 0, 0);
	mciSendString("open ./death_sound.mp3", 0, 0, 0);
	mciSendString("open ./eat_sound.mp3", 0, 0, 0);
	mciSendString("open ./menu_bgm.mp3", 0, 0, 0);
	mciSendString("open ./game_bgm.mp3", 0, 0, 0);
	//设置随机数
	srand((unsigned)time(NULL));
	//设置画面大小
	initgraph(900, 580, 0);
	int choice;
	while (1)
	{
		choice = menu();
		if ( choice == 1 )//进入游戏
		{	
			game();
			if ( == 1)
				 = 0;
		}
		if ( choice == 0 )//退出游戏
		{	
			exit(0);
		}
	}
	getchar();
	return 0;
}