Coordinates in Pygame Zero

Pixels

A screen is a grid of tiny coloured dots: pixels. Our game window is 600 pixels wide and 400 pixels high: 240,000 pixels in total.

Every pixel has an address: (x, y). That way Pygame Zero knows exactly where to draw something.

Where is (0, 0)?

In maths graphs the zero point (0, 0) is at the bottom left. In Pygame Zero (0, 0) is at the top left.

  • x goes to the right: the bigger x, the further to the right.
  • y goes down: the bigger y, the lower on the screen.

MathsPygame Zero

(0, 0)xyy ↑ (up)(0, 0)xyy ↓ downwards!

Star and basket

In Catch the Stars the star starts at (300, 30): horizontally in the middle (x = 300 = half of 600), right at the top (y = 30). The basket sits at (300, 370): also in the middle, almost all the way at the bottom.

Pygame Zero screen with the star and the basket marked

When you write:

star.y = star.y + 3

you move the star 3 pixels down per frame, because a bigger y value means lower on the screen.

Position of an Actor

actor.pos is the centre of the picture, not the corner. You can also ask for the edges:

★

actor.posactor.topactor.bottomactor.leftactor.right
PropertyMeaning
actor.leftleft edge of the picture
actor.rightright edge of the picture
actor.toptop edge of the picture
actor.bottombottom edge of the picture

This is how you check whether the basket runs off the screen:

if basket.right > WIDTH:
    basket.x = WIDTH - basket.width / 2

Corners of the screen

Cornerxy
Top left00
Top right6000
Bottom left0400
Bottom right600400
Middle300200

Further reading: “Pixel Coordinates and Pygame Objects” by albro