Teach Your Kids To Code: A Parent-friendly Guide to Python Programming

(vip2019) #1

180 Chapter 8


You should see an image like the one in Figure 8-2 when you
run ShowDot.py. Take some time to play around with this dot
program—create a different RGB color triplet, draw the dot in a
different location on the screen, or draw a second dot. You’ll begin
to see the power and ease of drawing graphics with Pygame, and
you’ll have fun along the way.
This first program contains the foundation that we’ll build
on to create more complex graphics, animation, and, eventually,
games.

What’s New in Pygame


Before we dive deeper into the exciting world of Pygame, it’s worth
noting some important differences between Pygame and our old
friend turtle graphics:
• We have a new coordinate system, as shown in Figure 8-3.
Back in turtle graphics, the origin was at the center of the
screen, and y got larger as we went up the screen. Pygame
uses a more common window-oriented coordinate system (we
see this in many other GUI programming languages, includ-
ing Java, C++, and more). The upper-left corner of a window
in Pygame is the origin, (0, 0). The x-coordinate values still
increase as you move to the right (but there are no negative
x-coordinate values, as they would be off the screen to the
left); y-coordinate values increase as you move down (and nega-
tive y-coordinate values would be off the top of the window).

(0, 0)

y

x

10
20
30
40
50
60
70

10 20 30 40 50 60 70

Figure 8-3: Pygame uses a window-oriented
coordinate system.
Free download pdf