c++打地鼠游戏代码

2024-01-08 15:53:09

c++打地鼠游戏代码

#include <graphics.h> //引入easyx图形库
#include <conio.h>
#include <time.h>
#include <stdlib.h>

#define WINDOW_WIDTH 800 //窗口宽度
#define WINDOW_HEITH 600 //窗口高度

#define LITTLE_MOUSE_BEGIN_X 100 //小地鼠起始横坐标
#define LITTLE_MOUSE_BEGIN_Y 100 //小地鼠起始纵坐标
#define LITTLE_MOUSE_SPACE 200 //小地鼠间隔
#define LITTLE_MOUSE_HEITH 100 //小地鼠高度

#define MID_MOUSE_BEGIN_X 50 //中地鼠起始横坐标
#define MID_MOUSE_BEGIN_Y 250 //中地鼠起始纵坐标
#define MID_MOUSE_SPACE 250 //中地鼠间隔
#define MID_MOUSE_HEITH 150 //中地鼠高度

#define BIG_MOUSE_BEGIN_X 0 //大地鼠起始横坐标
#define BIG_MOUSE_BEGIN_Y 400 //大地鼠起始纵坐标
#define BIG_MOUSE_SPACE 300 //大地鼠间隔
#define BIG_MOUSE_HEITH 200 //大地鼠高度

HWND hWnd; //窗口句柄

//图片变量
IMAGE top, mid_up, mid_down, bottom; //背景图片
IMAGE top_y, mid_up_y, mid_down_y, bottom_y; //背景图片的掩码
IMAGE little_mouse, mid_mouse, big_mouse; //地鼠图片
IMAGE little_mouse_y, mid_mouse_y, big_mouse_y; //地鼠图片的掩码
IMAGE hammer, hammer_down, hammer_y, hammer_down_y; //锤子图片和掩码

//地鼠类
class DiShu {
public:
	int x, y; //坐标
	IMAGE img, img_y; //图片和掩码
	bool fx; //方向,true为下,false为上
	bool isLive; //是否存活

	//地鼠绘制自己
	void draw() {
		putimage(x, y, &img_y, SRCAND);
		putimage(x, y, &img, SRCPAINT);
	}

	//地鼠向上移动
	void moveUp() {
		y -= 3;
	}

	//地鼠向下移动
	void moveDown() {
		y += 3;
	}
};

//锤子类
class ChuiZi {
public:
	int x, y; //坐标
	IMAGE img, img_y; //图片和掩码
	bool isDown; //是否落下

	//锤子绘制自己
	void draw() {
		putimage(x, y, &img_y, SRCAND);
		putimage(x, y, &img, SRCPAINT);
	}
};

DiShu ds[9]; //9个地鼠对象
ChuiZi cz; //一个锤子对象
int m; //当前活动的地鼠索引

