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
Gordon Reeder
2,521 PointsImprovments to dungon.py
I made a few improvements to dungon.py. taking it piece by piece...
# Dungon Game.
import random
# Define some game paramerers
Dungon_size_x = 4 # Size of dungon
Dungon_size_y = 4
CELLS = [(0,0), (1,0), (2,0), (3,0),
(0,1), (1,1), (2,1), (3,1),
(0,2), (1,2), (2,2), (3,2),
(0,3), (1,3), (2,3), (3,3)]
Eventually i want the size of the dungon and all functions to use the Dungon_size_x and Dungon_size_y variables. That way the dungon size can be changed by making one edit. I also made the dungon bigger by increasing the size of CELLS. To my mind x is horizontal and y is vertical. So I changed that too.
def get_locations():
monster = random.choice(CELLS)
door = random.choice(CELLS)
start = random.choice(CELLS)
weapon = random.choice(CELLS)
if monster == door or monster == start or start == door or start == weapon:
return get_locations()
return monster, door, start, weapon
I added a wepon to the dungon to give the play er a fighting chance. But I don't check to see if the monster is sitting on it or if it's at the door. i didn't say I was completely fair.
def move_player(player, move):
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']
if player[0] == 0:
moves.remove('LEFT')
if player[0] == 3:
moves.remove('RIGHT')
if player[1] == 0:
moves.remove('UP')
if player[1] == 3:
moves.remove('DOWN')
return moves
In the two functions above, changes were made to reflect the fact that x is now left/right. draw_map was modified to draw the larger map and also it looks for the right hand column in the first if statement.
def draw_map(player):
print(" _ _ _ _ ")
tile = '|{}'
for idx, cell in enumerate(CELLS):
if idx in [3, 7, 11, 15]:
if cell == player:
print(tile.format('X|'))
else:
print(tile.format('_|'))
else:
if cell == player:
print(tile.format('X'), end='')
else:
print(tile.format('_'), end='')
monster, door, player, sword = get_locations()
has_sword = False #player hasn't found the sword yet.
print("""
Welcome to the dungon!
Find your way to the door.
But watch out for the hungry grue! """)
while True:
moves = get_moves(player)
draw_map(player)
print("You are in room{}".format(player))
print("You can move {}".format(moves))
print("enter 'QUIT' to Quit")
move = input("> ")
move = move.upper()
if move == 'QUIT':
break
this next part is good for debugging.
if move == 'CHEAT':
print("monster is in {}".format(monster))
print("sword is in {}".format(sword))
print("door is at {}".format(door))
continue
An extra conditional was added to take into account that the player could enter 'foop' as a move and get a "stop running into the walls" message. the extra conditional causes the program to respond with "What?"
if move in ['LEFT', 'RIGHT', 'UP', 'DOWN']:
if move in moves:
player = move_player(player, move)
else:
print("** Walls are hard. Stop walking into them! **")
continue
else:
print("** What??")
continue
if player == door:
print("You found the Door. You have escaped\n")
break
A few mods to take into account that the player may be carriying a sword. Hmmm... After slaying the monster I probably should move him out of the dungon.
elif player == monster:
if has_sword == True:
print("** You have slain the grue!")
else:
print("Chomp, Chomp: you just got eaten by the grue")
break
elif player == sword:
print("** You found a sword!")
has_sword = True
Gordon Reeder
2,521 PointsRobert: I like the idea of a key to unlock the door. I would just have the key placed in a random location and have the player find it, like the sword. Slaying of the grue would be kept optional, but avoiding him would be mandatory (unless the player had the sword).
As the code is currently written, it is possible to find the sword twice, and also to slay the grue twice. So a tweek of the code is needed to move the monster and the sword out of the dungon when they are found/slain.
Oh great. i wanted to keep moving forward with the lessons. But now I just want to keep playing with this game. :-)
2 Answers
Kenneth Love
Treehouse Guest TeacherWow, those are excellent additions! Sorry about the X/Y thing. Brain fart that made it all the way through production.
I'd almost say, with these additions, that you should make the monster chase the player. I'd also say you show the monster on the map, too. Keep the door and the sword/key hidden, though. That's the real challenge in the game, finding the way out.
Gordon Reeder
2,521 PointsPerhaps later. At this point I have implemented the key. But, I've got to get back to the lessons.
Robert Richey
Courses Plus Student 16,352 PointsRobert Richey
Courses Plus Student 16,352 PointsThat's really cool Gordon. I've been wanting to do my own dungeon makeover for a while but keep getting side-tracked. I like the inclusion of a sword - that got me thinking about starting the door in a locked state; kill the grue to get the key to unlock the door.
Thanks for sharing :)