一个非常简单的“跑酷”游戏
2026-02-03 13:37:56
发布于:上海
// 一个非常简单的“跑酷”游戏
#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() {
stdsrand(stdtime(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;
}
全部评论 2
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <windows.h>using namespace std;
const int WIDTH = 30; // 屏幕宽度
const int GAME_LENGTH = 80;
const int GROUND_Y = 8;enum class ObstacleType { None, Small, Large };
struct Obstacle {
ObstacleType type;
int x;
};// 清屏
void clearScreen() {
system("cls");
}// 设置光标位置
void gotoxy(int x, int y) {
COORD coord = { static_cast<SHORT>(x), static_cast<SHORT>(y) };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}// 隐藏光标
void hideCursor() {
CONSOLE_CURSOR_INFO info = { 100, FALSE };
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
}int main() {
srand((unsigned)time(nullptr));
hideCursor();vector<Obstacle> obstacles(GAME_LENGTH); for (int i = 0; i < GAME_LENGTH; ++i) { if (rand() % 4 == 0) { obstacles[i].type = (rand() % 2 == 0) ? ObstacleType::Small : ObstacleType::Large; } else { obstacles[i].type = ObstacleType::None; } obstacles[i].x = i; } int playerX = 3; int playerY = GROUND_Y; bool jumping = false; bool crouching = false; int jumpHeight = 0; int frame = 0; bool running = true; while (running && frame + WIDTH < GAME_LENGTH) { // ===== 输入 ===== if (_kbhit()) { char ch = _getch(); if (ch == ' ') { if (!jumping && !crouching) jumping = true; } else if (ch == 17) { // Left Ctrl if (!jumping && !crouching) crouching = true; } else if (ch == 'q') { running = false; } } // ===== 物理 ===== if (jumping) { playerY -= 1; jumpHeight++; if (jumpHeight > 3) jumping = false; } else if (crouching) { playerY = GROUND_Y + 1; } else { playerY = GROUN2026-07-31 来自 四川
0亖亖亖亖亖
2026-07-31 来自 四川
0亖亖亖亖亖
4天前 来自 上海
0

























有帮助,赞一个