C++ 控制台跑酷小游戏

下面是C++ 控制台跑酷游戏,功能:

  • 金币系统(拾取加分)
  • 道具系统(无敌护盾、双倍积分、加速跑鞋)
  • 等级系统(等级提升、难度自动变强)
  • 生命值 / 护盾显示
  • 更长的游戏体验,结构完整,代码约 5500 字节左右
cpp 复制代码
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <string>

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 setColor(int c) {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c);
}

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

class ParkourGame {
private:
    const int W = 60, H = 20;
    const int GROUND = 16;
    const int JUMP_H = 6;
    const int GRAVITY = 1;

    int px, py;
    bool jumping;
    int jumpVel;

    vector<int> obsX, obsY;
    vector<Item> items;

    int score;
    int level;
    int coin;
    int life;
    bool shield;
    bool doubleScore;
    bool speedUp;
    int shieldTime, doubleTime, speedTime;

    bool gameOver;
    bool pause;
    int obsSpeed;
    int spawnRate;
    int frame;

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

    void drawPlayer() {
        setColor(shield ? 9 : 14);
        gotoxy(px, py); cout << "O";
        gotoxy(px, py+1); cout << "|";
    }

    void clearPlayer() {
        gotoxy(px, py); cout << " ";
        gotoxy(px, py+1); cout << " ";
    }

    void drawObs() {
        setColor(12);
        for(int i=0; i<obsX.size(); i++)
            if(obsX[i]>=0) { gotoxy(obsX[i], obsY[i]); cout << "■"; }
    }

    void clearObs() {
        for(int i=0; i<obsX.size(); i++)
            if(obsX[i]>=0) { gotoxy(obsX[i], obsY[i]); cout << "  "; }
    }

