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 (2016, retired 2019) Dungeon Game Win or Lose

Hussein Amr
Hussein Amr
2,461 Points

Extending the Dungeon game

if you take a look at line 27 you'll see how im trying to make the monster move as I make a move to the left , but it's not really working out. I need help

import os
import sys
import random 

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)]

movers_list = [(0,-1) ,(0,1),(1,0) , (-1,0)]

def clear_screen():
  os.system('cls' if os.name == 'nt' else 'clear')


def get_location():
  return  random.sample(CELLS , 3)



def move_player(player,monster , move):
  x , y = player
  xmon , ymon = monster
  if move == 'LEFT':
    x -= 1
    xmon += random.choice(movers_list)
  if move == 'RIGHT':
    x += 1
  if move == 'UP':
    y -= 1
  if 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")
    if x == 4:
      moves.remove("RIGHT")
    if y == 0:
      moves.remove("UP")
    if y == 4:
      moves.remove("DOWN")
    return moves

def draw_map(player, monster):
    print(" _"*5)
    tile = "|{}"

    for cell in CELLS:
      x , y = cell
      if x < 4:
        line_end = ""
        if cell == player:
          output = tile.format("X")
        elif cell == monster:
          output = tile.format("@")
        else:
          output = tile.format("_")
      else:
        line_end = "\n"
        if cell == player:
          output = tile.format("X|")
        elif cell == monster:
          output = tile.format("@|")
        else:
          output = tile.format("_|")
      print(output, end= line_end)




def game_loop():
  monster , door , player = get_location()
  playing = True

  while playing:
    clear_screen()  
    draw_map(player, monster)
    valid_moves = get_moves(player)

    print("You're currently in room {}".format(player))
    print("You can move {}".format(", ".join(valid_moves)))
    print("Enter QUIT to quit")

    move = input("> ")
    move = move.upper()

    if move == 'QUIT':
      print("good night kid")
      break
    if move in valid_moves:
      player = move_player(player,monster, move)
      if player == monster:
        print("\n ** You lose brah.. better luck next time fg ** \n")
        playing = False
      if player == door:
        print("\n ** congrats fg you just won ** \n")
        playing = False

    else:
      input("\n ** Walls are hard, don't run into them ** \n")

  else:
    if input("Play again? [Y/n] ").lower() != "n":
      game_loop()


clear_screen()
print("Welcome to the dungeon!")
input("press return to start!")
clear_screen()
game_loop()
Brendan Albert
Brendan Albert
32,954 Points

Hi Hussein,

If I understand your predicament correctly, it looks like you are trying to change the Monster's x location by adding to xmon from the movers_list list of tuples.

I don't believe tuples can be added unfortunately.

Good news though, all you need to do to make the monster move is randomly add either -1 , 0 or 1 to xmon. This should change the monster's x coordinate.

Try changing movers_list from tuples to a list of integers like so: movers_list = [-1,0,1]

I hope that helps!

Edit: My advice was not quite 100% what you need. This is because we need to make sure monster is modified outside of the move_player() function.

Soo.... lets create a move_monster(monster) function:

def move_monster(monster):

    xmon , ymon = monster

    xmon += random.choice(movers_list)

    ymon += random.choice(movers_list)

    return xmon , ymon

And try calling this function near player = move_player(player,monster,move) assignment, inside:

while playing:

...

if move in valid_moves:
    player = move_player(player,monster,move)
    monster = move_monster(monster)

Edit # 2: Hussein Amr I just tested and this code should work!

However there is a quirky behavior in the monsters movement functionality. It is possible for the monster to phase through the wall and disappear! Bug? Feature? You decide!

Hussein Amr
Hussein Amr
2,461 Points

Brendan Albert Alright I got you Brendan , now all I have to do is return xmon but it's not letting me return more than 2 values.. any ideas?

Hussein Amr
Hussein Amr
2,461 Points

Brendan Albert Definitely a bug xD gonna have to find a fix for that. Anyway , appreciate the help man . Thank you very much!

1 Answer

Brendan Albert
Brendan Albert
32,954 Points

Hussein Amr You got it fam. ? I had fun figuring that out, feel free to hit me up if you have any more questions, I'd be happy to help!