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

(vip2019) #1
Timers and Animation: What Would Disney Do? 203

Programming Challenges


Here are three challenge problems to extend the skills you
developed in this chapter. For sample answers, go to h t t p : //
http://www.nostarch.com/teachkids/.


#1: A Color-Changing Dot


Let’s explore RGB color triplets further. We worked
with some RGB colors in this chapter; remember, green
was (0,255,0), black was (0,0,0), and so on. At h t t p : //
colorschemer.com/online/, enter different red, green, and
blue values from 0 to 255 to see the colors you can create by
combining different amounts of red, green, and blue light
from your screen’s pixels. Start by choosing your own color
triplet to use in the ShowDot.py program. Then modify the
program to draw the dot larger or smaller and at differ-
ent locations on the screen. Finally, try creating a random
RGB color triplet using random.randint(0,255) for each of the
three color components (remember to import random at the top
of your program) so that the dot changes colors every time it
draws on the screen. The effect will be a color-changing dot.
Call your new creation DiscoDot.py.


#2: 100 Random Dots


As a second challenge, let’s replace the single dot with
100 dots in random colors, sizes, and locations. To do this,
let’s set up three arrays capable of storing 100 values each
for the colors, locations, and sizes:


Colors, locations, sizes arrays for 100 random dots


colors = [0]100
locations = [0]
100
sizes = [0]*100


Then, fill those three arrays with random color triplets,
location pairs, and size/radius values for 100 random dots:


import random


Store random values in colors, locations, sizes


continued
Free download pdf