欸嘿,上一篇文章:升级版吃豆人说下一篇大概率是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博客