Python文本类校园游戏开发求助:反派随机定位与物品收集逻辑优化
基于文本的Python游戏问题修复方案
问题1:实现反派在两个出口随机出现
解决步骤
- 导入
random模块,游戏启动时随机指定反派位于window或front door其中一个出口 - 给
Outside房间添加这两个出口选项 - 玩家尝试从这两个出口离开时,判断是否遭遇反派,触发对应结局(失败/成功)
问题2:优化look around逻辑
解决步骤
- 统一所有房间的
contents格式为列表(原代码中Kitchen的contents是字符串,需修正) - 给每个有可收集物品的房间单独设置
item_text字段,彻底分离房间进入文本和物品提示文本 - 修改look around逻辑:检查当前房间剩余未收集物品,有则显示对应提示,无则提示无物可看
- 收集物品时从房间的
contents中移除该物品,确保已收集的物品不再重复显示提示
修正后的完整代码
import random rooms = { 'Outside': { 'name': 'Outside', 'North': 'Dining Room', 'window': 'Escape Attempt', 'front door': 'Escape Attempt', 'text': 'You are outside and stare at the back door to the house.' }, 'Dining Room': { 'name': 'Dining Room', 'North': 'Living Room', 'contents': ['vase'], 'West': 'Kitchen', 'text': 'You are in the dining room.', 'item_text': 'There is a vase in this room... It looks valuable.' }, 'Kitchen': { 'name': 'Kitchen', 'East': 'Dining Room', 'contents': ['Blender'], 'text': 'You are in the kitchen.', 'item_text': 'There is a blender on the counter.' }, 'Living Room': { 'name': 'Living Room', 'South': 'Dining Room', 'East': 'Stairs', 'West': 'Bathroom', 'contents': ['TV'], 'text': 'You are in the Living room.', 'item_text': 'There is a TV on the wall.' }, 'Bathroom': { 'name': 'Bathroom', 'East': 'Living Room', 'contents': ['Candle'], 'text': 'You are in the Bathroom.', 'item_text': 'There is a candle on the counter.' }, 'Stairs': { 'name': 'Stairs', 'North': 'Hallway', 'West': 'Living Room', 'text': 'You are in the stairway. There is a hallway at the top.' }, 'Hallway': { 'name': 'Hallway', 'East': 'Bedroom', 'West': 'Kids Bedroom', 'text': 'You are in the hallway. There are bedrooms on either side of you.' }, 'Bedroom': { 'name': 'Bedroom', 'West': 'Hallway', 'contents': ['Jewelry'], 'text': 'You are in the bedroom.', 'item_text': 'There is jewelry on the dresser.' }, 'Kids Bedroom': { 'name': 'Kids Bedroom', 'East': 'Hallway', 'contents': ['Music box'], 'text': 'You are in a kids bedroom.', 'item_text': 'There is a music box on a shelf.' } } # 随机指定反派位置 villain_location = random.choice(['window', 'front door']) directions = ['North', 'South', 'East', 'West', 'window', 'front door'] currentRoom = rooms['Outside'] carrying = [] print('Hello. You are outside') while True: # 显示当前位置文本 print() print(currentRoom['text']) # 获取玩家输入 command = input('\nWhat next? ').strip() # 移动逻辑 if command in directions: if command in currentRoom: # 处理出口尝试 if currentRoom['name'] == 'Outside' and command in ['window', 'front door']: if command == villain_location: print('Oh no! You ran right into the villain. Game over!') break else: print('You escaped safely! You win!') break # 正常房间移动 currentRoom = rooms[currentRoom[command]] else: print("You can't go that way.") # look around逻辑 elif command.lower() == 'look around': # 检查当前房间是否有未收集物品 if 'contents' in currentRoom and len(currentRoom['contents']) > 0: print(currentRoom['item_text']) else: print("There is nothing to see.") # 退出游戏 elif command.lower() in ('e', 'exit'): break # 收集物品逻辑 elif command.lower().split()[0] == 'get': if len(command.split()) < 2: print('Specify an item to get!') continue item = command.lower().split()[1] # 匹配物品(忽略大小写) found_item = None if 'contents' in currentRoom: for i in currentRoom['contents']: if i.lower() == item: found_item = i break if found_item: carrying.append(found_item) currentRoom['contents'].remove(found_item) print(f'You stole the {found_item}!') else: print("That object is not in this room.")
内容的提问来源于stack exchange,提问作者moocow47
相关产品推荐
相关产品推荐

