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

张开发
2026/4/20 4:51:20 15 分钟阅读

分享文章

C++ 控制台跑酷小游戏2.0
下面给你大幅美化画质 多关卡系统 多种障碍物 道具金币保留 更流畅动画的完整版 C 控制台跑酷。风格更精致、分层地面、天空、云朵、多地形、关卡 BOSS 障碍、渐变难度代码完整可直接编译运行。C 控制台跑酷小游戏2.0#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; vectorObstacle obs; vectorItem items; vectorCloud 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 SPACEJUMP PPAUSE RRESTART ESCEXIT; } 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 ESCQUIT; } 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 cols70 lines24); system(title C 控制台跑酷小游戏2.0); ParkourGame g; g.run(); return 0; }本次升级内容画质 关卡 障碍物一、画质大幅美化蓝色天空背景动态白云飘动双层美化地面~角色更精致四肢O / | \彩色状态条、边框式游戏结束界面整体分层更像真正小游戏二、多关卡 等级系统LV1~LV∞自动升级每级所需分数提高升级时显示LEVEL UP等级越高障碍更密、速度更快、种类更多三、5 种障碍物小方块—— 基础障碍长障碍—— 占两格更难躲避高障碍—— 两层高必须跳更高尖刺—— 红色危险伤害更高滚动障碍—— 圆形快速障碍四、完整道具系统保留$金币加币 加分S护盾免疫一次碰撞2双倍分数得分翻倍R跑鞋加速模式五、操作空格跳跃P暂停 / 继续R游戏结束后重开ESC退出

更多文章