- C++
c++MC
- @ 2026-10-17 10:55:25
#include <windows.h> #include <conio.h> #include <bits/stdc++.h> #include <time.h>
using namespace std; #define MIN(a,b) ((a)<(b)?(a):(b)) #define MAX(a,b) ((a)>(b)?(a):(b)) #define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1 : 0)
// ========== 无闪清屏 ========== void clearScreen() { HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); COORD home = {0,0}; DWORD written; FillConsoleOutputCharacter(hOut, ' ', 10030, home, &written); FillConsoleOutputAttribute(hOut, 7, 10030, home, &written); SetConsoleCursorPosition(hOut, home); } void initConsole() { HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE); COORD bufferSize = {100, 30}; SMALL_RECT windowSize = {0, 0, 99, 29}; SetConsoleScreenBufferSize(hOut, bufferSize); SetConsoleWindowInfo(hOut, TRUE, &windowSize); DWORD outMode, inMode; GetConsoleMode(hOut, &outMode); outMode |= DISABLE_NEWLINE_AUTO_RETURN; outMode &= ~ENABLE_WRAP_AT_EOL_OUTPUT; SetConsoleMode(hOut, outMode); GetConsoleMode(hIn, &inMode); inMode &= ~ENABLE_QUICK_EDIT_MODE; SetConsoleMode(hIn, inMode); CONSOLE_CURSOR_INFO cur = {1, FALSE}; SetConsoleCursorInfo(hOut, &cur); Sleep(50); } void gotoxy(int x, int y) { COORD c = {(SHORT)x, (SHORT)y}; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c); } void setColor(int a) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), a); } string getDesktopPath() { char path[MAX_PATH]; GetEnvironmentVariableA("USERPROFILE", path, MAX_PATH); return string(path) + "\Desktop\建议、改进.txt"; }
// ========== 彩蛋:RH触发,密码lanpiaopiao ========== bool easterEggUnlocked = false; bool hurt = false; char eggQueue[2] = {0}; // 检测R->H int eggPos = 0; DWORD lastRPress = 0; bool lastKeyState[256] = {false}; bool isKeyPressed(int vk) { bool now = KEY_DOWN(vk); bool pressed = now && !lastKeyState[vk]; lastKeyState[vk] = now; return pressed; } void checkEasterEgg(int key) { if (easterEggUnlocked) return; // R键按下记录时间 if (tolower(key) == 'r') { lastRPress = GetTickCount(); return; } // R之后1秒内按H触发密码框 if (tolower(key) == 'h' && GetTickCount() - lastRPress < 1000) { clearScreen(); gotoxy(0,15); setColor(12); cout << "请输入密码:"; CONSOLE_CURSOR_INFO cur = {1, TRUE}; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cur); string pwd; char c; while((c=getch())!='\r') { if(c=='\b'&&pwd.size()){pwd.pop_back();cout<<"\b \b";} else if(c>=32&&c<127){pwd+=c;cout<<"*";} } cur.bVisible = FALSE; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cur); if(pwd == "lanpiaopiao"){ easterEggUnlocked = true; gotoxy(0,17); setColor(10); cout << "解锁成功!开发者模式已开启"; Sleep(1500); }else{ gotoxy(0,17); setColor(12); cout << "密码错误"; Sleep(800); } lastRPress = 0; } }
// ========== 方块系统 ========== const int BLOCK_COUNT = 30; struct Block { string name, symbol; int color; bool isSolid; int hardness, dropId; } blocks[BLOCK_COUNT] = { {"空气"," ",7,false,0,0}, {"草方块","??",10,true,1,2}, {"泥土","..",6,true,1,2}, {"石头","[]",8,true,3,3}, {"原木","||",6,true,2,4}, {"树叶","%%",10,false,1,0}, {"沙子","::",14,true,1,6}, {"水","~~",9,false,0,0}, {"岩浆","MM",12,false,0,0}, {"TNT","TT",12,true,1,9}, {"玻璃","GG",15,true,1,10}, {"煤矿","oo",8,true,3,11}, {"铁矿","OO",7,true,4,12}, {"金矿","$$",14,true,4,13}, {"钻石矿","**",11,true,5,14}, {"工作台","WW",6,true,2,15}, {"箱子","XX",6,true,2,16}, {"梯子","HH",6,false,1,17}, {"床","BB",12,true,1,18}, {"门","DD",6,true,2,19}, {"圆石","CC",7,true,3,20}, {"雪块","SS",15,true,1,21}, {"铁块","II",7,true,5,22}, {"金块","@@",14,true,5,23}, {"钻石块","dd",11,true,5,24}, {"黑曜石","██",0,true,10,25}, {"火把","!!",14,false,0,26}, {"石砖","ZZ",7,true,4,27}, {"红砖","RR",4,true,3,28}, {"宝藏","★★",14,true,1,29}, };
// ========== 游戏状态 ========== const int MAP_SIZE = 600; const int VIEW_Y = 10, VIEW_X = 22; int GRAVITY=1, JUMP_SPEED=-5, MOVE_SPEED=1; int board[MAP_SIZE][MAP_SIZE]; int playerX, playerY, fallSpeed=0, health=100, hunger=100, breath=100, spawnX, spawnY; int inventory[9]={0}, selectedSlot=0; bool gameRunning=false, creativeMode=false, flying=false, flyToggle=false, onGround=false; int lastSpacePress=0, digCd=0, placeCd=0; int hungerTimer=0, breathTimer=0, lavaTimer=0; // 计时:每秒才扣数值,不会太快 DWORD lastFrameTime=0;
// ========== 菜单功能 ========== void showGuide() { clearScreen(); setColor(11); cout << "==================== 方块图鉴 "; setColor(7); cout << "\n符号 -> 名称 -> 属性\n\n"; for(int i=1;i<BLOCK_COUNT;i++){ setColor(blocks[i].color); cout << blocks[i].symbol; setColor(7); cout << " -> " << blocks[i].name; if(blocks[i].isSolid) cout << " [固体]"; if(i7) cout << " [水]"; if(i8) cout << " [岩浆]"; cout << "\n"; } setColor(8); cout << "\n按任意键返回..."; getch(); } void showHotkeys() { clearScreen(); setColor(11); cout << "==== 快捷键说明 \n\n"; setColor(7); cout << "W/空格:跳跃/飞/上浮 A/D:左右移动 S:下蹲/下降\n"; cout << "J:挖方块 K:放方块 1-9:切换物品栏\n"; cout << "E:开工作台合成 /:输入指令 双击空格:飞行开关 ESC:返回菜单\n"; if(easterEggUnlocked){setColor(13);cout<<"\n【彩蛋已解锁】开发者模式开启,无敌飞行全方块";} setColor(8); cout << "\n\n按任意键返回..."; getch(); } void submitSuggestion() { clearScreen(); setColor(11); cout << " 提交建议 \n\n"; setColor(7); cout << "输入建议回车提交:\n→ "; CONSOLE_CURSOR_INFO cur = {1, TRUE}; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cur); string sug; cin.ignore(); getline(cin, sug); ofstream f(getDesktopPath().c_str(), ios::app); if(f.is_open()){ SYSTEMTIME t; GetLocalTime(&t); f << "= "<<t.wYear<<"/"<<t.wMonth<<"/"<<t.wDay<<" "<<t.wHour<<":"<<t.wMinute<<" =\n"<<sug<<"\n\n"; f.close(); setColor(10); cout << "\n√ 已保存到桌面【建议、改进.txt】"; }else{ ofstream lf("建议、改进.txt",ios::app); lf<<sug<<"\n\n"; lf.close(); setColor(12); cout << "\n√ 已保存到当前目录"; } cur.bVisible = FALSE; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cur); setColor(8); cout << "\n按任意键返回..."; getch(); } void showDevInfo() { clearScreen(); setColor(11); cout << " 开发者说明 ==\n\n"; setColor(14); cout << "开发者ID:蓝飘飘\n"; setColor(7); cout << "版本:v7.0 RH彩蛋版\n"; setColor(13); cout << "\n感谢游玩!"; setColor(8); cout << "\n\n按任意键返回..."; getch(); } void runCommand(string cmd) { if(cmd.size()>=5 && cmd.substr(0,5)"/kill"){playerX=spawnX;playerY=spawnY;health=0;} else if(cmd.size()>=6 && cmd.substr(0,5)"/give"){ int id=atoi(cmd.substr(6).c_str()); if(id>=0&&id<BLOCK_COUNT) for(int i=0;i<9;i++) if(!inventory[i]){inventory[i]=id;break;} } else if(cmd.size()>=11 && cmd.substr(0,10)"/gamemode 1"){creativeMode=1;flying=1;health=hunger=breath=100;} else if(cmd.size()>=11 && cmd.substr(0,10)"/gamemode 0"){creativeMode=0;flying=0;} else if(cmd.size()>=4 && cmd.substr(0,4)"/fly") flying=!flying; else if(cmd.size()>=5 && cmd.substr(0,5)"/heal") health=hunger=breath=100; else if(cmd.size()>=6 && cmd.substr(0,6)"/spawn") {playerX=spawnX;playerY=spawnY;} else if(cmd.size()>=6 && cmd.substr(0,6)"/clear") memset(inventory,0,sizeof(inventory)); gotoxy(0,VIEW_Y*2+4); cout << " "; } void openCraft() { bool hasWB = false; for(int dy=-1;dy<=1;dy++) for(int dx=-1;dx<=1;dx++) if(board[playerY+dy][playerX+dx]15) hasWB=true; if(!hasWB) return; clearScreen(); setColor(11); cout << "= 工作台合成 ===\n"; setColor(7); cout << "1. 木板(1原木→4) 2.石砖(4石头→4) 0.返回\n→ "; int c=getch()-'0'; if(c1) for(int i=0;i<9;i++) if(inventory[i]4){inventory[i]=0;for(int j=0,k=0;j<9&&k<4;j++) if(!inventory[j]){inventory[j]=2;k++;}break;} if(c2){ int cnt=0; for(int i=0;i<9;i++) if(inventory[i]==3) cnt++; if(cnt>=4){ int rm=4; for(int i=0;i<9&&rm;i++) if(inventory[i]==3){inventory[i]=0;rm--;} int add=4; for(int i=0;i<9&&add;i++) if(!inventory[i]){inventory[i]=27;add--;} } } }
// ========== 地图初始化 ========== void initMap() { for(int i=0;i<MAP_SIZE;i++) for(int j=0;j<MAP_SIZE;j++) board[i][j]=0; srand((unsigned)time(NULL)); int lastY=400; for(int x=0;x<MAP_SIZE;x++){ int gy=lastY+rand()%7-3; gy=MAX(350,MIN(450,gy)); int dirt=rand()%3+2; for(int y=MAP_SIZE-1;y>=gy;y--){ if(ygy) board[y][x]=(gy<=360)?21:1; else if(y<gy+dirt) board[y][x]=2; else{ board[y][x]=3; if(y>480){ int r=rand()%100; if(r<15)board[y][x]=11;else if(r<25)board[y][x]=12; else if(r<28)board[y][x]=13;else if(r<29)board[y][x]=14; } } } if(board[gy-1][x]0&&rand()%201){ int h=rand()%3+4; for(int i=1;i<=h;i++) board[gy-i][x]=4; for(int dy=-2;dy<=0;dy++) for(int dx=-2;dx<=2;dx++) if(abs(dx)+abs(dy)<=3&&board[gy-h+dy][x+dx]0) board[gy-h+dy][x+dx]=5; } if(rand()%801){ int w=rand()%10+5; for(int lx=x;lx<x+w&&lx<MAP_SIZE;lx++) for(int ly=gy;ly<gy+4&&ly<MAP_SIZE;ly++) board[ly][lx]=7; } if(rand()%1001&&board[gy-1][x]0) board[gy-1][x]=15; if(easterEggUnlocked&&rand()%2001&&board[gy-1][x]==0) board[gy-1][x]=29; lastY=gy; } for(int y=300;y<500;y++) for(int x=100;x<MAP_SIZE-100;x++) if(y+1<MAP_SIZE&&board[y+1][x]&&blocks[board[y+1][x]].isSolid&&!board[y][x]){ spawnX=x;spawnY=y;playerX=x;playerY=y;return; } }
// ========== 玩家逻辑 ========== void updatePlayer() { hurt=false; if(digCd>0)digCd--; if(placeCd>0)placeCd--; hungerTimer++; breathTimer++; lavaTimer++; int face=1; if(KEY_DOWN('A')&&playerX-MOVE_SPEED>=1&&!blocks[board[playerY][playerX-MOVE_SPEED]].isSolid) {playerX-=MOVE_SPEED;face=-1;} if(KEY_DOWN('D')&&playerX+MOVE_SPEED<MAP_SIZE-1&&!blocks[board[playerY][playerX+MOVE_SPEED]].isSolid) {playerX+=MOVE_SPEED;face=1;}
if(isKeyPressed(VK_SPACE)){
int now=GetTickCount();
if(now-lastSpacePress<300&&(creativeMode||easterEggUnlocked)) flying=!flying;
lastSpacePress=now;
}
if(flying&&(creativeMode||easterEggUnlocked)){
if(KEY_DOWN(VK_SPACE)&&playerY>1) playerY--;
if(KEY_DOWN('S')&&playerY<MAP_SIZE-2) playerY++;
fallSpeed=0; onGround=false;
}else{
// 水物理:每秒减1氧气,不太快
if(board[playerY][playerX]==7){
if(breathTimer>=60) {breath--; breathTimer=0;}
fallSpeed=KEY_DOWN(VK_SPACE)?-1:MIN(1,fallSpeed+GRAVITY);
}else{
if(breathTimer>=30) {breath=MIN(100,breath+2); breathTimer=0;}
if(isKeyPressed(VK_SPACE) && onGround) {fallSpeed=JUMP_SPEED; onGround=false;}
fallSpeed+=GRAVITY;
}
// 岩浆:每秒掉5血
if(board[playerY][playerX]==8&&!creativeMode&&!easterEggUnlocked){
if(lavaTimer>=60) {health-=5; hurt=true; lavaTimer=0;}
fallSpeed=MIN(1,fallSpeed);
}
// 碰撞检测
onGround=false;
for(int i=1;i<=abs(fallSpeed);i++){
int ny=playerY+(fallSpeed>0?1:-1);
if(ny<1||ny>=MAP_SIZE-1){playerX=spawnX;playerY=spawnY;fallSpeed=0;break;}
if(blocks[board[ny][playerX]].isSolid){
onGround=true;
if(fallSpeed>4&&!creativeMode&&!easterEggUnlocked){health-=(fallSpeed-4)*10;hurt=true;}
fallSpeed=0; break;
}
playerY=ny;
}
if(!onGround)playerY+=fallSpeed;
}
playerX=MAX(VIEW_X+1,MIN(MAP_SIZE-VIEW_X-1,playerX));
playerY=MAX(VIEW_Y+1,MIN(MAP_SIZE-VIEW_Y-1,playerY));
for(int i=0;i<9;i++) if(isKeyPressed('1'+i)) selectedSlot=i;
// 挖方块:左右都能挖
if(KEY_DOWN('J')&&digCd<=0){
int dx=playerX+face, dy=playerY;
if(dx>=1&&dx<MAP_SIZE-1){
int bid=board[dy][dx];
if(blocks[bid].hardness&&(creativeMode||easterEggUnlocked||blocks[bid].hardness<=3)){
int drop=blocks[bid].dropId;
board[dy][dx]=0;
if(drop&&!creativeMode&&!easterEggUnlocked) for(int i=0;i<9;i++) if(!inventory[i]){inventory[i]=drop;break;}
}
}
digCd=(creativeMode||easterEggUnlocked)?5:15;
}
// 放方块
if(KEY_DOWN('K')&&placeCd<=0){
int dx=playerX+face, dy=playerY;
if(dx>=1&&dx<MAP_SIZE-1&&!board[dy][dx]&&inventory[selectedSlot]){
board[dy][dx]=inventory[selectedSlot];
if(!creativeMode&&!easterEggUnlocked) inventory[selectedSlot]=0;
}
placeCd=10;
}
// 彩蛋功能
if(isKeyPressed('U')&&easterEggUnlocked)
for(int i=0;i<20;i++){int rx=playerX+rand()%21-10,ry=playerY-10;if(ry>=1&&rx>=1&&rx<MAP_SIZE-1)board[ry][rx]=9;}
if(isKeyPressed('I')&&easterEggUnlocked)
for(int dy=-15;dy<=5;dy++)for(int dx=-15;dx<=15;dx++)
if(playerY+dy>=1&&playerY+dy<MAP_SIZE-1&&playerX+dx>=1&&playerX+dx<MAP_SIZE-1&&abs(dx)+abs(dy)<=15)
board[playerY+dy][playerX+dx]=0;
if(KEY_DOWN('O')&&easterEggUnlocked&&playerY+1<MAP_SIZE-1) board[playerY+1][playerX]=24;
if(isKeyPressed('E')) openCraft();
// 指令输入
if(isKeyPressed('/')){
gotoxy(0,VIEW_Y*2+4); setColor(7); cout << "指令: ";
gotoxy(6,VIEW_Y*2+4);
CONSOLE_CURSOR_INFO cur = {1, TRUE};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cur);
string cmd; char c;
while((c=getch())!='\r'){
if(c=='\b'&&cmd.size()){cmd.pop_back();cout<<"\b \b";}
else if(c>=32&&c<127){cmd+=c;cout<<c;}
}
runCommand(cmd);
cur.bVisible = FALSE;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cur);
}
if(isKeyPressed(VK_ESCAPE)) gameRunning=0;
// 状态更新:饥饿每秒减1,不会太快
if(!creativeMode&&!easterEggUnlocked){
if(hungerTimer>=60){
hunger--;
hungerTimer=0;
}
if(hunger<=0){health--;hurt=true;}
if(breath<=0){health-=2;hurt=true;}
if(health<=0){playerX=spawnX;playerY=spawnY;health=100;hunger=100;breath=100;fallSpeed=0;}
health=MIN(100,health+(hunger>80?1:0));
}else health=hunger=breath=100;
}
void render() { gotoxy(0,0); setColor(7); cout << "HP:"; setColor(12); cout << health; setColor(7); cout << " 饥饿:"; setColor(14); cout << hunger; setColor(7); cout << " 氧气:"; setColor(11); cout << breath; if(creativeMode||easterEggUnlocked){setColor(10);cout<<" [创造]";} if(flying){setColor(11);cout<<" [飞行]";} cout << " \n"; for(int dy=-VIEW_Y;dy<=VIEW_Y;dy++){ for(int dx=-VIEW_X;dx<=VIEW_X;dx++){ int y=playerY+dy, x=playerX+dx; if(y<1||y>=MAP_SIZE-1||x<1||x>=MAP_SIZE-1){setColor(0);cout<<" ";continue;} if(dy0&&dx0){setColor(hurt?12:9);cout<<"MP";} else{int bid=board[y][x];setColor(blocks[bid].color);cout<<blocks[bid].symbol;} } cout << " \n"; } setColor(7); cout << "物品栏:"; for(int i=0;i<9;i++){ if(i==selectedSlot)setColor(14);else setColor(7); cout<<"["<<i+1<<"]"; if(inventory[i]&&inventory[i]<BLOCK_COUNT){setColor(blocks[inventory[i]].color);cout<<blocks[inventory[i]].symbol;} else cout<<" "; } if(inventory[selectedSlot]&&inventory[selectedSlot]<BLOCK_COUNT) cout<<" 当前:"<<blocks[inventory[selectedSlot]].name; }
void startGame() { clearScreen(); memset(lastKeyState,0,sizeof(lastKeyState)); hungerTimer=0; breathTimer=0; lavaTimer=0; initMap(); inventory[0]=3; inventory[1]=2; if(easterEggUnlocked){ for(int i=0;i<9;i++)inventory[i]=i+1; creativeMode=1; } else creativeMode=0; flying=false; gameRunning=1; fallSpeed=0; onGround=false; lastFrameTime=GetTickCount(); while(gameRunning){ DWORD t=GetTickCount(); if(t<lastFrameTime) lastFrameTime=t; if(t-lastFrameTime<16) {Sleep(1);continue;} lastFrameTime=t; updatePlayer(); render(); } }
void mainMenu() { memset(lastKeyState,0,sizeof(lastKeyState)); lastRPress=0; while(1){ clearScreen(); setColor(10); cout << " _____ _ __ __ _____ \n"; cout << " / | () \/ |/ |\n"; cout << "| | ___ _ __ ___ ___ | \ / | | \n"; cout << "| | / _ \| ' \/ |/ _ \ | |\/| | | \n"; cout << "| || () | | | \ \ / | | | | | \n"; cout << " \__\/|| ||/\||| ||\_____|\n"; setColor(7); cout << "================ 控制台MC 蓝飘飘版 ================\n\n"; setColor(11); cout << "请选择功能:\n"; setColor(7); cout << "1. 开始游戏\n2. 方块图鉴\n3. 快捷键说明\n4. 提交建议\n5. 开发者说明\n0. 退出\n"; if(easterEggUnlocked){setColor(13);cout<<"★ 开发者模式已解锁";} cout << "\n→ "; while(!kbhit()) Sleep(1); char c=getch(); for(int i=0;i<256;i++) lastKeyState[i] = KEY_DOWN(i); checkEasterEgg(c); if(c>='0'&&c<='5'){ int ch=c-'0'; switch(ch){ case 1: startGame(); break; case 2: showGuide(); break; case 3: showHotkeys(); break; case 4: submitSuggestion(); break; case 5: showDevInfo(); break; case 0: exit(0); } } } }
int main() { initConsole(); mainMenu(); return 0; }