    void drawItems() {
        for(auto &it : items) {
            if(it.x<0) continue;
            switch(it.type) {
                case 0: setColor(14); gotoxy(it.x,it.y); cout<<"$"; break;
                case 1: setColor(9);  gotoxy(it.x,it.y); cout<<"S"; break;
                case 2: setColor(13); gotoxy(it.x,it.y); cout<<"2"; break;
                case 3: setColor(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() {
        setColor(15);
        gotoxy(W-18,1); cout << "Lv:"<<level;
        gotoxy(W-12,1); cout << "Score:"<<score;
        gotoxy(W-18,2); cout << "Coin:"<<coin;
        gotoxy(W-12,2); cout << "Life:"<<life;
        if(shield)     { setColor(9);  gotoxy(2,1); cout<<"[SHIELD] "; }
        if(doubleScore){setColor(13); gotoxy(9,1); cout<<"[x2] "; }
        if(speedUp)    {setColor(11); gotoxy(14,1);cout<<"[SPEED] ";}
    }

    void drawTips() {
        setColor(11);
        gotoxy(2,3); cout << "Space=Jump  P=Pause  R=Restart  ESC=Exit";
    }

    void gameOverUI() {
        setColor(13);
        int cx=W/2-6, cy=H/2;
        gotoxy(cx,cy);   cout << "GAME OVER";
        gotoxy(cx-4,cy+1);cout<<"Final Score:"<<score;
        gotoxy(cx-6,cy+2);cout<<"Lv:"<<level<<"  Coin:"<<coin;
        gotoxy(cx-7,cy+3);cout<<"R=Restart  ESC=Exit";
    }

    void pauseUI() {
        setColor(13);
        gotoxy(W/2-3, H/2); cout << "PAUSE";
        gotoxy(W/2-6, H/2+1);cout<<"P to continue";
    }

    void spawnObs() {
        if(frame % spawnRate == 0) {
            obsX.push_back(W-2);
            obsY.push_back(GROUND);
        }
    }

    void spawnItem() {
        if(rand()%120 == 0) {
            Item it;
            it.x = W-2;
            it.y = GROUND - rand()%5 - 2;
            it.type = rand()%20;
            if(it.type<=10) it.type=0;
            else if(it.type<=14)it.type=1;
            else if(it.type<=17)it.type=2;
            else it.type=3;
            items.push_back(it);
        }
    }

    void moveObs() {
        for(int &x : obsX) x -= obsSpeed;
        if(!obsX.empty() && obsX[0]<0) {
            obsX.erase(obsX.begin());
            obsY.erase(obsY.begin());
            score += doubleScore ? 2 : 1;
        }
    }

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

    void checkItemCollide() {
        for(int i=0; i<items.size(); i++) {
            auto &it=items[i];
            if((it.x==px || it.x==px+1) && abs(it.y-py)<=1) {
                switch(it.type) {
                    case 0: coin++; score+=doubleScore?4:2; break;
                    case 1: shield=1; shieldTime=300; break;
                    case 2: doubleScore=1; doubleTime=500; break;
                    case 3: speedUp=1; speedTime=400; break;
                }
                gotoxy(it.x,it.y);cout<<" ";
                items.erase(items.begin()+i);
                break;
            }
        }
    }

    bool checkHit() {
        for(int i=0; i<obsX.size(); i++) {
            int x=obsX[i], y=obsY[i];
            if((px==x||px==x+1) && py>=y-1 && py<=y+1)
                return true;
        }
        return false;
    }

    void doJump() {
        if(jumping) {
            clearPlayer();
            py -= jumpVel;
            jumpVel -= GRAVITY;
            if(py >= GROUND) {
                py = GROUND;
                jumping = false;
                jumpVel = JUMP_H;
            }
        }
    }

    void updateBuff() {
        if(shield && --shieldTime<=0) shield=0;
        if(doubleScore && --doubleTime<=0) doubleScore=0;
        if(speedUp && --speedTime<=0) speedUp=0;
    }

    void levelUp() {
        if(score > level*15) {
            level++;
            if(obsSpeed<4) obsSpeed++;
            if(spawnRate>8) spawnRate--;
        }
    }

    void input() {
        if(_kbhit()) {
            char k=_getch();
            if(k==' ' && !jumping && !gameOver && !pause) jumping=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=5; py=GROUND; jumping=0; jumpVel=JUMP_H;
        obsX.clear(); obsY.clear(); items.clear();
        score=0; coin=0; level=1; life=1;
        shield=0; doubleScore=0; speedUp=0;
        shieldTime=doubleTime=speedTime=0;
        obsSpeed=1; spawnRate=20;
        gameOver=0; pause=0; frame=0;
        system("cls");
        drawGround(); drawTips();
    }

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

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

            doJump();
            spawnObs(); spawnItem();
            moveObs(); moveItems();
            checkItemCollide();
            updateBuff();
            levelUp();

            if(checkHit()) {
                if(shield) { shield=0; }
                else { life--; if(life<=0) gameOver=1; }
            }

            drawPlayer(); drawObs(); drawItems(); drawInfo();
            Sleep(speedUp ? 15 : 30);
        }
    }
};

int main() {
    system("mode con cols=60 lines=22");
    system("title C++跑酷 · 金币+道具+等级系统");
    ParkourGame g;
    g.run();
    return 0;
}

游戏内容说明

1. 金币系统

  • 金币 $ 拾取后增加金币数量
  • 金币也会额外增加分数

2. 道具系统(吃到对应字母触发)

  • $ 金币:+ 分数 + 金币
  • S 护盾:一段时间内碰撞不损失生命
  • 2 双倍积分:得分 ×2
  • R 跑鞋:游戏速度变快,躲避更刺激

3. 等级系统

  • 等级随分数自动提升
  • 等级越高:障碍物移动越快、刷新越密集
  • 游戏节奏循序渐进

4. 生命与护盾

  • 初始 1 点生命
  • 护盾期间碰撞不会死亡
  • 界面实时显示护盾 / 双倍 / 加速状态

5. 操作

  • 空格:跳跃
  • P:暂停 / 继续
  • R:游戏结束后重开
  • ESC:退出
相关推荐
周末也要写八哥2 小时前
C++实际开发之泛型编程(模版编程)
java·开发语言·c++
好家伙VCC2 小时前
**发散创新:用 Rust实现游戏日引擎核心模块——从事件驱动到多线程调度的实战
java·开发语言·python·游戏·rust
兵哥工控2 小时前
MFC中return和break用法示例
c++·mfc
2401_841495643 小时前
Linux C++ TCP 服务端经典的监听骨架
linux·网络·c++·网络编程·ip·tcp·服务端
春栀怡铃声3 小时前
【C++修仙录02】筑基篇:类和对象(中)
c++
楼田莉子4 小时前
同步/异步日志系统:日志器管理器模块\全局接口\性能测试
linux·服务器·开发语言·c++·后端·设计模式
故事和你914 小时前
洛谷-数据结构-1-3-集合3
数据结构·c++·算法·leetcode·贪心算法·动态规划·图论
春栀怡铃声4 小时前
【C++修仙录02】筑基篇:类和对象(上)
开发语言·c++·算法
ulias2124 小时前
leetcode热题 - 3
c++·算法·leetcode·职场和发展