C++实训easyx打地鼠(动画版)

2 篇文章 0 订阅


# https://github.com/XYEXY/LYELYE#配置文件夹与Players.txt文本文件 点此下载

如果没有接触过easyx 可以去看一下我的另外一个关于2048的文章,里面开头有教程,教你怎么操作。

此次打地鼠的主要功能有动画实现地鼠的移动、登录注册系统、游戏的排名系统、鼠标光标的转变。

游戏界面鼠标光标是一个锤子,其他界面鼠标光标是一个可爱的老鼠。

主界面
在这里插入图片描述
游戏界面
在这里插入图片描述

登录和注册界面
在这里插入图片描述
排名系统
在这里插入图片描述

运行前需要在当前文件夹内创建一个Players.txt的文本文档用于存储用户信息,否则将运行失败
需要的图片资源有
在这里插入图片描述

感谢 ouuuuu欧图 的帮助

1,针对运行时报错:需更改项目属性中的两个内容。
首先是【高级】里的【字符集】改成【使用多字节字符集】。其次是【c/c++】里【常规】中的【SDL检查】更改为【否】。
2,如果项目此时报错内容为c2065,提示“i”未声明,那么在报错的for中添加定义“i”。再次运行应该就没问题啦。
3,提示:配置文件和Players.txt应该放置在与.exe同目录下。

