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

(vip2019) #1
Turtle Graphics: Drawing with Python 23

c hanging Background colors


Let’s mix things up a bit again to see if we can create some-
thing even more beautiful than Figure 2-5. For example, as my
five-year-old son Alex pointed out, the yellow sides are hard to
see. That’s because, just like yellow crayons on white drawing
paper, the yellow pixels on the screen don’t show up well against
the white background color. Let’s fix that by changing the back-
ground color to black. Type the following line of code anywhere
after the import line in our program:


turtle.bgcolor("black")


Adding this one line gives us an even neater picture: all of
the colors now stand out on the black background. Notice that
we’re not changing anything about the turtle’s pen (represented
by the variable t in our program). Instead, we’re changing some-
thing about the turtle screen, namely the background color. The
turtle.bgcolor() command allows us to change the color of the
entire drawing screen to any of the named colors in Python. In
the line turtle.bgcolor("black"), we’ve chosen black as the screen
color, so the bright red, yellow, blue, and green show up nicely.
While we’re at it, we can change the range() in our loop to
200 , or even more, to make larger squares in our spiral. See
Figure 2-6 for the new version of our picture with 200 lines on
a black background.


Figure 2-6: Our square spiral has come a long
way from its simple beginnings.

Free download pdf