下面是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双倍积分:得分 ×2R跑鞋:游戏速度变快,躲避更刺激
3. 等级系统
- 等级随分数自动提升
- 等级越高:障碍物移动越快、刷新越密集
- 游戏节奏循序渐进
4. 生命与护盾
- 初始 1 点生命
- 护盾期间碰撞不会死亡
- 界面实时显示护盾 / 双倍 / 加速状态
5. 操作
- 空格:跳跃
- P:暂停 / 继续
- R:游戏结束后重开
- ESC:退出