获得文本游戏进入另一个房间并收集物品



我需要能够使用命令进入不同的房间,如go north,并通过输入get然后输入项目的名称来添加项目到库存。但是当我运行它并尝试获得一个项目时,它连续打印我的else语句。我甚至不知道我是否要去其他房间。

# this is a text game for my class IT140 intro into scripting
# Derek Pruitt 10/01/2021
# this is to have speech to text
# NOTE: user needs to install this with pip install pyttsx3
import pyttsx3
engine = pyttsx3.init()
# changing the voice to a female voice
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
# setting up new voice rate
engine.setProperty('rate', 125)
# engine.stop()
# welcome the player
engine.say('Hello, welcome to my text based game!')
print('Hello, welcome to my text based game!')
engine.say('what is your name')
engine.runAndWait()
player = input('What is your namen')
engine.say(player + 'we welcome you')
engine.runAndWait()
# creating player inventory, starts with nothing
inventory = []
# all the rooms with directional commands to connecting rooms, items in each room
rooms = {
'Cabin': {'West': 'Coat room'},
'Coat room': {'South': 'Operating room', 'East': 'Cabin', 'item': 'Key'},
'Operating room': {'South': 'Chemical room', 'North': 'Cabin',
'West': 'Armoire', 'East': 'Prayer room', 'item': 'Oil'},
'Armoire': {'East': 'Operating room', 'item': 'Crossbow'},
'Prayer room': {'West': 'Operating room', 'North': 'Sleeping chamber',
'item': 'Holy cross'},
'Sleeping chamber': {'South': 'Prayer room', 'item': 'The Skinner'},
'Chemical room': {'North': 'operating room', 'East': 'Meat room',
'item': 'poison'},
'Meat room': {'West': 'Chemical room', 'item': 'Cooked meat'}
}

def show_instructions():
# print a main menu and the commands
print("To quit the game at anytime, type 'Quit'.")
print("Collect 6 items to win the game, or be killed by the Skinner.")
print("Move commands: go South, go North, go East, go West")
print("Add to Inventory: get 'item name'")
# commands the player can use

# for putting items in player inventory
def get_item(item):
if command == 'get':
inventory.append(item)
print(inventory)
return

if __name__ == '__main__':
show_instructions()
command = input()
while command != 'Quit':
if command == 'go west':
print(rooms['Coat room'])
rooms['Coat room']
command = input()
else:
print('Please enter a valid command.')
show_instructions()
while rooms == rooms['Coat room']:
if command == 'go south':
print(rooms['Operating room'])
rooms['Operating room']
command = input()
elif command == 'go east':
print(rooms['Cabin'])
rooms['Cabin']
command == input()
elif command == 'get ' '':
get_item()
else:
print('Please enter a valid command.')
show_instructions()

这里有很多问题。我将制作一个项目符号列表,列出目前为止我找到的所有内容。

  • 大小写敏感性. 要小心你给用户的说明。你写了">goS嘴巴"但是你比较">gos嘴巴"。

注意:这也是(很可能)您获得第一个else的原因语句一遍又一遍地打印……您没有输入go west"rooms == rooms['Coat room']总是等于false,但不等于有意义吗?所以,你直接跳到第一个while的顶部,再和"去西部"比较一下;你回到了你的else部分。

  • 状态变量. 你需要某种状态变量来存储游戏的几个方面。例如,存储用户当前所在房间信息的变量。我假设你想用rooms['Coat room']更新房间。这条指令毫无意义。你需要current_room = rooms['Coat room']之类的东西。其他可能的状态变量包括game_won: boolean(表示玩家是否赢得游戏),person_killed: boolean(表示玩家是否被杀死并输掉游戏),以及inventory: List[String](你实际上已经拥有它了)。循环
  • . 你的嵌套循环没有任何意义。为什么你要把房间换到衣帽间,然后进入嵌套循环。你可以这样做:

不管什么原因,我必须在这里写一行。否则,Stackoverflow会破坏我的格式。

command = input()    
while command != 'Quit':
while not person_killed and not game_won:
# 1) execute command here 
# => update all state variables (person_killed, item_inventory, game_won, current_room)
# 2) print information to user
# 3) check if game is over (if game_won or person_killed is true) and eventually break the loop.
  • 打印语句. 您需要在每个操作之后打印语句通知用户在执行命令后实际发生了什么(例如在房间更改后)。

尝试使用else if样式,如下所示。

user = input("left or right r for right l for left")
if user == "l"
print("you went left")
elif user == "r"
print("you went right")
elif user not in("r", "l")
print("invalid input")
exit()

或点击此链接

相关内容

最新更新