// 定义船的类 class Ship { public: string id; float health; float shield; Ship(string shipId, float shipHealth, float shipShield) { id = shipId; health = shipHealth; shield = shipShield; } void takeDamage(float damage) { if (shield > 0) { float shieldDamage = min(damage, shield * 0.1); shield -= shieldDamage; damage -= shieldDamage; } if (damage > 0) { if (id == "ship_l") { damage *= 0.7; // l级船受到的伤害减少30% } else if (id == "ship_xl") { damage *= 0.5; // xl级船受到的伤害减少50% } health -= damage; } } void regenerate() { if (id == "ship_l") { health += health * 0.001; // l级船每1秒回复0.1%船体值 } else if (id == "ship_xl") { health += health * 0.002; // xl级船每1秒回复0.2%船体值 } shield = min(shield + shield * 0.05, 100.0); // 护盾每1秒最多受到5%的伤害 } }; // 游戏中所有的船 vector<Ship> allShips; // 每1秒更新所有船的状态 void updateShips() { for (Ship& ship : allShips) { ship.regenerate(); } } // 受到伤害时更新船的状态 void takeDamage(string shipId, float damage) { for (Ship& ship : allShips) { if (ship.id == shipId) { ship.takeDamage(damage); break; } } } // 在游戏中创建一艘船 void createShip(string shipId, float shipHealth, float shipShield) { Ship ship = Ship(shipId, shipHealth, shipShield); allShips.push_back(ship); } // 在游戏中移除一艘船 void removeShip(string shipId) { for (auto it = allShips.begin(); it != allShips.end(); ++it) { if (it->id == shipId) { allShips.erase(it); break; } } } // 游戏循环 while (true) { updateShips(); // 处理游戏逻辑 } [2024-11-18 23:57:31 | AI写代码神器 | 1281点数解答]