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 trialRobert Baucus
3,400 PointsDungeon_game, trying to print out a sweet ASCII monster image, doesn't come out right
So after much banging my head on the wall and some help from the knowledgeable folks here at Treehouse , I got my program to work. I even put in some custom error messages and jazzed it up a notch.
The problem is that the sweet ASCII text image that i print when the player hits a monster does not come out right: there is apparently a couple of lines shifted when it runs in the program.
I saved the ASCII text in between triple quotes and put that into a variable and printed that variable , here is the code:
import random
import os
import sys
error_messages = [
"Not without a shovel, mate!",
"Sorry guv, you've hit the wall!",
"Ain't gonna happen, brev!",
"Stop knockin' yer 'ead on the wall guv'na ! Turn around!",
"Ain't going no furver, mate!",
"Stop slammin the wall, ya cheeky bastard!",
"Hear that clunk? That's your head banging the wall!",
"Go ahead, keep banging into the wall, see what I care!"
]
monster_image = '''\\
,--, ,.-.
, \, '-,-`,'-.' | ._
/| \ , |\ } )/ / `-,',
[ ' |\ /| | | / \| |/` ,`
| | ,.` `,` `, | | _,...( ( _',
\ \ __ ,-` ` , , `/ |,' Y ( \_L\
\ \_\,``, ` , , / | ) _,/
\ ' ` ,_ _`_,-,<._.< / /
', `>.,` ` ` ,., |_ | /
\/` `, ` ,` | /__,.-` _, `\
-,-..\ _ \ ` / , / `._) _,-\` \
\_,,.) /\ ` / / ) (-,, `` , |
,` ) | \_\ '-` | `( \
/ /```( , --, ,' \ |`<` , |
/ /_,--`\ <\ V /> ,` )<_/) | \ _____)
,-, ,` ` (_,\ \ | /) / __/ / `----`
(-, \ ) \ ('_.-._)/ /,` /
| / ` `/ \\ V V, /` /
,--\( , <_/`\\ || /
( ,``- \/| \-A.A-`| /
,>,_ )_,..( )\ -,,_-` _--`
(_ \|` _,/_ / \_ ,--`
\( ` <.,../` `-.._ _,-`
` ```
'''
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 clear_screen():
os.system('cls' if os.name == 'nt' else 'clear')
def get_locations():
return random.sample(CELLS,3)
def move_player(player, move):
x, y = player
if move == 'LEFT':
x -= 1
if x < 0:
x = 0
if move == 'RIGHT':
x += 1
if x > 4:
x = 4
if move == 'UP':
y -= 1
if y < 0:
y = 0
if move == 'DOWN':
y += 1
if y > 4:
y = 4
return x,y
def get_move(player):
move = ["LEFT", "RIGHT", "UP", "DOWN"]
x, y = player
if x == 0:
move.remove("LEFT")
if x == 4:
move.remove("RIGHT")
if y == 0:
move.remove("UP")
if y == 4:
move.remove("DOWN")
return move
monster, door, player = get_locations()
while True:
valid_moves = get_move(player)
print("Welcome to the DUNGEON !")
print("You are currently in room {}".format(player))
print("You can move {}".format(", ".join(valid_moves)))
print("Enter QUIT to quit")
move = input("> ")
move = move.upper()
if player == door:
print("")
print("")
print(" BY JOVE, YOU'VE DONE IT , YOU HAVE FOUND THE DOOR AND LEFT THE MAZE! ")
break
if player == monster:
print("")
print("")
print("\n")
print(monster_image)
print("RUN AWAY, IT'S A MONSTER!!!")
break
if move == 'QUIT':
print("I guess you were afraid of the DUNGEON!")
break
if move in valid_moves:
player = move_player(player, move)
else:
print(random.choice(error_messages))
continue
clear_screen()
The actual image comes out like this though:
\
,--, ,.-.
, \, '-,-,'-.' | ._
/| \ , |\ } )/ /
-,',
[ ' |\ /| | | / | |/,
| | ,.,
, | | ,...( ( _',
\ \ _ ,-
, ,
/ |,' Y ( \_L \ \_\,
,
, , / | ) ,/
\ ' ` , `,-,<..< / /
', >.,
,., | | /
\/
,
,
| /,.-_,
-,-..\ _ \ / , /
.) _,-` _,,.) /\ / / ) (-,,
, |
,
) | _\ '-|
( / /``( , --, ,' \ |
<` , |
/ /,--\ <\ V /> ,
)</) | \ _)
,-, , (,\ \ | /) / _/ /
----
(-, \ ) \ ('.-.)/ /,/
| /
/ \ V V, /
/
,--( , </\ || /
( ,
- \/| \-A.A-
| /
,>,_ ),..( )\ -,,-_--
(_ |_,/_ / \_ ,--
( <.,../
-.._ _,-
``
RUN AWAY, IT'S A MONSTER!!!
I am not sure why it is not coming out right. I hope it is something simple that can be changed to fix it, otherwise I am not sure if a huge amount of work is worth it for something so silly
1 Answer
Steven Parker
230,995 PointsTo avoid having a backslash ("\
") treated as a special "escape" character, put the string in "raw" mode by adding the letter "R" (or "r") in front of the first quote:
monster_image = R'''
,--, ,.-.
, \, '-,-`,'-.' | ._
/| \ , |\ } )/ / `-,',
[ ' |\ /| | | / \| |/` ,`
| | ,.` `,` `, | | _,...( ( _',
\ \ __ ,-` ` , , `/ |,' Y ( \_L\
\ \_\,``, ` , , / | ) _,/
\ ' ` ,_ _`_,-,<._.< / /
', `>.,` ` ` ,., |_ | /
\/` `, ` ,` | /__,.-` _, `\
-,-..\ _ \ ` / , / `._) _,-\` \
\_,,.) /\ ` / / ) (-,, `` , |
,` ) | \_\ '-` | `( \
/ /```( , --, ,' \ |`<` , |
/ /_,--`\ <\ V /> ,` )<_/) | \ _____)
,-, ,` ` (_,\ \ | /) / __/ / `----`
(-, \ ) \ ('_.-._)/ /,` /
| / ` `/ \\ V V, /` /
,--\( , <_/`\\ || /
( ,``- \/| \-A.A-`| /
,>,_ )_,..( )\ -,,_-` _--`
(_ \|` _,/_ / \_ ,--`
\( ` <.,../` `-.._ _,-`
` ```
'''
I also removed some stray backslashes and adjusted the spacing on the first line.
Robert Baucus
3,400 PointsRobert Baucus
3,400 PointsThanks! As Sean Connery's character said in "Finding Forester",
"You're the man now dog!"