跑酷游戏
2024-12-20 17:23:24
发布于:北京
J键移动,C键蹲下
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
// 一个非常简单的“跑酷”游戏
class Obstacle {
public:
char type; // 'S' for small, 'L' for large
int position; // 0 = start, 1 = middle, 2 = end
Obstacle(char t, int p) : type(t), position(p) {}
};
int main() {
std::srand(std::time(nullptr)); // 用当前时间作为随机数生成器的种子
std::vector<Obstacle> obstacles; // 存储障碍物
int gameLength = 10; // 游戏长度
int playerPosition = 0; // 玩家位置
bool isRunning = true; // 游戏是否进行中
// 生成障碍物
for (int i = 0; i < gameLength; ++i) {
if (i % 3 == 0) { // 每三个位置生成一个障碍物
char type = (std::rand() % 2 == 0) ? 'S' : 'L';
int position = std::rand() % 3;
obstacles.push_back(Obstacle(type, position));
}
}
std::cout << "Welcome to the Parkour Game!" << std::endl;
std::cout << "Use 'j' to jump and 'c' to crouch." << std::endl;
while (isRunning && playerPosition < gameLength) {
std::cout << "You are at position " << playerPosition << ". What's next? (j/c/q): ";
char action;
std::cin >> action;
if (action == 'q') {
isRunning = false;
} else {
bool hasObstacle = !obstacles.empty() && obstacles.front().position == playerPosition % 3;
if (hasObstacle) {
Obstacle obstacle = obstacles.front();
obstacles.erase(obstacles.begin());
if (obstacle.type == 'S' && action == 'j') {
std::cout << "You jumped over a small obstacle!" << std::endl;
} else if (obstacle.type == 'L' && action == 'j') {
std::cout << "You can't jump over a large obstacle. Game Over!" << std::endl;
isRunning = false;
} else if (action == 'c') {
std::cout << "You crouched under an obstacle!" << std::endl;
} else {
std::cout << "You hit an obstacle. Game Over!" << std::endl;
isRunning = false;
}
}
if (isRunning) {
playerPosition++; // 移动到下一个位置
}
}
}
if (playerPosition >= gameLength) {
std::cout << "Congratulations! You finished the parkour!" << std::endl;
} else {
std::cout << "Game Over!" << std::endl;
}
return 0;
}
全部评论 1
会报错
1周前 来自 浙江
0
有帮助,赞一个