Pygame Platformer Part 15: Powerups

KidsCanCode · Beginner ·🌐 Frontend Engineering ·10y ago

Key Takeaways

The video demonstrates how to add a boost powerup to a Pygame platformer, including coding and implementation details.

Full Transcript

welcome back to game development with pygame this is part 15 of our platformer game and in this video we will be adding power-ups okay so before we go too far into the power-ups there's one thing I wanted to do to talk about about the platform collisions now you may have noticed this and it's kind of should be obvious why it's happening but if you stand over here on the edge you can look like you are floating in space right because there we go so the hitbox of my player is just still barely touching the hitbox of the platform so the you know our code is seeing this as a collision and of course it happens over here too and you know that's a little weird we shouldn't be able to jump when we're standing in them in midair like that so fortunately there's an easy solution to this and since we're using the X the center here as our position for our player we can just say when that position if that position extends past the edge of the platform on either side then we won't collide we won't stop falling okay so we can go down to our platform collision code right and this little this if statement here is what puts us on the top of the platform stops us from falling so basically we'll only want to do that if our player is between the the left and right edge of the platform so we can say if the players pause X is less than the platform that we're hitting right and the player pause X is greater than the lowest rect left now you'll notice this line is getting pretty long right that's a really long line and long long lines are kind of a pain there's a couple things you can do to solve that I mean Python does let you do this if you put a backslash and you continue around then you can you can say this it sees this as one line right so it doesn't count as two separate lines we do want to indent the F now that's not super pretty but it's good enough for now so let's see what this looks like okay so I'm hitting this platform because my exposition is less than this spot and it's greater than this spot so let's go over to a platform where we can fall off now watch as I reach the edge there we go see how so that is not counting as a collision right I'm falling right off the side as soon as my Center gets that site but that doesn't quite look right because my foot should still be touching so we want to give that a little bit extra space so we're gonna say I'm just gonna add a little bit to the little extra ten pixels on either side and I just picked 10 out of thin air just to see what it looks like so let's get up to one where we can fall so now I can walk to there my foot still touching oh and it I fall okay so you can see that looks much better it looks much more natural especially when we walk off the edge that we fall when our feet aren't on the edge supporting us okay on to power ups now power ups are gonna be objects that are gonna that are gonna spawn on the screen and for these I want these power-ups to spawn attached to a platform so I don't want them floating in mid-air or or kind of half overlapping a platform I want them to be resting on platforms like their objects that are sitting there that we'll go find so so when we make the new sprite and I'm just gonna go ahead and I'm gonna copy some of this stuff so I don't have to retype it alright bad indenting there we go okay so so what we want to do is we're gonna call this I'm just gonna call this a pal for powerup okay now when we spawn a powerup we're just gonna pass it a platform that you know we're gonna so we're gonna pass it what platform it needs to be sitting on so that way it'll know where it needs to be and so we're gonna do in itself game will keep track of that platform we don't have a list of images I don't need that but we do need our image sorry so first of all we are gonna have multiple types of platforms now to start with I'm going to do we import yes we imported choice okay I'm gonna just have one platform or I mean I'm sorry one type of power up to start with and that's a boost one that shoots you upwards later we can have more and we can improve this randomness here and choose different random ones what we want to do but for now we're just gonna stick with one and the boost image is going to be over here in our spreadsheet it's this little jetpack so we're gonna use that set the color key to black get the rectangle in our location so our where we place the where we place the powerup sprite is just going to be at the center of the platform and then the bottom of the sprite will be at the top and a little bit up so I just want to look like it's sort of just barely floating right there above it now as far as the update for our sprite we're gonna just make sure we keep we just want to keep the bottom of the sprite at that point right so that way if the put when the platform moves so will the power and if now if we want to delete these these power-ups when the platform disappears right and the platform so the platform's go off the bottom and then they disappear right that means this self dot plat won't exist anymore so we get an error message so we want to make sure we delete the sprite whenever our the powerup sprite whenever the platform sprite gets deleted so we can do that by using the self dot game dot platform's group right that group has a list of all the platform's right and they get removed from that group when they go off the bottom and get deleted so in PI game with groups you can do you can do the halves command and this will return true or false so this this command will be true or false whether the platform in the platform group and if it doesn't so if this is false then we will delete the powerup sprite as well okay so that's our powerup sprite now we have to talk a little bit about groups some more so you recall if I go back over here to our new we have these we have these groups where we are placing the different sprites in right we add the player to the all sprites group when we spawn a platform we added to both groups that starts to get tedious having to do this right we had to do that here down here when we spawn some new platforms we also have to add it to both right well there's an easier way to handle groups and that way is if we go over here actually real quick let me make a we're gonna need a group for power-ups so that we can do the collision so I'm gonna say power-ups is a PG sprite group right now so the game has these three groups and we know we're gonna be wanting to add different sprites to them so we can do that instead of doing it as a separate command every time we spawn the sprite we can do it when the sprite is actually initiated so let's see here so we'll start with our power up here so if we make a list here right when right before and it's important we do this before the sprite class in it okay we name a list of groups so in this case we want game dot all sprites and game dot power-ups right those are the two groups that I want the powerup sprites to be in then I can just adhere to the init that it should do that now if I do that I can do the same thing with the platform's right soft out-groups equals so this is gonna be a game dot all sprites and game dot platforms this makes it a lot easier if later on as your as you're changing things and you add different groups for different kinds of effects or different different code that you want to add that needs more groups you can easily just add here in the sprite a list of which which groups it needs to be a part of okay so if we do that then over here we no longer need this line oops we no longer need that line we no longer need either of these lines and in fact we don't need when we spawn a platform we don't even need to assign it to a variable because it's going to get added to the group so I can do that there and I can do it here as well okay and now everything should still work the same right we're still spawning and the other group collisions and stuff is still working but we've simplified some of our sprite creation and we're ready to do the powerup spawning okay so let's add a couple more game settings over here we're going to add a couple of things here I'm going to add a boost power that we're gonna use for when we how how fast the boost shoots us upwards and we're also going to add a number for how frequently we want these power-ups to spawn so I'm gonna put about something less than ten right so this is how likely every time a platform spawns that it will have it how like it is to have a tower up on it all right so in the platform in it right here when the platform spawns we're just going to give it a chance to have a powerup on it and all we need to do for that is we could pick a random number between 1 and 100 or 0 to 99 technically and if it's less than the power of spawn percentage then we'll spawn a powerup we pass it a copy of self game and we pass it a copy of the platform itself ok and then we are going to need Rand range up here and let's see how that works up there we go so that one spawned and has a plot a powerup on it so now we can see that most of the platforms aren't having power ups on them but every once in a while we got a dry spell here hope there's another one and again once we start responding different kinds of power-ups we can change that percentage around very easily so all that's left is here in our update to just check for collisions with the platform's I mean with the power-ups so we can stick this in here this is going to be if player it's a powerup okay so much like we've done before we're gonna do a sprite collide between the player and the power-ups and we want the power-ups to disappear we might conceivably hit more than one so if the powerup type is Boost remember we might have other types of power-ups later then we are going to take our players Y velocity and we're gonna just shoot upwards at whatever the boost power was and we're also gonna set our jumping equal to false because we don't want to have the jump cut stop our stop our boost in the middle okay so let's see how that works now I'll go find a boost power up spawn for us there we go OOP OOP self dot player player velocity game doesn't have a velocity oh there's a couple alright there we go so now we're getting some all right we can get pretty high scores now up unless that happens what a bummer anyway you can see the power-ups are working and now we have a chance to boost if we had another one along the way it's gonna boost us again so we can chain them together my maybe we will update or increase that spawn percentage because they're not coming up very often I thought 7% would be plenty but that's good and so that'll about do it except I do want to add one more thing which is it would be nice to have a sound play when we hit the powerup so let's go and add a sound here so I am going to duplicate that line and made it make a boost sound and the sound I have for that I got this one so I'm just going to I'm gonna duplicate this line it would be a boost sound and this was the sound I have is boost 16 another one I just created you can use sound effects there again like we did in the previous one and then we're just gonna want to play this boost sound right here where we just added alright I'll give us some good feedback when we get boosted let's find one first when you're trying to find one to test it out the random number generator doesn't cooperate with you there we go perfect nice okay this video has gone a little bit long I apologize for that it seemed to go by fast as I was doing it but suddenly we were way over ten minutes so we will stop there for this time and as always please click like below that helps other people find these videos and if you're not subscribed subscribe so you can get the next one as soon as it's released and I will see you next time thanks you

