Friday, May 23, 2014

Making yet another Flappy bird clone with Pygame


Python logo (source: Wikimedia Commons)
There are literally thousands of Flappy bird clones everywhere in the appstores and on the web. That's why I thought it would be a great idea to make my own clone. I was learning how to use the python game library Pygame at the time, so I decided to make this game in Pygame to learn the library better.

The code is hosted at github, at https://github.com/liu916/Flappy

-------------------------------------------------------------

The first step to making the clone was getting the pictures and sprites for the bird, pipes, etc. Since the sprites aren't released in creative commons, I'll give you an accurate representation of the sprites drawn by Mr. Lamonae.
(Credit: Lemonae)
After loading the images into python, the next step was to implement the "bird" pygame.sprite.Sprite class. There really wasn't much to the class besides loading the bird image and implementing the movement of the bird.

In all the flappy bird clones, the bird you play as is always moves constantly rightward and accelerates downward if the jump button isn't pressed.

The bird may look like its traveling rightward, but the game only implements the pipes moving leftward to give the illusion of the bird moving right. So, the bird only needed to be changed in its vertical position.

Implementing its vertical position over time was easy enough:
y_velocity = y_velocity + y_acceleration
y_position = y_position + y_velocity
where the y_acceleration is some negative falling value.

When the user presses the jump button, the bird's y_velocity is to be changed to the bird's jump speed regardless of its value. That wouldn't work in real life, but it makes the game easier to play.

Next up was implementing the pipes. Making sure there was enough room between the pipes vertically and horizontally was tricky. The vertical height had to be just be just above the bird's total jump height so it wasn't impossible but wasn't too easy either.

The pipes also had to be generated at random heights, also adjusted so the bird is not flying too close to the ground.

Then, I was faced with an interesting problem: how should I make an infinite number of pipes coming at the bird (as long as the bird doesn't crash) ? To solve that problem, I made three pipes that acted as all pipes. Once one pipe went off the screen, I moved it back behind the other two pipes and adjusted its height.


Tying all the pieces together, the game needed to detect when the bird touched anything, which
For all sprite in sprites:
    if bird.rect.coliderect(sprite):
        game_over
worked pretty well.

Last of all, I made the score integer increment when the bird passed a pipe.

-------------------------------------------------------------------------------------

The Pygame Flappy bird clone is still vastly unfinished. The score prints to the terminal, not screen, the game_over() function does nothing but exit Pygame, and there is no background to the game.

Even though its only a working prototype, I learned a lot about the Pygame library. In programming, the only way to truly learn about something is to do it yourself.

Questions, comments? Post them below.

No comments:

Post a Comment