Operation Manual
Experiments in Python
74
Notes:
Then you can use your arrow keys to move the cursor up to the line beginning with
“for” and then press Return, twice. If you are wondering why you need to press
Return twice; the first time is to complete the “for” command. When you press the
second time, Python recognises you have finished that command and executes
what you have typed.
Now let’s get some real computer graphics going. Enter this:
>>> import pygame
After a couple of seconds you’ll see the prompt reappear.
>>> import pygame
>>>
Wow! You imported “PyGame”. So what is that, and what does it mean? Again,
don’t worry about that. You have just told Python that you want to use its game-
creation features. “Import” loads extra commands into Python that you can use
from now on.
A note about text colours
While you were typing “import pygame” you should have noticed
that the word “import” changed colour as soon as you finished
typing it. This is because IDLE has a feature called “syntax
highlighting”. The colouring isn’t required for any Python
program to work but it is helpful to programmers when reading
code. When the text of a program is highlighted with colours,
it makes it easier to spot mistakes.
For now, just think of it as the opposite of the little red squiggles
that mark spelling errors when you write an essay in a word
processor. It doesn’t point out errors, but highlights that IDLE
has recognised a key Python command.
Making an amazing game will take ages and lots of commands, but while we’re
building up to that, here are just a few more lines for you to type to see something
wonderful. Type these in exactly as you see below.
>>> deepblue = (26,0,255)
>>> mintcream = (254,255,250)
This has given some names to a couple of “tuples” for you to use later. These
tuples are just data for the computer, and it will be using them to create colours in
just a moment.
>>> pygame.init()
>>> size = (500,500)
>>> surface = pygame.display.set_mode(size)