| 1 |
Learn programming with Pygame Zero |
| 2 |
================================== |
| 3 |
|
| 4 |
Let's start at the beginning. This tutorial is for those with no programming |
| 5 |
experience whatsoever. We're going to learn to make a simple but perhaps fun |
| 6 |
game. |
| 7 |
|
| 8 |
I'm going to assume Pygame Zero is installed already and you know how to use |
| 9 |
a text editor. |
| 10 |
|
| 11 |
Creating a blank file |
| 12 |
===================== |
| 13 |
|
| 14 |
First, create an empty file called "fishfrenzy.py" by creating a new, empty |
| 15 |
file in your editor and saving it as "fishfrenzy.py" in a new directory. |
| 16 |
|
| 17 |
Verify that this runs and creates a blank window by running :: |
| 18 |
|
| 19 |
cd the_directory_you_created |
| 20 |
pgzrun fishfrenzy.py |
| 21 |
|
| 22 |
How you run this may depend on your editor, shell, or loads of other things. |
| 23 |
|
| 24 |
If this step failed, then you may be in the wrong directory, or Python or |
| 25 |
Pygame Zero may not be installed correctly - or hundreds of other things. In |
| 26 |
many ways this is the hardest step, because it's beyond the scope of this |
| 27 |
tutorial to explain things, given how many different configurations there are: |
| 28 |
what operating system you are running, what version of Python, what version of |
| 29 |
Pygame Zero. |
| 30 |
|
| 31 |
But with any luck you can get it to work! If you see a blank "Pygame Zero Game" |
| 32 |
window, we're good to move on. |
| 33 |
|
| 34 |
Really stuck? |
| 35 |
------------- |
| 36 |
|
| 37 |
You can get into a support chat room by going to |
| 38 |
|
| 39 |
http://webchat.freenode.net?randomnick=1&channels=%23%23learnpython&uio=d4 |
| 40 |
|
| 41 |
Functions |
| 42 |
========= |
| 43 |
|
| 44 |
Let's begin for real. |
| 45 |
|
| 46 |
In most programming languages we program by defining a series of steps that the |
| 47 |
computer will carry out for us, like a **recipe**. In bakery, you might have |
| 48 |
a recipe like: |
| 49 |
|
| 50 |
**Cake Recipe** |
| 51 |
|
| 52 |
* Add flour to the bowl |
| 53 |
* Add sugar to the bowl |
| 54 |
* Add butter to the bowl |
| 55 |
* Add an egg to the bowl |
| 56 |
* Whisk the ingredients |
| 57 |
* Pour into a cake tin |
| 58 |
* Bake it |
| 59 |
|
| 60 |
... and so on. (Don't try this recipe. I am not a chef.) |
| 61 |
|
| 62 |
A game among programmers is to imagine how this would look as a program in an |
| 63 |
imaginary computer that understood whatever instructions you could name (this |
| 64 |
is called **pseudocode** - because it looks like code, but it doesn't actually |
| 65 |
work). If we translated this recipe into Python, it would look like this:: |
| 66 |
|
| 67 |
def cake_recipe(): |
| 68 |
bowl.add(flour) |
| 69 |
bowl.add(sugar) |
| 70 |
bowl.add(butter) |
| 71 |
bowl.add(egg) |
| 72 |
whisk(bowl) |
| 73 |
cake_tin.add(bowl.get_contents()) |
| 74 |
bake(cake_tin) |
| 75 |
|
| 76 |
That's pretty similar, isn't it? We've had to spell the names of some of the |
| 77 |
words differently, but basically, each line of the program is equivalent to one |
| 78 |
line of the recipe - one "action". |
| 79 |
|
| 80 |
The top line with ``def`` is defining a recipe called ``cake_recipe``, and all |
| 81 |
of the other lines are steps in the recipe. And in fact - because they all use |
| 82 |
parentheses - they refer to other recipes that the computer might know. |
| 83 |
|
| 84 |
So, let's write a real recipe in Pygame Zero. In your editor, let's write a |
| 85 |
``draw`` recipe. Pygame Zero knows how to call a function called ``draw``:: |
| 86 |
|
| 87 |
def draw(): |
| 88 |
screen.clear() |
| 89 |
screen.draw.circle((400, 300), 30, (255, 255, 255)) |
| 90 |
|
| 91 |
Run it with:: |
| 92 |
|
| 93 |
pgzrun fishfrenzy.py |
| 94 |
|
| 95 |
Did it work? Do you see the circle? On my screen it looks like this: |
| 96 |
|
| 97 |
.. image:: _static/grabs/circle.png |
| 98 |
|
| 99 |
If Pygame Zero didn't show a window, or showed a window that disappeared |
| 100 |
straight away, look at the error message it displayed: |
| 101 |
|
| 102 |
* ``SyntaxError`` - Probably means you are missing a parenthesis or your |
| 103 |
indentation doesn't match mine. Note how each step of the function has to be |
| 104 |
aligned with each other, but indented **more** than the ``def`` at the top. |
| 105 |
* ``AttributeError`` or ``NameError`` - You probably mispelled something. |
| 106 |
* ``TypeError`` - did you put some of the parentheses in the wrong places? |
| 107 |
|
| 108 |
If the window appears, but nothing happens, did you misspell "draw"? Or any of |
| 109 |
the numbers? |
| 110 |
|
| 111 |
Check carefully if it doesn't work first time and you should be able to get it |
| 112 |
to work after a few attempts. |
| 113 |
|
| 114 |
So we created a recipe that includes two steps: |
| 115 |
|
| 116 |
1. Clear the screen to black |
| 117 |
2. Draw a circle on the screen: |
| 118 |
|
| 119 |
* At the middle ``(400, 300)``. These are coordinates for the center of the |
| 120 |
circle, in pixels from the left then pixels from the top. |
| 121 |
* Of radius ``30`` pixels. |
| 122 |
* And draw it in white, which is written as three numbers, red, green and |
| 123 |
blue: ``(255, 255, 255)``. White is an equal mix of red, green and blue, |
| 124 |
and ``255`` is the most you can have. |
| 125 |
|
| 126 |
You can play with all of these numbers. |
| 127 |
|
| 128 |
|
| 129 |
Diversion: Local and Global Variables |
| 130 |
------------------------------------- |
| 131 |
|
| 132 |
Suppose we wrote the following code:: |
| 133 |
|
| 134 |
RED = 150, 0, 0 |
| 135 |
GREEN = 0, 128, 0 |
| 136 |
|
| 137 |
bg = RED |
| 138 |
|
| 139 |
def draw(): |
| 140 |
screen.fill(bg) |
| 141 |
|
| 142 |
def on_mouse_down(): |
| 143 |
bg = GREEN |
| 144 |
|
| 145 |
def on_mouse_up(): |
| 146 |
bg = RED |
| 147 |
|
| 148 |
In some languages, this would work: the screen would change to green |
| 149 |
when the mouse button was pressed, and change back to red when the button is |
| 150 |
released. |
| 151 |
|
| 152 |
This code doesn't work in Python. If you try it, you will not see the screen |
| 153 |
change to green. Why? |
| 154 |
|
| 155 |
When you assign with the ``=`` operator inside a function, as at line 10 (and |
| 156 |
line 13), you create a "local" variable called ``bg`` that exists only with the |
| 157 |
function. The ``bg`` we want to change is in the global scope. It's actually |
| 158 |
a very sensible feature; you don't want to |
| 159 |
|
| 160 |
The fix is to declare in ``on_mouse_down()`` and ``on_mouse_up()`` that we |
| 161 |
want to modify the global variable, not create a new local variable. We do |
| 162 |
this with the ``global`` statement. The correct code in Pygame Zero is:: |
| 163 |
|
| 164 |
RED = 150, 0, 0 |
| 165 |
GREEN = 0, 128, 0 |
| 166 |
|
| 167 |
bg = RED |
| 168 |
|
| 169 |
def draw(): |
| 170 |
screen.fill(bg) |
| 171 |
|
| 172 |
def on_mouse_down(): |
| 173 |
global bg |
| 174 |
bg = GREEN |
| 175 |
|
| 176 |
def on_mouse_up(): |
| 177 |
global bg |
| 178 |
bg = RED |