Ad
Text Based Game 'Exit Game' Process
Good evening, I am working on a text-based game for my scripting class. This week we are incrementing the "move between rooms" process. My code is working smoothly except I cannot get the user to exit the game. When I type "Exit" It tells me "Ouch! I hit a wall." Not sure why it's going through the if statement and not the elif statement I have for exiting the game.
# Nick Thomas
# declaration
rooms = {
'Ground Level': {'Right': 'Library', 'Up': 'Bedroom'},
'Library': {'Left': 'Ground Level', 'Up': 'Enchanting Lab'},
'Bedroom': {'Right': 'Enchanting Lab', 'Down': 'Ground Level'},
'Enchanting Lab': {'Left': 'Bedroom', 'Down': 'Library'}
}
state = 'Ground Level'
# function
def get_new_state(state, direction):
new_state = state # declaration
for i in rooms: # loop
if i == state: # if statement
if direction in rooms[i]: # if statement
new_state = rooms[i][direction] # assigns new_state
return new_state # return
# FIXME: a function for getting items
# function
def instructions():
# print a main menu and the commands
print('Necromancer’s Lair Text Game')
print('Move commands: go Up, go Down, go Left, go Right')
print('To Exit: type Exit')
# FIXME: add instructions for what do and how to get an item
instructions()
# FIXME: add empty inventory list
while 1:
print('You are in the', state) # prints state
print('------------------------------')
direction = input('Enter your move: ') # asking user
if direction == 'go Up' or 'go Down' or 'go Left' or 'go Right':
direction = direction[3:]
new_state = get_new_state(state, direction) # calling function
if new_state == state: # if statement
print('Ouch! You hit a wall.')
else:
state = new_state
elif direction == 'Exit':
print('Thanks for playing!')
exit(0)
else:
print('Invalid Direction!!')
Ad
Answer
you need to change the if condition as below where direction is compared against each direction i.e. go Up, go Down, go Left and go Right
if direction == 'go Up' or direction=='go Down' or direction=='go Left' or direction=='go Right':
Ad
source: stackoverflow.com
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module
Ad