黄金矿工 多文件 C++ easyx
时间: 2023-08-05 18:07:31 浏览: 53
下面是一个简单的多文件C++黄金矿工游戏示例,使用EasyX图形库实现。这个游戏有两个C++文件,一个是主文件,另一个是一个工具文件。主文件包含了游戏的主要逻辑,工具文件包含了一些帮助函数。
注意:这个示例只是一个基本的框架,你可以根据自己的需要进行修改和扩展。
主文件:
```c++
#include "tool.h"
#include <graphics.h>
#include <conio.h>
#include <time.h>
// 游戏区域宽度和高度
const int GAME_WIDTH = 640;
const int GAME_HEIGHT = 480;
// 矿工的位置和大小
const int MINER_X = GAME_WIDTH / 2;
const int MINER_Y = GAME_HEIGHT - 50;
const int MINER_WIDTH = 40;
const int MINER_HEIGHT = 50;
// 矿石的数量
const int STONE_COUNT = 10;
// 矿石的位置和大小
const int STONE_WIDTH = 40;
const int STONE_HEIGHT = 40;
// 矿工移动速度
const int MINER_SPEED = 5;
// 矿工的得分
int score = 0;
// 游戏是否结束
bool gameover = false;
// 矿工结构体
struct Miner {
int x;
int y;
int width;
int height;
};
// 矿石结构体
struct Stone {
int x;
int y;
int width;
int height;
bool visible;
};
// 初始化矿工
void initMiner(Miner& miner) {
miner.x = MINER_X;
miner.y = MINER_Y;
miner.width = MINER_WIDTH;
miner.height = MINER_HEIGHT;
}
// 初始化矿石
void initStones(Stone stones[], int count) {
for (int i = 0; i < count; i++) {
stones[i].x = rand() % (GAME_WIDTH - STONE_WIDTH);
stones[i].y = rand() % (GAME_HEIGHT / 2);
stones[i].width = STONE_WIDTH;
stones[i].height = STONE_HEIGHT;
stones[i].visible = true;
}
}
// 绘制矿工
void drawMiner(Miner& miner) {
setfillcolor(YELLOW);
fillrectangle(miner.x, miner.y, miner.x + miner.width, miner.y + miner.height);
}
// 绘制矿石
void drawStone(Stone& stone) {
if (stone.visible) {
setfillcolor(BROWN);
fillrectangle(stone.x, stone.y, stone.x + stone.width, stone.y + stone.height);
}
}
// 绘制得分
void drawScore() {
char buffer[32];
sprintf_s(buffer, "Score: %d", score);
setbkmode(TRANSPARENT);
settextcolor(WHITE);
settextstyle(20, 0, _T("Consolas"));
outtextxy(10, 10, buffer);
}
// 更新游戏状态
void update(Miner& miner, Stone stones[], int count) {
if (gameover) {
return;
}
// 移动矿工
if (_kbhit()) {
int key = _getch();
switch (key) {
case 'a':
miner.x -= MINER_SPEED;
break;
case 'd':
miner.x += MINER_SPEED;
break;
}
}
// 检查矿石是否与矿工碰撞
for (int i = 0; i < count; i++) {
if (stones[i].visible && hitTest(miner, stones[i])) {
stones[i].visible = false;
score += 10;
}
}
// 检查游戏是否结束
bool allStonesGone = true;
for (int i = 0; i < count; i++) {
if (stones[i].visible) {
allStonesGone = false;
}
}
if (allStonesGone) {
gameover = true;
}
}
int main() {
// 初始化随机数生成器
srand((unsigned)time(NULL));
// 初始化图形库
initgraph(GAME_WIDTH, GAME_HEIGHT);
// 初始化矿工和矿石
Miner miner;
Stone stones[STONE_COUNT];
initMiner(miner);
initStones(stones, STONE_COUNT);
// 游戏循环
while (!gameover) {
// 清空屏幕
cleardevice();
// 绘制矿工和矿石
drawMiner(miner);
for (int i = 0; i < STONE_COUNT; i++) {
drawStone(stones[i]);
}
// 绘制得分
drawScore();
// 更新游戏状态
update(miner, stones, STONE_COUNT);
// 刷新屏幕
flush();
// 暂停一段时间
Sleep(10);
}
// 游戏结束,显示得分
cleardevice();
char buffer[32];
sprintf_s(buffer, "Final Score: %d", score);
setbkmode(TRANSPARENT);
settextcolor(WHITE);
settextstyle(40, 0, _T("Consolas"));
outtextxy(GAME_WIDTH / 2 - 150, GAME_HEIGHT / 2, buffer);
// 等待用户关闭窗口
getch();
// 关闭图形库
closegraph();
return 0;
}
```
工具文件:
```c++
#include "tool.h"
// 检测两个矩形是否相交
bool hitTest(const Rect& rect1, const Rect& rect2) {
if (rect1.x + rect1.width <= rect2.x) {
return false;
}
if (rect1.x >= rect2.x + rect2.width) {
return false;
}
if (rect1.y + rect1.height <= rect2.y) {
return false;
}
if (rect1.y >= rect2.y + rect2.height) {
return false;
}
return true;
}
// 检测两个矩形是否相交
bool hitTest(const Miner& miner, const Stone& stone) {
Rect rect1 = { miner.x, miner.y, miner.width, miner.height };
Rect rect2 = { stone.x, stone.y, stone.width, stone.height };
return hitTest(rect1, rect2);
}
```
tool.h文件:
```c++
#pragma once
// 矩形结构体
struct Rect {
int x;
int y;
int width;
int height;
};
// 矿工结构体
struct Miner {
int x;
int y;
int width;
int height;
};
// 矿石结构体
struct Stone {
int x;
int y;
int width;
int height;
bool visible;
};
// 检测两个矩形是否相交
bool hitTest(const Rect& rect1, const Rect& rect2);
// 检测两个矩形是否相交
bool hitTest(const Miner& miner, const Stone& stone);
```