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

Dungeon Game Enhanced Issues

So after the dungeon game lesson, I went on to do a couple of things: a. make the dungeon bigger (no issue) b. trail breadcrumbs (issues solved) c. make monster move (big issue(the monster moves according to the player, instead of random, I don't know why)) d. make the monster move 1 or 2 steps(most likely will be solved after previous issue is) e. leave an ending message to keep game open on desktop program reader. (no issue)

So this code is the one I attempted to use, making the monster visible just for debugging purposes, or to show how random it moves, but it sometimes goes invisible, and follows the player:

If you have any advice or could hint me in the direction I need to go, it'd be appreciated.

Most if any problems with spacing here will be due to having to put 4 (may have put more or less) spaces in front of the code.

import random

CELLS = [(0, 0), (0, 1), (0, 2), (0, 3),
         (1, 0), (1, 1), (1, 2), (1, 3),
         (2, 0), (2, 1), (2, 2), (2, 3),
         (3, 0), (3, 1), (3, 2), (3, 3),
         (4, 0), (4, 1), (4, 2), (4, 3)]

ONETOTWO = (1,2)

dragon_set_moves = ['LEFT', 'RIGHT', 'UP', 'DOWN']

def get_locations():
  monster = random.choice(CELLS)
  door = random.choice(CELLS)
  start = random.choice(CELLS)

  if monster == door or door == start or start == monster:
    return get_locations()

  return monster, door, start

def move_player(player, move):
  x, y = player

  if move == 'LEFT':
    y -= 1
  elif move == 'RIGHT':
    y += 1
  elif move == 'UP':
    x -= 1
  elif move == 'DOWN':
    x += 1  

  return x, y

def move_monster(monster, m_move):
  s, t = monster

  if m_move == 'LEFT':
    t -= random.choice(ONETOTWO)
  elif m_move == 'RIGHT':
    t += random.choice(ONETOTWO)
  elif m_move == 'UP':
    s -= random.choice(ONETOTWO)
  elif m_move == 'DOWN':
    s += random.choice(ONETOTWO)

  return s, t

the_player = []

def get_moves(player):
  moves = ['LEFT', 'RIGHT', 'UP', 'DOWN']
  # player = (x,y)

  if player[1] == 0:
    moves.remove('LEFT')
  if player[1] == 3:
    moves.remove('RIGHT')
  if player[0] == 0:
    moves.remove('UP')
  if player[0] == 4:
    moves.remove('DOWN')
  return moves

def monster_moves(monster):
  m_moves = ['LEFT', 'RIGHT', 'UP', 'DOWN']
  # monster = (x,y)

  if monster[1] == 0:
    m_moves.remove('LEFT')
  if monster[1] == 3:
    m_moves.remove('RIGHT')
  if monster[0] == 0:
    m_moves.remove('UP')
  if monster[0] == 4:
    m_moves.remove('DOWN')
  return m_moves

def draw_map(player, monster):
  print(' _ _ _ _')
  tile = '|{}'

  for idx, cell in enumerate(CELLS):
    if idx in {0, 1, 2, 4, 5, 6, 8, 9, 10, 12, 13, 14, 16, 17, 18}:
      if cell == player:
        print(tile.format('X'),end='')
      elif cell == monster:
        print(tile.format('O'),end='')
      elif cell in the_player:
        print(tile.format('.'),end='')
      else:
        print(tile.format('_'),end='')
    else:
      if cell == player:
        print(tile.format('X|'))
     elif cell == monster:
       print(tile.format('O|'),end='')
     elif cell in the_player:
      print(tile.format('.|'))
    else: print(tile.format('_|'))

monster, door, player = get_locations()
print("Welcome to the dungeon!")

while True:
  moves = get_moves(player)

  print("You are currently in room {}".format(player))

  draw_map(player, monster)

  print("You can move {}".format(moves))
  print("Enter 'QUIT' to ragequit")

  move = input("> ")
  move = move.upper()
  the_player.append(player)

  dragon_move = random.choice(dragon_set_moves)
  dragon_moves = monster_moves(monster)

  if move == 'QUIT':
    break

  if move in moves:
    player = move_player(player, move)
  else:
    print("** Walls are hard, stop walking into them. **")
    continue

  if dragon_move in dragon_moves:
    monster = move_monster(monster, move)
  else:
    continue

  if player == door:
    print("You escaped!")
    leave = input("Great job, let's get outta here. ")
    break
  elif player == monster:
    print("You were eaten by the dragon.")
    leave = input("Damn, too bad, if you like having your arm chewed off, consider trying again!")
    break
Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

If you add the word python after the triple-backticks, you will get color coded formatting making it much easier to read. Thanks.

import random

CELLS = [(0, 0), (0, 1), (0, 2), (0, 3),
         (1, 0), (1, 1), (1, 2), (1, 3),
         (2, 0), (2, 1), (2, 2), (2, 3),
         (3, 0), (3, 1), (3, 2), (3, 3),
         (4, 0), (4, 1), (4, 2), (4, 3)]

ONETOTWO = (1,2)

dragon_set_moves = ['LEFT', 'RIGHT', 'UP', 'DOWN']

def get_locations():
  monster = random.choice(CELLS)
  door = random.choice(CELLS)
  start = random.choice(CELLS)

  if monster == door or door == start or start == monster:
    return get_locations()

  return monster, door, start

def move_player(player, move):
  x, y = player

  if move == 'LEFT':
    y -= 1
  elif move == 'RIGHT':
    y += 1
  elif move == 'UP':
    x -= 1
  elif move == 'DOWN':
    x += 1  

  return x, y

def move_monster(monster, m_move):
  s, t = monster

  if m_move == 'LEFT':
    t -= random.choice(ONETOTWO)
  elif m_move == 'RIGHT':
    t += random.choice(ONETOTWO)
  elif m_move == 'UP':
    s -= random.choice(ONETOTWO)
  elif m_move == 'DOWN':
    s += random.choice(ONETOTWO)

  return s, t

the_player = []

def get_moves(player):
  moves = ['LEFT', 'RIGHT', 'UP', 'DOWN']
  # player = (x,y)

  if player[1] == 0:
    moves.remove('LEFT')
  if player[1] == 3:
    moves.remove('RIGHT')
  if player[0] == 0:
    moves.remove('UP')
  if player[0] == 4:
    moves.remove('DOWN')
  return moves

def monster_moves(monster):
  m_moves = ['LEFT', 'RIGHT', 'UP', 'DOWN']
  # monster = (x,y)

  if monster[1] == 0:
    m_moves.remove('LEFT')
  if monster[1] == 3:
    m_moves.remove('RIGHT')
  if monster[0] == 0:
    m_moves.remove('UP')
  if monster[0] == 4:
    m_moves.remove('DOWN')
  return m_moves

def draw_map(player, monster):
  print(' _ _ _ _')
  tile = '|{}'

  for idx, cell in enumerate(CELLS):
    if idx in {0, 1, 2, 4, 5, 6, 8, 9, 10, 12, 13, 14, 16, 17, 18}:
      if cell == player:
        print(tile.format('X'),end='')
      elif cell == monster:
        print(tile.format('O'),end='')
      elif cell in the_player:
        print(tile.format('.'),end='')
      else:
        print(tile.format('_'),end='')
    else:
      if cell == player:
        print(tile.format('X|'))
     elif cell == monster:
       print(tile.format('O|'),end='')
     elif cell in the_player:
      print(tile.format('.|'))
    else: print(tile.format('_|'))

monster, door, player = get_locations()
print("Welcome to the dungeon!")

while True:
  moves = get_moves(player)

  print("You are currently in room {}".format(player))

  draw_map(player, monster)

  print("You can move {}".format(moves))
  print("Enter 'QUIT' to ragequit")

  move = input("> ")
  move = move.upper()
  the_player.append(player)

  dragon_move = random.choice(dragon_set_moves)
  dragon_moves = monster_moves(monster)

  if move == 'QUIT':
    break

  if move in moves:
    player = move_player(player, move)
  else:
    print("** Walls are hard, stop walking into them. **")
    continue

  if dragon_move in dragon_moves:
    monster = move_monster(monster, move)
  else:
    continue

  if player == door:
    print("You escaped!")
    leave = input("Great job, let's get outta here. ")
    break
  elif player == monster:
    print("You were eaten by the dragon.")
    leave = input("Damn, too bad, if you like having your arm chewed off, consider trying again!")
    break

3 Answers

*sigh, I was looking for answers to my problem, but thanks to both of you for teaching me a bit :p Good thing, since I found the answer myself just a moment ago. :p

I also made improvements to the game. To move the monster, I implemented a chase() function which simply looked at the monster and the players positions relative to each other. A simple call to chase(monster, player) is all it took.

   def chase(self, prey):
        moves = ['LEFT', 'RIGHT', 'UP', 'DOWN']

        if self.state == 'active':

            if self.position[0] <= prey.position[0]:
                moves.remove('LEFT')

            if self.position[0] >= prey.position[0]:
                moves.remove('RIGHT')

            if self.position[1] <= prey.position[1]:
                moves.remove('UP')

            if self.position[1] >= prey.position[1]:
                moves.remove('DOWN')

            if len(moves) == 0:
                moves.append('HOLD')

            return(random.choice(moves)) 

My version is on Github (username: greeder59). But I gotta warn you, I am a bit further along in the course and it's all been rewritten as Object Oriented Python. But you should still be able to follow it.

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

Yukiru Senake : You seem to be passing move to both the player and dragon moves. Replace move with dragon_move as below:

``python if dragon_move in dragon_moves: monster = move_monster(monster, dragon_move) #<-- changed to dragon_move else: continue