c++打地鼠游戏代码
#include <graphics.h>
#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;
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];
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) {
if (x1 > x4 || x2 < x3 || y1 > y4 || y2 < y3) {
return false;
}
return true;
}
void timer() {
time_t now = time(NULL);
int remain = gameTime - (now - startTime);
if (remain <= 0) {
gameOver = true;
remain = 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;
}
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();
return 0;
}