#创作计划# 自制游戏——密室逃脱1.7
2025-07-22 14:15:14
发布于:上海
1.1 完成框架
1.2 添加“房间”
1.3 添加“物品”
1.4 添加“操作”
1.5 添加“错误提示”
1.6 添加“提前结束”
1.7 添加“你通关了”
import time
class AdventureGame:
    def __init__(self):
        self.rooms = {
            "大厅": {
                "描述": "你站在一个宽敞的大厅,四周空荡荡的,北边有一个大门。",
                "北": "大门",
                "东": "武器库",
                "南": "厨房",
                "物品": []
            },
            
            "武器库": {
                "描述": "这里摆满了各种武器,角落有一把闪亮的剑。",
                "西": "大厅",
                "物品": ["剑"]
            },
            "厨房": {
                "描述": "厨房里弥漫着腐烂的气味,有一个门通向地牢。",
                "北": "大厅",
                "东": "地牢",
                "物品": []
            },
            "地牢": {
                "描述": "阴暗潮湿的地牢,铁栅栏后似乎有东西在动...",
                "西": "厨房",
                "物品": ["钥匙"]
            },
            "大门": {
                "描述": "门被锁上了",
                "南": "大厅",
                "物品": []
            }
        }
        self.player = {
            "位置": "大厅",
            "物品": [],
            "生命值": 100
        }
    def play(self):
        print("欢迎来到文字冒险游戏!")
        time.sleep(1)
        
        while True:
            current_room = self.rooms[self.player["位置"]]
            print(f"\n{current_room['描述']}")
            if self.player["位置"] == "大门" and "钥匙" in self.player["物品"]:
                print("你用钥匙打开了锁,你通关了!");
                break
            
            print("可行动作:")
            print("1. 查看出口")
            print("2. 移动")
            print("3. 拾取物品")
            print("4. 查看背包")
            print("5. 退出游戏")
            
            choice = input("你的选择: ")
            
            if choice == "1":
                exits = [k for k in current_room.keys() if k in ["东","南","西","北"]]
                print("可用出口:", " ".join(exits))
            elif choice == "2":
                direction = input("方向(东/南/西/北): ")
                if direction in current_room:
                    self.player["位置"] = current_room[direction]
                else:
                    print("无效方向!")
            elif choice == "3":
                if current_room["物品"]:
                    item = current_room["物品"].pop()
                    self.player["物品"].append(item)
                    print(f"获得了{item}!")
                else:
                    print("这里没有物品")
            elif choice == "4":
                print("背包物品:", " ".join(self.player["物品"]))
            elif choice == "5":
                print("游戏结束!")
                break
            else:
                print("无效输入!")
game = AdventureGame()
game.play()
这里空空如也












有帮助,赞一个