C++ 控制台跑酷小游戏2.0

下面给你大幅美化画质 + 多关卡系统 + 多种障碍物 + 道具金币保留 + 更流畅动画的完整版 C++ 控制台跑酷。风格更精致、分层地面、天空、云朵、多地形、关卡 BOSS 障碍、渐变难度,代码完整可直接编译运行。

C++ 控制台跑酷小游戏2.0

cpp 复制代码
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;

void gotoxy(int x, int y) {
    COORD pos = { (SHORT)x, (SHORT)y };
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}

void hideCursor() {
    CONSOLE_CURSOR_INFO ci = { 1, 0 };
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &ci);
}

void color(int c) {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c);
}

struct Obstacle {
    int x, y;
    int type; // 0=小方块 1=长障碍 2=高障碍 3=尖刺 4=滚动障碍
};

struct Item {
    int x, y;
    int type; // 0=金币 1=护盾 2=双倍 3=跑鞋
};

struct Cloud {
    int x, y;
    int len;
};

class ParkourGame {
private:
    const int W = 70, H = 22;
    const int GROUND = 18;
    const int JUMP_H = 7;
    const int GRAVITY = 1;

    int px, py;
    bool jump;
    int jmpVel;

    vector<Obstacle> obs;
    vector<Item> items;
    vector<Cloud> clouds;

    int score, coin, level, life;
    bool shield, doubleScd, speedUp;
    int sTime, dTime, spTime;

    bool gameOver, pause;
    int obsSpd, spawnFreq;
    int frame;

    void drawSky() {
        color(1);
        for (int i = 0; i < W; i++) {
            for (int j = 0; j < 6; j++) {
                gotoxy(i, j); cout << " ";
            }
        }
    }

    void drawCloud() {
        color(15);
        for (auto& c : clouds) {
            gotoxy(c.x, c.y);
            for (int i = 0; i < c.len; i++) cout << "~";
        }
    }

    void moveCloud() {
        for (auto& c : clouds) c.x--;
        if (!clouds.empty() && clouds[0].x < -5)
            clouds.erase(clouds.begin());
        if (frame % 60 == 0) {
            clouds.push_back({ W, rand() % 4 + 1, rand() % 4 + 3 });
        }
    }

    void drawGround() {
        color(2);
        for (int i = 0; i < W; i++) {
            gotoxy(i, GROUND); cout << "=";
            gotoxy(i, GROUND + 1); cout << "~";
        }
    }

    void drawPlayer() {
        color(shield ? 3 : 14);
        gotoxy(px, py);   cout << "O";
        gotoxy(px, py + 1); cout << "|";
        gotoxy(px - 1, py + 1); cout << "/";
        gotoxy(px + 1, py + 1); cout << "\\";
    }

    void clearPlayer() {
        for (int dx = -1; dx <= 1; dx++) {
            for (int dy = 0; dy <= 1; dy++) {
                gotoxy(px + dx, py + dy); cout << " ";
            }
        }
    }

    void drawObs() {
        for (auto& o : obs) {
            if (o.x < 0) continue;
            switch (o.type) {
            case 0: color(12); gotoxy(o.x, o.y); cout << "■"; break;
            case 1: color(12); gotoxy(o.x, o.y); cout << "■■"; break;
            case 2: color(13); gotoxy(o.x, o.y); cout << "■"; gotoxy(o.x, o.y - 1); cout << "■"; break;
            case 3: color(4); gotoxy(o.x, o.y); cout << "▲"; break;
            case 4: color(6); gotoxy(o.x, o.y); cout << "●"; break;
            }
        }
    }

    void clearObs() {
        for (auto& o : obs) {
            if (o.x >= 0) {
                if (o.type == 1) { gotoxy(o.x, o.y); cout << "  "; }
                else if (o.type == 2) { gotoxy(o.x, o.y); cout << " "; gotoxy(o.x, o.y - 1); cout << " "; }
                else { gotoxy(o.x, o.y); cout << " "; }
            }
        }
    }

    void drawItems() {
        for (auto& it : items) {
            if (it.x < 0) continue;
            switch (it.type) {
            case 0: color(14); gotoxy(it.x, it.y); cout << "$"; break;
            case 1: color(9);  gotoxy(it.x, it.y); cout << "S"; break;
            case 2: color(13); gotoxy(it.x, it.y); cout << "2"; break;
            case 3: color(11); gotoxy(it.x, it.y); cout << "R"; break;
            }
        }
    }

