I added the pipes! This was kind of hard to figure out, but I finally got it working!
- For the pipes, I made a new
Pipeclass as a subclass ofpygame.sprite.Sprite. But this is not like an ordinary sprite class, as it’s actually 2 pipes LARPing as 1. I made a basic pipe sprite, and then I duplicate it in thePipe.__init__()method. This allows me to have a top pipe that is flipped and then the bottom pipe is just the sprite. The actualimageattribute is a surface that has both the top and bottom pipes blitted on to it. It took me quite a bit of time to figure out the transparency, though. This is because at first it was just showing up as a fully black column, even though the middle gap was supposed to be transparent. I fixed this by making sure the blitting was actually being done where it was supposed to, and then adding theSRCALPHAflag to the part where I make the imageSurface. - For collisions, I used a feature called masking, which allows for pixel-perfect collisions based on the actual image of the sprite and not the rectangle. I made both the player and the full pipe image (both top and bottom) have a mask, and then as of right now I am running
exit()if they collide. This allows for the player to pass through the middle but then exit the game if it touches the pipes. - I am not keeping score just yet, but I managed to get that done by giving the
Pipeclass apassedboolean attribute, and in the main event loop I am simply looping over the pipes in thepipesGroupand checking if their x-coordinate value is behind the player, which means that the player must’ve gotten through the pipe, and later triggering the score to increase.
It’s coming along really well, and I’ve been learning a lot from this project!
Comments 0
No comments yet. Be the first!
Sign in to join the conversation.