Pygame Platformer Part 4: Jumping

KidsCanCode · Beginner ·🌐 Frontend Engineering ·10y ago

Key Takeaways

Adds a jumping mechanic to a Pygame platformer using Python

Full Transcript

welcome back this is part four of our platformer game project and we finally gotten to the one you've all been waiting for uh we're going to make our character jump so we have our game uh right now where we have our Sprite with gravity that acts on it that can run back and forth and we can stand on platforms that's good but now we want to talk about jumping so what I've done um since the last time um I did go ahead and I went over here and I added uh a variable here for player gravity in our settings so that in our player Sprite we can just use that here instead of putting the number um that way those of you who want to adjust gravity make your make the gravity in your game different bigger or smaller um you can just change that value right here okay so so how are we going to do jumping well at this point is when I always get lots of questions about uh things like double jumping and um and wall jumping and all that kind of stuff but and we we'll get to that but uh for right now I just want to talk about the very simple jumping where you press a key and you jump in this case the space bar now we don't really want to do it the way we did the keys because we don't want to jump as long as we're holding it down right that would be flying not jumping right if we had pressing the space bar accelerated Us in the negative y direction that would would be like a a jetpack game or a or it' be a good way to do a lunar lander kind of game where you launch your rocket and you thrust upwards and then um when you're coming down you want to thrust again to to slow down and and and land gently uh but that's flying not jumping so what we want to do is we want to go over to our game uh event section and add a an event for the key press uh for the key down so we want to look for the pg. key down event and the and the key that we want to look for is pg. kpace and if that happens we're going to say player. uh jump all right now what do we need to do in player. Jump well over here in our player when we Define jump there a couple things we could do well we could just say we could just say JUMP means self. v.y equals uh -20 or something like that right and that gives us an upward speed and that's fine that will make us go upwards but the problem is that it's going to do it whenever we hit space so if I go over here and Run the game you can see um whenever I hit space I jump right so I can keep hitting it and go higher um which is fine if you're making Flappy Bird or something um um where flapping makes you go up gravity makes you go down uh this is not much of a platform game because we never we could just run around and Never Land on anything okay so here's what we want so what we want to do is we only want to be able to jump if we're standing on something right and how do we tell we're standing on something well we're standing on something if there's something below us so if we just look one pixel below where our Sprite is see if we if there's a platform there meaning see if we collide with a platform then we know we're standing on something and we're allowed to jump so that means in here we need to check to see if we're standing on something right I'm going to go ahead and put that here jump only if standing on a platform okay so we need to be able to check the platforms but the player Sprite doesn't know anything about the platforms player doesn't know they exist so what we can do is over in our game Loop when we spawn the player we can basically send the player it's called a reference we send it a link to the game and then it knows all about all the games variables including this platforms group right so when our player spawns it knows about the game and we can say self. game equals game and now the player has a essentially a copy of the game it's not really a copy it's a reference but um it knows about everything that's part of the game and that means that we can go right here and do that Collision so we want to look underneath us so we want to take the rectangle of the of the player Right add one to it so one one pixel lower we do our Collision just like we've done before for right we Collide our self with the self. game. platforms still false and then we don't really want to be inside the inside the platform so we move the rect right back up again and so this happens right away so it's you're never going to actually see this because there's no drawing or updating happening between these two commands so all we're doing is just shifting it down temporarily to see if there's anything below us if there is then we can jump okay so let's see how that looks let's go over here run our game and there we go okay so that's much better so you can see if I'm if I hit space again while I'm in the air nothing happens right until I land back on the platform then I can jump again right very cool can jump around it's already lots of fun I can jump around ah except now we see something strange right when I jump so I'm snapping to the top of this platform right if you think about how we did the platform collisions for falling um it'll be kind of obvious why it's doing that right we start jumping up and then as soon as we touch a platform we're instantly snapping to the top of it and setting our velocity to zero so that's not good right we don't want that to happen and for this game since I'm thinking about doing this as sort of a if you're famili with Doodle Jump kind of game um I do want to be able to jump up through the platforms I just don't want to snap to them like this so I want to be able to jump up and then land on it so I actually only want to land on things when I am going downwards not anytime so in our update right here we're just checking all the time to see if we hit a platform all right this is the part I should have labeled this last time uh check if player hits uh hits a platform only if falling right so only if falling so how do we tell we're falling well it means our velocity is downwards or greater than zero so if player do X the x of the player or sorry the Y of the player not the x is greater than zero okay then we can do all of this stuff but not if it's not okay so that's going to look more like this okay so I still jump like I did but if I'm underneath it I jump up and land on it okay so that's much more doodle jumpy and works the way we want it to for the game that we're going to be making now obviously there are lots of other ways we could do this um sometimes in game you don't want to uh jump to the top of it right or be able to jump through it you would want jumping up like this to stop you when you're head hit the platform um and if you come in from the side right now we have a thing where if we come in from the side we're going to land on the top um because we're only really looking at the top um now that's fine for this game um to do collisions in all four directions to a platform is a little more complicated in the code so I'm going to hold off before we add that until we've explored a little bit more of how to get this simple Doodle Jump Style game working then we can talk a little bit more about how um how we're going to make something more advanced work okay so try that out um you can go ahead and add some more platforms if you want although I think we have I'll take a few more minutes to to talk about a simpler way to add more platforms without adding them uh one at a time like we're doing uh like we're doing here so what I want to do is just set up a list of the platforms that I want uh on the screen when it starts kind of like the the start screen okay and I'm going to go over here and I'm going to add that to our settings platform or we'll call this starting platforms because we're going to do other things with the platforms later okay so let's just call it the platform list okay and this is just going to be a list of a bunch of platform coordinates right now I know the first two that I want to do I want to do this one right because I want to make the the one that's on the ground okay and then I'm going to do a comma and I'm going to do I'll just go ahead and do this second one right that's a good one to do and then we could add a few more now you can play around there's different ways you could do where you want to put them just remember it's it's the first number is the X right so the second number is the Y so like you could do something like height minus 350 and that would be 350 pixels from the bottom um the width of the platform and the and the thickness of the platform okay and I'll do a couple more just so I have a good assortment of uh platform on the screen you can play around with these numbers uh put them in different places um you know obviously if you wanted a smaller platform I'm doing them all at 100 we could do this one could be a nice skinny one right but then I have a list of one two three four five platforms that are going to show up on the screen uh at the start of the game okay so once I have that list that's just a list of you know sets of four numbers that I'm going to use to create each platform so over here instead of all of this we're going to do a loop and just Loop through we're going to Loop through this list and use the first entry in the list is this set of four second one third one right and it'll just make each one so I'm going to sa for just call it a plat for short that's another little abbreviation I like to use for Platforms in uh platform list okay so for each one of those I want to make a platform so let's just call it P platform right and then we know we have to put the four numbers now now there's two ways we can do this we could say uh plat zero right plat one plat two and plat three right and that would do the the zero the one the two and the three item in the list but there's a shortcut for that if you want to take a list and break it up into all of its pieces just like we're doing here that's also called exploding the list so in Python we just say star plat right the star stands for explode in this case so take the plat take that list and explode it into its four components and then we have a platform and we just add the platform the platform to the all Sprites list and to the platforms list right and that's a lot less uh that's a lot less verbose than doing it all one at a time and if you want to change them you can go over and change those coordinates let's see what that looks like all right there we go now I've got some different platforms that I can jump to um and bounce around the screen and you can play around with that you can do fun stuff like jumping off the side and landing on another one that's going to be a good strategy maybe um okay and so we'll stop there for now uh play around with how it works try changing some of those settings right that's why we that's why we put them in the settings uh file here and what I recommend you do though is just make sure you have for moving forward make sure you have not too many platforms but not too few um and a good spread of them so that you can get up to the top of the screen because next video we'll start talking about how are we going to scroll this scroll the screen when we get up near the top or move the camera if you like to look at it that way how are we going to keep going higher and get to more platforms all right thanks I'll see you next time

Original Description

In this video, we add a jumping mechanic. 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%204 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
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from KidsCanCode · KidsCanCode · 37 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
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
48 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

Related Reads

Up next
How to Speed Up Your WordPress Website with WP Rocket ⚡Tutorial 2026
Matt Tutorials
Watch →