    void clearItems() {
        for (auto& it : items) if (it.x >= 0) { gotoxy(it.x, it.y); cout << " "; }
    }

    void drawInfo() {
        color(15);
        gotoxy(2, 1); cout << "LEVEL " << level;
        gotoxy(12, 1); cout << "SCORE: " << score;
        gotoxy(25, 1); cout << "COIN: " << coin;
        gotoxy(36, 1); cout << "LIFE: " << life;
        if (shield)   { color(9);  gotoxy(2, 2); cout << "[SHIELD] "; }
        if (doubleScd){color(13); gotoxy(10,2); cout << "[x2 SCORE]";}
        if (speedUp) { color(11); gotoxy(22,2); cout << "[SPEED UP]"; }
    }

    void drawTips() {
        color(11);
        gotoxy(2, 3); cout << "SPACE=JUMP  P=PAUSE  R=RESTART  ESC=EXIT";
    }

    void drawGameOver() {
        color(13);
        int cx = W / 2 - 6;
        int cy = H / 2 - 2;
        gotoxy(cx, cy);     cout << "=====================";
        gotoxy(cx, cy + 1); cout << "     GAME OVER       ";
        gotoxy(cx, cy + 2); cout << "  SCORE: " << score << "   LV:" << level;
        gotoxy(cx, cy + 3); cout << "  COIN: " << coin;
        gotoxy(cx, cy + 4); cout << "=====================";
        gotoxy(cx, cy + 5); cout << "  R = REPLAY   ESC=QUIT";
    }

    void drawPause() {
        color(14);
        gotoxy(W / 2 - 3, H / 2); cout << "PAUSED";
        gotoxy(W / 2 - 7, H / 2 + 1); cout << "P = CONTINUE";
    }

    void spawnObs() {
        if (frame % spawnFreq == 0) {
            int t = rand() % 20;
            int ty = GROUND - 1;
            if (level >= 3) ty -= rand() % 2;
            if (t < 8)  obs.push_back({ W, ty, 0 });
            else if (t < 12) obs.push_back({ W, ty, 1 });
            else if (t < 15 && level >= 2) obs.push_back({ W, ty - 1, 2 });
            else if (t < 18 && level >= 4) obs.push_back({ W, ty, 3 });
            else obs.push_back({ W, ty, 4 });
        }
    }

    void spawnItem() {
        if (rand() % 100 == 0) {
            Item it;
            it.x = W;
            it.y = GROUND - rand() % 6 - 2;
            int r = rand() % 10;
            if (r < 5) it.type = 0;
            else if (r < 7) it.type = 1;
            else if (r < 9) it.type = 2;
            else it.type = 3;
            items.push_back(it);
        }
    }

    void moveObs() {
        for (auto& o : obs) o.x -= obsSpd;
        if (!obs.empty() && obs[0].x < -3) {
            obs.erase(obs.begin());
            score += doubleScd ? 2 : 1;
        }
    }

    void moveItems() {
        for (auto& it : items) it.x -= obsSpd;
        if (!items.empty() && items[0].x < -1)
            items.erase(items.begin());
    }

    void itemCollide() {
        for (int i = 0; i < items.size(); i++) {
            auto& t = items[i];
            if ((t.x == px || t.x == px - 1 || t.x == px + 1) && abs(t.y - py) < 2) {
                switch (t.type) {
                case 0: coin++; score += doubleScd ? 5 : 2; break;
                case 1: shield = 1; sTime = 400; break;
                case 2: doubleScd = 1; dTime = 600; break;
                case 3: speedUp = 1; spTime = 500; break;
                }
                items.erase(items.begin() + i);
                break;
            }
        }
    }

    bool checkCollide() {
        for (auto& o : obs) {
            int bx = o.x, by = o.y;
            int bw = (o.type == 1) ? 2 : 1;
            int bh = (o.type == 2) ? 2 : 1;
            bool xok = (px >= bx - 1 && px <= bx + bw);
            bool yok = (py >= by - bh && py <= by + 1);
            if (xok && yok) return true;
        }
        return false;
    }

    void doJump() {
        if (jump) {
            clearPlayer();
            py -= jmpVel;
            jmpVel -= GRAVITY;
            if (py >= GROUND - 1) {
                py = GROUND - 1;
                jump = false;
                jmpVel = JUMP_H;
            }
        }
    }

    void updateBuff() {
        if (shield && --sTime <= 0) shield = 0;
        if (doubleScd && --dTime <= 0) doubleScd = 0;
        if (speedUp && --spTime <= 0) speedUp = 0;
    }

