VisualStudio2022 C++ EasyX EGE简易版打地鼠

欸嘿,上一篇文章:升级版吃豆人说下一篇大概率是flappy bird,但是我想先发个简单的铺垫铺垫,上代码吧

#include<bits/stdc++.h>
#include<graphics.h>
#define WIDTH					420
#define HEIGHT					420
#define xrow					5
#define yrow					5
#define width					70
#define rad						30
#define mst						5
typedef long long ll;
using namespace std;
struct Hamsters {
	ll xr, yr, tmpr, spd, staysec;
	bool alive, up;
}hamster[mst + 1];
ll tot_hamster, msx, msy;
bool h[6];
void InitHamsters();
void initialize_hamster(ll id);
void ClickOnHamster();
void DrawPaint();
void PrintKill();
ll dist(ll ax, ll ay, ll bx, ll by);
int main() {
	HWND hwnd = initgraph(WIDTH, HEIGHT);
	CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)InitHamsters, NULL, NULL, NULL);
	CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ClickOnHamster, NULL, NULL, NULL);
	BeginBatchDraw();
	while (1) {
		DrawPaint();
		Sleep(50);
	}
	EndBatchDraw();
	return 0;
}
void initialize_hamster(ll id) {
reinit:
	ll x = rand() % xrow + 1, y = rand() % yrow + 1;
	for (ll i = 1; i <= mst; i++) if (hamster[i].alive == 1 && hamster[i].xr == x && hamster[i].yr == y) goto reinit;
	hamster[id].xr = x * width, hamster[id].yr = y * width, hamster[id].alive = 1;
	hamster[id].tmpr = 1, hamster[id].up = 1;
	hamster[id].spd = rand() % 8 + 2, hamster[id].staysec = rand() % 5 + 2;
}
void InitHamsters() {
	srand(GetTickCount64());
	while (1) {
		ll wait = rand() % 50 + 250;
		Sleep(wait);
		for (ll i = 1; i <= mst; i++) if (!hamster[i].alive) {
			initialize_hamster(i);
			break;
		}
		for (ll i = 1; i <= mst; i++) {
			if (hamster[i].up) {
				if (hamster[i].tmpr + hamster[i].spd >= 15) hamster[i].tmpr = 15;
				else hamster[i].tmpr += hamster[i].spd;
				if (hamster[i].tmpr == 15) hamster[i].staysec--;
				if (hamster[i].staysec < 0) hamster[i].up = 0;
			}
			else {
				if (hamster[i].tmpr - hamster[i].spd <= 0) hamster[i].alive = false;
				else hamster[i].tmpr -= hamster[i].spd;
			}
		}
	}
}
void ClickOnHamster() {
	while (1) {
		ExMessage msg = { 0 };
		ll clickx, clicky;
		if (peekmessage(&msg, EM_MOUSE)) {
			switch (msg.message) {
			case WM_LBUTTONDOWN:
				clickx = msg.x, clicky = msg.y;
				for (ll i = 1; i <= xrow; i++) {
					for (ll j = 1; j <= yrow; j++) {
						ll midx = i * width, midy = j * width;
						if (dist(midx, midy, clickx, clicky) <= rad * rad) {
							for (ll k = 1; k <= mst; k++) {
								if (hamster[k].xr == midx && hamster[k].yr == midy) {
									hamster[k].alive = 0;
									msx = clickx, msy = clicky;
									CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)PrintKill, NULL, NULL, NULL);
									tot_hamster++;
									setfillcolor(RED);
								}
							}
						}
					}
				}
				break;
			default:
				break;
			}
		}
	}
}
void DrawPaint() {
	setbkcolor(WHITE);
	cleardevice();
	char score[100];
	sprintf(score, "score: %lld", tot_hamster);
	outtextxy(0, 0, score);
	for (ll i = 1; i <= xrow; i++) {
		for (ll j = 1; j <= yrow; j++) {
			setlinecolor(LIGHTCYAN);
			setlinestyle(PS_SOLID, 3);
			circle(i * width, j * width, rad);
		}
	}
	for (ll i = 1; i <= mst; i++) {
		if (hamster[i].alive) {
			circle(hamster[i].xr, hamster[i].yr, hamster[i].tmpr);
		}
	}
	FlushBatchDraw();
}
void PrintKill() {
	ll a = GetTickCount64(), b = GetTickCount64(), x = msx, y = msy;
	settextstyle(25, 0, "Times New Roman");
	settextcolor(RED);
	while (b - a < 150) {
		outtextxy(x, y, "Kill");
		b = GetTickCount64();
	}
}
ll dist(ll ax, ll ay, ll bx, ll by) {
	return (ll)pow((ax - bx), 2) + (ll)pow((ay - by), 2);
}

哎呀,我这样善良的小盆友哪里找得到?求点赞,关注,收藏,转发!谢谢啦

致:好朋友 阳了个阳C++,第一版文字板框:C++之打地鼠_c++打地鼠的游戏大家应该都玩过或听说过,每当有地鼠露头,我们要在规定时间内消灭-CSDN博客

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

C++小盆友

你的鼓励将是我创作的最大动力呦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值