Home > Software engineering >  Turning main gameplay loop into function
Turning main gameplay loop into function

Time:10-16

For my first scripting project I had to create a text based game and I am having trouble turning my gameplay loop into a function (which is required). We are suppose to have our dictionary within the function and name it def main():. I can't figure out how to do this for the life of me without getting errors for my other two functions and it's variables. I provided my working code below. I appreciate the help (hardcore newbie at 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)

CodePudding user response:

If I understand correctly, your gameplay is everything in the while True loop. I would put that code into a function and then run the while loop over the function like so:

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()

Edit: You also had an indentation error on line 46. Edit 2: make sure when you are using variables with other functions, you either pass them as arguments, or you set them to global.

  • Related