C++#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <algorithm>
#include <graphics.h>
#include <windows.h>
#include <math.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif
using namespace std;
// 游戏窗口大小
const int WIDTH = 400;
const int HEIGHT = 300;
// 角色结构体
struct Player {
double x;
double y;
int health;
int radius;
int score;
int color;
double bodyAngle; // 车身角度
double turretAngle; // 炮塔角度
};
// 子弹结构体
struct Bullet {
double x;
double y;
int dx;
int dy;
bool isAlive;
int color;
};
// 敌人结构体
struct Enemy {
double x;
double y;
bool isAlive;
int radius;
bool hasCollided;
double speedFactor;
double moveCounter;
int color;
int width;
int height;
};
// 全局变量
Player player;
vector<Bullet> bullets;
vector<Enemy> enemies;
int lastEnemySpawnTime = 0;
double targetTurretAngle = 0;
// 定义删除子弹的函数
bool isBulletDead(const Bullet& b) {
return !b.isAlive;
}
// 定义删除敌人的函数
bool isEnemyDead(const Enemy& e) {
return !e.isAlive;
}
// 初始化游戏
void initGame() {
srand(time(0));
player.x = WIDTH / 2.0;
player.y = HEIGHT / 2.0;
player.health = 100;
player.radius = 10;
player.score = 0;
player.color = WHITE;
player.bodyAngle = 0.0;
player.turretAngle = 0.0;
HWND consoleWindow = GetConsoleWindow();
HDC consoleDC = GetDC(consoleWindow);
HWND hwnd = WindowFromDC(consoleDC);
initgraph((uintptr_t)hwnd, WIDTH, HEIGHT);
}
// 绘制角色 (修改为坦克)
void drawPlayer() {
setcolor(player.color);
// 绘制车身
int bodyLength = 20;
int bodyWidth = 15;
int x1 = static_cast<int>(round(player.x - bodyLength / 2.0 * cos(player.bodyAngle) + bodyWidth / 2.0 * sin(player.bodyAngle)));
int y1 = static_cast<int>(round(player.y - bodyLength / 2.0 * sin(player.bodyAngle) - bodyWidth / 2.0 * cos(player.bodyAngle)));
int x2 = static_cast<int>(round(player.x + bodyLength / 2.0 * cos(player.bodyAngle) + bodyWidth / 2.0 * sin(player.bodyAngle)));
int y2 = static_cast<int>(round(player.y + bodyLength / 2.0 * sin(player.bodyAngle) - bodyWidth / 2.0 * cos(player.bodyAngle)));
int x3 = static_cast<int>(round(player.x + bodyLength / 2.0 * cos(player.bodyAngle) - bodyWidth / 2.0 * sin(player.bodyAngle)));
int y3 = static_cast<int>(round(player.y + bodyLength / 2.0 * sin(player.bodyAngle) + bodyWidth / 2.0 * cos(player.bodyAngle)));
int x4 = static_cast<int>(round(player.x - bodyLength / 2.0 * cos(player.bodyAngle) - bodyWidth / 2.0 * sin(player.bodyAngle)));
int y4 = static_cast<int>(round(player.y - bodyLength / 2.0 * sin(player.bodyAngle) + bodyWidth / 2.0 * cos(player.bodyAngle)));
POINT bodyPoints[] = { {x1, y1}, {x2, y2}, {x3, y3}, {x4, y4} };
fillpolygon(bodyPoints, 4);
// 绘制炮塔
int turretLength = 15;
int turretX = static_cast<int>(round(player.x + turretLength / 2.0 * cos(player.turretAngle)));
int turretY = static_cast<int>(round(player.y + turretLength / 2.0 * sin(player.turretAngle)));
line(static_cast<int>(player.x), static_cast<int>(player.y), turretX, turretY);
}
// 绘制游戏界面
void drawGame() {
cleardevice();
// 绘制角色
drawPlayer();
// 绘制敌人
for (size_t i = 0; i < enemies.size(); ++i) {
if (enemies[i].isAlive) {
setcolor(enemies[i].color);
int left = static_cast<int>(round(enemies[i].x - enemies[i].width / 2.0));
int top = static_cast<int>(round(enemies[i].y - enemies[i].height / 2.0));
int right = static_cast<int>(round(enemies[i].x + enemies[i].width / 2.0));
int bottom = static_cast<int>(round(enemies[i].y + enemies[i].height / 2.0));
bar(left, top, right, bottom);
}
}
// 绘制子弹
for (size_t i = 0; i < bullets.size(); ++i) {
if (bullets[i].isAlive) {
setcolor(bullets[i].color);
circle(static_cast<int>(round(bullets[i].x)), static_cast<int>(round(bullets[i].y)), 2);
}
}
// 显示血量和积分
char healthStr[20];
char scoreStr[20];
sprintf(healthStr, "Health: %d", player.health);
sprintf(scoreStr, "Score: %d", player.score);
setcolor(WHITE);
outtextxy(10, 10, healthStr);
outtextxy(10, 25, scoreStr);
}
// 处理玩家输入
void handleInput() {
double moveSpeed = 5;
bool moving = false;
double moveX = 0.0;
double moveY = 0.0;
if (GetAsyncKeyState('W') & 0x8000) { // W
moveY -= moveSpeed;
moving = true;
}
if (GetAsyncKeyState('A') & 0x8000) { // A
moveX -= moveSpeed;
moving = true;
}
if (GetAsyncKeyState('S') & 0x8000) { // S
moveY += moveSpeed;
moving = true;
}
if (GetAsyncKeyState('D') & 0x8000) { // D
moveX += moveSpeed;
moving = true;
}
if (moving) {
player.x += moveX;
player.y += moveY;
player.bodyAngle = atan2(moveY,moveX); // 车身跟随移动方向
}
if (GetAsyncKeyState('I') & 0x8000) { // I
targetTurretAngle = -M_PI/2.0;
Bullet newBullet;
newBullet.x = player.x + 15 * cos(targetTurretAngle);
newBullet.y = player.y + 15 * sin(targetTurretAngle);
newBullet.dx = 0;
newBullet.dy = -15;
newBullet.isAlive = true;
newBullet.color = YELLOW;
bullets.push_back(newBullet);
}
if (GetAsyncKeyState('J') & 0x8000) { // J
targetTurretAngle = M_PI;
Bullet newBullet;
newBullet.x = player.x + 15 * cos(targetTurretAngle);
newBullet.y = player.y + 15 * sin(targetTurretAngle);
newBullet.dx = -15;
newBullet.dy = 0;
newBullet.isAlive = true;
newBullet.color = YELLOW;
bullets.push_back(newBullet);
}
if (GetAsyncKeyState('K') & 0x8000) { // K
targetTurretAngle = M_PI/2.0;
Bullet newBullet;
newBullet.x = player.x + 15 * cos(targetTurretAngle);
newBullet.y = player.y + 15 * sin(targetTurretAngle);
newBullet.dx = 0;
newBullet.dy = 15;
newBullet.isAlive = true;
newBullet.color = YELLOW;
bullets.push_back(newBullet);
}
if (GetAsyncKeyState('L') & 0x8000) { // L
targetTurretAngle = 0.0;
Bullet newBullet;
newBullet.x = player.x + 15 * cos(targetTurretAngle);
newBullet.y = player.y + 15 * sin(targetTurretAngle);
newBullet.dx = 15;
newBullet.dy = 0;
newBullet.isAlive = true;
newBullet.color = YELLOW;
bullets.push_back(newBullet);
}
// 边界检测
if (player.x - player.radius < 0) {
player.x = player.radius;
} else if (player.x + player.radius > WIDTH) {
player.x = WIDTH - player.radius;
}
if (player.y - player.radius < 0) {
player.y = player.radius;
} else if (player.y + player.radius > HEIGHT) {
player.y = HEIGHT - player.radius;
}
}
// 计算两点之间的距离
double calculateDistance(double x1, double y1, double x2, double y2) {
double dx = x2 - x1;
double dy = y2 - y1;
return sqrt(dx * dx + dy * dy);
}
// 更新子弹位置和碰撞检测
void updateBullets() {
for (size_t i = 0; i < bullets.size(); ++i) {
if (bullets[i].isAlive) {
bullets[i].x += bullets[i].dx;
bullets[i].y += bullets[i].dy;
// 边界检测
if (bullets[i].x < 0 || bullets[i].x > WIDTH || bullets[i].y < 0 || bullets[i].y > HEIGHT) {
bullets[i].isAlive = false;
continue;
}
// 碰撞检测(敌人)
for (size_t j = 0; j < enemies.size(); ++j) {
if (enemies[j].isAlive) {
double distance = sqrt(pow(bullets[i].x - enemies[j].x, 2) + pow(bullets[i].y - enemies[j].y, 2));
int enemyLeft = static_cast<int>(round(enemies[j].x - enemies[j].width/2.0));
int enemyTop = static_cast<int>(round(enemies[j].y - enemies[j].height/2.0));
int enemyRight = static_cast<int>(round(enemies[j].x + enemies[j].width/2.0));
int enemyBottom = static_cast<int>(round(enemies[j].y + enemies[j].height/2.0));
if (bullets[i].x > enemyLeft && bullets[i].x < enemyRight &&
bullets[i].y > enemyTop && bullets[i].y < enemyBottom) {
enemies[j].isAlive = false;
bullets[i].isAlive = false;
player.score++;
break;
}
}
}
}
}
//移除死亡的子弹
bullets.erase(remove_if(bullets.begin(), bullets.end(), isBulletDead), bullets.end());
}
// 更新敌人位置
void updateEnemies() {
for (size_t i = 0; i < enemies.size(); ++i) {
if (enemies[i].isAlive) {
// 计算敌人和玩家的距离
double dx = player.x - enemies[i].x;
double dy = player.y - enemies[i].y;
double distance = calculateDistance(player.x, player.y, enemies[i].x, enemies[i].y);
// 更新移动计数器
enemies[i].moveCounter += enemies[i].speedFactor;
if (enemies[i].moveCounter >= 1.0) {
// 根据距离更新敌人位置
if (abs(dx) > abs(dy)) {
if (dx > 0 && enemies[i].x < WIDTH)
enemies[i].x++;
else if (dx < 0 && enemies[i].x > 0)
enemies[i].x--;
} else {
if (dy > 0 && enemies[i].y < HEIGHT)
enemies[i].y++;
else if (dy < 0 && enemies[i].y > 0)
enemies[i].y--;
}
enemies[i].moveCounter -= 1.0;
}
// 碰撞检测(玩家)
int enemyLeft = static_cast<int>(round(enemies[i].x - enemies[i].width/2.0));
int enemyTop = static_cast<int>(round(enemies[i].y - enemies[i].height/2.0));
int enemyRight = static_cast<int>(round(enemies[i].x + enemies[i].width/2.0));
int enemyBottom = static_cast<int>(round(enemies[i].y + enemies[i].height/2.0));
if (player.x + player.radius > enemyLeft && player.x - player.radius < enemyRight &&
player.y + player.radius > enemyTop && player.y - player.radius < enemyBottom){
player.health -= 5;
enemies[i].isAlive = false;
}
}
}
//移除死亡的敌人
enemies.erase(remove_if(enemies.begin(), enemies.end(), isEnemyDead), enemies.end());
}
// 生成新的敌人
void spawnEnemy() {
if (enemies.size() < 5) {
int currentTime = time(0);
if (currentTime - lastEnemySpawnTime >= 2) {
double x = rand() % (WIDTH);
double y = rand() % (HEIGHT);
do {
x = rand() % (WIDTH);
y = rand() % (HEIGHT);
} while (abs(x - player.x) < 2 * player.radius && abs(y - player.y) < 2 * player.radius);
Enemy newEnemy;
newEnemy.x = x;
newEnemy.y = y;
newEnemy.isAlive = true;
newEnemy.radius = 10;
newEnemy.width = 20;
newEnemy.height = 20;
newEnemy.hasCollided = false;
newEnemy.speedFactor = 2.0;
newEnemy.moveCounter = 0.0;
newEnemy.color = RED;
enemies.push_back(newEnemy);
lastEnemySpawnTime = currentTime;
}
}
}
// 跨平台暂停函数,使用sleep
void sleep_ms(int milliseconds) {
#ifdef _WIN32
Sleep(milliseconds);
#else
usleep(milliseconds * 1000);
#endif
}
int main() {
initGame();
while (player.health > 0) {
// 计算炮塔过渡动画
double turretSpeed = 0.1;
double angleDiff = targetTurretAngle - player.turretAngle;
if (abs(angleDiff) > turretSpeed) {
player.turretAngle += turretSpeed * (angleDiff > 0 ? 1 : -1);
} else {
player.turretAngle = targetTurretAngle;
}
drawGame();
handleInput();
updateBullets();
updateEnemies();
spawnEnemy();
if (player.score >= 5 && player.health < 100) {
player.health += 10;
if (player.health > 100) {
player.health = 100;
}
player.score -= 5;
if (player.score<0){
player.score = 0;
}
}
if (player.score >= 25 && player.health == 100) {
cout << "你赢了!" << endl;
sleep_ms(3000);
closegraph();
return 0;
}
sleep_ms(16);
}
cout << "你死了!" << endl;
sleep_ms(3000);
closegraph();
return 0;
}