[英]python-text based game- adding it to the inventory with no duplicates
[英]python text hockey game, issue adding items to inventory
我正在尝试为学校做一个文字游戏,但在将物品添加到库存时遇到问题,我可以在房间之间移动,但库存是我现在的动力。 无论我如何设置获取项目,我都会不断收到错误,我有问题。 有没有一种简单的方法可以添加我缺少的这个功能?
# Game Description
# The main character is an ice hockey goalie for the local home team.
# Their cities rival is playing them tonight and the opposing team’s goalie broke in to the locker room
# and hid the home teams goalie equipment all over the building to try and rattle him causing him to lose time warming up.
# This includes his helmet, chest armor, blocker, glove, and each leg pad.
# Time is clicking off and the home team goalie needs to find his stuff so he can get dressed and warm up.
rooms = {
'Front Lobby': {'East': 'Ice Rink', 'Item': 'Glove'},
'Ice Rink': {'North': 'Away Locker Room', 'West': 'Front Lobby', 'East': 'Mens Locker Room', 'South': 'Lounge', 'Item': 'Helmet'},
'Away Locker Room': {'East': 'Pro Shop', 'South': 'Ice Rink'},
'Pro Shop': {'West': 'Away Locker Room', 'Item': 'Villian'},
'Mens Locker Room': {'West': 'Ice Rink', 'Item': 'Leg Pad 2'},
'Lounge': {'West': 'Womens Locker Room', 'East': 'Skate Rentals', 'North': 'Ice Rink', 'Item': 'Blocker'},
'Womens Locker Room': {'East': 'Lounge', 'Item': 'Leg Pad 1'},
'Skate Rentals': {'West': 'Lounge', 'Item': 'Chest Armor'}
}
gameOn = True
inventory = []
#moves player from room to room
def move(player, direction):
global rooms
current_room = player
# check if there is a room in the specified direction
if direction in rooms[current_room]:
current_room = rooms[current_room][direction]
# error handling
else:
print("There is nothing in that direction!")
return player # Returning the same room if nothing Exists in the direction
# return the player state
return current_room # Indent Error
#displays rules at start of game
def showRules():
print("- Collect 6 items to win the game, or have to get clowned by the other goalie.\n" "Move commands: go South, go North, go East, go West.\n" "Add to Inventory: get 'item name'. Once all items collected type: ‘Finish’")
def main():
showRules()
player = "Front Lobby"
while gameOn:
current_room = player
# output
print(f"\nYou are in the {current_room}")
print('Inventory: ', inventory)
# Goalie got you
if player == 'Pro Shop':
print('You make more excuses than saves. GAME OVER')
break
# input validation and parsing
print("----------------")
player_move = input("Enter your move:\n")
# invalid move syntax
if 'go' in player_move or 'Finish' in player_move:
# split string
action = player_move.split(' ')
print(action) # move
if action[0] == 'go':
player = move(player, action[1]) # Assigning the value to player
elif action == 'Finish':
if len(inventory) == 6:
print('Awesome! Now get on the ice! You Win.')
else:
print('Keep collecting items')
# invalid action
else:
print("Invalid command!")
elif 'get' in player_move:
action = player_move.split(' ')
getItem(current_room, action[1])
else:
print('Invalid Command')
continue
def getItem(player, item):
current_room = player
if item in rooms[current_room]:
inventory.append(item)
print(inventory)
main()
更新代码 11:05 EST
尝试像这样编写您的 getItem function :
def getItem(current_room, item):
if item == rooms[current_room]["Item"]:
inventory.append(item)
print(inventory)
解释:在你原来的表达式item in rooms[current_room]
中,rooms[current_room] 指向一个字典,所以x in dict
表达式将检查x
是否是该字典的键的一部分。 但如果我理解得很好,你只对该字典的“Item”部分感兴趣,你必须与值而不是键进行比较。
我通过我的 Python 编译器运行它并提出语法错误。 您错过了main()
中最后一个 if/elif/else 块中块中的第二个条件和操作。 我不太了解,但我认为代码可能是这样的:
elif 'get' in player_move:
action = player_move.split(' ')
if action[1] == <second condition here>: #The second condition was missed here
<action> #The action was missed here
IDK 关于你想要的代码。
我不完全确定,但我认为move()
function 有问题。尝试其他答案,将此片段添加到getItem()
:
if item == rooms[current_room]["Item"]:
inventory.append(item)
print(inventory)
如果这不起作用,请给我发送一个 email:shivernscorch@gmail.com,我们可以从那里获取。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.