對于我的第一個腳本專案,我必須創建一個基于文本的游戲,但我無法將我的游戲回圈轉換為一個函式(這是必需的)。我們假設在函式中擁有我們的字典并將其命名為def main():。我無法弄清楚如何在我的生活中做到這一點而不會出現其他兩個函式及其變數的錯誤。我在下面提供了我的作業代碼。我感謝幫助(python 的鐵桿新手)!
def show_instructions():
print('\nWelcome to Cat and Mouse Adventure Game!\n')
print('Collect all 7 items to win the game, or else get eaten by the cat!')
print('Move commands: go North, go South, go East, go West')
print('Add item to Inventory: get \'item name\' ')
def player_stats():
print('_' * 20)
print('You are in the {}'.format(current_room))
print('Inventory: ' str(inventory))
if 'item' in rooms[current_room]:
print('You see ' rooms[current_room]['item'])
print('_' * 20)
rooms = {
'Living Room': {'South': 'Study', 'North': 'Kitchen', 'East': 'Bedroom', 'West': 'Bathroom'},
'Bedroom': {'North': 'Dining Room', 'West': 'Living Room', 'South': 'Sunroom', 'item': 'Glasses'},
'Dining Room': {'West': 'Kitchen', 'South': 'Bedroom', 'item': 'Napkin'},
'Kitchen': {'South': 'Living Room', 'East': 'Dining Room', 'West': 'Basement', 'item': 'Cheese'},
'Basement': {'East': 'Kitchen', 'South': 'Bathroom', 'item': 'Catnip'},
'Bathroom': {'East': 'Living Room', 'North': 'Basement', 'South': 'Walk-in Closet', 'item': 'Water'},
'Walk-in Closet': {'North': 'Bathroom', 'East': 'Study', 'item': 'Shoes'},
'Study': {'North': 'Living Room', 'East': 'Sunroom', 'West': 'Walk-in Closet', 'item': 'Cotton'},
'Sunroom': {'North': 'Bedroom', 'West': 'Study', 'item': 'Cat'} # villian room
}
current_room = 'Living Room'
inventory = []
show_instructions()
while True:
player_stats()
player_move = ''
while player_move == '':
player_move = input('Enter your move:\n').title()
if player_move == 'Go North' or player_move == 'Go South' or player_move == 'Go East' or player_move == 'Go West':
player_move = player_move[3:]
if player_move not in rooms[current_room]:
print('That is not a valid move, enter another.')
else:
current_room = rooms[current_room][player_move]
elif player_move[0:3] == 'Get':
if 'item' not in rooms[current_room] or player_move[4:] not in rooms[current_room]['item']:
print('Can\'t get {}!'.format(player_move[4:]))
else:
inventory = [player_move[4:]]
print(player_move[4:] ' retrieved!')
del rooms[current_room]['item']
if current_room == 'Sunroom':
print('OH NO! The cat found you and ate you up!')
print('GAME OVER!')
exit(0)
if len(inventory) == 7:
print('\nCongratulations! You collected all 7 items to build a shelter and avoid the cat!')
exit(0)
if player_move == 'Exit':
print('Play again soon!')
exit(0)
uj5u.com熱心網友回復:
如果我理解正確,您的游戲玩法就是while True回圈中的一切。我會將該代碼放入一個函式中,然后在該函式上運行 while 回圈,如下所示:
def show_instructions():
print('\nWelcome to Cat and Mouse Adventure Game!\n')
print('Collect all 7 items to win the game, or else get eaten by the cat!')
print('Move commands: go North, go South, go East, go West')
print('Add item to Inventory: get \'item name\' ')
def player_stats():
global current_room
global inventory
print('_' * 20)
print('You are in the {}'.format(current_room))
print('Inventory: ' str(inventory))
if 'item' in rooms[current_room]:
print('You see ' rooms[current_room]['item'])
print('_' * 20)
rooms = {
'Living Room': {'South': 'Study', 'North': 'Kitchen', 'East': 'Bedroom', 'West': 'Bathroom'},
'Bedroom': {'North': 'Dining Room', 'West': 'Living Room', 'South': 'Sunroom', 'item': 'Glasses'},
'Dining Room': {'West': 'Kitchen', 'South': 'Bedroom', 'item': 'Napkin'},
'Kitchen': {'South': 'Living Room', 'East': 'Dining Room', 'West': 'Basement', 'item': 'Cheese'},
'Basement': {'East': 'Kitchen', 'South': 'Bathroom', 'item': 'Catnip'},
'Bathroom': {'East': 'Living Room', 'North': 'Basement', 'South': 'Walk-in Closet', 'item': 'Water'},
'Walk-in Closet': {'North': 'Bathroom', 'East': 'Study', 'item': 'Shoes'},
'Study': {'North': 'Living Room', 'East': 'Sunroom', 'West': 'Walk-in Closet', 'item': 'Cotton'},
'Sunroom': {'North': 'Bedroom', 'West': 'Study', 'item': 'Cat'} # villian room
}
global current_room
current_room = 'Living Room'
global inventory
inventory = []
show_instructions()
def main():
global current_room
global inventory
player_stats()
player_move = ''
while player_move == '':
player_move = input('Enter your move:\n').title()
if player_move == 'Go North' or player_move == 'Go South' or player_move == 'Go East' or player_move == 'Go West':
player_move = player_move[3:]
if player_move not in rooms[current_room]:
print('That is not a valid move, enter another.')
else:
current_room = rooms[current_room][player_move]
elif player_move[0:3] == 'Get':
if 'item' not in rooms[current_room] or player_move[4:] not in rooms[current_room]['item']:
print('Can\'t get {}!'.format(player_move[4:]))
else:
inventory = [player_move[4:]]
print(player_move[4:] ' retrieved!')
del rooms[current_room]['item']
if current_room == 'Sunroom':
print('OH NO! The cat found you and ate you up!')
print('GAME OVER!')
exit(0)
if len(inventory) == 7:
print('\nCongratulations! You collected all 7 items to build a shelter and avoid the cat!')
exit(0)
if player_move == 'Exit':
print('Play again soon!')
exit(0)
while True:
main()
編輯:您在第 46 行也有一個縮進錯誤。編輯 2:確保當您將變數與其他函式一起使用時,您要么將它們作為引數傳遞,要么將它們設定為全域。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/318280.html
上一篇:無法在反應中將道具傳遞到dict
