Flappy Bird On Scratch

Posted on by
Flappy Bird On Scratch Rating: 8,3/10 3750 reviews

Now we've added some ground. Attach a block to the 'when hit the ground' block. Your code needs to 'END GAME' when Flappy crashes. Vird The Flapping Dragon: A Flappy Wings Bird Game - by Cobalt Play Games. Amazing wild animal adventure scratch off & color game for for kids, boys.

  1. Flappy Bird Scratch Games
  2. Flappy Bird On Scratch
  3. Simple Flappy Bird On Scratch
  4. How To Create Flappy Bird On Scratch

This tutorial will compare an implementation of Flappy Bird written in Scratchwith one written in Pygame Zero. The Scratch and Pygame Zero programs aresimilar to a remarkable extent.

The Pygame Zero version can be found in Pygame Zero repository.

Desi serial star plus. Desi TV Serial that are loved by desi people around the world.We live in the Internet age. Now, the Internet is no more just a medium for communication, it is much more than that it is a fast growing culture that promises to dominate most other cultural identities.

Flappy Bird Scratch Games

You can also download the Scratch version from the repository.

The Pygame Zero version includes scoring logic, which is omitted in the codeexamples on this page to make it a closer comparison.

Bird

The Python code shown below is re-arranged for clarity within the examples.

Man vs wild full episodes hd. Live TV may vary by subscription and location. Multiple concurrent streams and HD content may require higher bandwidth. Streaming content may count against your data usage. Location data required to access content on mobile devices for any Live TV subscription. Compatible device and high-speed, broadband Internet connection required.

The stage¶

Here’s how the stage is laid out in our Scratch program:

Flappy

There are just three objects, aside from the background: the bird, and the topand bottom pipes.

This corresponds to the Pygame Zero code setting these objects up asActors:

In Pygame Zero we also have to ensure we draw these objects. In principle thisgives a little more flexibility about how to draw the scene:

Pipe movement¶

The pipes move at a constant rate irrespective of the bird. When they move offthe left-hand side of the screen, they loop around to the right, and theirvertical position moves at random.

In Scratch this can be achieved by creating two different scripts for the topand bottom pipe.

To summarise what’s happening here:

  • The condition xposition<-240 is true when a pipe is off the left-handside of the screen, and this is the trigger to reset the pipes.
  • The pipe_height variable is used to coordinate the two pipes. Because thegap between them should remain the same, we can’t pick both heightsrandomly. Therefore one of the scripts has this logic and the other doesn’t.
  • The setypositiontopipeheight+/-230 sets one pipe to be abovepipe_height and the other pipe below pipe_height.

This code becomes much simpler in Pygame Zero. We could write a single functionthat updates both pipes. In fact I split it a different way to make it clearthat the reset actions go together:

A small difference here is that I can extract values that I want to re-use as“constants”, spelled in UPPERCASE. This lets me change them in one place when Iwant to tune the game. For example, in the code above, I could widen or narrowthe gap between the two pipes simply by changing GAP.

The biggest thing that differs is that there is no forever loop in Pythoncode. This is the big difference between Scratch and most text-basedprogramming languages: you must update the game by one animation step and thenreturn. Returning gives Pygame Zero a chance to do things like processinginput or redrawing the screen. Loop forever and the game would just sit there,so any loops need to finish quickly.

Pygame Zero calls an update() function when it wants you to update theanimation by one step, so we just need to a call to update_walls():

The Bird¶

The patterns described above for how Scratch logic translates to Python codealso apply for the bird logic. Let’s look at the Python code first this time.

Flappy Bird On Scratch

The code to update the bird is organised into a function calledupdate_bird(). The first thing this function contains is some code to movethe bird according to gravity:

This is a simple gravity formula:

  • Gravity means constant acceleration downwards.
  • Acceleration is change in velocity.
  • Velocity is change in position.

To represent this we need to track a variable bird.vy, which is the bird’svelocity in the y direction. This is a new variable that we are defining,not something that Pygame Zero provides for us.

Simple Flappy Bird On Scratch

  • Gravity means constant acceleration downwards: GRAVITY is greater than 0.
  • Acceleration is change in velocity: GRAVITY gets added to bird.vy
  • Velocity is change in position: bird.vy gets added to bird.y

Note that the bird does not move horizontally! Its x position stays at75 through the whole game. We simulate movement by moving the pipes towardsit. This looks as though it’s a moving camera following the bird. So there’sno need for a vx variable in this game.

The next section makes the bird flap its wings:

This checks if the bird is moving upwards or downwards. We show the bird2image if it is moving upwards fast and the bird1 image otherwise. (-3 waspicked by trial and error to make this look convincing).

The next section checks if the bird has collided with a wall:

If so we set bird.dead to True. This is a boolean value meaning itis either True or False. We can use this to easily check if the bird isalive. If it isn’t alive it won’t respond to player input.

And the final section checks if the bird has fallen off the bottom (or the top)of the game screen. If so it resets the bird:

What’s reset_pipes() doing there? Because I’d organised my pipes code tobe a separate function, I can just call it whenever I want to reset my walls.In this case it makes it a better game because it gives the player a chance toreact when the bird moves back to its start position.

Again, this needs to be called every frame, so we add it to update():

How To Create Flappy Bird On Scratch

The final part of the bird logic is that it has to respond to player control.When we press a key, the bird flaps upwards. Pygame Zero will call anon_key_down() function - if you’ve defined one - whenever a key ispressed:

Here, if the bird is not dead, we set its vy to a negative number: inPygame Zero this means it starts moving upwards.

You should be able to find a lot of parallels between the Python code and thisScratch code:

The biggest differences between Scratch and Pygame Zero are these:

  • You cannot loop forever in Pygame Zero - just update for one frame and thenreturn.
  • The coordinates are different. In Pygame Zero, the top left of the screen isx=0,y=0. The x direction goes from left to right as before, buty goes down the screen! This is why GRAVITY is a positive number andFLAP_VELOCITY is a negative number in Python.
  • bird.dead is a bool, so I can write code like ifnotbird.deadinstead of dead=0 as in Scratch.

Summary¶

Many of the concepts available in Scratch can be translated directly intoPygame Zero.

Here are some comparisons:

In ScratchIn Pygame Zero
changeyby1 (up)bird.y-=1
changeyby-1 (down)bird.y+=1
setcostumeto<name>bird.image='name'
ifdead=0ifnotbird.dead:
setdeadto0bird.dead=False
iftouchingTop?ifbird.colliderect(pipe_top)
WhenFlagclickedforeverPut code into the update() function.
When[any]keypresseddefon_key_down():
pickrandomatobimportrandom to load the randommodule, then random.randint(a,b)
(0, 0) is the centre ofthe stage(0, 0) is the top-left of the window

In some cases, the code is simpler in Python because it can beorganised in a way that helps it make sense when you read it.

The power of Pygame Zero’s actors also makes the coordinate manipulationeasier. We used the anchor position to position the pipes, and we were ableto see if a pipe was off-screen by checking pipe_top.right<0 rather thanifxposition<-240.