```python import random class AdventureGame: def __init__(self): self.player_health = 100 # 玩家初始生命值 self.monster_health = 50 # 怪物初始生命值 def attack_player(self): # 怪物攻击玩家,造成随机伤害 damage = random.randint(10, 20) self.player_health -= damage print(f"怪物攻击了你,造成 {damage} 点伤害,你的生命值变为 {self.player_health}") def attack_monster(self): # 玩家攻击怪物,造成随机伤害 damage = random.randint(15, 25) self.monster_health -= damage print(f"你攻击了怪物,造成 {damage} 点伤害,怪物的生命值变为 {self.monster_health}") def check_game_over(self): # 检查游戏是否结束 if self.player_health <= 0: print("你被怪物打败了,游戏结束!") return True elif self.monster_health <= 0: print("你打败了怪物,游戏胜利!") return True else: return False def play_game(self): while not self.check_game_over(): print("选择你的行动:") print("1. 攻击怪物") choice = int(input()) if choice == 1: self.attack_monster() self.attack_player() if __name__ == "__main__": game = AdventureGame() game.play_game() # 注意逻辑点: # 1. 游戏中的伤害值可以根据游戏难度和平衡进行调整。 # 2. 可以增加更多的游戏元素,如道具、技能等。 # 3. 对于游戏的界面和交互可以进一步优化,提高用户体验。 ``` [2025-04-19 08:38:27 | AI写代码神器 | 430点数解答]