- 分享
游戏代码(下)
- @ 2026-7-23 22:27:37
return "边界无法放置方块";
if(map[by][bx] == ' ')
{
if(player.stone < 1)
return "石头不足,无法放置方块";
player.stone--;
map[by][bx] = '#';
return "成功放置石头方块";
}
else if(map[by][bx] == '#')
{
map[by][bx] = ' ';
player.stone++;
return "拆除石头方块,回收1石头";
}
return "此处无法放置方块";
}
string SpawnDoor() { int dirs[4][2] = {{0,1},{0,-1},{-1,0},{1,0}}; for(int d=0; d<4; d++) { int tx = player.x + dirs[d][0]; int ty = player.y + dirs[d][1]; if(tx <=0 || tx >= MAP_WIDTH-1 || ty <=0 || ty >= MAP_HEIGHT-1) continue; if(map[ty][tx] == ' ') { if(player.wood < 2) return "木头不足,放置门需要2木头"; player.wood -=2; map[ty][tx] = '+'; return "四周空地放置关闭木门"; } } return "上下左右无空地可放置门"; }
string ToggleDoor() { char tile = map[player.y+1][player.x]; if(tile == '+') { map[player.y+1][player.x] = '-'; return "门已打开,自由通行"; } else if(tile == '-') { map[player.y+1][player.x] = '+'; return "门已关闭,阻挡怪物子弹"; } return "你没有站在门上"; }
string RemoveDoor() { char tile = map[player.y+1][player.x]; if(tile == '+' || tile == '-') { map[player.y+1][player.x] = ' '; player.wood += 1; return "拆除木门,回收1木头"; } return "脚下没有木门可拆除"; }
string CraftPortal() { if(player.hasPortal) return "你已拥有传送门!"; if(player.crystal < 10) return "水晶不足,需要10个水晶"; if(player.stone < 5) return "石头不足,需要5个石头(代替黑曜石)"; if(player.enderPearl < 3) return "末影珍珠不足,需要3个";
player.crystal -= 10;
player.stone -= 5;
player.enderPearl -= 3;
player.hasPortal = true;
return "传送门制作完成!按E使用传送门进入末地";
}
string UsePortal() { if(!player.hasPortal) return "你没有传送门!按N制作传送门";
if(!player.inEnd)
{
player.inEnd = true;
player.x = MAP_WIDTH / 2;
player.y = MAP_HEIGHT / 2;
endTimer = 0;
bool hasDragon = false;
for (auto& m : monsters)
{
if (m.type == MON_ENDER_DRAGON && m.alive)
{
hasDragon = true;
break;
}
}
if (!hasDragon)
{
Monster dragon;
dragon.x = MAP_WIDTH / 2 + 2;
dragon.y = MAP_HEIGHT / 2 - 2;
dragon.type = MON_ENDER_DRAGON;
dragon.hp = 200;
dragon.maxHp = 200;
dragon.alive = true;
dragon.isResting = false;
dragon.restTimer = 0;
monsters.push_back(dragon);
}
return "已进入末地!击败末影龙通关!";
}
else
{
player.inEnd = false;
player.x = MAP_WIDTH / 2;
player.y = MAP_HEIGHT / 2;
return "已离开末地";
}
}
string CraftArmor(int type) { if(player.armorType != 0) return "你已装备护甲,请先拆除再合成新护甲";
if(type == 1)
{
if(player.vine >= 3 && player.herb >= 2)
{
player.vine -= 3;
player.herb -= 2;
player.armorType = 1;
player.armorDefense = 3;
player.armorDurability = 30;
player.armor = player.armorDurability;
return "合成布甲!减少3点伤害,耐久30";
}
return "缺材料:3藤蔓+2草药";
}
else if(type == 2)
{
if(player.iron >= 5 && player.stone >= 3 && player.vine >= 2)
{
player.iron -= 5;
player.stone -= 3;
player.vine -= 2;
player.armorType = 2;
player.armorDefense = 8;
player.armorDurability = 60;
player.armor = player.armorDurability;
return "合成铁甲!减少8点伤害,耐久60";
}
return "缺材料:5铁+3石头+2藤蔓";
}
else if(type == 3)
{
if(player.crystal >= 8 && player.iron >= 8 && player.stone >= 5)
{
player.crystal -= 8;
player.iron -= 8;
player.stone -= 5;
player.armorType = 3;
player.armorDefense = 15;
player.armorDurability = 100;
player.armor = player.armorDurability;
return "合成钻石甲!减少15点伤害,耐久100";
}
return "缺材料:8水晶+8铁+5石头";
}
return "无效的护甲类型";
}
string RemoveArmor() { if(player.armorType == 0) return "你没有装备护甲";
if(player.armorType == 1)
{
player.vine += 1;
player.herb += 1;
}
else if(player.armorType == 2)
{
player.iron += 2;
player.stone += 1;
}
else if(player.armorType == 3)
{
player.crystal += 3;
player.iron += 2;
}
player.armorType = 0;
player.armorDefense = 0;
player.armorDurability = 0;
player.armor = 0;
return "拆除护甲,回收部分材料";
}
void DrawMap(string tip = "") { Gotoxy(0, 0);
if (player.inEnd)
{
for (int i = 0; i < MAP_HEIGHT; i++)
{
for (int j = 0; j < MAP_WIDTH; j++)
{
if (i == player.y && j == player.x)
cout << '@';
else
cout << endMap[i][j];
}
cout << endl;
}
}
else
{
for (int i = 0; i < MAP_HEIGHT; i++)
{
for (int j = 0; j < MAP_WIDTH; j++)
{
cout << map[i][j];
}
cout << endl;
}
}
int uiLine = MAP_HEIGHT;
Gotoxy(0, uiLine++);
cout << "========================================";
string location = player.inEnd ? "【末地】" : "【主世界】";
Gotoxy(0, uiLine++);
cout << location << " 生命:" << player.hp << "/" << player.maxHp
<< " 木:" << player.wood << " 石:" << player.stone << " 草:" << player.herb;
Gotoxy(0, uiLine++);
cout << "铁:" << player.iron << " 藤:" << player.vine << " 水晶:" << player.crystal
<< " 子弹:" << player.bullet << " 末影珍珠:" << player.enderPearl;
string armorName = "无";
if(player.armorType == 1) armorName = "布甲";
else if(player.armorType == 2) armorName = "铁甲";
else if(player.armorType == 3) armorName = "钻石甲";
Gotoxy(0, uiLine++);
cout << "护甲:" << armorName << " 防御:" << player.armorDefense
<< " 耐久:" << player.armor << "/" << player.armorDurability;
string gunStr = "无";
if(player.gun == GUN_PISTOL) gunStr = "手枪";
if(player.gun == GUN_SHOTGUN) gunStr = "霰弹枪";
if(player.gun == GUN_RIFLE) gunStr = "步枪";
if(player.gun == GUN_GATLING) gunStr = "加特林";
Gotoxy(0, uiLine++);
cout << "当前武器:" << gunStr << " 传送门:" << (player.hasPortal ? "有" : "无");
if (!player.inEnd)
{
string dayStr = isDay ? "【白天】" : "【黑夜】";
int progress = (dayTimer * 20) / DAY_FRAME_COUNT;
string bar = "[";
for(int i = 0; i < 20; i++)
{
if(i < progress) bar += "=";
else if(i == progress) bar += ">";
else bar += " ";
}
bar += "]";
Gotoxy(0, uiLine++);
cout << dayStr << bar << " (" << (dayTimer * 100 / DAY_FRAME_COUNT) << "%)";
cout << " | 怪物" << (isDay ? "静止" : "追击");
// 显示资源刷新倒计时
int remaining = (RESOURCE_REFRESH_INTERVAL - resourceTimer) / 20; // 转换为秒
Gotoxy(0, uiLine++);
cout << "资源刷新: " << remaining << "秒后刷新";
}
else
{
Gotoxy(0, uiLine++);
for (auto& m : monsters)
{
if (m.type == MON_ENDER_DRAGON && m.alive)
{
cout << "末影龙 HP:" << m.hp << "/" << m.maxHp;
if (m.isResting)
cout << " 【休息中】";
else
cout << " 【飞行中】";
break;
}
}
}
Gotoxy(0, uiLine++);
if (player.inEnd)
{
cout << "WASD移动 | 空格射击 | ESC返回主世界 | 末影龙:击败即通关";
}
else
{
cout << "WASD移动 | 空格射击(八方向) | P石头墙 | G放门 O开关 U拆门 | "
<< "1手枪 2霰弹 3步枪 4回血 5子弹 6加特林 | ESC退出";
}
Gotoxy(0, uiLine++);
if (player.inEnd)
{
cout << "末地资源: ~末地石 A祭坛 B黑曜石 P石柱 | 龙息伤害10点/次";
}
else
{
cout << "7布甲 8铁甲 9钻石甲 0拆护甲 | N制作传送门 E使用传送门 | "
<< "资源:T木 S石 H草 I铁 V藤 C水晶 E末影珍珠";
}
Gotoxy(0, uiLine++);
cout << "提示:" << tip;
}
void CollectResource() { char c = map[player.y][player.x]; switch(c) { case 'T': player.wood++; break; case 'S': player.stone++; break; case 'H': player.herb++; break; case 'I': player.iron++; break; case 'V': player.vine++; break; case 'C': player.crystal++; break; case 'E': player.enderPearl++; break; default: return; } map[player.y][player.x] = ' '; }
string Craft(int type) { if(type == 1) { if(player.gun != GUN_NONE) return "已有武器,无需重复制作"; if(player.wood >=3 && player.iron >=2) { player.wood -=3; player.iron -=2; player.gun = GUN_PISTOL; return "合成手枪!单发,伤害15"; } return "缺材料:3木头+2铁矿"; } if(type == 2) { if(player.gun != GUN_NONE) return "已有武器"; if(player.iron >=5 && player.crystal >=3 && player.vine >=2) { player.iron -=5; player.crystal -=3; player.vine -=2; player.gun = GUN_SHOTGUN; return "合成霰弹枪!一次3发子弹,每发12伤害"; } return "缺材料:5铁+3水晶+2藤蔓"; } if(type ==3) { if(player.gun != GUN_NONE) return "已有武器"; if(player.iron >=6 && player.crystal >=4 && player.wood >=4) { player.iron -=6; player.crystal -=4; player.wood -=4; player.gun = GUN_RIFLE; return "合成步枪!高伤,单发22"; } return "缺材料:6铁+4水晶+4木头"; } if(type ==6) { if(player.gun != GUN_NONE) return "已有武器"; if(player.iron >=10 && player.crystal >=8 && player.vine >=5) { player.iron -=10; player.crystal -=8; player.vine -=5; player.gun = GUN_GATLING; return "合成加特林!快速连发,伤害8,消耗2子弹"; } return "缺材料:10铁矿+8水晶+5藤蔓"; } if(type ==4) { if(player.herb >=3) { player.herb -=3; player.hp = min(player.maxHp, player.hp + 50); player.healTimer = 0; return "服用草药,恢复50生命值"; } return "草药不足,需要3个草药"; } if(type ==5) { if(player.stone >=1 && player.iron >=1) { player.stone--; player.iron--; player.bullet +=50; return "消耗1石头+1铁矿,合成50发子弹"; } return "材料不足:需要1石头+1铁矿"; } return ""; }
void ShootInDirection(int dirX, int dirY) { if(player.gun == GUN_NONE || player.bullet <= 0) return;
if(player.gun == GUN_PISTOL)
{
if(player.bullet < 1) return;
Bullet b;
b.x = player.x;
b.y = player.y;
b.dirX = dirX;
b.dirY = dirY;
b.damage = 15;
b.alive = true;
bullets.push_back(b);
player.bullet--;
}
else if(player.gun == GUN_SHOTGUN)
{
if(player.bullet < 3) return;
player.bullet -= 3;
Bullet b1;
b1.x = player.x;
b1.y = player.y;
b1.dirX = dirX;
b1.dirY = dirY;
b1.damage = 12;
b1.alive = true;
bullets.push_back(b1);
Bullet b2;
b2.x = player.x;
b2.y = player.y;
b2.dirX = dirX - dirY;
b2.dirY = dirY + dirX;
b2.damage = 12;
b2.alive = true;
bullets.push_back(b2);
Bullet b3;
b3.x = player.x;
b3.y = player.y;
b3.dirX = dirX + dirY;
b3.dirY = dirY - dirX;
b3.damage = 12;
b3.alive = true;
bullets.push_back(b3);
}
else if(player.gun == GUN_RIFLE)
{
if(player.bullet < 1) return;
Bullet b;
b.x = player.x;
b.y = player.y;
b.dirX = dirX;
b.dirY = dirY;
b.damage = 22;
b.alive = true;
bullets.push_back(b);
player.bullet--;
}
else if(player.gun == GUN_GATLING)
{
if(player.bullet < 2) return;
player.bullet -= 2;
Bullet b;
b.x = player.x;
b.y = player.y;
b.dirX = dirX;
b.dirY = dirY;
b.damage = 8;
b.alive = true;
bullets.push_back(b);
}
}
void UpdateBullet() { for (auto& b : bullets) { if (b.alive) { if (player.inEnd) endMap[b.y][b.x] = '~'; else map[b.y][b.x] = ' '; } }
for (auto& b : bullets)
{
if (!b.alive) continue;
b.x += b.dirX;
b.y += b.dirY;
if (b.x <= 0 || b.x >= MAP_WIDTH - 1 || b.y <= 0 || b.y >= MAP_HEIGHT - 1)
{
b.alive = false;
continue;
}
char tile;
if (player.inEnd)
tile = endMap[b.y][b.x];
else
tile = map[b.y][b.x];
if (tile == '#' || tile == '+')
{
b.alive = false;
continue;
}
for (auto& m : monsters)
{
if (m.alive && m.x == b.x && m.y == b.y)
{
m.hp -= b.damage;
b.alive = false;
if (m.hp <= 0)
{
if (player.inEnd)
endMap[m.y][m.x] = '~';
else
map[m.y][m.x] = ' ';
m.alive = false;
}
break;
}
}
if (b.alive)
{
if (player.inEnd)
endMap[b.y][b.x] = '*';
else
map[b.y][b.x] = '*';
}
}
bullets.erase(remove_if(bullets.begin(), bullets.end(),
[](const Bullet& b) { return !b.alive; }), bullets.end());
}
void UpdateDragonBreath() { for (auto& b : dragonBreaths) { if (b.alive) { if (player.inEnd) endMap[b.y][b.x] = '~'; } }
for (auto& b : dragonBreaths)
{
if (!b.alive) continue;
b.x += b.dirX;
b.y += b.dirY;
b.lifeTime--;
if (b.x <= 0 || b.x >= MAP_WIDTH - 1 || b.y <= 0 || b.y >= MAP_HEIGHT - 1 || b.lifeTime <= 0)
{
b.alive = false;
continue;
}
if (b.x == player.x && b.y == player.y && player.hitCool == 0)
{
int dmg = b.damage;
if(player.armor > 0 && player.armorDefense > 0)
{
dmg = max(0, dmg - player.armorDefense);
player.armor--;
if(player.armor <= 0)
{
player.armor = 0;
player.armorDefense = 0;
player.armorType = 0;
player.armorDurability = 0;
}
}
player.hp -= dmg;
player.hitCool = 18;
b.alive = false;
}
if (b.alive)
{
if (player.inEnd)
endMap[b.y][b.x] = '&';
}
}
dragonBreaths.erase(remove_if(dragonBreaths.begin(), dragonBreaths.end(),
[](const DragonBreath& b) { return !b.alive; }), dragonBreaths.end());
}
void UpdateMonster() { if (player.hitCool > 0) player.hitCool--; bool isHitThisFrame = false;
for (auto& m : monsters)
{
if (m.alive)
{
if (player.inEnd)
endMap[m.y][m.x] = '~';
else
map[m.y][m.x] = ' ';
}
}
for (auto& m : monsters)
{
if (!m.alive) continue;
if (m.type == MON_ENDER_DRAGON)
{
if (!player.inEnd) continue;
int centerX = MAP_WIDTH / 2;
int centerY = MAP_HEIGHT / 2;
if (!m.isResting)
{
int moveRate = 2;
if (rand() % moveRate == 0)
{
int nx = m.x;
int ny = m.y;
if (m.x < player.x) nx++;
else if (m.x > player.x) nx--;
if (m.y < player.y) ny++;
else if (m.y > player.y) ny--;
char nextTile = endMap[ny][nx];
if (nextTile == '~' || nextTile == 'B' || nextTile == 'P')
{
m.x = nx;
m.y = ny;
}
}
if (rand() % 5 == 0)
{
int dx = 0, dy = 0;
if (player.x > m.x) dx = 1;
else if (player.x < m.x) dx = -1;
if (player.y > m.y) dy = 1;
else if (player.y < m.y) dy = -1;
DragonBreath breath;
breath.x = m.x;
breath.y = m.y;
breath.dirX = dx;
breath.dirY = dy;
breath.damage = 10;
breath.alive = true;
breath.lifeTime = 10;
dragonBreaths.push_back(breath);
}
if (endTimer % 1500 == 0 && endTimer > 0)
{
if (abs(m.x - centerX) <= 2 && abs(m.y - centerY) <= 2)
{
m.isResting = true;
m.restTimer = 0;
}
else
{
if (m.x < centerX) m.x++;
else if (m.x > centerX) m.x--;
if (m.y < centerY) m.y++;
else if (m.y > centerY) m.y--;
}
}
}
else
{
m.restTimer++;
if (m.restTimer >= 500)
{
m.isResting = false;
m.restTimer = 0;
}
}
char dragonChar = m.isResting ? 'D' : 'R';
endMap[m.y][m.x] = dragonChar;
continue;
}
if (player.inEnd) continue;
if(isDay)
{
map[m.y][m.x] = (m.type == MON_ZOMBIE ? 'M' : m.type == MON_GHOST ? 'F' : 'B');
continue;
}
int moveRate = 3;
if(m.type == MON_GHOST) moveRate = 1;
if(m.type == MON_BOSS) moveRate = 5;
if (rand() % moveRate == 0)
{
int nx = m.x;
int ny = m.y;
if (m.x < player.x) nx++;
else if (m.x > player.x) nx--;
if (m.y < player.y) ny++;
else if (m.y > player.y) ny--;
char nextTile = map[ny][nx];
if (nextTile == ' ' || nextTile == '-')
{
m.x = nx;
m.y = ny;
}
}
if (m.x == player.x && m.y == player.y && player.hitCool == 0)
{
int dmg = 5;
if(m.type == MON_GHOST) dmg = 3;
if(m.type == MON_BOSS) dmg = 12;
if(player.armor > 0 && player.armorDefense > 0)
{
dmg = max(0, dmg - player.armorDefense);
player.armor--;
if(player.armor <= 0)
{
player.armor = 0;
player.armorDefense = 0;
player.armorType = 0;
player.armorDurability = 0;
}
}
if(dmg > 0)
{
player.hp -= dmg;
}
player.hitCool = 18;
isHitThisFrame = true;
}
if(m.type == MON_ZOMBIE) map[m.y][m.x] = 'M';
if(m.type == MON_GHOST) map[m.y][m.x] = 'F';
if(m.type == MON_BOSS) map[m.y][m.x] = 'B';
}
if (isHitThisFrame)
player.healTimer = 0;
}
void AutoHealUpdate() { if (player.hp >= player.maxHp) return; player.healTimer++; if (player.healTimer >= HEAL_DELAY) { if (player.healTimer % HEAL_RATE == 0) { player.hp = min(player.maxHp, player.hp + 1); } } }
void MovePlayer(int dx, int dy) { int nx = player.x + dx; int ny = player.y + dy; char tile;
if (player.inEnd)
tile = endMap[ny][nx];
else
tile = map[ny][nx];
if (player.inEnd)
{
if (tile == '#' || tile == 'A' || tile == 'B' || tile == 'P')
return;
}
else
{
if (tile == '#' || tile == '+')
return;
}
if (player.inEnd)
{
endMap[player.y][player.x] = '~';
}
else
{
map[player.y][player.x] = ' ';
}
player.x = nx;
player.y = ny;
if (!player.inEnd)
{
CollectResource();
map[player.y][player.x] = '@';
}
else
{
endMap[player.y][player.x] = '@';
}
}
int main() { InitConsoleBuffer(); HideCursor(); InitMap(); string tipMsg = "";
int lastDirX = 0, lastDirY = -1;
while (true)
{
if (!player.inEnd)
{
dayTimer++;
if(dayTimer >= DAY_FRAME_COUNT)
{
dayTimer = 0;
isDay = !isDay;
}
// 资源刷新计时
resourceTimer++;
if (resourceTimer >= RESOURCE_REFRESH_INTERVAL)
{
resourceTimer = 0;
RefreshResources();
tipMsg = "资源已刷新!";
}
}
else
{
endTimer++;
}
if (_kbhit())
{
char key = _getch();
switch (key)
{
case 'w':
case 'W':
MovePlayer(0, -1);
lastDirX = 0; lastDirY = -1;
break;
case 's':
case 'S':
MovePlayer(0, 1);
lastDirX = 0; lastDirY = 1;
break;
case 'a':
case 'A':
MovePlayer(-1, 0);
lastDirX = -1; lastDirY = 0;
break;
case 'd':
case 'D':
MovePlayer(1, 0);
lastDirX = 1; lastDirY = 0;
break;
case 'g': case 'G':
if (!player.inEnd) tipMsg = SpawnDoor();
else tipMsg = "末地无法放置门";
break;
case 'o': case 'O':
if (!player.inEnd) tipMsg = ToggleDoor();
else tipMsg = "末地无法开关门";
break;
case 'u': case 'U':
if (!player.inEnd) tipMsg = RemoveDoor();
else tipMsg = "末地无法拆除门";
break;
case 'p': case 'P':
if (!player.inEnd) tipMsg = PlaceBlock();
else tipMsg = "末地无法放置方块";
break;
case 'n': case 'N': tipMsg = CraftPortal(); break;
case 'e': case 'E': tipMsg = UsePortal(); break;
case ' ':
ShootInDirection(lastDirX, lastDirY);
break;
case '1': tipMsg = Craft(1); break;
case '2': tipMsg = Craft(2); break;
case '3': tipMsg = Craft(3); break;
case '4': tipMsg = Craft(4); break;
case '5': tipMsg = Craft(5); break;
case '6': tipMsg = Craft(6); break;
case '7': tipMsg = CraftArmor(1); break;
case '8': tipMsg = CraftArmor(2); break;
case '9': tipMsg = CraftArmor(3); break;
case '0': tipMsg = RemoveArmor(); break;
case 27:
if (player.inEnd)
{
tipMsg = UsePortal();
}
else
{
return 0;
}
break;
}
}
UpdateBullet();
UpdateDragonBreath();
UpdateMonster();
AutoHealUpdate();
DrawMap(tipMsg);
if (tipMsg != "资源已刷新!") // 资源刷新提示保留一帧
tipMsg = "";
if (player.hp <= 0)
{
Gotoxy(0, MAP_HEIGHT + 10);
cout << "你被击杀,游戏结束!";
break;
}
bool dragonAlive = false;
for (auto& m : monsters)
{
if (m.type == MON_ENDER_DRAGON && m.alive)
{
dragonAlive = true;
break;
}
}
if (!dragonAlive && player.inEnd)
{
Gotoxy(0, MAP_HEIGHT + 10);
cout << "末影龙已被击败!恭喜通关!";
break;
}
if (!player.inEnd)
{
int aliveCnt = 0;
for (auto& m : monsters)
if (m.alive) aliveCnt++;
if (aliveCnt == 0)
{
Gotoxy(0, MAP_HEIGHT + 10);
cout << "全部怪物清除!现在可以制作传送门前往末地!";
}
}
Sleep(50);
}
system("pause");
return 0;
}