    void levelCheck() {
        int need = level * 25;
        if (score >= need) {
            level++;
            if (obsSpd < 4) obsSpd++;
            if (spawnFreq > 7) spawnFreq--;
            color(10);
            gotoxy(W / 2 - 6, H / 2 - 1); cout << "LEVEL UP! " << level;
            Sleep(300);
            gotoxy(W / 2 - 6, H / 2 - 1); cout << "             ";
        }
    }

    void input() {
        if (_kbhit()) {
            char k = _getch();
            if (k == ' ' && !jump && !gameOver && !pause) jump = 1;
            if (k == 'P' || k == 'p') pause = !pause;
            if ((k == 'R' || k == 'r') && gameOver) init();
            if (k == 27) { system("cls"); exit(0); }
        }
    }

public:
    ParkourGame() { hideCursor(); srand(time(0)); init(); }

    void init() {
        px = 6; py = GROUND - 1; jump = 0; jmpVel = JUMP_H;
        obs.clear(); items.clear(); clouds.clear();
        score = coin = 0; level = 1; life = 2;
        shield = doubleScd = speedUp = 0;
        sTime = dTime = spTime = 0;
        obsSpd = 1; spawnFreq = 18;
        gameOver = pause = 0; frame = 0;
        system("cls");
        drawSky(); drawGround(); drawTips();
    }

    void run() {
        while (1) {
            input();
            if (pause) { drawPause(); Sleep(50); continue; }
            if (gameOver) { drawGameOver(); Sleep(50); continue; }

            frame++;
            clearPlayer(); clearObs(); clearItems();

            moveCloud();
            doJump();
            spawnObs(); spawnItem();
            moveObs(); moveItems();
            itemCollide();
            updateBuff();
            levelCheck();

            if (checkCollide()) {
                if (shield) shield = 0;
                else {
                    life--;
                    if (life <= 0) gameOver = 1;
                    Sleep(200);
                }
            }

            drawSky();
            drawCloud();
            drawPlayer();
            drawObs();
            drawItems();
            drawInfo();
            drawGround();

            Sleep(speedUp ? 18 : 32);
        }
    }
};

int main() {
    system("mode con cols=70 lines=24");
    system("title C++ 控制台跑酷小游戏2.0");
    ParkourGame g;
    g.run();
    return 0;
}

本次升级内容(画质 + 关卡 + 障碍物)

一、画质大幅美化

  • 蓝色天空背景
  • 动态白云飘动
  • 双层美化地面 = + ~
  • 角色更精致:四肢 O / | \
  • 彩色状态条、边框式游戏结束界面
  • 整体分层更像真正小游戏

二、多关卡 & 等级系统

  • LV1~LV∞ 自动升级
  • 每级所需分数提高
  • 升级时显示 LEVEL UP
  • 等级越高:障碍更密、速度更快、种类更多

三、5 种障碍物

  1. 小方块 ------ 基础障碍
  2. 长障碍 ------ 占两格,更难躲避
  3. 高障碍 ------ 两层高,必须跳更高
  4. 尖刺 ------ 红色危险,伤害更高
  5. 滚动障碍 ------ 圆形快速障碍

四、完整道具系统保留

  • $ 金币:加币 + 加分
  • S 护盾:免疫一次碰撞
  • 2 双倍分数:得分翻倍
  • R 跑鞋:加速模式

五、操作

  • 空格:跳跃
  • P:暂停 / 继续
  • R:游戏结束后重开
  • ESC:退出
相关推荐
Huangxy__2 小时前
java相机手搓(后续是文件保存以及接入大模型)
java·开发语言·数码相机
刚子编程2 小时前
C#事务处理最佳实践:别再让“主表存了、明细丢了”的破事发生
开发语言·c#·事务处理·trycatch
lsx2024062 小时前
jEasyUI 自定义对话框
开发语言
陶然同学2 小时前
【Python】文件操作
开发语言·python
Wild_Pointer.2 小时前
C++:内存顺序(Memory Order)的概念以及使用
c++
来自远方的老作者2 小时前
第10章 面向对象-10.3 封装
开发语言·python·私有属性·私有方法·封装
j_xxx404_2 小时前
用系统调用从零封装一个C语言标准I/O库 | 附源码
linux·c语言·开发语言·后端
并不喜欢吃鱼2 小时前
从零开始C++----七.继承相关模型,解析多继承与菱形继承问题(下篇)
开发语言·c++
devil-J2 小时前
vue3+three.js中国3D地图
开发语言·javascript·3d