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 Planning Our Dungeon

x and y coordinates

I would have set the x-coordinate for the horizontal position and movement (LEFT or RIGHT) and the y-coordinate for the vertical position and movement (UP or DOWN) to be consistent with the standard Cartesian-plane and 2D physics. What do you think?

I'm looking at this way later, but I had the exact same thought. My version will be Cartesian, with X horizontal and Y vertical. It already is, but I haven't built the second part yet, where we actually draw it out.

6 Answers

The standard convention for an xy-coordinate system (Cartesian plane) is that for a given coordinate, the x-value represents the horizontal (left/right) position and the y-value represents the vertical (up/down) position. In the dungeon game, you did the reverse of this convention. It's not a big deal, I just thought I'd mention it.

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Ah, you're right. Not sure why I blanked and went with a different scheme. Regardless, so long as the scheme is consistent, it's easy enough to figure out your direction and location.

Feel free to submit a cartesian version!

the cartesian mehtod i had in mind and adam probably had in mind would only work i believe if the middle cell was (0, 0) the one above it was (0, 1) and below was (0, -1)

@Aboubacar Diane : To make things easy, we can choose one of the positive quadrants of the cartesian plan.

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

I'm not sure what you're suggestion. Can you show an example?

lol i was going to ask the same thing, to make it easier for me i used Y as the vertical, and x as the horizontal.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Public and published brain farts are awesome ;)

never mind i see why you did it that way, i was thinking of it as if the cells were like a graph, but the way the cells are aligned if you want to move left, x would have to - 1. i didn't realize it till i started placing roadblocks where player can not go any further

I programmed a little pong game once where instead of using Cartesian coordinates where (0, 0) is the center of the graph, (0, 0) was actually the upper left hand corner. Even with that method, we used x for horizontal and y for diagonal. Every time we wanted to move something down, we would increase the y coordinate. It was the opposite of the Cartesian method in that regard. I think that's what's happening here, even if the axes are not labeled conventionally. (I'm going to rename them as I work through this codes since my brain doesn't want to accept y as a horizontal axis. Might be easier to call it Steve than to call it y. :-)

Here's the code with x as the horizontal axes and (0,0) as the upper left corner. Not exactly Cartesian, but the axes are now what many of us are used to. I had to change the code in both the list of cells and the move_player function.

import random

# map code (a list of coordinates) - this is a constant
CELLS = [(0, 0), (1, 0), (2, 0), 
         (0, 1), (1, 1), (2, 1),
         (0, 2), (1, 2), (2, 2)]

# code to set the initial locations
def get_locations():
  # monster = random
  monster = random.choice(CELLS)
  # door = random
  door = random.choice(CELLS)
  # start = random
  start = random.choice(CELLS) 
  # if monster, door, or start are the same, do it again

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

  return monster, door, start

# function for moving the player
def move_player(player, move):
  # get the player's current location
  x, y = player  
  # if move is LEFT, x - 1
  if move == 'LEFT':
    x -= 1
  # if move is RIGHT, x + 1
  elif move == 'RIGHT':
    x += 1
  # if move is UP, y -1
  elif move == 'UP':
    y -= 1
  # if move is DOWN, y + 1
  elif move == 'DOWN':
    y += 1
  return x, y


def get_moves(player):
  moves = ['LEFT', 'RIGHT', 'UP', 'DOWN']
  # player = (x, y)
  if player[0] == 0:
    moves.remove('LEFT')
  if player[0] == 2:
    moves.remove('RIGHT')
  if player[1] == 0:
    moves.remove('UP')
  if player[1] == 2:
    moves.remove('DOWN')

  return moves

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

  for idx, cell in enumerate(CELLS):
    if idx in [0, 1, 3, 4, 6, 7]:
      if cell == player:
        print(tile.format('X'), end='')
      else:
        print(tile.format('_'), end='')
    else:
      if cell == player:
        print(tile.format('X|'))
      else:
        print(tile.format('_|'))

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

while True:
  moves = get_moves(player)
  print("You're currently in room {}".format(player)) # fill in with player position

  draw_map(player)

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

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


  if move == 'QUIT':
    break

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

  if player == door:
    print("You escaped!")
    break
  elif player == monster:
    print("You were eaten by the grue!")
    break