您现在的位置是:首页 >学无止境 >C语言实战 - 贪吃蛇(图形界面)网站首页学无止境

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

小白麋鹿 2024-06-17 10:19:19
简介C语言实战 - 贪吃蛇(图形界面)
  • 由于本人精力有限,暂时先把素材和代码放上,等以后有空再补教程。

目录

效果预览

准备工作

EasyX图形库

音频素材

代码编写

Transfer.h文件

game.cpp文件

main.c文件


效果预览

先来看一下最终成品效果

贪吃蛇图形界面

准备工作

EasyX图形库

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

EasyX Graphics Library for C++https://easyx.cn/

音频素材

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

PS:如果遇到非会员下载不了的情况可以私信或者在评论区留言,作者再想办法。

代码编写

  • Transfer.h文件


//头文件
#pragma once
#include<stdio.h>
#include<graphics.h>
#include<easyx.h>
#include<windows.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
#include<mmsystem.h>//包含多媒体设备接口头文件
#pragma comment(lib,"winmm.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();										//游戏函数
  • game.cpp文件

#include"Transfer.h"
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 (click.uMsg == WM_LBUTTONDOWN)							//按下左键时
			{
			//第一个按钮
			if (click.x >= 260 && click.x <= 420 && click.y >= 80 && click.y <= 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 (click.x >= 260 && click.x <= 420 && click.y >= 200 && click.y <= 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" ,snake.length - 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()
{
	//初始化长度
	snake.length = 3;
	//初始化蛇头
	snake.coor[0].x = 320;
	snake.coor[0].y = 250;
	//初始化蛇身
	snake.coor[1].x = 310;
	snake.coor[1].y = 250;
	//初始化蛇尾
	snake.coor[2].x = 300;
	snake.coor[2].y = 250;
	//初始化食物坐标
	food.x = (rand() % 69) * 10;
	food.y = (rand() % 56) * 10;
	//初始化方向
	snake.direction = right;
}
void snake_draw()
{
	int i = 0, j = 0;
	for (i = 0; i < snake.length; i++)
	{
			//线条颜色
			setlinecolor(YELLOW);
			//蛇身颜色
			if (i!=0&&i!=snake.length-1)
			{
				setfillcolor(RGB(51,255,88));			
				fillrectangle(snake.coor[i].x, snake.coor[i].y, snake.coor[i].x + 10, snake.coor[i].y + 10);
			}
			//蛇头颜色
			if (i == 0)
			{
				setfillcolor(RGB(31,100,255));				
				fillrectangle(snake.coor[i].x, snake.coor[i].y, snake.coor[i].x + 10, snake.coor[i].y + 10);
			}
			//蛇尾颜色
			if (i == snake.length - 1)
			{
				setfillcolor(RGB(159,255,213)); 			
				fillrectangle(snake.coor[i].x, snake.coor[i].y, snake.coor[i].x + 10, snake.coor[i].y + 10);
			}				
	}
}
void snkae_key()
{
	char get;
	//获取键盘内容
	printf("2");
	if (_kbhit())
	{
		printf("3");
		get = _getch();
		printf("1");
		if ( (get == 'W' || get =='w') && snake.direction != down)
		{
			snake.direction = up;			//上 --- W
		}
		if ( (get == 'A' || get == 'a') && snake.direction != right)
		{
			snake.direction = left;			//左 --- A 
		}
		if ((get == 'S' || get == 's') && snake.direction != up)
		{
			snake.direction = down;			//下 --- S
		}
		if ((get == 'D' || get == 'd') && snake.direction != left)
		{
			snake.direction = right;		//右 --- D
		}
		if (get == ' ')						
		{
			system("pause");			    //暂停 - Space
		}
		if (get == 27)
		{
			exit(0);						//退出 - Esc
		}
		if (get == 72 && snake.direction != down)
		{
			snake.direction = up;			//上					
		}
		if (get == 80 && snake.direction != up)
		{
			snake.direction = down;			//下					
		}
		if (get == 75 && snake.direction != right)
		{
			snake.direction = left;			//左					
		}
		if (get == 77 && snake.direction != left)
		{
			snake.direction = right;		//右					
		}

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

}
  • main.c文件

#include"Transfer.h"

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 (snake.state == 1)
				snake.state = 0;
		}
		if ( choice == 0 )//退出游戏
		{	
			exit(0);
		}
	}
	getchar();
	return 0;
}

风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。