for HCC classes taught by Rylan Schubkegel
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!
Instead of exiting the program, show an “end game” screen:
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
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 in Pygame is a three-step process:
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.
When your program meets the requirements, submit your .py
file here: