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
Charles Willden
12,357 PointsDungeon Game with Emojis for Player, Monster, Door, Wall Collision
A fun way I came up with to make dungeon game a bit more fun was to add emojis to the map. The default character is a smiley face emoji. When the player encounters a wall, it changes to the emoji with the bandaged head. When the player encounters the monster, it shows the ogre emoji. When the player finds the exit, it shows the door. Here's the code
import random
import os
CELLS = [
(0, 0), (1, 0), (2, 0), (3, 0), (4, 0),
(0, 1), (1, 1), (2, 1), (3, 1), (4, 1),
(0, 2), (1, 2), (2, 2), (3, 2), (4, 2),
(0, 3), (1, 3), (2, 3), (3, 3), (4, 3),
(0, 4), (1, 4), (2, 4), (3, 4), (4, 4),
]
def get_locations():
return random.sample(CELLS, 3)
def move_player(player, move):
#get player's location
x, y = player
if move == 'LEFT':
x -= 1
elif move == 'RIGHT':
x+= 1
elif move == 'UP':
y -= 1
elif move == 'DOWN':
y += 1
return x, y
def get_moves(player):
moves = ['LEFT','RIGHT','UP','DOWN']
x, y = player
if x == 0:
moves.remove("LEFT")
elif x == 4:
moves.remove("RIGHT")
elif y == 0:
moves.remove("UP")
elif y == 4:
moves.remove("DOWN")
return moves
def clear_screen():
if os.name == 'nt':
os.system('cls')
else:
os.system('clear')
def draw_map(player, monster, door, hurt = False):
print(".__"*5 + ".")
tile = "|{}"
if player == monster:
emoji = '\N{Japanese Ogre}'
elif player == door:
emoji = '\N{door}'
elif hurt == True:
emoji = '\N{face with head-bandage}'
else:
emoji = '\N{slightly smiling face}'
for cell in CELLS:
x, y = cell
if x < 4:
line_end = ""
if cell == player:
output = tile.format(emoji)
else:
output = tile.format("__")
else:
line_end = "\n"
if cell == player:
output = tile.format(emoji+"|")
else:
output = tile.format("__|")
print(output, end=line_end)
def game_loop():
monster, door, player = get_locations()
playing = True
while playing:
valid_moves = get_moves(player)
draw_map(player, monster, door);
print("You're currently in the room {}".format(player))
print("You can move {}".format(", ".join(valid_moves)))
print("Enter QUIT to quit")
move = input("> ")
move = move.upper()
clear_screen()
if move == 'QUIT':
break
elif move not in valid_moves:
draw_map(player, monster, door, hurt=True)
input("\n ** Walls are hard! Don't run into them! **\n")
else:
player = move_player(player, move)
if player == door:
draw_map(player, monster, door);
print("You win! Congratulations")
playing = False
elif player == monster:
draw_map(player, monster, door);
print("Oh no! The monster has devoured you! YOU LOSE!!!")
playing = False
else:
if input('Play again? [y/n] ').lower() != 'n':
game_loop()
clear_screen()
print("Welcome to the dungeon!")
input("Press ENTER to start!")
clear_screen()
game_loop()
Attn: Kenneth Love
KRIS NIKOLAISEN
54,974 PointsSecond format works - thanks. Good job.
KRIS NIKOLAISEN
54,974 PointsKRIS NIKOLAISEN
54,974 PointsI'd be interested in seeing it but don't want to go through all the formatting to get it to run. Can you post a snapshot instead?