//初始化游戏
void initGame() {
	//创建窗口
	hWnd = initgraph(WINDOW_WIDTH, WINDOW_HEITH, SHOWCONSOLE);

	//加载图片
	loadimage(&top, "top.bmp");
	loadimage(&mid_up, "mid_up.bmp");
	loadimage(&mid_down, "mid_down.bmp");
	loadimage(&bottom, "bottom.bmp");

	loadimage(&top_y, "top_y.bmp");
	loadimage(&mid_up_y, "mid_up_y.bmp");
	loadimage(&mid_down_y, "mid_down_y.bmp");
	loadimage(&bottom_y, "bottom_y.bmp");

	loadimage(&little_mouse, "little_mouse.bmp");
	loadimage(&mid_mouse, "mid_mouse.bmp");
	loadimage(&big_mouse, "big_mouse.bmp");

	loadimage(&little_mouse_y, "little_mouse_y.bmp");
	loadimage(&mid_mouse_y, "mid_mouse_y.bmp");
	loadimage(&big_mouse_y, "big_mouse_y.bmp");

	loadimage(&hammer, "hammer.bmp", 40, 40, true);
	loadimage(&hammer_down, "hammer_down.bmp", 40, 40, true);
	loadimage(&hammer_y, "hammer_y.bmp", 40, 40, true);
	loadimage(&hammer_down_y, "hammer_down_y.bmp", 40, 40, true);

	//初始化地鼠
	for (int i = 0; i < 9; i++) {
		if (i >= 0 && i < 3) {
			ds[i].x = LITTLE_MOUSE_BEGIN_X + i * LITTLE_MOUSE_SPACE;
			ds[i].y = LITTLE_MOUSE_BEGIN_Y;
			ds[i].img = little_mouse;
			ds[i].img_y = little_mouse_y;
		}
		if (i >= 3 && i < 6) {
			ds[i].x = MID_MOUSE_BEGIN_X + (i - 3) * MID_MOUSE_SPACE;
			ds[i].y = MID_MOUSE_BEGIN_Y;
			ds[i].img = mid_mouse;
			ds[i].img_y = mid_mouse_y;
		}
		if (i >= 6 && i < 9) {
			ds[i].x = BIG_MOUSE_BEGIN_X + (i - 6) * BIG_MOUSE_SPACE;
			ds[i].y = BIG_MOUSE_BEGIN_Y;
			ds[i].img = big_mouse;
			ds[i].img_y = big_mouse_y;
		}
		ds[i].fx = false; //初始方向为上
		ds[i].isLive = true; //初始状态为存活
	}

	//随机选择一个地鼠
	srand(time(NULL));
	m = rand() % 9;

	//初始化锤子
	cz.x = 100;
	cz.y = 100;
	cz.isDown = false;
	cz.img = hammer;
	cz.img_y = hammer_y;
}

//显示游戏
void showGame() {
	BeginBatchDraw(); //开启批量绘图

	//绘制背景
	putimage(0, 0, &top_y, SRCAND);
	putimage(0, 0, &top, SRCPAINT);

	//绘制地鼠
	for (int i = 0; i < 9; i++) {
		if (ds[i].isLive) {
			ds[i].draw();
		}
	}

	//绘制锤子
	cz.draw();

	//显示分数,生命值,目标分数
	settextcolor(WHITE); //设置文字颜色为白色
	settextstyle(20, 0, _T("宋体")); //设置文字样式
	outtextxy(10, 10, _T("得分:")); //输出文字
	TCHAR str[10]; //定义一个字符串变量
	_stprintf_s(str, _T("%d"), score); //将分数转换为字符串
	outtextxy(60, 10, str); //输出字符串
	outtextxy(10, 40, _T("生命值:")); //输出文字
	_stprintf_s(str, _T("%d"), life); //将生命值转换为字符串
	outtextxy(80, 40, str); //输出字符串
	outtextxy(10, 70, _T("目标分数:")); //输出文字
	_stprintf_s(str, _T("%d"), target); //将目标分数转换为字符串
	outtextxy(90, 70, str); //输出字符串

	//显示剩余时间
	timer();

	EndBatchDraw(); //结束批量绘图
}


//控制游戏
void ctolGame() {
	//控制地鼠移动
	if (ds[m].fx) { //向下移动
		ds[m].moveDown();
		if (0 <= m && m < 3) { //小地鼠
			if (ds[m].y >= (LITTLE_MOUSE_BEGIN_Y)) {
				ds[m].fx = false; //改变方向为上
				m = rand() % 9; //随机选择下一个地鼠
			}
		}
		else if (3 <= m && m < 6) { //中地鼠
			if (ds[m].y >= (MID_MOUSE_BEGIN_Y)) {
				ds[m].fx = false;
				m = rand() % 9;
			}
		}
		else if (6 <= m && m < 9) { //大地鼠
			if (ds[m].y >= (BIG_MOUSE_BEGIN_Y)) {
				ds[m].fx = false;
				m = rand() % 9;
			}
		}
	}
	else { //向上移动
		ds[m].moveUp();
		if (0 <= m && m < 3) { //小地鼠
			if (ds[m].y <= (LITTLE_MOUSE_BEGIN_Y - LITTLE_MOUSE_HEITH)) {
				ds[m].fx = true; //改变方向为下
			}
		}
		else if (3 <= m && m < 6) { //中地鼠
			if (ds[m].y <= (MID_MOUSE_BEGIN_Y - MID_MOUSE_HEITH)) {
				ds[m].fx = true;
			}
		}
		else if (6 <= m && m < 9) { //大地鼠
			if (ds[m].y <= (BIG_MOUSE_BEGIN_Y - BIG_MOUSE_HEITH)) {
				ds[m].fx = true;
			}
		}
	}

	//控制锤子移动
	moveChuiZi();

	//判断是否击中地鼠
	isHit();

	//计时器函数,控制游戏时间
	timer();
}

