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
Emmanuel Obi
14,912 PointsAnother Dungeon Game -- Any Feedback Welcome :)
Kenneth Love , feedback on my approach (specifically concern separation) would be greatly appreciated.
import random
input = raw_input # delete if using python3
#-- GAME SETUP --------------------------------->>>
SIZE = 10
def generate_board(size):
x_coords = range(size)
y_coords = x_coords
board = []
for x_coord in x_coords:
for y_coord in y_coords:
board.append((x_coord, y_coord))
return board
BOARD = generate_board(SIZE) # should be a constant...
def get_location(num, board):
locations = []
for i in range(num):
locations.append(random.choice(board))
while i >= 1 and locations[i-1] == locations[i]:
locations[i] = random.choice(board)
return locations
PLAYER, MONSTER, DOOR = get_location(3, BOARD) # not constants...
#-- GAME MECHANICS --------------------------------->>>
def render_board(size, player, door):
player_x, player_y = player
#monster_x, monster_y = monster
door_x, door_y = door
board_arr = [['[ ]' for i in range(size)] for j in range(size)]
board_arr[player_x][player_y] = '[X]'
board_arr[door_x][door_y] = '[#]'
for row in board_arr:
print(''.join(row))
available_moves = list('LEFT RIGHT UP DOWN J L I K'.split())
def move_player(player, move):
if move == 'LEFT' or move == 'J':
player = (player[0], player[1] - 1)
elif move == 'RIGHT' or move == 'L':
player = (player[0], player[1] + 1)
elif move == 'UP' or move == 'I':
player = (player[0] - 1, player[1])
elif move == 'DOWN' or move == 'K':
player = (player[0] + 1, player[1])
return player
def game_over(player):
if player == DOOR:
print("\n\nCONGRATULATIONS!!! You've won.\n")
return True
elif player == MONSTER:
print("\n\nEEEEKK! ..you've been devoured by the MONSTER at position {}..".format(MONSTER))
return True
else:
return False
available_commands = list('HELP QUIT WHEREAMI'.split()) + available_moves
command_descriptions = list('Get a list of available commands. | End gameplay. | Find out where you are on the board. | Move one unit left. | Move one unit right. | Move one unit up. | Move one unit down.'.split('|'))
#-- GAME HELPERS --------------------------------->>>
def show_help():
print('\nHere is a list of available commands:')
print('-'*25)
command_dictionary = { k:v for (k,v) in zip(available_commands, command_descriptions) }
print('QUIT: {QUIT}\nHELP: {HELP}\nWHEREAMI: {WHEREAMI}\nLEFT (J): {LEFT}\nRIGHT (L): {RIGHT}\nUP (I): {UP}\nDOWN (K): {DOWN}'.format(**command_dictionary))
def show_location():
print("You are here, {}".format(PLAYER))
#-- ACTUAL GAME --------------------------------->>>
print('Welcome to the game. Your are here {}, and the door is here {}. GO!'.format(PLAYER, DOOR))
while True: # game loop
print('\nChoose a direction')
try:
move = input('> ').upper()
if move not in available_commands:
raise Warning("Unknown command entered.")
except:
print("\n\n***Please enter a known command. Enter 'HELP' for details.***\n\n")
if move == 'QUIT':
break
elif move == 'HELP':
show_help()
elif move == 'WHEREAMI':
show_location()
else:
# Make the monster "faster"
MONSTER = get_location(1, BOARD)
PLAYER = move_player(PLAYER, move)
# MONSTER = get_location(1)
render_board(SIZE, PLAYER, DOOR)
if game_over(PLAYER):
break
1 Answer
John Steer-Fowler
Courses Plus Student 11,734 PointsHi Emmanuel,
I played your first game, is this not the same game?