CPSC120A
Fundamentals of Computer Science

Activity 26

Physics

Flappy Bird

Flappy Bird was an incredibly popular game that made A LOT of money, especially considering it was a "free" game. The game was suddenly removed from the App Store, but the initial success of the game resulted in many clones being available almost instantly. The game is not difficult to replicate, now that we have lists!

Details

Create a python program that plays a simplified version of the game Flappy Bird. The program should have a while loop for the main animation loop. At the beginning of the loop the screen is cleared, in the middle of the loop the scene is updated and drawn, at the end of the loop the program waits. Use the code we created during class that animates the bird as the basis for this. Don't forget to place the graphics module and sprite module in your directory.

Every 3 seconds, a new pipe Sprite object should be created. The new pipe should start at the right side of the screen and and have a random height. All pipes should be stored in a list to simplify updating the pipes and testing for collision with the pipes.

Create the function move_pipes(pipes) that moves all of the pipes to left by the same small amount. The parameter pipes is a list of Sprite objects that represent all of the pipes in the game. If a pipe is past the left edge of the screen, the pipe should be removed from the list.

Also create the function is_collision(bird, pipes) that determines whether the bird is colliding with any pipes. The parameter bird is the player controlled bird Sprite object and the parameter pipes is a list of Sprite objects that represent all of the pipes in the game. The function should return True if the bird sprite is colliding with any of the pipes.

Use these two functions to replicate a simple version of flappy bird, with pipes only on the bottom of the screen. You can download and use the following images:

Example

  • Because the graphics module can not resize images, the height of pipes is actually controlled by changing where the pipe is drawn vertically. If most of the pipe is off the bottom of the screen, the pipe will be short. If most of the pipe is on the screen, the pipe will be tall. So to generate random heights actually requires generating random y coordinates for the pipes. Don't forget that the sprite's x and y attributes specify the location of the center of the sprite.
  • Changing the x and y attributes of a Sprite object changes where it is drawn the next time the graphics.draw_sprite function is called. To move a pipe horizontally to the left, decrease the pipe Sprite object's x attribute.
  • To test if the bird Sprite object collides with any of the pipe Sprite objects, test if the bird collides with each of pipes individually inside of a loop. Use the sprite module's collides method to test if two sprites collide. If the bird collides with 1 pipe, the function should return True. If the bird object does not collide with any pipes, it should return False

Challenge

Modify your program so that there are pipe sprites along the top of the screen as well.