//控制锤子移动
void moveChuiZi() {
	//获取鼠标的位置和状态
	MOUSEMSG m = GetMouseMsg();
	//如果鼠标左键按下
	if (m.uMsg == WM_LBUTTONDOWN) {
		//改变锤子的图片和坐标
		cz.img = hammer_down;
		cz.img_y = hammer_down_y;
		cz.x = m.x - 20;
		cz.y = m.y - 20;
		cz.isDown = true; //锤子落下
	}
	//如果鼠标左键松开
	else if (m.uMsg == WM_LBUTTONUP) {
		//改变锤子的图片和坐标
		cz.img = hammer;
		cz.img_y = hammer_y;
		cz.x = m.x - 20;
		cz.y = m.y - 20;
		cz.isDown = false; //锤子抬起
	}
	//如果鼠标移动
	else if (m.uMsg == WM_MOUSEMOVE) {
		//改变锤子的坐标
		cz.x = m.x - 20;
		cz.y = m.y - 20;
	}
}

//判断是否击中地鼠
void isHit() {
	//如果锤子落下
	if (cz.isDown) {
		//遍历所有地鼠
		for (int i = 0; i < 9; i++) {
			//如果地鼠存活
			if (ds[i].isLive) {
				//判断锤子和地鼠是否碰撞
				if (isCollide(cz.x, cz.y, cz.x + 40, cz.y + 40, ds[i].x, ds[i].y, ds[i].x + 100, ds[i].y + 100)) {
					//击中地鼠
					ds[i].isLive = false; //地鼠死亡
					score++; //分数加一
				}
			}
		}
	}
}

//碰撞检测函数,判断两个矩形是否重叠
bool isCollide(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {
	//如果两个矩形的水平方向或垂直方向没有重叠,就返回false
	if (x1 > x4 || x2 < x3 || y1 > y4 || y2 < y3) {
		return false;
	}
	//否则返回true
	return true;
}

//计时器函数,控制游戏时间
void timer() {
	//获取当前时间
	time_t now = time(NULL);
	//计算剩余时间
	int remain = gameTime - (now - startTime);
	//如果剩余时间小于等于0,游戏结束
	if (remain <= 0) {
		gameOver = true; //游戏结束标志
		remain = 0; //剩余时间为0
	}
	//在窗口右上角显示剩余时间
	settextcolor(WHITE); //设置文字颜色为白色
	settextstyle(20, 0, _T("宋体")); //设置文字样式
	outtextxy(WINDOW_WIDTH - 100, 10, _T("剩余时间:")); //输出文字
	TCHAR str[10]; //定义一个字符串变量
	_stprintf_s(str, _T("%d"), remain); //将剩余时间转换为字符串
	outtextxy(WINDOW_WIDTH - 40, 10, str); //输出字符串
}

//主函数
int main() {
	//初始化游戏
	initGame();
	//游戏循环
	while (1) {
		//显示游戏
		showGame();
		//控制游戏
		ctolGame();
		//如果游戏结束,跳出循环
		if (gameOver) {
			break;
		}
		//延时10毫秒,降低CPU占用率
		Sleep(10);
	}
	//清屏
	cleardevice();
	//显示最终的分数
	settextcolor(WHITE); //设置文字颜色为白色
	settextstyle(40, 0, _T("宋体")); //设置文字样式
	outtextxy(200, 200, _T("游戏结束,你的分数是:")); //输出文字
	TCHAR str[10]; //定义一个字符串变量
	_stprintf_s(str, _T("%d"), score); //将分数转换为字符串
	outtextxy(500, 200, str); //输出字符串
	//等待用户按任意键退出
	_getch();
	//关闭图形窗口
	closegraph();
	//返回0
	return 0;
}

文章来源:https://blog.csdn.net/zhoushi331/article/details/135457459
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。