Original Description

In this video we add a "boost" powerup to our game. In each video in this series, we'll add another feature to our platformer until we have a full game experience with graphics, animations, sound, and much more! Code for this part: https://github.com/kidscancode/pygame_tutorials/tree/master/platform/part%2015 If you like these videos please consider supporting me on Patreon: https://www.patreon.com/kidscancode Other helpful links: * Installing Python: http://kidscancode.org/python-install.html * Installing Pygame: http://kidscancode.org/blog/2015/09/pygame_install/ * Setting up Atom: https://www.youtube.com/watch?v=uve1tjVIQ6c&ab_channel=KidsCanCode
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from KidsCanCode · KidsCanCode · 48 of 60

1 Learning to Code with Python: Lesson 1.1 - What is Programming?
Learning to Code with Python: Lesson 1.1 - What is Programming?
KidsCanCode
2 Learning to Code with Python: Lesson 1.2 - Drawing with Turtles
Learning to Code with Python: Lesson 1.2 - Drawing with Turtles
KidsCanCode
3 Learning to Code with Python: Lesson 1.3 - Variables
Learning to Code with Python: Lesson 1.3 - Variables
KidsCanCode
4 Learning to Code with Python: Lesson 1.4 - Loops (and more turtles!)
Learning to Code with Python: Lesson 1.4 - Loops (and more turtles!)
KidsCanCode
5 Learning to Code with Python: Lesson 1.5 - Saving and Running Programs
Learning to Code with Python: Lesson 1.5 - Saving and Running Programs
KidsCanCode
6 Learning to Code with Python: Lesson 1.6 - Functions
Learning to Code with Python: Lesson 1.6 - Functions
KidsCanCode
7 Learning to Code with Python: Lesson 1.7 - Input and Conditional Statements
Learning to Code with Python: Lesson 1.7 - Input and Conditional Statements
KidsCanCode
8 Learning to Code with Python: Lesson 1.8 - Number Guessing Game
Learning to Code with Python: Lesson 1.8 - Number Guessing Game
KidsCanCode
9 KidsCanCode - Patreon Intro Video
KidsCanCode - Patreon Intro Video
KidsCanCode
10 Learning to Code with Python: Lesson 1.9 - Rock Paper Scissors Game
Learning to Code with Python: Lesson 1.9 - Rock Paper Scissors Game
KidsCanCode
11 Learning to Code with Python: Lesson 1.10 - Secret Codes
Learning to Code with Python: Lesson 1.10 - Secret Codes
KidsCanCode
12 Learning to Code with Python: Lesson 2.1 Creating Computer Graphics
Learning to Code with Python: Lesson 2.1 Creating Computer Graphics
KidsCanCode
13 Learning to Code with Python: Lesson 2.2 Simple Animation
Learning to Code with Python: Lesson 2.2 Simple Animation
KidsCanCode
14 Learning to Code with Python: Lesson 2.3: Animating More Objects
Learning to Code with Python: Lesson 2.3: Animating More Objects
KidsCanCode
15 Learning to Code with Python: Lesson 2.4: More Fun with Animation
Learning to Code with Python: Lesson 2.4: More Fun with Animation
KidsCanCode
16 Extra: Setting up the Atom Editor for Python
Extra: Setting up the Atom Editor for Python
KidsCanCode
17 Game Development 1-1: Getting Started with Pygame
Game Development 1-1: Getting Started with Pygame
KidsCanCode
18 Game Development 1-2: Working with Sprites
Game Development 1-2: Working with Sprites
KidsCanCode
19 Game Development 1-3: More About Sprites
Game Development 1-3: More About Sprites
KidsCanCode
20 Pygame Shmup Part 1: Player Sprite and Controls
Pygame Shmup Part 1: Player Sprite and Controls
KidsCanCode
21 Pygame Shmup Part 2: Enemy Sprites
Pygame Shmup Part 2: Enemy Sprites
KidsCanCode
22 Pygame Shmup Part 3: Collisions (and Bullets!)
Pygame Shmup Part 3: Collisions (and Bullets!)
KidsCanCode
23 Pygame Shmup Part 4: Adding Graphics
Pygame Shmup Part 4: Adding Graphics
KidsCanCode
24 Pygame Shmup Part 5: Improved Collisions
Pygame Shmup Part 5: Improved Collisions
KidsCanCode
25 Pygame Shmup Part 6: Sprite Animation
Pygame Shmup Part 6: Sprite Animation
KidsCanCode
26 Pygame Shmup Part 7: Score (and Drawing Text)
Pygame Shmup Part 7: Score (and Drawing Text)
KidsCanCode
27 Pygame Shmup Part 8: Sound and Music
Pygame Shmup Part 8: Sound and Music
KidsCanCode
28 Pygame Shmup Part 9: Shields
Pygame Shmup Part 9: Shields
KidsCanCode
29 Pygame Shmup Part 10: Explosions
Pygame Shmup Part 10: Explosions
KidsCanCode
30 Pygame Shmup Part 11: Player Lives
Pygame Shmup Part 11: Player Lives
KidsCanCode
31 Pygame Shmup Part 12: Powerups
Pygame Shmup Part 12: Powerups
KidsCanCode
32 Pygame Shmup Part 13: Powerups (part 2)
Pygame Shmup Part 13: Powerups (part 2)
KidsCanCode
33 Pygame Shmup Part 14: Game Over Screen
Pygame Shmup Part 14: Game Over Screen
KidsCanCode
34 Pygame Platformer Part 1: Setting Up
Pygame Platformer Part 1: Setting Up
KidsCanCode
35 Pygame Platformer Part 2: Player Movement
Pygame Platformer Part 2: Player Movement
KidsCanCode
36 Pygame Platformer Part 3: Gravity and Platforms
Pygame Platformer Part 3: Gravity and Platforms
KidsCanCode
37 Pygame Platformer Part 4: Jumping
Pygame Platformer Part 4: Jumping
KidsCanCode
38 Pygame Platformer Part 5: Scrolling the Window
Pygame Platformer Part 5: Scrolling the Window
KidsCanCode
39 Pygame Platformer Part 6: Game Over
Pygame Platformer Part 6: Game Over
KidsCanCode
40 Pygame Platformer Part 7: Splash & End Screens
Pygame Platformer Part 7: Splash & End Screens
KidsCanCode
41 Pygame Platformer Part 8: Saving High Score
Pygame Platformer Part 8: Saving High Score
KidsCanCode
42 Pygame Platformer Part 9: Using Spritesheets
Pygame Platformer Part 9: Using Spritesheets
KidsCanCode
43 Pygame Platformer Part 10: Character Animation (part 1)
Pygame Platformer Part 10: Character Animation (part 1)
KidsCanCode
44 Pygame Platformer Part 11: Character Animation (part 2)
Pygame Platformer Part 11: Character Animation (part 2)
KidsCanCode
45 Pygame Platformer Part 12: Platform Graphics
Pygame Platformer Part 12: Platform Graphics
KidsCanCode
46 Pygame Platformer Part 13: Improved Jumping
Pygame Platformer Part 13: Improved Jumping
KidsCanCode
47 Pygame Platformer Part 14: Sound and Music
Pygame Platformer Part 14: Sound and Music
KidsCanCode
▶ Pygame Platformer Part 15: Powerups
Pygame Platformer Part 15: Powerups
KidsCanCode
49 Pygame Platformer Part 16: Enemies
Pygame Platformer Part 16: Enemies
KidsCanCode
50 Pygame Platformer Part 17: Using Collision Masks
Pygame Platformer Part 17: Using Collision Masks
KidsCanCode
51 Pygame Platformer Part 18: Scrolling Background
Pygame Platformer Part 18: Scrolling Background
KidsCanCode
52 Pygame Platformer Part 19: Wrapping Up
Pygame Platformer Part 19: Wrapping Up
KidsCanCode
53 Gamedev In-depth Topics: 4-way vs. 8-way Movement
Gamedev In-depth Topics: 4-way vs. 8-way Movement
KidsCanCode
54 Gamedev In-depth Topics: Time-based vs. Frame-based Movement
Gamedev In-depth Topics: Time-based vs. Frame-based Movement
KidsCanCode
55 Gamedev In-depth Topics: Non-integer Movement
Gamedev In-depth Topics: Non-integer Movement
KidsCanCode
56 Tile-based game Part 1: Setting up
Tile-based game Part 1: Setting up
KidsCanCode
57 Tile-based game Part 2: Collisions and Tilemap
Tile-based game Part 2: Collisions and Tilemap
KidsCanCode
58 Tile-based game Part 3: Smooth Movement
Tile-based game Part 3: Smooth Movement
KidsCanCode
59 Tile-based game Part 4: Scrolling Map / Camera
Tile-based game Part 4: Scrolling Map / Camera
KidsCanCode
60 Tile-based game Part 5: Player Graphics
Tile-based game Part 5: Player Graphics
KidsCanCode

This video teaches how to add a boost powerup to a Pygame platformer, covering coding and implementation details. By following this tutorial, viewers can learn how to create a full game experience with graphics, animations, sound, and more. The video is part of a series that builds upon previous features to create a complete game.

Key Takeaways
  1. Create a new class for the powerup
  2. Define the powerup's properties and behavior
  3. Add the powerup to the game world
  4. Implement collision detection and response
  5. Test and refine the powerup's functionality
💡 Adding powerups to a game can enhance gameplay and provide a more engaging experience for players.

Related Reads

Up next
Free UI Components Library for Web Developers
Alamin
Watch →