Pygame Platformer Part 3: Gravity and Platforms

KidsCanCode · Beginner ·🌐 Frontend Engineering ·10y ago

Key Takeaways

Implements gravity and platforms in a Pygame platformer using Python

Full Transcript

In this episode of game development with Pygame, we're going to continue with part three of our platformer game. In this video, we will learn how to add gravity and also some platforms that our player can land on. In the last video, we added our player sprite. Um, worked on the movement so we have a kind of a realistic movement with a little bit of inertia, friction slowing us down uh when we let go of the key. Um, and we only did side to side movement because obviously for this we're going to want to have downward movement be caused by gravity. Now, gravity itself is going to be really easy to do. Uh, we just want an acceleration downwards. Now, of course, as soon as I add gravity, we are going to fall off the bottom of the screen. So, we're going to need to add some platforms pretty quick. But, let's go ahead and talk about how we do gravity. So over in our player sprite, we have in our update section, so the part of the sprite that runs every frame of the animation, um we're setting the acceleration in the x direction and in the y direction to zero. So for gravity, we want an acceleration in the y direction, uh positive y direction. So we could put say 0.5 here. So every frame we will be accelerating downwards, right? So you want that to be an acceleration, not a velocity because we don't fall at a steady speed. We accelerate. That's what will look realistic. Okay. So let's see what that looks like. All right. Now, you might notice I'll run it one more time. I don't look like I'm accelerating. I'm falling at a steady speed. Now, can you guess why that is? has to do with what we did last time. Remember when we added the friction? So, we actually have friction slowing us down, basically putting a a maximum on our speed in the y direction. But we don't want that. We want this friction to only apply in the x direction. So, we're going to have just the acceleration in the x direction um affected by friction. And that will make gravity work a little more like we expect it to. There. See how I fell off the bottom of the screen. Okay. So, before we can do even begin to talk about jumping, uh we're going to need to have uh some platforms to land on, right? And eventually, we're going to want to have multiple platforms, but to start with, uh we need to put something that we can land on uh at the bottom of the screen, sort of like the ground of the of our world. So, I'm going to hide my player class for the moment, and we're going to jump down here to the bottom, and we're going to add a class platform. Now, platforms are going to be pretty simple, uh, at least to start with, uh, because all we really want them to do, underscores there, all we really need is when we spawn a new platform, we're going to want to specify where on the screen it is. So, we're going to give it an X and a Y, and we're going to give it a a width and a height for how big we want that platform to be. So, every time we make a new platform, we give it an X and Y location and a width and a height. So um so what we're going to do is first we're going to create the image and that's going to be a surface uh using the width and height. I'm going to just use green for my color. that I'll fill the image with. And then the rect. And we just want to place the rectangle at the at the x and y we specified. Okay. And that is our entire platform class. That's all we need to make the platform work. So, if we come over to our main uh and we go to our new, we're going to be able to spawn a platform. But I'm also while I'm here going to add a platforms group um to hold all the platforms so that we can do collisions just like we did in the schmup where we had all of our mobs in one uh group to do the collision very easily. So So we're going to spawn a platform. We're going to add it to the sprites and to the platforms. Now to start with I'm just going to manually spawn one. Okay. Um, and this is going to be a platform. And remember, we need to give it the X, Y, width, and height. So, I want this to be at the bottom. So, I want the X to be zero. I want the Y to be height minus 40. So, a little bit up from the bottom. I want the width to be the same as the width of the screen. And I want the height to be 40 pixels. Okay. And then I'm going to do uh go ahead and add it to the groups. So it'll be ready to work with our collisions. So that should give us a platform down here at the bottom. 40 pixels tall, width of the width of the window wide. So now we're ready to start talking about collisions. So if we go over here and look at our player sprite, um remember we have our acceleration, our velocity, and our position, right? And those are being calculated using those equations of motion. And at the end of all that calculation, we have whatever our position of our sprite should be. And we're using that position variable to set the center of our player's rectangle. So it puts the sprite wherever the position has been calculated that it should be. And that's okay and it works fine. But it is does mean that if we look at this diagram that I drew that we are going to have to do a few other things. So, if here's our yellow player sprite, and when the player sprite falls and hits the platform, we want to put the bottom of the player against the top of the platform, right? But that means that we want to put the players y position, we want to put this dot this far above the top of the of the platform. So, we're going to have to use some sort of formula like this where the y position of the player is equal to the top of the platform minus half the height of the player. And that's kind of cumbersome and it really doesn't make a whole lot of sense to track the center since most of the time when we're standing on things, we're talking about the feet of the player. So, we might as well do it like this. So instead of using that center position, we're going to use this position of the rectangle, which is called the mid bottom. So all we have to do when we collided with the platform is set the player's Y position equal to the top of the rectangle uh of the rectangle of the platform. And that is a lot clearer and simpler and we don't have to deal with all those height over two problems. So all we have to do to change that is just go right here and set this to mid bottom. So now our position calculated position will always be the mid bottom of the player. And then we're ready to start trying the collisions. So if we go over here to our update section of our game loop, we're just going to do a collision check. We're going to do the collision check between the player and the platforms. And we're going to put false. Remember that tells you whether or not to delete the item you ran into. And we don't want to delete the platforms when we run into them. So, if we hit something, then we want to take this the player's Y position and set it equal to whatever we hit its top. And you might think that that would be enough, but something weird is going to happen. I'm going to fall. And you see how I fall through the platform? I'll run it one more time so you can see it. I sort of sink through there. Now, why does that happen? Well, if you think about what's happening, remember we have our our velocity and our acceleration are continuing to be calculated, right? And we because we keep accelerating, we have this acceleration. So, when we hit that when we hit that platform, our velocity is not zero. Our velocity is some number greater than zero because we're falling. And even though we stopped the rectangle and put it at the top, we didn't set our velocity to zero. So our velocity is going to continue to grow because of the acceleration. So our velocity keeps getting bigger and bigger. So we keep moving even though we said to stop. So we need to make sure we also tell set the players y velocity equal to zero. That way we can fall and stop moving and we can run along the platform, but we don't fall through it. So, I know the last couple of videos have been kind of long and we're always I always try to keep them around 10 minutes and we've just crossed the 10-minute mark now. So, um, just one other thing that I wanted to do was, um, I'm going to add one more platform just so that we have something else to look at. Okay. And so, this platform is going to be I'm going to put it, uh, width over two, uh, say minus 50, just so it's not right at the center. Um, I'm going to put the y at height time 3 over 4, 3/4 of the way. Um, oops. And I'm going to put the width to 100 and the height to say 20. Okay. Now, you can see I have to add it to the groups. And in an upcoming video, we'll talk about a cleaner way to do this. So, we don't have to keep doing all the repeating all of this. added to the group, added to the group, and and we're not going to manually create a whole bunch of platforms, but just for now, just have a second one. It's okay to do it uh temporarily. So, I just wanted to show you that if we had if we had add here, if we had two platforms on the screen, then see, we'll land on the one we hit. And if I walk off the side, I'll fall through it. Just so you see how it works with the platforms. Okay. And I will stop it there. So you can experiment with your gravity. You could make your gravity lower or higher. Right? Go in and change this uh 0.5 right here. Um if you make that number bigger, um you're going to fall faster, right? You have a stronger gravity. And if you make it lower, you can pretend like you're on the moon uh and fall slower. Um, so in the next video we'll actually get started having jumping. All right, thanks for watching.

Original Description

In this video, we will add gravity and platforms to land on. 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%203 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 · 36 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
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
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
Elementor Angie Ai Plugin Tutorial
Quick Tips - Web Desiign & Ai Tools
Watch →