Python Python Collections (2016, retired 2019) Dungeon Game Dungeon Entrance

Melissa Bonnici
Melissa Bonnici
2,000 Points

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

Melissa Bonnici
Melissa Bonnici
2,000 Points

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

Melissa Bonnici
Melissa Bonnici
2,000 Points

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?

Ed H-P
Ed H-P
2,557 Points

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

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