Class Resources

for HCC classes taught by Rylan Schubkegel

Course Homepage

Flappy Bird 4

Due 1/22 at 11:59

In part 3, you added scrolling pipes. In this final part, you will allow the bird to collide with the pipes!

Extra Challenge (optional)

Instead of exiting the program, show an “end game” screen:

Detecting Collisions

Review the slideshow on collisions. Remember that you’ll have to check four conditions:

As an example, here’s a sketch of the first condition you’ll be checking:

And here is the code to check it (you may have to adjust the calculations):

# Calculate right/left sides of pipe (rectangle) hitbox
pipe_right = pipe_x + PIPE_WIDTH
pipe_left = pipe_x

# Calculate right/left sides of bird (circle) hitbox
bird_right = bird_x + BIRD_SIZE / 2
bird_left = bird_x - BIRD_SIZE / 2

# Check if there is a collision in the X dimension
x_intersects = pipe_left < bird_right and pipe_right > bird_left

Tracking Score

You can keep track of the score in a couple of different ways:

Either way, you need to make the score increase as the game continues.

Rendering Text

Rendering text in Pygame is a three-step process:

  1. Create a font
  2. Use font to render text to a surface
  3. “Blit” the surface to the screen

Here’s what those steps look like:

# Create a font object (using the default font)
size = 32
font = pygame.font.Font(None, size)

# Render text to a surface
text = f"Score: {score}"
antialiased = True
color = (0, 0, 0)
rendered_text = font.render(text, antialiased, color)

# Blit the surface to the screen
coordinates = (16, 16)
game_window.blit(rendered_text, coordinates)

Remember: you will need to re-draw the text every frame.

See the pygame.font documentation for more info.

Submit Assignment

When your program meets the requirements, submit your .py file here:

Submit Assignment