酷代码 AI
菜单
全部AI  /  热门  /  AI写代码神器
服务商

我要一段Python的冒险游戏的代码

# 简单Python冒险游戏 ```python import random import time class AdventureGame: def __init__(self): self.player_health = 100 self.inventory = [] self.game_over = False self.current_room = "森林入口" self.rooms = { "森林入口": { "description": "你站在一片神秘的森林入口处,前方有三条小路。", "exits": {"北": "黑暗洞穴", "东": "古老神庙", "西": "精灵村庄"}, "items": ["火把"] }, "黑暗洞穴": { "description": "洞穴内漆黑一片,你能听到滴水声和奇怪的响动。", "exits": {"南": "森林入口", "东": "洞穴深处"}, "enemy": {"name": "洞穴蜘蛛", "health": 30, "damage": 10}, "items": ["宝石"] }, "洞穴深处": { "description": "洞穴最深处有一个发光的宝箱。", "exits": {"西": "黑暗洞穴"}, "items": ["金钥匙"], "treasure": True }, "古老神庙": { "description": "一座破败的神庙,墙上刻着奇怪的符号。", "exits": {"西": "森林入口", "北": "神庙密室"}, "puzzle": "什么东西越洗越脏?", "answer": "水", "reward": "银钥匙" }, "神庙密室": { "description": "密室中央有一个需要钥匙的祭坛。", "exits": {"南": "古老神庙"}, "locked": True }, "精灵村庄": { "description": "友好的精灵们欢迎你的到来。", "exits": {"东": "森林入口"}, "healer": True } } def print_status(self): print(f"\n当前位置: {self.current_room}") print(f"生命值: {self.player_health}") print(f"物品: {', '.join(self.inventory) if self.inventory else '无'}") print(self.rooms[self.current_room]["description"]) if "exits" in self.rooms[self.current_room]: exits = self.rooms[self.current_room]["exits"] print(f"出口: {', '.join(exits.keys())}") def move(self, direction): if direction in self.rooms[self.current_room]["exits"]: new_room = self.rooms[self.current_room]["exits"][direction] if new_room == "神庙密室" and "银钥匙" not in self.inventory: print("密室被锁住了,你需要找到钥匙!") return self.current_room = new_room print(f"\n你来到了{self.current_room}") # 检查新房间是否有敌人 if "enemy" in self.rooms[self.current_room]: self.fight_enemy() # 检查是否找到宝藏 if "treasure" in self.rooms[self.current_room] and "金钥匙" in self.inventory: print("\n恭喜! 你打开了宝箱,获得了胜利!") self.game_over = True else: print("你不能往那个方向走!") def fight_enemy(self): enemy = self.rooms[self.current_room]["enemy"] print(f"\n一只{enemy['name']}跳出来攻击你!") while enemy["health"] > 0 and self.player_health > 0: print(f"\n{enemy['name']} 生命值: {enemy['health']}") print(f"你的生命值: {self.player_health}") action = input("你要做什么? (攻击/逃跑) ").lower() if action == "攻击": damage = random.randint(5, 15) enemy["health"] -= damage print(f"你对{enemy['name']}造成了{damage}点伤害!") if enemy["health"] <= 0: print(f"你击败了{enemy['name']}!") # 敌人可能掉落物品 if "items" in self.rooms[self.current_room]: item = self.rooms[self.current_room]["items"].pop(0) self.inventory.append(item) print(f"你获得了{item}!") break # 敌人反击 self.player_health -= enemy["damage"] print(f"{enemy['name']}对你造成了{enemy['damage']}点伤害!") if self.player_health <= 0: print("你被击败了! 游戏结束!") self.game_over = True break elif action == "逃跑": print("你成功逃跑了!") self.current_room = "森林入口" break def take_item(self): if "items" in self.rooms[self.current_room] and self.rooms[self.current_room]["items"]: item = self.rooms[self.current_room]["items"][0] self.inventory.append(item) self.rooms[self.current_room]["items"].remove(item) print(f"你捡起了{item}!") else: print("这里没有物品可拿。") def solve_puzzle(self): if "puzzle" in self.rooms[self.current_room]: print(f"\n谜题: {self.rooms[self.current_room]['puzzle']}") answer = input("你的答案是: ").strip().lower() if answer == self.rooms[self.current_room]["answer"].lower(): print("正确! 你获得了奖励!") self.inventory.append(self.rooms[self.current_room]["reward"]) print(f"你获得了{self.rooms[self.current_room]['reward']}!") del self.rooms[self.current_room]["puzzle"] else: print("答案错误!") else: print("这里没有谜题需要解决。") def heal(self): if "healer" in self.rooms[self.current_room]: print("精灵们用魔法治愈了你!") self.player_health = min(100, self.player_health + 50) print(f"你的生命值恢复到了{self.player_health}!") else: print("这里没有人能治疗你。") def play(self): print("欢迎来到冒险游戏!") print("输入 '帮助' 查看游戏指令") while not self.game_over: self.print_status() command = input("\n你要做什么? ").lower().split() if not command: continue if command[0] == "帮助": print("\n可用命令:") print("移动 [方向] - 向北/南/东/西移动") print("拿取 - 捡起当前房间的物品") print("攻击 - 与敌人战斗") print("解谜 - 尝试解答谜题") print("治疗 - 恢复生命值") print("物品 - 查看你的物品") print("退出 - 结束游戏") elif command[0] == "移动" and len(command) > 1: self.move(command[1]) elif command[0] == "拿取": self.take_item() elif command[0] == "攻击" and "enemy" in self.rooms[self.current_room]: self.fight_enemy() elif command[0] == "解谜": self.solve_puzzle() elif command[0] == "治疗": self.heal() elif command[0] == "物品": print(f"\n你的物品: {', '.join(self.inventory) if self.inventory else '无'}") elif command[0] == "退出": print("游戏结束!") self.game_over = True else: print("无效命令! 输入'帮助'查看可用命令。") # 检查玩家是否死亡 if self.player_health <= 0: self.game_over = True # 启动游戏 if __name__ == "__main__": game = AdventureGame() game.play() ``` 1. 游戏使用面向对象设计,便于扩展新功能 2. 房间数据存储在字典结构中,方便修改和添加新场景 3. 战斗系统包含随机伤害计算,增加游戏不可预测性 [2025-04-19 08:36:40 | AI写代码神器 | 1847点数解答]

相关提问