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 Dungeon Entrance

y axis

Maybe I am totally off base here, but I am a little confused as to why when the y value equals 4, they can't move down...isn't x (whatever value), 4 the TOP of the grid, so isn't down (or left or right depending on the x value) the only way they could go? Same with x (whatever value), 0 - wouldn't they be somewhere on the bottom "line" of the grid, so wouldn't they be able to move up?

3 Answers

Hi there!

If I'm understanding you correctly, the question is about how the coordinates are laid out. This might help for a 4x4 grid:

Y
_  _ _ _ _
0 |_|_|_|#|
1 |_|_|_|_|
2 |_|_|_|_|
3 |_|_|_|_|
   0 1 2 3 | X

In graphics, screens are usually drawn from the top left corner of the display down. In my little dungeon there I've put a # in one "room" to give an example. It's coords are y=0, x=3. It's Y is 0 so it can't go up, and its X is 3 so it can't go right.

I hope it helps :)

Hi Jon! So this helped a lot thank you. I didn't realize it started from the top left corner of the display down...I thought it went like how you would draw a grid in algebra class - i.e. the bottom left corner is 0,0, then then the 1 would be where the 2 is, the 3 would be the TOP left, etc. Do you see what I mean?

Is there a reason in design it goes from the top left corner down? That doesn't seem very intuitive to me, but again my grid experience is from elementary math classes, not from a technological standpoint..

I don't know the exact specifics of how computer graphic cards and displays work, but in our example it's because we'll be using the print() function to draw the dungeon from beginning to end. The print function will print one "row" of the board, then another then another. This will result in the first row being at the top, then the second then the third and so on. We could have reversed the numbering in our coordinates to make it more like drawing a graph, but as this is how all displays work, I guess it makes sense to keep it as it is.

You'll also find sometimes in graphics the Y and X are swapped around form the usual position = (X, Y) you'll be used to in algebra. Generally display coordinates follow the same order, but it's not uncommon to find them in the form of positioning = (Y, X).

As an example, I used a totally different structure for my dungeon:

character_sprite = "#" 
rows = 4
cols = 4
dungeon = [["_" for x in range(cols)] for y in range(rows)]
dungeon[0][3] = character_sprite  # 0 is Y, 3 is x
print(" _" * rows)
for row in dungeon:
    print("|".join(row), end = "|\n")

It will look the same:

 _ _ _ _
|_|_|_|#|
|_|_|_|_|
|_|_|_|_|
|_|_|_|_|

But because each row is a list in the dungeon list, to access a dungeon, I first have to select the row (Y) and then the element in the row (X). Python makes it very easy to swap coordinates around with y, x = x, y but I kept it this way around because I'm currently expanding my dungeon game with the curses library (to make a command line game) and that also uses (Y, X).

Hope it's not too confusing? I just meant to highlight that AFAIK in graphics it seems to always be (0,0) is the top left and (n, n) is bottom right, it's just that which is x or y might change from time to time!

Hope it helps :)

PS sorry I don't have a definitive on why it's always (0, 0) at top left, it seems from google that that's where screens started drawing from, but I don't know the hardware to confirm sorry!)

Hello! Sorry for the delay! No this is very very helpful, thanks so much for taking the time to help! :)

@Jon Your syntax for creating the dungeon wasn't taught in the course. Can you elaborate on how it works?

Your code didn't generate that output for me. With a little tweaking (I'm sure I've not done it the best way), I can use this:

character_sprite = "|#" 
rows = 4
cols = 4
dungeon = [["|_" for x in range(cols)] for y in range(rows)]
dungeon[0][3] = character_sprite  # 0 is Y, 3 is x
print(" _" * rows)
for row in dungeon:
    print("".join(row), end = "|\n")

I then get

 _ _ _ _
|_|_|_|#|
|_|_|_|_|
|_|_|_|_|
|_|_|_|_|