看看AI的力量
#include <iostream>
#include <conio.h>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <thread>
#include <chrono>
using namespace std;
// 定义赛车类
class Car {
public:
int x;
int y;
int speed;
int direction; // 0: 上, 1: 右, 2: 下, 3: 左
Car(int x, int y) {
this->x = x;
this->y = y;
speed = 1;
direction = 1; // 初始方向为向右
}
void move() {
switch (direction) {
case 0: y -= speed; break; // 向上
case 1: x += speed; break; // 向右
case 2: y += speed; break; // 向下
case 3: x -= speed; break; // 向左
}
}
// 设置速度
void setSpeed(int newSpeed) {
speed = newSpeed;
}
};
// 定义障碍物类
class Obstacle {
public:
int x;
int y;
Obstacle(int x, int y) {
this->x = x;
this->y = y;
}
void moveDown() {
y += 1; // 障碍物向下移动
}
};
// 定义 AI 赛车手类
class AICar : public Car {
public:
AICar(int x, int y) : Car(x, y) {}
void update(const Car& playerCar, const vector<Obstacle>& obstacles) {
// 1. 判断与玩家赛车的距离
int distanceX = abs(x - playerCar.x);
int distanceY = abs(y - playerCar.y);
// 2. 判断是否需要加速
if (playerCar.speed > speed) {
setSpeed(speed + 1); // 加速
}
// 3. 判断是否需要避开障碍物
bool obstacleAhead = false;
for (const auto& obstacle : obstacles) {
if (x == obstacle.x && y - 1 == obstacle.y) {
obstacleAhead = true;
break;
}
}
// 4. 调整方向
if (distanceX > 2) {
// 如果距离玩家赛车较远,向玩家赛车方向移动
if (x < playerCar.x) {
direction = 1; // 向右
} else {
direction = 3; // 向左
}
} else {
// 如果距离玩家赛车较近,随机选择向前或向后移动,并避开障碍物
if (rand() % 2 == 0 && !obstacleAhead) {
direction = 2; // 向下
} else if (!obstacleAhead) {
direction = 0; // 向上
} else {
// 如果前方有障碍物,选择左右移动
if (rand() % 2 == 0) {
direction = 1; // 向右
} else {
direction = 3; // 向左
}
}
}
// 5. 判断是否出界,如果出界则调整方向
if (x < 0) {
direction = 1; // 向右
} else if (x > 19) {
direction = 3; // 向左
}
if (y < 0) {
direction = 2; // 向下
} else if (y > 19) {
direction = 0; // 向上
}
move();
}
};
int main() {
// 初始化随机数种子
srand(time(0));
// 创建玩家赛车
Car playerCar(5, 18); // 将赛车初始位置设置在底部
// 创建 AI 赛车
AICar aiCar(15, 18); // 将 AI 赛车初始位置设置在底部
// 创建障碍物
vector<Obstacle> obstacles;
for (int i = 0; i < 5; ++i) {
obstacles.push_back(Obstacle(rand() % 20, -5 - i)); // 初始位置在屏幕上方
}
// 游戏循环
bool gameOver = false;
while (!gameOver) {
// 清除屏幕
system("cls");
// 绘制赛道和障碍物
for (int i = 0; i < 20; ++i) {
for (int j = 0; j < 20; ++j) {
bool isObstacle = false;
for (auto& obstacle : obstacles) {
if (obstacle.x == j && obstacle.y == i) {
isObstacle = true;
break;
}
}
if (isObstacle) {
cout << "#";
} else if (playerCar.x == j && playerCar.y == i) {
cout << "*";
} else if (aiCar.x == j && aiCar.y == i) {
cout << "A"; // 使用 "A" 代表 AI 赛车
} else {
cout << " ";
}
}
cout << endl;
}
// 处理玩家输入
if (_kbhit()) {
char key = _getch();
switch (key) {
case 'w': playerCar.direction = 0; break; // 向上
case 'd': playerCar.direction = 1; break; // 向右
case 's': playerCar.direction = 2; break; // 向下
case 'a': playerCar.direction = 3; break; // 向左
case ' ': playerCar.setSpeed(playerCar.speed + 1); break; // 加速
}
}
// 更新玩家赛车位置
playerCar.move();
// 更新 AI 赛车位置
aiCar.update(playerCar, obstacles);
// 更新障碍物位置
for (auto& obstacle : obstacles) {
obstacle.moveDown();
// 障碍物移出屏幕后重新生成
if (obstacle.y > 20) {
obstacle.x = rand() % 20;
obstacle.y = -5; // 重新生成在屏幕上方
}
// 检查碰撞
if (playerCar.x == obstacle.x && playerCar.y == obstacle.y) {
gameOver = true;
cout << "玩家赛车碰撞!" << endl;
break;
}
if (aiCar.x == obstacle.x && aiCar.y == obstacle.y) {
gameOver = true;
cout << "AI 赛车碰撞!" << endl;
break;
}
}
// 延迟
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
return 0;
}