Chapter 7 Physical Computing with the Sense HAT 171
THE OFFICIAL RASPBERRY PI BEGINNER’S GUIDE
Before you click Run, look at the coordinates and compare them to the matrix: can you
guess what picture those instructions are going to draw? Click Run to find out if you’re right!
Drawing a detailed picture using individual set_pixel() functions is slow, though.
To speed things up, you can change multiple pixels at the same time. Delete all your
set_pixel() lines and type the following:
g = ( 0 , 255 , 0 )
b = ( 0 , 0 , 0 )
creeper_pixels = [
g, g, g, g, g, g, g, g,
g, g, g, g, g, g, g, g,
g, b, b, g, g, b, b, g,
g, b, b, g, g, b, b, g,
g, g, g, b, b, g, g, g,
g, g, b, b, b, b, g, g,
g, g, b, b, b, b, g, g,
g, g, b, g, g, b, g, g
]
sense.set_pixels(creeper_pixels)
There’s a lot there, but start by clicking Run to see if you recognise a certain little creeper. The
first two lines create two variables to hold colours: green and black. To make the code for the
drawing easier to write and read, the variables are single letters: ‘g’ for green and ‘b’ for black.
The next block of code creates a variable which holds colour values for all 64 pixels on
the LED matrix, separated by commas and enclosed between square brackets. Instead of
numbers, though, it uses the colour variables you created earlier: look closely, remembering
‘g’ is for green and ‘b’ is for black, and you can already see the picture that will appear
(Figure 7-16, overleaf).
Finally, sense.set_pixels(creeper_pixels) takes that variable and uses the
sense.set_pixels() function to draw on the entire matrix at once. Much easier than
trying to draw pixel-by-pixel!