Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

dungeon.py add-on issues

I have been adding on functionality, but I think my eyes were bigger than my stomach/brain in this case. Trying to break out functions for building the maze, among other things. I am very confused on how to call each function within other functions to make them run. Thought I was doing it correctly, but apparently not. Any advice/guidance?

'''python

import random

def welcome(): print('In the welcome function') monster, door, player = get_locations(DUNGEON) print('Welcome to the dungeon!') print('How big would you like the dungeon to be? (cannot be bigger than 10x10, enter one number to build a square dungeon)') size = input(int('> ')) if size > 0 and size <= 10: build_dungeon() else: print('You must enter a number from 1 to 10') welcome()

def build_dungeon(size): print('In the build dungeon function')

size = 8 # test size

DUNGEON = [] for x in range(size): for y in range(size): DUNGEON.append((x,y))

play_game()

def get_locations(DUNGEON): print('In the get locations function') monster = random.choice(DUNGEON) door = random.choice(DUNGEON) player = random.choice(DUNGEON)

if monster == door or monster == player or door == player: return get_locations() else: return monster, door, player

def move_player(player, move): print('In the move player function') x, y = player

if move == 'LEFT': y -= 1 elif move == 'RIGHT': y += 1 elif move == 'UP': x -= 1 elif move == 'DOWN': x += 1 return x, y

def move_monster(): print('In the move monster function') monster = random.choice(DUNGEON)

if monster == door: return move_monster() else: return monster

def get_moves(player): print('In the get moves function') moves = ['LEFT', 'RIGHT', 'UP', 'DOWN']

if player[1] == 0: moves.remove('LEFT') if player[0] == 0: moves.remove('UP') if player[1] == size - 1: moves.remove('RIGHT') if player[0] == size - 1: moves.remove('DOWN') return moves

def draw_map(player, DUNGEON): print('In the draw map function') dungeon_lid = ' _' * size print(dungeon_lid) tile = '|{}' dungeon_array = []

for i in range(len(DUNGEON)): if (i + 1) % (size) == 0: dungeon_array.append(i)

for idx, cell in enumerate(DUNGEON): if idx not in dungeon_array: if cell == player: print(tile.format('X'), end='') elif cell == monster: print(tile.format('O'), end='') else: print(tile.format('_'), end='')

else:
  if cell == player:
    print(tile.format('X|'))
  elif cell == monster:
    print(tile.format('O|'))
  else:
    print(tile.format('_|'))

def play_game(DUNGEON, player, monster, door): print('In the play game function') while True: moves = get_moves(player) print('You are currently in room {}'.format(player)) draw_map(player) print('You can move {}'.format(moves)) print('Enter QUIT to quit')

move = input('> ')
move = move.upper()

if move == 'QUIT':
  break

if move in moves:
  player = move_player(player, move)
  monster = move_monster()
else:
  print('** walls are hard...stop walking into them **')
  continue

if player == door:
  print("Congratulations, adventuror! You've escaped the dungeon!")
  break
elif player == monster:
  print("You've been ripped limb from limb by the monster.  Sorry about your life.")
  break

welcome()

'''

1 Answer

This reminds me. Doing the dungeon game, I kept feeling like...I wish there were a way to break it down into smaller chunks. Maybe tomorrow.