Pygame Platformer Part 6: Game Over

KidsCanCode · Beginner ·🌐 Frontend Engineering ·10y ago

Key Takeaways

Handles game over condition and adds a score in a Pygame platformer

Full Transcript

welcome back to game development of pi game we're working on our platformer game and this time we're going to do the game over and add a score so before we get started I did make one minor change to last episodes code I just added a variable here in our settings for the power of the player's jump just to make it easier again to have all of these properties grouped together and and and set as constants here so you can easily go to one place and change them so I just changed that and in the player code I just have the jumps that to do whatever that number is just like we did with the acceleration or the gravity makes it a little easier to keep track okay so where we left off is we had a sprite that we could we could jump and we could continue climbing upwards and we could keep going and hopefully our randomly spawned platforms will usually allow us to make the jump because of the way we're doing the really simplistic collision all you have to do is just barely hit the platform and you'll be put on top of it but that's okay for now again we didn't want to get too fancy with it right out of the gate so but now we have the problem that when we fall off the bottom nothing really happens we're now falling you know forever down into nothingness so we want that to be the game over condition so that's going to be really easy to actually do we just want to say if the player sprite goes the top of the player sprite goes past the bottom of the screen then you know then that's game over but we can do a little better than that we can give it a little personality so I'll go in here into my game update all right where we're keeping track of where the player hits a platform or the player reaches the top of the screen I'm gonna make another little block here for if we die okay so if the self dot player bottom it's greater than height right so if we hit the bottom of the screen now at the beginning of the game we've got a platform down there so we can't hit the bottom of the screen so hitting the bottom of the screen will be V the game over condition so we could just say playing equals false here right and it would just it would just end kind of like this but yeah we just started over but we can make a little more interesting reaction here if when we hit the bottom we scroll all of the platforms up so it looks like where the camera is falling down as well so if we just go through all the sprites and we take the rect and we move it upwards so that means we want to subtract right but how much do we want to subtract well I don't want to do it at a constant speed because it kind of depends on how fast our player is falling right if we missed a jump way up high we're gonna be falling pretty fast when we hit the bottom if we jumped from low and then what our speed won't be as great so we want to move it at whatever the player speed is but that could be too fast so what I'm gonna use the max function which lets me gives me the maximum of two numbers okay and the reason I want to do that is so that we don't fall too slow for it to be you know worth seeing it's gonna fall it's gonna pick whatever the max number is between 10 and the velocity that we're falling at okay and move everything up that amount and if the bottom of the sprite goes pet goes off the screen off the top of the screen then we can then we can kill it okay and then and then we can put here that if the length of the platform's is zero that means we got rid of them all then it can be game over okay and that'll look a little bit more interesting when we get up here and we've got some platforms on the screen and we die they kind of shoot up off the screen before we reset and start again all right now when we're our goal is that this will of course go to the game over screen when we drop off like that but we'll get to that a little bit later for now let's just go ahead and add a score quickly before we finish up this video if you remember if you followed the shmoop video we did a little draw text function to handle all the rendering of text onto the screen so what I'm gonna do is I'm gonna go over to our settings here and I'm also going to add a font name just so that we can easily configure it again so you can use any font you might have on your system just recall that you're gonna have an issue with portability with moving into other computers if you use a font name that doesn't exist on that computer you might not get what you expect so so we're gonna go over here and in the in the set up here in the init we're gonna go ahead and get the font name from the system that matches as close as possible to that one so we use the match font function and we're going to match that to the font name that way we have the closest match to that name you put in there so and then we're going to define ourselves a new game method called draw text okay and all draw text is going to do is well it's going to need a couple more parameters right we're gonna need to know what text we want to draw we're going to need to know the font size the color and the X and the y of where we want to place it okay so we use the size there to get the font object we generate a surface to render the font on to and what we're gonna render is the text true for anti-aliasing and the color and then we will get a rec generator rectangle too to help us locate it on the screen correct okay then we need to place it now one option we have here is what part of the rectangle we want to use to place the X and the y do we want to use the upper left-hand corner do we want to use the center all that kind of thing and it kind of depends on how you want to lay out your screen to keep things easy I'm just gonna use mid top here and that's going to be the middle on top of the rectangle and then we're going to blip that onto the screen okay so and I don't know if you notice but I just noticed that I accidentally deleted a couple lines there okay so to start with let's just add a score so we're gonna keep track of how well we're doing in the game so under deaf new so when we initialize a new game we're gonna set our score equal to zero and then when do we get points well again to keep things simple for the moment we know we got higher if a plot if we pushed platforms down off the bottom and deleted them so I'm gonna give points there so we're going to add 10 to our score whenever we push a platform I'll down off the bottom of the screen because we jumped higher and then we just need to display that on the screen so if we go to our draw section here we can use our new draw text function and our text is just going to be the score we're just going to show the score the size we want to use is going to be let's try 22 for color I'm going to use white and then I just want it to be centered with over two at the top of the screen so we'll do it say 15 pixels down okay so let's see if that looks like we wanted it doesn't because of course I forgot here to do PG there we go so now we get some points as we jump higher okay so I think we'll stop there for this video and in the next one we'll use this draw text function that we made to also draw our start and game over screens so we can display the things we want to display on them alright thanks for watching you

Original Description

In this video, we'll handle the game over condition (player falling off the bottom of the screen) and also add a score. 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%206 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 · 39 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
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

📰
Why Vanilla JS? In the article below, I am sharing my story of building SaaS product in vanilla js and explaining why I decided to go with this approach. https://guseyn.com/html/posts/why-vanilla-js.html
Learn why choosing Vanilla JS can be a great approach for building SaaS products and how it can simplify development
Dev.to · Guseyn Ismayylov
📰
How to Create a Cursor Tail Using HTML, CSS, and JavaScript
Learn to create a colorful cursor tail using HTML, CSS, and JavaScript for a unique user interface effect
Medium · JavaScript
📰
I built a landing page with Three.js, vanilla JS, and zero frameworks — here's what I learned
Learn how to build a landing page with Three.js and vanilla JS without relying on frameworks, and discover the key takeaways from this project
Dev.to · Ayush Shekhar
📰
Part 1: Why I Rarely Use useEffect Anymore (and what I use instead)
Learn why useEffect is not always the best choice in React and discover alternative solutions
Dev.to · Alejandro
Up next
How To Build A Twitter Clone - React Next JS - Appwrite Crash Course
Adrian Twarog
Watch →