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 Python Collections (Retired) Dungeon Game The Assignment

Questions about the Dungeons Game

I just want to clarify one thing about the Dungeon's game:

1) Are the dragon and door locations unknown to the player throughout the game?

2) Does the dragon move at all?

Thanks for any clarification. Regards,

Hani

4 Answers

No, the location of door & dragons is not known to player. No, the dragon does not move. You could add that functionality though to make it interesting.

Okay, thanks for the clarification! I saw in the demo video that the dragon moved, so I just needed to know.

Keerthi Suria Kumar Arumugam
Keerthi Suria Kumar Arumugam
4,585 Points

Yeah. The video is confusing since they show that the dragon moves. Thanks for the clarification

Martin Krkoska
Martin Krkoska
10,514 Points

I tried something . I am not sure if everithing is ok , but feel free to look, try and comment my alfa version of the game :-)

import random
import os

def clear():
    if os.name == 'nt':
        os.system("clc")
    else:
        os.system("clear")

def dungeon_map_size():
    clear()
    width = int(input('what width do you want your dungeon in meters? : '))
    height = int(input('what height do you want your dungeon in meters? : '))
    hg = height
    dungeon_map = []
    while width:
        while height:
            dungeon_map.append((width,height))
            height -= 1
        height = hg 
        width -= 1
    return(dungeon_map)

def random_position(dungeon_map_size):
    #random position of monster / door and player
    door = random.choice(dungeon_map_size)
    monster = random.choice(dungeon_map_size)
    player = random.choice(dungeon_map_size)
    while door == monster or door == player or monster == player:
        door = random.choice(dungeon_map_size)
        monster = random.choice(dungeon_map_size)
        player = random.choice(dungeon_map_size)
    return ({"door" : door, "monster" : monster, "player" : player})


def print_map(dungeon_map_size,door_position,player_position,monster_position,old_track):
  x,y = max(dungeon_map_size)
  x_size = x
  y_size = y
  while x_size:
    print_map = ''
    while y_size:
      if (x_size,y_size) == player_position:
        print_map += " 0" 
      elif (x_size,y_size) in old_track:
        print_map += " +"
# show door position / to hide just comment
      elif (x_size,y_size) == door_position:
        print_map += " D" 
# show monster position / to hide just comment
      elif (x_size,y_size) == monster_position:
        print_map += " X"
      else:
        print_map += " #" 
      y_size -= 1
    y_size = y 
    x_size -= 1
    print(print_map)

def possible_moving(player_position,dungeon):
    x_max,y_max = max(dungeon)
    x,y = player_position
    list_of_moves = []
    if x != 1:
        list_of_moves.append('DOWN')
    if x != x_max:
        list_of_moves.append('UP')
    if y != 1:
        list_of_moves.append('RIGHT')
    if y != y_max:
        list_of_moves.append('LEFT')

    print ("""
You can move in this directions : {}
""".format( " ".join(list_of_moves))) 
    return(list_of_moves)

def win_lose(player_position, door_position, monster_position):
    if player_position == door_position:
        clear()
        print("""
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!        

X   X  XXX   X   X      X     X  XXX  XX   X
 X X  X   X  X   X      X  X  X X   X X X  X
  X   X   X  X   X       X X X  X   X X  X X
  X    XXX    XXX         X X    XXX  X   XX

   CONGRATULATION YOU JUST WON THE GAME

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
""")
        return(True)
    elif player_position == monster_position:
        clear()
        print("""
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!        

            XXXXXXX
          XX       XX
     XX  O   O  XX
    XX           XX
     XX  V---V  XX
          XX       XX
            XXXXXXX

    YOU ARE EATEN BY MONSTER

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
""")
        return(True)

def moving(possible_moving,player_position):
  x,y = player_position
  move = input("Where do you want to move > ")
  print (x,y)
  if move.upper() in possible_moving:
    if move.upper() == 'LEFT':
      y +=1
    elif move.upper() == 'RIGHT':
      y -=1
    elif move.upper() == 'UP':
      x +=1
    elif move.upper() == 'DOWN':
      x -=1
  elif move.upper() == 'HELP':
    global move 
    move = 2
  else:
    global move 
    move = 0
  return(x,y)

def print_header():
    print("""
*******************************************
              DUNGEON MAP
*******************************************
""")    

def keep_track(player_position, old_track):
  old_track.append(player_position)
  return (old_track)

def print_help():
    print('')
    print('''
0 - Hero
X - monster
D - Door
+ - Hero track
''')

def wrong_move(move):
  if move == 0:
    print('')
    print('You cannot move like this, try again')
  elif move == 2:
    print_help()
  return (1)

dungeon =   dungeon_map_size()
random_position = random_position(dungeon)
door_position = random_position['door']
player_position = random_position['player']
monster_position = random_position['monster']
old_track = []
move = 1
while True:
  clear()
  print_header()
  old_track = keep_track(player_position, old_track)
  print_map(dungeon,door_position,player_position,monster_position, old_track)
  move = wrong_move(move)
  poss = possible_moving(player_position,dungeon)
  player_position = moving(poss,player_position)
  if win_lose(player_position, door_position, monster_position):
    break