全部评论 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 = GROUN
    

    2026-07-31 来自 四川

    0
  • 亖亖亖亖亖

    2026-07-31 来自 四川

    0

热门讨论