Drawing Random Lines in Python
by Sloan Kelly
My first computer was a ZX Spectrum. My parents purchased it for me as a surprise gift in the summer of 1982. Before school started up again in August, I had taught myself how to program using the BASIC (Beginner’s All-Purpose Symbolic Instruction Code) programming language using the supplied manual and by typing in listing from magazines like Sinclair Projects and Your Sinclair. I would invariably get something wrong while typing in the programs and that taught me a valuable lesson too—how to debug code; the art of problem solving. As Steve Jobs once said, computers teach you how to think.
Why should you learn to code? I believe that computer programming will be a required skill for the 21st century. It might not be the main part of your job, but it will be a component of it. The question that I get asked a lot is, "Where do I get started?" There are several options, but I think one stands out over the rest.
The Raspberry Pi computer is a powerful, low-cost computer that helps children and adults learn to program. The operating system comes pre-loaded with the Python programming language and the PyGame framework. This means that you can start coding as soon as you plug the machine in. All you need to do is provide a TV or monitor, keyboard and mouse.
So, let’s say you now have a Raspberry Pi and you want to start with Python and PyGame. Why not start with an example program that will hopefully whet your appetite for more discovery?
One of the first programs that was included in the ZX Spectrum manual was how to draw random lines on the screen. The screen would slowly fill up until it was completely covered.
The code below will work on a Raspberry Pi, or any computer running Python/PyGame. The breakdown of the code is as follows:
- Import various frameworks that extend the Python language
- Initialize PyGame
- Create a clock to maintain 60 frames per second
- Create a window 800x600 pixels in size and fill with black. 0 is no color, 255 is full color and the three colors represent the red, green and blue components
- Set the start value of the line to a random point inside the 800x600 window
The main loop (while True:) contains code that:
- Checks to see if the user has clicked the close button on the window. If so, the program exits
- Set the end value of the line to a random point inside the 800x600 window
- Creates a random color
- Draws a line between the start and end points
- Sets the start to the old end. This means that the random lines will join each other
- Updates the display
- Updates the clock
The full listing for the program is:
import pygame, random, sys
from pygame.locals import *
fpsClock = pygame.time.Clock()
surface = pygame.display.set_mode((800, 600))
surface.fill((0, 0, 0))
start = (random.random() * 800, random.random() * 600)
for event in pygame.event.get():
if event.type == QUIT:
end = (random.random() * 800, random.random() * 600)
color = (random.random() * 255, random.random() * 255, random.random() * 255)
pygame.draw.line(surface, color, start, end)
start = end
When you run the program you will see an output similar to the one below after the lines have covered most of the screen:
The next step after typing in a program like this is to experiment. How would you make the lines all the same shade of red, or green, or blue? What if you restrict the lines to draw only in the top left quarter of the screen? How could we never draw red lines? Experimentation is part of learning, so do it often.
About the Author
Sloan Kelly has worked in the games industry for nearly 12 years. He has worked on a number of AAA and indie titles and currently works for an educational game company. He lives in Ontario, Canada with his wife and children. Sloan is on Twitter @codehoose and makes YouTube videos in his spare time.
This article was contributed by Sloan Kelly, author of Python, PyGame, and Raspberry Pi Game Development.