| 1 |
Migrating from Scratch |
| 2 |
====================== |
| 3 |
|
| 4 |
This tutorial will compare an implementation of Flappy Bird written in Scratch |
| 5 |
with one written in Pygame Zero. The Scratch and Pygame Zero programs are |
| 6 |
similar to a remarkable extent. |
| 7 |
|
| 8 |
The `Pygame Zero version`__ can be found in Pygame Zero repository. |
| 9 |
|
| 10 |
.. __: https://github.com/lordmauve/pgzero/blob/master/examples/flappybird/flappybird.py |
| 11 |
|
| 12 |
You can also download the `Scratch version`__ from the repository. |
| 13 |
|
| 14 |
.. __: https://github.com/lordmauve/pgzero/raw/master/examples/flappybird/Flappy%20Bird.sb |
| 15 |
|
| 16 |
The Pygame Zero version includes scoring logic, which is omitted in the code |
| 17 |
examples on this page to make it a closer comparison. |
| 18 |
|
| 19 |
The Python code shown below is re-arranged for clarity within the examples. |
| 20 |
|
| 21 |
|
| 22 |
The stage |
| 23 |
--------- |
| 24 |
|
| 25 |
Here's how the stage is laid out in our Scratch program: |
| 26 |
|
| 27 |
.. image:: _static/scratch/flappybird-stage.png |
| 28 |
|
| 29 |
There are just three objects, aside from the background: the bird, and the top |
| 30 |
and bottom pipes. |
| 31 |
|
| 32 |
This corresponds to the Pygame Zero code setting these objects up as |
| 33 |
``Actors``:: |
| 34 |
|
| 35 |
bird = Actor('bird1', (75, 200)) |
| 36 |
pipe_top = Actor('top', anchor=('left', 'bottom')) |
| 37 |
pipe_bottom = Actor('bottom', anchor=('left', 'top')) |
| 38 |
|
| 39 |
In Pygame Zero we also have to ensure we draw these objects. In principle this |
| 40 |
gives a little more flexibility about how to draw the scene:: |
| 41 |
|
| 42 |
def draw(): |
| 43 |
screen.blit('background', (0, 0)) |
| 44 |
pipe_top.draw() |
| 45 |
pipe_bottom.draw() |
| 46 |
bird.draw() |
| 47 |
|
| 48 |
|
| 49 |
Pipe movement |
| 50 |
------------- |
| 51 |
|
| 52 |
The pipes move at a constant rate irrespective of the bird. When they move off |
| 53 |
the left-hand side of the screen, they loop around to the right, and their |
| 54 |
vertical position moves at random. |
| 55 |
|
| 56 |
In Scratch this can be achieved by creating two different scripts for the top |
| 57 |
and bottom pipe. |
| 58 |
|
| 59 |
.. image:: _static/scratch/flappybird-top-start.png |
| 60 |
|
| 61 |
.. image:: _static/scratch/flappybird-bottom-start.png |
| 62 |
|
| 63 |
To summarise what's happening here: |
| 64 |
|
| 65 |
* The condition ``x position < -240`` is true when a pipe is off the left-hand |
| 66 |
side of the screen, and this is the trigger to reset the pipes. |
| 67 |
* The ``pipe_height`` variable is used to coordinate the two pipes. Because the |
| 68 |
gap between them should remain the same, we can't pick *both* heights |
| 69 |
randomly. Therefore one of the scripts has this logic and the other doesn't. |
| 70 |
* The ``set y position to pipe height +/- 230`` sets one pipe to be above |
| 71 |
``pipe_height`` and the other pipe below ``pipe_height``. |
| 72 |
|
| 73 |
This code becomes much simpler in Pygame Zero. We could write a single function |
| 74 |
that updates both pipes. In fact I split it a different way to make it clear |
| 75 |
that the reset actions go together:: |
| 76 |
|
| 77 |
import random |
| 78 |
|
| 79 |
WIDTH = 400 |
| 80 |
HEIGHT = 708 |
| 81 |
GAP = 130 |
| 82 |
SPEED = 3 |
| 83 |
|
| 84 |
def reset_pipes(): |
| 85 |
pipe_gap_y = random.randint(200, HEIGHT - 200) |
| 86 |
pipe_top.pos = (WIDTH, pipe_gap_y - GAP // 2) |
| 87 |
pipe_bottom.pos = (WIDTH, pipe_gap_y + GAP // 2) |
| 88 |
|
| 89 |
def update_pipes(): |
| 90 |
pipe_top.left -= SPEED |
| 91 |
pipe_bottom.left -= SPEED |
| 92 |
if pipe_top.right < 0: |
| 93 |
reset_pipes() |
| 94 |
|
| 95 |
A small difference here is that I can extract values that I want to re-use as |
| 96 |
"constants", spelled in UPPERCASE. This lets me change them in one place when I |
| 97 |
want to tune the game. For example, in the code above, I could widen or narrow |
| 98 |
the gap between the two pipes simply by changing ``GAP``. |
| 99 |
|
| 100 |
The biggest thing that differs is that there is no ``forever`` loop in Python |
| 101 |
code. This is the big difference between Scratch and most text-based |
| 102 |
programming languages: you must update the game by one animation step and then |
| 103 |
return. Returning gives Pygame Zero a chance to do things like processing |
| 104 |
input or redrawing the screen. Loop forever and the game would just sit there, |
| 105 |
so any loops need to finish quickly. |
| 106 |
|
| 107 |
Pygame Zero calls an ``update()`` function when it wants you to update the |
| 108 |
animation by one step, so we just need to a call to ``update_walls()``:: |
| 109 |
|
| 110 |
def update(): |
| 111 |
update_walls() |
| 112 |
|
| 113 |
|
| 114 |
The Bird |
| 115 |
-------- |
| 116 |
|
| 117 |
The patterns described above for how Scratch logic translates to Python code |
| 118 |
also apply for the bird logic. Let's look at the Python code first this time. |
| 119 |
|
| 120 |
The code to update the bird is organised into a function called |
| 121 |
``update_bird()``. The first thing this function contains is some code to move |
| 122 |
the bird according to gravity:: |
| 123 |
|
| 124 |
GRAVITY = 0.3 |
| 125 |
|
| 126 |
# Initial state of the bird |
| 127 |
bird.dead = False |
| 128 |
bird.vy = 0 |
| 129 |
|
| 130 |
def update_bird(): |
| 131 |
uy = bird.vy |
| 132 |
bird.vy += GRAVITY |
| 133 |
bird.y += bird.vy |
| 134 |
bird.x = 75 |
| 135 |
|
| 136 |
This is a simple gravity formula: |
| 137 |
|
| 138 |
* Gravity means constant **acceleration downwards**. |
| 139 |
* Acceleration is change in **velocity**. |
| 140 |
* Velocity is change in **position**. |
| 141 |
|
| 142 |
To represent this we need to track a variable ``bird.vy``, which is the bird's |
| 143 |
velocity in the ``y`` direction. This is a new variable that we are defining, |
| 144 |
not something that Pygame Zero provides for us. |
| 145 |
|
| 146 |
* Gravity means constant acceleration downwards: ``GRAVITY`` is greater than 0. |
| 147 |
* Acceleration is change in velocity: ``GRAVITY`` gets added to ``bird.vy`` |
| 148 |
* Velocity is change in position: ``bird.vy`` gets added to ``bird.y`` |
| 149 |
|
| 150 |
Note that the bird does not move horizontally! Its ``x`` position stays at |
| 151 |
75 through the whole game. We simulate movement by moving the pipes towards |
| 152 |
it. This looks as though it's a moving camera following the bird. So there's |
| 153 |
no need for a ``vx`` variable in this game. |
| 154 |
|
| 155 |
The next section makes the bird flap its wings:: |
| 156 |
|
| 157 |
if not bird.dead: |
| 158 |
if bird.vy < -3: |
| 159 |
bird.image = 'bird2' |
| 160 |
else: |
| 161 |
bird.image = 'bird1' |
| 162 |
|
| 163 |
This checks if the bird is moving upwards or downwards. We show the ``bird2`` |
| 164 |
image if it is moving upwards fast and the ``bird1`` image otherwise. (-3 was |
| 165 |
picked by trial and error to make this look convincing). |
| 166 |
|
| 167 |
The next section checks if the bird has collided with a wall:: |
| 168 |
|
| 169 |
if bird.colliderect(pipe_top) or bird.colliderect(pipe_bottom): |
| 170 |
bird.dead = True |
| 171 |
bird.image = 'birddead' |
| 172 |
|
| 173 |
If so we set ``bird.dead`` to ``True``. This is a **boolean value** meaning it |
| 174 |
is either ``True`` or ``False``. We can use this to easily check if the bird is |
| 175 |
alive. If it isn't alive it won't respond to player input. |
| 176 |
|
| 177 |
And the final section checks if the bird has fallen off the bottom (or the top) |
| 178 |
of the game screen. If so it resets the bird:: |
| 179 |
|
| 180 |
if not 0 < bird.y < 720: |
| 181 |
bird.y = 200 |
| 182 |
bird.dead = False |
| 183 |
bird.vy = 0 |
| 184 |
reset_pipes() |
| 185 |
|
| 186 |
What's ``reset_pipes()`` doing there? Because I'd organised my pipes code to |
| 187 |
be a separate function, I can just call it whenever I want to reset my walls. |
| 188 |
In this case it makes it a better game because it gives the player a chance to |
| 189 |
react when the bird moves back to its start position. |
| 190 |
|
| 191 |
Again, this needs to be called every frame, so we add it to ``update()``:: |
| 192 |
|
| 193 |
def update(): |
| 194 |
update_walls() |
| 195 |
update_bird() |
| 196 |
|
| 197 |
The final part of the bird logic is that it has to respond to player control. |
| 198 |
When we press a key, the bird flaps upwards. Pygame Zero will call an |
| 199 |
``on_key_down()`` function - if you've defined one - whenever a key is |
| 200 |
pressed:: |
| 201 |
|
| 202 |
FLAP_VELOCITY = -6.5 |
| 203 |
|
| 204 |
def on_key_down(): |
| 205 |
if not bird.dead: |
| 206 |
bird.vy = FLAP_VELOCITY |
| 207 |
|
| 208 |
Here, if the bird is not dead, we set its ``vy`` to a negative number: in |
| 209 |
Pygame Zero this means it starts moving upwards. |
| 210 |
|
| 211 |
You should be able to find a lot of parallels between the Python code and this |
| 212 |
Scratch code: |
| 213 |
|
| 214 |
.. image:: _static/scratch/flappybird-bird-start.png |
| 215 |
.. image:: _static/scratch/flappybird-bird-space.png |
| 216 |
|
| 217 |
|
| 218 |
The biggest differences between Scratch and Pygame Zero are these: |
| 219 |
|
| 220 |
* You cannot loop forever in Pygame Zero - just update for one frame and then |
| 221 |
return. |
| 222 |
* The coordinates are different. In Pygame Zero, the top left of the screen is |
| 223 |
``x = 0, y = 0``. The ``x`` direction goes from left to right as before, but |
| 224 |
``y`` goes down the screen! This is why ``GRAVITY`` is a positive number and |
| 225 |
``FLAP_VELOCITY`` is a negative number in Python. |
| 226 |
* ``bird.dead`` is a bool, so I can write code like ``if not bird.dead`` |
| 227 |
instead of ``dead = 0`` as in Scratch. |
| 228 |
|
| 229 |
|
| 230 |
Summary |
| 231 |
------- |
| 232 |
|
| 233 |
Many of the concepts available in Scratch can be translated directly into |
| 234 |
Pygame Zero. |
| 235 |
|
| 236 |
Here are some comparisons: |
| 237 |
|
| 238 |
+----------------------------+--------------------------------------------+ |
| 239 |
| In Scratch | In Pygame Zero | |
| 240 |
+============================+============================================+ |
| 241 |
| ``change y by 1`` (up) | ``bird.y -= 1`` | |
| 242 |
+----------------------------+--------------------------------------------+ |
| 243 |
| ``change y by -1`` (down) | ``bird.y += 1`` | |
| 244 |
+----------------------------+--------------------------------------------+ |
| 245 |
| ``set costume to <name>`` | ``bird.image = 'name'`` | |
| 246 |
+----------------------------+--------------------------------------------+ |
| 247 |
| ``if dead = 0`` | ``if not bird.dead:`` | |
| 248 |
+----------------------------+--------------------------------------------+ |
| 249 |
| ``set dead to 0`` | ``bird.dead = False`` | |
| 250 |
+----------------------------+--------------------------------------------+ |
| 251 |
| ``if touching Top?`` | ``if bird.colliderect(pipe_top)`` | |
| 252 |
+----------------------------+--------------------------------------------+ |
| 253 |
| ``When Flag clicked``... | Put code into the ``update()`` function. | |
| 254 |
| ``forever`` | | |
| 255 |
+----------------------------+--------------------------------------------+ |
| 256 |
| ``When [any] key pressed`` | ``def on_key_down():`` | |
| 257 |
+----------------------------+--------------------------------------------+ |
| 258 |
| ``pick random a to b`` | ``import random`` to load the ``random`` | |
| 259 |
| | module, then ``random.randint(a, b)`` | |
| 260 |
+----------------------------+--------------------------------------------+ |
| 261 |
| (0, 0) is the centre of | (0, 0) is the top-left of the window | |
| 262 |
| the stage | | |
| 263 |
+----------------------------+--------------------------------------------+ |
| 264 |
|
| 265 |
In some cases, the code is simpler in Python because it can be |
| 266 |
organised in a way that helps it make sense when you read it. |
| 267 |
|
| 268 |
The power of Pygame Zero's actors also makes the coordinate manipulation |
| 269 |
easier. We used the ``anchor`` position to position the pipes, and we were able |
| 270 |
to see if a pipe was off-screen by checking ``pipe_top.right < 0`` rather than |
| 271 |
``if x position < -240``. |