- 分享
游戏代码(上)
- @ 2026-7-23 22:24:45
#include #include <conio.h> #include <windows.h> #include #include #include #include #include
using namespace std;
#define MAP_WIDTH 40 #define MAP_HEIGHT 20 #define DAY_FRAME_COUNT 360 #define HEAL_DELAY 80 #define HEAL_RATE 1 #define RESOURCE_REFRESH_INTERVAL 1200 // 1分钟 = 1200帧 (每帧50ms)
enum ItemID { ITEM_NONE = 0, ITEM_WOOD, ITEM_STONE, ITEM_HERB, ITEM_IRON, ITEM_VINE, ITEM_CRYSTAL, ITEM_BULLET, ITEM_GUN_PISTOL, ITEM_GUN_SHOT, ITEM_GUN_RIFLE, ITEM_GUN_GATLING, ITEM_POTION, ITEM_ARMOR, ITEM_ENDER_PEARL };
enum GunType { GUN_NONE, GUN_PISTOL, GUN_SHOTGUN, GUN_RIFLE, GUN_GATLING };
enum MonsterType { MON_ZOMBIE, MON_GHOST, MON_BOSS, MON_ENDER_DRAGON };
struct Player { int x, y; int hp = 100; int maxHp = 100; int wood = 0, stone = 0, herb = 0; int iron = 0, vine = 0, crystal = 0; int bullet = 10; GunType gun = GUN_NONE; int hitCool = 0; int healTimer = 0;
int armor = 0;
int maxArmor = 50;
int armorDurability = 0;
int armorDefense = 0;
int armorType = 0;
bool inEnd = false;
bool hasPortal = false;
int enderPearl = 0;
};
struct DragonBreath { int x, y; int dirX, dirY; int damage; bool alive; int lifeTime; };
struct Bullet { int x, y; int dirX, dirY; int damage; bool alive; };
struct Monster { int x, y; int hp; int maxHp; MonsterType type; bool alive; bool isResting; int restTimer; };
char map[MAP_HEIGHT][MAP_WIDTH]; char endMap[MAP_HEIGHT][MAP_WIDTH]; Player player; vector bullets; vector dragonBreaths; vector monsters; int dayTimer = 0; bool isDay = true; int endTimer = 0; int resourceTimer = 0; // 资源刷新计时器
void InitConsoleBuffer() { COORD bufferSize; bufferSize.X = 140; bufferSize.Y = 60; SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), bufferSize); }
void HideCursor() { CONSOLE_CURSOR_INFO cursor; cursor.dwSize = 1; cursor.bVisible = FALSE; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor); }
void Gotoxy(int x, int y) { COORD coord; coord.X = x; coord.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); }
void InitEndMap() { for (int i = 0; i < MAP_HEIGHT; i++) { for (int j = 0; j < MAP_WIDTH; j++) { if (i == 0 || i == MAP_HEIGHT - 1 || j == 0 || j == MAP_WIDTH - 1) endMap[i][j] = '#'; else endMap[i][j] = '~'; } }
int centerX = MAP_WIDTH / 2;
int centerY = MAP_HEIGHT / 2;
for (int i = -2; i <= 2; i++)
{
for (int j = -2; j <= 2; j++)
{
int x = centerX + j;
int y = centerY + i;
if (x > 0 && x < MAP_WIDTH - 1 && y > 0 && y < MAP_HEIGHT - 1)
{
if (abs(i) <= 1 && abs(j) <= 1)
endMap[y][x] = 'A';
else
endMap[y][x] = 'B';
}
}
}
for (int i = 0; i < 8; i++)
{
int x = rand() % (MAP_WIDTH - 10) + 5;
int y = rand() % (MAP_HEIGHT - 10) + 5;
int centerX2 = MAP_WIDTH / 2;
int centerY2 = MAP_HEIGHT / 2;
if (abs(x - centerX2) > 5 || abs(y - centerY2) > 5)
{
for (int h = 0; h < 3; h++)
{
if (y + h < MAP_HEIGHT - 1 && x < MAP_WIDTH - 1)
endMap[y + h][x] = 'P';
}
}
}
}
// 资源刷新函数 void RefreshResources() { if (player.inEnd) return; // 末地不刷新资源
// 统计当前资源数量
int woodCount = 0, stoneCount = 0, herbCount = 0, ironCount = 0, vineCount = 0, crystalCount = 0, enderCount = 0;
for (int i = 1; i < MAP_HEIGHT - 1; i++)
{
for (int j = 1; j < MAP_WIDTH - 1; j++)
{
char c = map[i][j];
switch(c)
{
case 'T': woodCount++; break;
case 'S': stoneCount++; break;
case 'H': herbCount++; break;
case 'I': ironCount++; break;
case 'V': vineCount++; break;
case 'C': crystalCount++; break;
case 'E': enderCount++; break;
default: break;
}
}
}
// 每种资源保持至少20个,最多40个
int targetCount = 25;
int maxCount = 40;
// 刷新木头 (T)
if (woodCount < targetCount)
{
int toAdd = min(10, targetCount - woodCount);
for (int i = 0; i < toAdd; i++)
{
int x = rand() % (MAP_WIDTH - 4) + 2;
int y = rand() % (MAP_HEIGHT - 4) + 2;
if (map[y][x] == ' ')
map[y][x] = 'T';
}
}
// 刷新石头 (S)
if (stoneCount < targetCount)
{
int toAdd = min(10, targetCount - stoneCount);
for (int i = 0; i < toAdd; i++)
{
int x = rand() % (MAP_WIDTH - 4) + 2;
int y = rand() % (MAP_HEIGHT - 4) + 2;
if (map[y][x] == ' ')
map[y][x] = 'S';
}
}
// 刷新草药 (H)
if (herbCount < targetCount)
{
int toAdd = min(10, targetCount - herbCount);
for (int i = 0; i < toAdd; i++)
{
int x = rand() % (MAP_WIDTH - 4) + 2;
int y = rand() % (MAP_HEIGHT - 4) + 2;
if (map[y][x] == ' ')
map[y][x] = 'H';
}
}
// 刷新铁 (I)
if (ironCount < targetCount)
{
int toAdd = min(10, targetCount - ironCount);
for (int i = 0; i < toAdd; i++)
{
int x = rand() % (MAP_WIDTH - 4) + 2;
int y = rand() % (MAP_HEIGHT - 4) + 2;
if (map[y][x] == ' ')
map[y][x] = 'I';
}
}
// 刷新藤蔓 (V)
if (vineCount < targetCount)
{
int toAdd = min(10, targetCount - vineCount);
for (int i = 0; i < toAdd; i++)
{
int x = rand() % (MAP_WIDTH - 4) + 2;
int y = rand() % (MAP_HEIGHT - 4) + 2;
if (map[y][x] == ' ')
map[y][x] = 'V';
}
}
// 刷新水晶 (C)
if (crystalCount < targetCount)
{
int toAdd = min(10, targetCount - crystalCount);
for (int i = 0; i < toAdd; i++)
{
int x = rand() % (MAP_WIDTH - 4) + 2;
int y = rand() % (MAP_HEIGHT - 4) + 2;
if (map[y][x] == ' ')
map[y][x] = 'C';
}
}
// 刷新末影珍珠 (E) - 较少刷新
if (enderCount < 3)
{
int toAdd = min(3, 3 - enderCount);
for (int i = 0; i < toAdd; i++)
{
int x = rand() % (MAP_WIDTH - 4) + 2;
int y = rand() % (MAP_HEIGHT - 4) + 2;
if (map[y][x] == ' ')
map[y][x] = 'E';
}
}
}
void InitMap() { for (int i = 0; i < MAP_HEIGHT; i++) { for (int j = 0; j < MAP_WIDTH; j++) { if (i == 0 || i == MAP_HEIGHT - 1 || j == 0 || j == MAP_WIDTH - 1) map[i][j] = '#'; else map[i][j] = ' '; } }
srand((unsigned)time(NULL));
// 初始生成大量资源
char normalRes[] = {'T','S','H','V','C','I'};
for(int r=0; r<6; r++)
{
for(int i=0; i<25; i++) // 增加初始资源数量
{
int x = rand() % (MAP_WIDTH - 4) + 2;
int y = rand() % (MAP_HEIGHT - 4) + 2;
map[y][x] = normalRes[r];
}
}
// 添加末影珍珠
for(int i=0; i<5; i++)
{
int x = rand() % (MAP_WIDTH - 4) + 2;
int y = rand() % (MAP_HEIGHT - 4) + 2;
map[y][x] = 'E';
}
player.x = MAP_WIDTH / 2;
player.y = MAP_HEIGHT / 2;
map[player.y][player.x] = '@';
player.healTimer = 0;
player.inEnd = false;
player.hasPortal = false;
player.enderPearl = 0;
player.armor = 0;
player.maxArmor = 50;
player.armorDurability = 0;
player.armorDefense = 0;
player.armorType = 0;
monsters.clear();
for(int i=0; i<6; i++)
{
Monster m;
do
{
m.x = rand() % (MAP_WIDTH - 4) + 2;
m.y = rand() % (MAP_HEIGHT - 4) + 2;
} while (abs(m.x - player.x) < 7 && abs(m.y - player.y) < 7);
m.type = MON_ZOMBIE;
m.hp = 30; m.maxHp = 30;
m.alive = true;
m.isResting = false;
m.restTimer = 0;
monsters.push_back(m);
map[m.y][m.x] = 'M';
}
for(int i=0; i<4; i++)
{
Monster m;
do
{
m.x = rand() % (MAP_WIDTH - 4) + 2;
m.y = rand() % (MAP_HEIGHT - 4) + 2;
} while (abs(m.x - player.x) < 7 && abs(m.y - player.y) < 7);
m.type = MON_GHOST;
m.hp = 18; m.maxHp = 18;
m.alive = true;
m.isResting = false;
m.restTimer = 0;
monsters.push_back(m);
map[m.y][m.x] = 'F';
}
for(int i=0; i<2; i++)
{
Monster m;
do
{
m.x = rand() % (MAP_WIDTH - 4) + 2;
m.y = rand() % (MAP_HEIGHT - 4) + 2;
} while (abs(m.x - player.x) < 8 && abs(m.y - player.y) < 8);
m.type = MON_BOSS;
m.hp = 80; m.maxHp = 80;
m.alive = true;
m.isResting = false;
m.restTimer = 0;
monsters.push_back(m);
map[m.y][m.x] = 'B';
}
bullets.clear();
dragonBreaths.clear();
dayTimer = 0;
isDay = true;
endTimer = 0;
resourceTimer = 0;
InitEndMap();
}
string PlaceBlock() { int bx = player.x; int by = player.y + 1; if(bx <= 0 || bx >= MAP_WIDTH-1 || by <=0 || by >= MAP_HEIGHT-1) 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)
{