上代码:(运行前需要下载easyx库 https://easyx.cn/ )

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <string.h>
#include <graphics.h>
#pragma comment(lib,"Winmm.lib")
#include<vector>
#include<fstream>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<Windows.h>
using namespace std;
//窗口宽
#define WINDOW_WIDTH  549
//窗口高
#define WINDOW_HEITH  836
//背景图TOP高
#define TOP_WIDTH  496
//背景图mid_up高
#define MID_UP_HEITH  126
//背景图mid_down高
#define MID_DOWN_HEITH  126
//背景图bottom高
#define BOTTOM_HEITH  88
//背景图mid_up起始y坐标
#define MID_UP_HEITH  495
//背景图bottom_up起始y坐标
#define MID_DOWN_HEITH  622
//背景图bottom_up起始y坐标
#define BOTTOM_HEITH  748
//地鼠的宽
#define DISHU_WIDTH  140
//地鼠的高
#define DISHU_HEITH  170
//地鼠的横距
#define DISHU_SPACE  185
//第一排地鼠y坐标
#define DISHU_SPACE_1  475
//第二排地鼠y坐标
#define DISHU_SPACE_2  605
//第三排地鼠y坐标
#define DISHU_SPACE_3  735
//每步移动的像素点难度1
#define STEP 10
#define SIZE 1000//最大用户容量
#define WIDTH 549
#define HEIGHT 836
/*
	1.做窗口。
	2.背景图。
	3.做地鼠。
	4.地鼠自己动。
	5.用鼠标控制锤子。
	6.击打。
	7.计分(速度调整)。
*/
void initGame();//初始化游戏
void showGame();//显示游戏界面
void dsMove();//地鼠移动
void start();//游戏开始
void lent();//接口
void startnandu();//选择排行榜
void zhujiemian();//主界面
void paihangbang();//排行榜
bool cmp(int a, int b);

int isId = 1, isPassWord = 1, diji = 0;
int scount = 1;//用作储存当前已注册用户数
int TIME_SPACE = 0;//所有玩家信息操作
static int music_hit = 0;
IMAGE img1, img2, img3, img4;
IMAGE top, mid_up, mid_down, bottom;//彩色黑色
IMAGE top_y, mid_up_y, mid_down_y, bottom_y;//黑白
IMAGE mouse, mouse_y, mouse_x;
IMAGE nandubeijintu;
HCURSOR hcur,hcur1,hcur2;
MOUSEMSG msg;

class Dishu
{
	int x;
	int y;
	int ymax;
	int ymin;
	bool fx;//方向
	bool behit;//被打中
	bool zuigao;//达到最高点
	IMAGE img, img_y, img_x;
public:
	void Draw()
	{
		putimage(x, y, &img_y, SRCAND);//贴掩码图
		if (behit == false)
			putimage(x, y, &img, SRCPAINT);//贴原图
		else
			putimage(x, y, &img_x, SRCPAINT);//贴原图被打的图像
	}
	void moveUP() {
		y -= STEP;
	}
	void moveDOWN() {
		y += STEP;
	}
	friend class Game;
	friend void start();
	friend void initGame();
	friend void dsMove();
}dis;
class Game
{
	int score;
	char str[10];
	//被选中地鼠的下标
	int m;
	//连击
	char strlian[10], strlife[10];
	int lianji;
	//计时器
	time_t startt, end;
	int life;
	int nandu;
	int pai;
public:
	void zhujiemian();
	void showGame();
	void paihangbang();
	void painandu();
	void startnandu();	
	friend void initGame();
	friend void start();
	friend class Dishu;
	friend void dsMove();
};
class Players
{
public:
	Players() {};
	void Registers();//注册
	void Login();//登录
	void save();//保存
	void read();//读取
	void print(int cpp);
	void SetId(int cpp);
	void SetPassWord(int cpp);
	int Score1;   //该玩家的最高分
	int Score2;
	int Score3;
private:
	string Id;//电话
	string PassWord;//密码
	friend class Game;
	friend class Dishu;
}us;

Dishu ds[9];//地鼠初始化
Game game;//游戏初始化数据
Players user[SIZE];//初始化用户

void Players::print(int cpp = scount)
{
	cleardevice();
	putimage(0, 0, &nandubeijintu);
	string str1, str2;
	setbkcolor(WHITE);
	setbkmode(TRANSPARENT);
	settextstyle(40, 0, "黑体");

	outtextxy(100, 220, " Id :");
	if (isId == 0)
		str1 = user[cpp].Id + "|";
	else
		str1 = user[cpp].Id;
	setbkcolor(RGB(241, 122, 105));
	solidroundrect(200, 215, 400, 263, 15, 15);
	outtextxy(200, 220, str1.c_str());  //在窗口坐标为(10,220)的地方开始输出str2的内容
	setbkcolor(WHITE);

	outtextxy(20, 285, "PassWord:");
	if (isPassWord == 0)
		str2 = user[cpp].PassWord + "|";
	else
		str2 = user[cpp].PassWord;
	setbkcolor(RGB(241, 122, 105));
	solidroundrect(200, 280, 400, 328, 15, 15);
	outtextxy(200, 285, str2.c_str());  //在窗口坐标为(10,220)的地方开始输出str2的内容
	setbkcolor(WHITE);
}
void Players::SetId(int cpp = scount)
{
here:
	user[cpp].Id = "";
	user[cpp].PassWord = "";
	if (cpp == 0)
		us.print(0);
	else
		us.print();
	isId = 0;
	string str1;
	setbkcolor(RGB(241, 122, 105));
	str1 = user[cpp].Id + "|";

	outtextxy(200, 220, str1.c_str());  //在窗口坐标为(10,220)的地方开始输出str2的内容
	setbkcolor(WHITE);

	char c;  //定义字符c接收键盘输入
	while (1)
	{
		if (kbhit())
		{
			c = _getch();
			if (c == '\r')
				break;
			else
			{
				if (c == '\b')  //如果c是退格符,str1删掉一个
				{
					user[cpp].Id = user[cpp].Id.substr(0, user[cpp].Id.size() - 1);
				}
				else
				{
					user[cpp].Id += (int)c;
				}

				cleardevice();
				if (user[cpp].Id.size() > 9)
				{
					putimage(0, 0, &nandubeijintu);
					outtextxy(50, 325, "Id过长,请重新输入!");
					Sleep(2000);
					goto here;

				}
				if (cpp == 0)
					us.print(0);
				else
					us.print();

			}
		}
		if (MouseHit())
		{
			msg = GetMouseMsg();
			switch (msg.uMsg)
			{
			case WM_RBUTTONDOWN:
				isId = 1;
				lent();
			}
		}
	}
	isId = 1;
	setbkcolor(RGB(241, 122, 105));
	str1 = user[cpp].Id;

	solidroundrect(200, 215, 400, 263, 15, 15);
	outtextxy(200, 220, str1.c_str());  //在窗口坐标为(10,220)的地方开始输出str2的内容
	setbkcolor(WHITE);
}
void Players::SetPassWord(int cpp = scount)
{
	if (cpp == 0)
		us.print(0);
	else
		us.print();
	isPassWord = 0;
	string str2;
	setbkcolor(RGB(241, 122, 105));
	str2 = user[cpp].PassWord + "|";

	outtextxy(200, 285, str2.c_str());  //在窗口坐标为(10,220)的地方开始输出str2的内容
	setbkcolor(WHITE);

	char c;  //定义字符c接收键盘输入
	while (1)
	{
		if (kbhit())
		{
			c = _getch();
			if (c == '\r')
				break;
			else
			{
				if (c == '\b')  //如果c是退格符,str1删掉一个
				{
					user[cpp].PassWord = user[cpp].PassWord.substr(0, user[cpp].PassWord.size() - 1);
				}
				else
				{
					user[cpp].PassWord += (int)c;
				}
				cleardevice();
				if (cpp == 0)
					us.print(0);
				else
					us.print();
			}
		}
		if (MouseHit())
		{
			msg = GetMouseMsg();
			switch (msg.uMsg)
			{
			case WM_RBUTTONDOWN:
				isPassWord = 1;
				lent();
			}
		}
	}
	isPassWord = 1;
	setbkcolor(RGB(241, 122, 105));
	str2 = user[cpp].PassWord;

	solidroundrect(200, 280, 400, 328, 15, 15);
	outtextxy(200, 285, str2.c_str());  //在窗口坐标为(10,220)的地方开始输出str2的内容
	setbkcolor(WHITE);
}
void Players::save()
{
	ofstream ofile;
	ofile.open("Players.txt", ios::out);

	for (int i = 1; i < scount; i++)
	{
		ofile << user[i].Id << " " << user[i].PassWord << " " << user[i].Score1 << " " << user[i].Score2 << " " << user[i].Score3 << endl;
	}
	ofile.close();
}
void Players::read()
{
	ifstream ifile;
	ifile.open("Players.txt", ios::in);

	scount = 1;

	if (!ifile.is_open())
	{
		setbkmode(TRANSPARENT);
		outtextxy(20, 285, "文件打开失败!");
		_getch();
		return;
	}

	for (int i = 1; !ifile.eof(); i++)
	{
		ifile >> user[i].Id >> user[i].PassWord >> user[i].Score1 >> user[i].Score2 >> user[i].Score3;
		scount++;
	}
	scount--;
	ifile.close();
}//读取
void Players::Registers()
{
	putimage(0, 0, &nandubeijintu);
	string ph;
	string pw1;
here:
	us.SetId();
	//判断新输入的用户信息是否已存在(如果已存在则重新输入)
	for (int j = 0; j < scount; j++)
	{
		if (user[scount].Id == user[j].Id)
		{
			cleardevice();
			putimage(0, 0, &nandubeijintu);
			setbkmode(TRANSPARENT);
			outtextxy(20, 285, "【系统提示】用户已存在!");
			Sleep(1000);
			goto here;
		}
	}
	us.SetPassWord();
	user[scount].Score1 = 0;
	user[scount].Score2 = 0;
	user[scount].Score3 = 0;

	scount++;//已注册用户加1
	us.save();//保存用户数据
	cleardevice();
	putimage(0, 0, &nandubeijintu);
	setbkmode(TRANSPARENT);
	outtextxy(20, 285, "【系统提示】注册成功!");
	Sleep(1000);
	lent();
}//注册
void Players::Login()
{
	putimage(0, 0, &nandubeijintu);
	int time = 0;//统计比较次数
here:
	int x = 0;
	SetId(x);
	SetPassWord(x);
	for (int i = 1; i < scount; i++)
	{
		if (user[0].Id == user[i].Id && user[0].PassWord == user[i].PassWord)
		{
			time++;
			diji = i;
			cleardevice();
			putimage(0, 0, &nandubeijintu);
			setbkmode(TRANSPARENT);
			outtextxy(20, 285, "【系统提示】登录成功!");
			Sleep(1000);
			lent();
		}
	}
	if (time == 0)
	{
		cleardevice();
		putimage(0, 0, &nandubeijintu);
		setbkmode(TRANSPARENT);
		outtextxy(20, 285, "【系统提示】Id或PassWord错误!");
		Sleep(1000);
		goto here;
	}

}//登录
void Game::startnandu()
{
	MOUSEMSG m;
	putimage(0, 0, &nandubeijintu);
	setbkcolor(RGB(251, 248, 241));
	setfillcolor(RGB(251, 248, 241));
	solidroundrect(180, 300, 350, 380, 15, 15);
	solidroundrect(180, 400, 350, 480, 15, 15);
	solidroundrect(180, 500, 350, 580, 15, 15);
	setfont(65, 0, "Microsoft Yahei UI Bold");
	settextcolor(RGB(255, 116, 101));
	outtextxy(208, 304, "简单");
	settextcolor(RGB(255, 116, 101));
	outtextxy(208, 404, "困难");
	settextcolor(RGB(255, 116, 101));
	outtextxy(208, 504, "地狱");
	while (1)
	{
		EndBatchDraw();//结束批量绘图
		BeginBatchDraw();//批量绘图
		m = GetMouseMsg();
		if (m.uMsg == WM_LBUTTONDOWN)
		{
			EndBatchDraw();//结束批量绘图
			if (m.x > 180 && m.x < 350 && m.y>300 && m.y < 380)
			{
				game.nandu = 1;
				start();
			}
			else if (m.x > 180 && m.x < 350 && m.y>400 && m.y < 480)
			{
				game.nandu = 2;
				start();
			}
			else if (m.x > 180 && m.x < 350 && m.y>500 && m.y < 580)
			{
				game.nandu = 3;
				start();
			}

		}
		if (m.uMsg == WM_RBUTTONDOWN)
		{
			lent();
		}
	}
}
void Game::painandu()
{
	MOUSEMSG m;
	putimage(0, 0, &nandubeijintu);
	setbkcolor(RGB(251, 248, 241));
	setfillcolor(RGB(251, 248, 241));
	solidroundrect(180, 300, 350, 380, 15, 15);
	solidroundrect(180, 400, 350, 480, 15, 15);
	solidroundrect(180, 500, 350, 580, 15, 15);
	setfont(65, 0, "Microsoft Yahei UI Bold");
	settextcolor(RGB(255, 116, 101));
	outtextxy(208, 304, "简单");
	settextcolor(RGB(255, 116, 101));
	outtextxy(208, 404, "困难");
	settextcolor(RGB(255, 116, 101));
	outtextxy(208, 504, "地狱");
	while (1)
	{
		EndBatchDraw();//结束批量绘图
		BeginBatchDraw();//批量绘图
		m = GetMouseMsg();
		if (m.uMsg == WM_LBUTTONDOWN)
		{
			EndBatchDraw();//结束批量绘图
			if (m.x > 180 && m.x < 350 && m.y>300 && m.y < 380)
			{
				game.pai = 1;
				paihangbang();
			}
			else if (m.x > 180 && m.x < 350 && m.y>400 && m.y < 480)
			{
				game.pai = 2;
				paihangbang();
			}
			else if (m.x > 180 && m.x < 350 && m.y>500 && m.y < 580)
			{
				game.pai = 3;
				
				
				paihangbang();
			}
		}
		if (m.uMsg ==WM_RBUTTONDOWN)
		{
			lent();
		}
	}
}
void Game::paihangbang()
{
	putimage(0, 0, &nandubeijintu);
	string str6, str7;
	setfont(50, 0, "Microsoft Yahei UI Bold");//设置字体
	settextcolor(RGB(255, 116, 101));
	setbkcolor(RGB(141, 122, 105));//背景颜色
	setfillcolor(RGB(156, 219, 250));
	
	if (game.pai == 1)
		outtextxy(105, 32, "简单模式排行榜");
	if (game.pai == 2)
		outtextxy(105, 32, "困难模式排行榜");
	if (game.pai == 3)
		outtextxy(105, 32, "地狱模式排行榜");

	solidroundrect(95, 125, 470, 200, 15, 15);
	outtextxy(205, 132, "第一名");
	solidroundrect(95, 225, 470, 300, 15, 15);

	solidroundrect(95, 325, 470, 400, 15, 15);
	outtextxy(205, 332, "第二名");
	solidroundrect(95, 425, 470, 500, 15, 15);

	solidroundrect(95, 525, 470, 600, 15, 15);
	outtextxy(205, 532, "第三名");
	solidroundrect(95, 625, 470, 700, 15, 15);


	int a[100];
	if (game.pai == 1)
	{
		for (int i = 1; i < scount; i++)
		{
			a[i] = user[i].Score1;
		}
		sort(a, a + scount, cmp);

		for (int k = 0; k < 3; k++)
		{
			for (int j = 0; j < scount; j++)
			{
				if (user[j].Score1 == a[k])
				{
					str7 = user[j].Id;
					outtextxy(110, 235 + 200 * k, str7.c_str());  //在窗口坐标为(10,220)的地方开始输出str2的内容

					char s[5];
					sprintf(s, "%d", a[k]);
					outtextxy(320, 235 + 200 * k, s);
				}
			}
		}
	}
	if (game.pai == 2)
	{
		for (int i = 1; i < scount; i++)
		{
			a[i] = user[i].Score2;
		}
		sort(a, a + scount, cmp);

		for (int k = 0; k < 3; k++)
		{
			for (int j = 0; j < scount; j++)
			{
				if (user[j].Score2 == a[k])
				{
					str7 = user[j].Id;
					outtextxy(110, 235 + 200 * k, str7.c_str());  //在窗口坐标为(10,220)的地方开始输出str2的内容

					char s[5];
					sprintf(s, "%d", a[k]);
					outtextxy(320, 235 + 200 * k, s);
				}
			}
		}
	}
	if (game.pai == 3)
	{
		for (int i = 1; i < scount; i++)
		{
			a[i] = user[i].Score3;
		}
		sort(a, a + scount, cmp);

		for (int k = 0; k < 3; k++)
		{
			for (int j = 0; j < scount; j++)
			{
				if (user[j].Score3 == a[k])
				{
					str7 = user[j].Id;
					outtextxy(110, 235 + 200 * k, str7.c_str());  //在窗口坐标为(10,220)的地方开始输出str2的内容

					char s[5];
					sprintf(s, "%d", a[k]);
					outtextxy(320, 235 + 200 * k, s);
				}
			}
		}
	}
	MOUSEMSG m;
	while (1)
	{
		m = GetMouseMsg();//获取鼠标信息
		if (m.uMsg == WM_RBUTTONUP)
		{
			if (m.x > 0 && m.x < 549 && m.y>0 && m.y < 836)//开始游戏
				painandu();
		}
	}
}
void Game::showGame()
{
	BeginBatchDraw();//防止闪烁

	putimage(0, 0, &top_y, SRCAND);//贴掩码图
	putimage(0, 0, &top);//贴原图

	//贴地鼠图片
	for (int i = 0; i < 3; i++)
	{
		ds[i].Draw();
	}

	putimage(0, MID_UP_HEITH, &mid_up_y, SRCAND);//贴掩码图
	putimage(0, MID_UP_HEITH, &mid_up, SRCPAINT);//贴原图


	//贴地鼠图片
	for (int i = 3; i < 6; i++)
	{
		ds[i].Draw();
	}


	putimage(0, MID_DOWN_HEITH, &mid_down_y, SRCAND);//贴掩码图
	putimage(0, MID_DOWN_HEITH, &mid_down, SRCPAINT);//贴原图


	//贴地鼠图片
	for (int i = 6; i < 9; i++)
	{
		ds[i].Draw();
	}


	putimage(0, BOTTOM_HEITH, &bottom_y, SRCAND);//贴掩码图
	putimage(0, BOTTOM_HEITH, &bottom, SRCPAINT);//贴原图

	setfont(70, 0, "Microsoft Yahei UI Bold");//设置字体
	settextcolor(RGB(255, 116, 101));
	setbkcolor(RGB(141, 122, 105));//背景颜色
	setfillcolor(RGB(141, 122, 105));
	solidroundrect(13, 25, 300, 107, 15, 15);
	outtextxy(17, 32, "得分:");
	itoa(game.score, game.str, 10);;
	RECT r1 = { 140, 32, 300, 107 };
	drawtext(_T(game.str), &r1, DT_CENTER | DT_VCENTER | DT_SINGLELINE);

	solidroundrect(310, 25, 540, 107, 15, 15);
	outtextxy(317, 32, "生命:");
	itoa(game.life, game.strlife, 10);
	RECT r3 = { 440, 25, 530, 107 };
	drawtext(_T(game.strlife), &r3, DT_CENTER | DT_VCENTER | DT_SINGLELINE);


	if (game.lianji > 0)
	{
		solidroundrect(13, 130, 300, 207, 15, 15);
		outtextxy(17, 132, "连击 X");
		itoa(game.lianji, game.strlian, 10);
		RECT r2 = { 162, 130, 300, 207 };
		drawtext(_T(game.strlian), &r2, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
	}

	EndBatchDraw();
}//显示游戏界面
void Game::zhujiemian()
{
	HWND hWnd = GetHWnd();
	SetClassLong(hWnd, GCL_HCURSOR, (long)hcur2);
	BeginBatchDraw();//批量绘图
	mciSendString("close music  ", NULL, 0, NULL);
	mciSendString("open \".\\配置\\背景音乐.mp3\" alias music ", NULL, 0, NULL);//背景音乐
	mciSendString("play music repeat ", NULL, 0, NULL);
	putimage(0, 0, &img1);
	putimage(20, 20, &img2, SRCAND);
	putimage(20, 20, &img3, SRCPAINT);
	setbkmode(TRANSPARENT);
	setfont(37, 0, "黑体");
	setbkcolor(YELLOW);
	settextcolor(RGB(125, 125, 125));
	outtextxy(365, 757, "登录|注册");
	settextcolor(RGB(125, 125, 125));
	outtextxy(193, 755, "进入游戏");
	EndBatchDraw();//结束批量绘图
}
void dsMove()//地鼠的移动
{
	if (ds[game.m].zuigao != true)
	{
		if (ds[game.m].fx)
		{
			ds[game.m].moveUP();
			game.showGame();
			if (ds[game.m].y <= ds[game.m].ymax)//向上移动超过一个地鼠身位
			{
				ds[game.m].zuigao = true;
				game.startt = time(NULL);
			}
		}
		else
		{
			ds[game.m].moveDOWN();
			game.showGame();
			if (ds[game.m].y >= ds[game.m].ymin)//向下移动超过一个地鼠身位
			{
				if (ds[game.m].behit == false)
				{
					game.life--;
					game.lianji = 0;
				}
				ds[game.m].fx = true;
				ds[game.m].behit = false;
				game.m = rand() % 9;
			}
		}
	}
	else
	{
		game.end = time(NULL);
		if (game.end - game.startt >= 1)
		{
			ds[game.m].fx = false;
			ds[game.m].zuigao = false;
			game.end = 0;
			game.startt = 0;
		}
	}
}

bool cmp(int a, int b)
{
	return a > b;
}
void start()
{
	cleardevice();
	HWND hWnd = GetHWnd();
	SetClassLong(hWnd, GCL_HCURSOR, (long)hcur);	// 设置窗口类的鼠标样式
	srand((int)time(0));//随机性
	for (int i = 0; i < 9; i++)
	{
		ds[i].x = -50 + (i % 3) * 184;//-50   横相隔184
		ds[i].y = 475 + (i / 3) * 125;//475   竖相隔125
		ds[i].ymax = 475 + (i / 3) * 125 - DISHU_HEITH;
		ds[i].ymin = 475 + (i / 3) * 125;
		ds[i].img = mouse;
		ds[i].img_y = mouse_y;
		ds[i].img_x = mouse_x;
		ds[i].fx = true;//上
		ds[i].zuigao = false;//不是最高点
	}
	game.score = 0;
	game.lianji = 0;
	game.m = rand() % 9;
	if (game.nandu == 1)
	{
		TIME_SPACE = 30;
		game.life = 15;
	}
	if (game.nandu == 2)
	{
		TIME_SPACE = 15;
		game.life = 10;
	}
	if (game.nandu == 3)
	{
		TIME_SPACE = 1;
		game.life = 5;
	}
	//启动定时器
	//参数 1窗口句柄 2定时器id 3间隔时间 4 调用函数
	SetTimer(hWnd, 66666, TIME_SPACE, (TIMERPROC)dsMove);
	while (1)
	{
		while (MouseHit())							// 当有鼠标消息的时候执行
		{
			msg = GetMouseMsg();					// 获取鼠标消息
			switch (msg.uMsg)						// 根据不同的鼠标消息,执行不同的代码
			{
			case WM_LBUTTONUP:
				mciSendString("close music ", NULL, 0, NULL);
				mciSendString("open \".\\配置\\击打.mp3\" alias music ", NULL, 0, NULL);//背景音乐
				mciSendString("play music ", NULL, 0, NULL);
				SetClassLong(hWnd, GCL_HCURSOR, (long)hcur);	// 设置窗口类的鼠标样式
				if (msg.x > (game.m % 3) * 184 && msg.x < 170 + (game.m % 3) * 184 && msg.y>335 + (game.m / 3) * 125 && msg.y < 505 + (game.m / 3) * 125)
				{
					mciSendString("open \".\\配置\\得分.mp3\" alias music ", NULL, 0, NULL);//背景音乐
					mciSendString("play music ", NULL, 0, NULL);
					if (ds[game.m].fx == true)
					{
						game.score = game.score + 2;
						game.lianji++;
						ds[game.m].fx = false;
						ds[game.m].behit = true;
						ds[game.m].zuigao = false;
					}
					if (game.lianji % 5 == 0 && game.lianji != 0)
						game.score = game.score + game.lianji;
				}
				; break;
			case WM_LBUTTONDOWN:
				SetClassLong(hWnd, GCL_HCURSOR, (long)hcur1);	// 设置窗口类的鼠标样式
				break;
			case WM_RBUTTONUP:
				KillTimer(hWnd, 66666);
				lent();
				break;
			default:break;
			}
		}
		if (game.life == 0)
		{
			KillTimer(hWnd, 66666);
			itoa(game.life, game.strlife, 10);
			RECT r5 = { 440, 25, 530, 107 };
			drawtext(_T(game.strlife), &r5, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
			break;
		}
	}
	BeginBatchDraw();//防止闪烁
	setfont(70, 0, "Microsoft Yahei UI Bold");//设置字体
	settextcolor(RGB(255, 116, 101));
	setbkcolor(RGB(141, 122, 105));//背景颜色
	setfillcolor(RGB(141, 122, 105));
	solidroundrect(90, 225, 480, 607, 15, 15);
	outtextxy(180, 275, "游戏结束");
	outtextxy(180, 355, "本次得分");
	itoa(game.score, game.str, 10);;
	RECT r4 = { 90, 355, 480, 607 };
	drawtext(_T(game.str), &r4, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
	EndBatchDraw();
	if (game.nandu == 1)
	{
		user[diji].Score1 = game.score;
	}
	if (game.nandu == 2)
	{
		user[diji].Score2 = game.score;
	}
	if (game.nandu == 3)
	{
		user[diji].Score3 = game.score;
	}
	us.save();
	Sleep(2000);
	lent();
}
void initGame()
{
	hcur = (HCURSOR)LoadImage(NULL, _T(".\\配置\\chuizi.cur"), IMAGE_CURSOR, 100, 100, LR_LOADFROMFILE);
	hcur1 = (HCURSOR)LoadImage(NULL, _T(".\\配置\\chuizi1.cur"), IMAGE_CURSOR, 100, 100, LR_LOADFROMFILE);
	hcur2 = (HCURSOR)LoadImage(NULL, _T(".\\配置\\chuizi2.cur"), IMAGE_CURSOR, 60, 60, LR_LOADFROMFILE);
	loadimage(&nandubeijintu, ".\\配置\\难度背景图.jpg");

	loadimage(&img1, ".\\配置\\打地鼠主界面.jpg");
	loadimage(&img2, ".\\配置\\声音.jpg");
	loadimage(&img3, ".\\配置\\声音_y.jpg");
	loadimage(&img4, ".\\配置\\声音_y1.jpg");//鼠标操作

	loadimage(&top, ".\\配置\\top.png");
	loadimage(&mid_up, ".\\配置\\mid_up.png");
	loadimage(&mid_down, ".\\配置\\mid_down.png");
	loadimage(&bottom, ".\\配置\\bottom.png");

	loadimage(&top_y, ".\\配置\\top_y.png");
	loadimage(&mid_up_y, ".\\配置\\mid_up_y.png");
	loadimage(&mid_down_y, ".\\配置\\mid_down_y.png");
	loadimage(&bottom_y, ".\\配置\\bottom_y.png");


	loadimage(&mouse, ".\\配置\\地鼠3.png");//加载地鼠图片
	loadimage(&mouse_y, ".\\配置\\地鼠3_y.png");
	loadimage(&mouse_x, ".\\配置\\地鼠3_x.png");


	
}//初始化游戏
void lent()
{
	cleardevice();
	game.zhujiemian();
	MOUSEMSG m;
	while (1)
	{
		EndBatchDraw();//结束批量绘图
		BeginBatchDraw();//批量绘图
		m = GetMouseMsg();//获取鼠标信息
		if (m.uMsg == WM_LBUTTONDOWN)
		{
			EndBatchDraw();//结束批量绘图
			if (m.x > 180 && m.x < 350 && m.y>700 && m.y < 820)//开始游戏
				game.startnandu();
			else if (m.x > 365 && m.x < 450 && m.y>750 && m.y < 820)//登录   
			{
				us.read();
				setbkcolor(WHITE);
				cleardevice();

				setfillcolor(RGB(241, 122, 105));
				solidroundrect(200, 215, 400, 263, 15, 15);
				solidroundrect(200, 280, 400, 328, 15, 15);

				settextcolor(BLACK);
				setbkmode(TRANSPARENT);
				settextstyle(40, 0, "黑体");
				outtextxy(100, 220, " Id :");
				outtextxy(20, 285, "PassWord:");

				us.Login();
			}
			else if (m.x > 20 && m.x < 150 && m.y>700 && m.y < 820)//排行榜
			{
				us.read();
				game.painandu();
			}
			else if (m.x > 450 && m.x < 535 && m.y>700 && m.y < 820)//注册
			{
				us.read();
				setbkcolor(WHITE);
				cleardevice();

				setfillcolor(RGB(241, 122, 105));
				solidroundrect(200, 215, 400, 263, 15, 15);
				solidroundrect(200, 280, 400, 328, 15, 15);

				settextcolor(BLACK);
				setbkmode(TRANSPARENT);
				settextstyle(40, 0, "黑体");
				outtextxy(100, 220, " Id :");
				outtextxy(20, 285, "PassWord:");

				us.Registers();
			}
			else if (m.x > 0 && m.x < 50 && m.y>0 && m.y < 60)
			{
				music_hit++;
				if (music_hit % 2 == 1)
				{
					mciSendString("close music", NULL, 0, NULL);
					putimage(20, 20, &img2, SRCAND);
					putimage(20, 20, &img4, SRCPAINT);
				}
				else
				{
					mciSendString("play music repeat ", NULL, 0, NULL);
					cleardevice();
					game.zhujiemian();
				}
			}

		}
	}
}

int main()
{
	HWND hWnd = initgraph(549, 836);//界面大小
	initGame();//加载所有资源
	us.read();//读取文件
	lent();//显示主界面
	_getch();
	closegraph();
	return 0;
}
  • 26
    点赞
  • 59
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
两个使用easyx优化的C语言小游戏.zip 探索C语言小游戏的无限可能 一、开启C语言游戏之门 对于许多初学者来说,C语言可能是一门既神秘又令人畏惧的语言。但其,C语言也可以非常有趣!这次我们为您带来了一系列C语言小游戏资源,旨在让您在轻松愉快的氛围中,逐步掌握C语言的精髓。 二、资源亮点 由浅入深:我们为您提供了从入门级到进阶级的多种小游戏资源,满足您不同阶段的学习需求。 践为王:这些资源不仅仅是理论,更有际可运行的代码,让您亲身体验编程的乐趣。 模块化设计:每个游戏都按照功能模块进行划分,方便您学习和理解。 社区参与:我们鼓励您参与到社区中,与其他学习者分享经验,共同进步。 三、适用人群 无论您是初涉编程的新手,还是希望深入了解C语言的进阶者,这些资源都能为您提供宝贵的践机会。 四、使用建议 边学边做:建议您在学习过程中,积极动手践,亲自感受C语言的魅力。 不断挑战:尝试自行修改和优化游戏代码,培养独立思考和解决问题的能力。 交流与分享:加入我们的学习社群,与其他学习者交流心得,共同成长。 五、注意事项 尊重权:请确保在使用这些资源时,遵循权法规,尊重原创者的权益。 安全为先:在编写和运行代码时,请确保您的开发环境安全可靠,避免潜在风险。 持续学习:编程是一个不断进阶的过程,希望您能保持对知识的热情,持续深入学习。 感谢您选择我们的C语言小游戏资源系列!让我们一起在探索中成长,用代码书写属于我们的精彩故事!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值