Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Key Takeaways
This video series demonstrates the development of a side-scroller game using Pygame, with a focus on implementing scoring and end screen functionality, as well as fixing various bugs and glitches.
Full Transcript
hey guys and welcome back to the fourth and final video in my PI game a side scroller series in today's video I'm going to be doing the finishing touches on our game so that's gonna include adding some sort of scoring mechanism and then adding an end screen and kind of fixing touching up a few bugs making a few tweaks I'm just gonna say right now that everything I do in this video you guys don't have to do this you can change any of the aspects you'd like if you want to make the game of a different experiment with things feel free to do that and I encourage you to do that because the more that you can learn by yourself the more you're gonna retain that information so pretty much again I'm not gonna be making everything perfect in this video there are a few glitches I'm aware of so I encourage you guys to try to fix them on your own and to add new things into the game this is just trying to give you a good starting point for where you can hopefully create a more complex game in the future so I'm gonna show you a quick what do you called example of what I have so I've already coated this and I'm just looking off my other screen and kind of explaining it so that I don't get sidetracked in this video you can see already at the top here we have our score and I've tweaked the hitboxes a little bit in here and you can see we can now slide underneath this and keep sliding and then if I hit it I will fall over and you can see we're brought to kind of an end screen here where it says this is our best score and this is the score we just received again the screen is not very pretty it doesn't show much I'm just simply doing this to show you how you can cut a time out an end screen does everything works with your game and then if I click down on the most button it just resets the game and I'm able to try again a nice thing I kind of would want to add is maybe just at the bottom say press the mouse button to continue so that if someone were to be playing this game and they didn't know what to do or they didn't create the game then they would know what to do again that's a good so you can see that he kind of flies up I'm not gonna worry about that too much you guys can feel free to fix that in your own thing so that's what its gonna look like and let's get right into the code now so the first thing I want to do is just fix a glitch that I noticed as I was writing this next section here and that's gonna come up a lot when you guys are making games you're gonna code something and there's no possible way again that you can think of every situation so we're just gonna fix this really small I'm gonna copy where we're in runner class here at player class and it says self thoughtful now what I want to do here is pretty much this is when we say that our player is falling we want to display that image of them falling now what ends up happening if I as I have this here in this program is if we are to like be jumping in the air and then we hit a saw our players gonna finish the jump and then he's gonna start falling so if you want it to stay that way where you still see the animation of him finishing the jump and then there's a slight delay and he falls afterwards then you can leave like this but what I'm gonna do is just make it so what the second he hits an object he's gonna look like you fell so you know exactly where you kind of messed up in the game so I'm just gonna copy this and I'm going to paste it up at the very top of our draw method here like that now we do just have to change this so get rid of the Yale we're gonna say if and then where it says self jumping I'm gonna type in Elif now what this is doing is just changing the sequence so we're first gonna check if the players falling and then if he's falling we're gonna do this this is the reason why he doesn't continue to move down as as he's falling like once he hits an object it just starts splitting his falling body so if you want it the other way just leave it like that but yeah that's the first kind of bug fix in my opinion we're gonna be doing that's all we're gonna be touching really in the running class now what we need to do here is figure out what's gonna happen once our player falls so before we just had it delayed the game for like a few seconds or like I'm so that we could see that our player had fell had fallen what I want to happen is a screen to continue to keep scrolling and then a few seconds later it pops up that end screen so what you saw in my program so the way we're gonna do this is just we need to create a few new variables here so the first one I'm gonna create is just gonna be called pause I'm gonna set this equal to zero we're simply gonna use this just to increment our our time so we don't actually delay like we were before now I'm gonna send another variable full-speed I'm just gonna set this equal to zero for now and what I'm gonna do is when our object collides with our runner I'm just gonna say that fall speed is equal to speed just like that okay and what we're gonna come back to that now I'm also going to add in one thing here which is just gonna say if pause equals equals zero and then I'm gonna indent this in and then we're gonna say pause plus equals one like that okay so if pause is equal to zero we're gonna increment pause to be plus one we're actually gonna make it sorry not plus one we're gonna make it equal to one and reset the false speed equal to speed now I'm gonna explain how this works in a second but it's easier just to type it all out right now now up at the top right underneath I redraw window we're gonna see if pause is greater than zero but we're gonna increment to pause so pause two plus equals one and then after that we're gonna say if pause it's greater than false speed times two and I'm gonna get to that in a minute then entry okay now this we haven't created yet I'm just typing this in for now we're gonna get to that in a second so that reset function I have here I'm actually gonna change the name of that to n-screen so let's just explain what I've done here pretty well so what's gonna happen now is once our player collides with with an object we're gonna check first of all to see if pause which is a new variable we created is equal to zero now if that is true then that means we haven't already collided with an object okay because if pause is set to zero at the beginning so once that happens we're gonna set our full speed equal to our current speed so say for example our current speed is like 35 that's how many frames per second removing that's gonna be set and stored in full speed this needs to be speed not sped or whatever had there and then we're setting pause equal to one so that this isn't gonna keep resetting because if we don't set pause equal to one then pause is still going to be zero and it's gonna keep changing our fall speed to our speed even after we've fallen um I hope you hopefully can understand that now up here what this is gonna do is we're saying if pause is greater than zero meaning if it's equal to one pretty well or two or whatever then we're gonna increment pause now what this is doing is we're only going to change this variable pause once we've started falling because that's gonna be the delay for how long we should wait until we move on to our end screen we say if it's greater than zero incremented and then we were checking now if pause is greater than false speed times 2 now what this does is it pretty much says that whatever our speed is so say our speed is like 27 frames per second we're gonna multiply it by 2 now what that's going to give us is equivalent of two seconds there about two seconds so when we get to the higher speeds if we had just set this to a constant for example a hundred and where maybe we're running at a speed of 100 frames per second then the delay is gonna be really short so I want this to be equal to whatever the speed is when we fell so that it's gonna change dynamically based on how fast we're moving it's kind of hmm maybe a complex idea but just throw that in there for now it's not really that important to understand okay and then what I want to do is I'm just gonna put this redraw window at the bottom of our wall appear I should have done this at the very beginning but I just I don't know why I'm I'm gonna put it actually underneath our clock tick here I like that redraw window okay just to clean things up a bit and then that should be it for you know a while loop at least for right now now we need to go to our end screen so what what I have here is I'm going to change this reset to end screen and then inside of our end screen to make sure that we stay inside of our end screen we need another well loop so in the end screen what's gonna happen is we're gonna reset a few variables so that our game will be able to reset when we move out of the end screen and then we're gonna just do what we've done down below or that's a run is equal to true pretty much copy a bit of our loop here I'm just gonna say well done and then for event in PI game down event oops don't get and then again if event dot type equals equals PI game got quit we always want our user to be able to leave the game if possible don't quit and that we say Ronnie equals false PI game dot quit like so okay and then after this we're gonna do another else if statement or say if event got type equals equals PI game oops equal sign dot all capitals Mouse motion not Mouse motion mouse button down then we were gonna say run is equal to false now what this is gonna do is it's pretty much means whenever we click any of the mouse buttons so right left middle mouse button side mouse buttons whatever then we're gonna exit this loop and when we exit this loop we now exit this function so it's because we're exiting that function then we just continue on with the rest of our loop and everything's gonna be reset so it's gonna look like we just started a new game when in reality we're just running through the same loop we're just kind of moving on to a different screen okay that's pretty good now L so in this wall loop we want to draw some other things so I'll get to that in a second but let's just first put a delay in here so that our loop doesn't run super fast do delay and maybe we'll do like a hundred we don't need to go that fast consider all we're gonna do is just click a button and then we can get to drawing everything in just a second so let's just run the program really quickly and see what's happening so far so you can see we've got our player I can run you can jump I can slide whatever now let's see what happens when he hits the saw just test everything out again hits the saw you can see he falls over we wait we wait we wait and then it calls the end screen I believe so maybe we aren't calling on-screen let's just check this year quickly if paul is greater than full speed times two and screen Oh so we've spelled two paused wrong that's great that's the issue okay so we actually just create a new variable instead of a setting Paul is equal to one that would explain why things happening well I run into a lot of errors with spelling mistakes here don't I okay so to give it a second again to see what's happening we'll just collide with these objects and see if we get to our end screen okay so we fall in one two there we go okay it's paused perfect so you can see the game is just pause now if I click the mouse button it it continues so the reason why it's like lagging like this because we haven't reset any the variables but you can see after that two-second delay about we've we pause the screen so that goal has been accomplished so now let's move on to the next step sorry about that a little issue before okay so now we're in our end screen so once we're in our n screen we want to display a mess we want to say like press this button to continue so on we also want to reset a few variables so if we want to reset variables from a function that we've defined down here we can't simply just type for example Paul is equal to zero because if we do this what actually ends up happening is it creates a new variable in this function which is local to this function rather than resetting this puzzle so what we need to do to start is you need to start by typing global and I know the name of all the variables that we want to be able to change and access so we'll do objects and then I guess we will do speed as well like that okay so now we're gonna change we're gonna say objects equals a blank list the reason we do this is so that when we spawn back in we don't see any objects on our screen and then speed again equal to 30 and that should be good for right now we don't need to worry about changing any of the attributes of the player of the runner so everything should be fun at that and now what we want to do in here is we want to draw everything so usually I would say draw everything from a function but because we're doing it just one time in this loop it's fine to draw it inside of the loop and we're gonna start by just drawing the background so wind up blitz background to zero just to reset everything so it's like Conant clears the screen if you make a font object here so that we can display some fonts a large font is equal to PI game got fog dot sys font and then in here you guys know I like comics and we'll use that one sighs oops give a size of eighty and we're gonna render some volunteers so let's just say previous score which we're gonna find in a second is equal to large font rendering and then we'll render up let's say previous score plus actually we'll just leave a previous score for now because I'm going to get to in a second comma 1 comma and then whatever color you want so in this case I'm going to do white and then we'll do our next thing where you need to blip this onto the screen so we're going to say win double it previous score and then you guys should know hopefully how we can get this go in the middle of the screen I think I've done this a few times maybe not in this series but in another series of mine the way that we can do this is simply by taking the width of the screen dividing it by two and then subtracting it from the width of our object so in this case I'm just gonna copy this in to save me a bit of typing here as you can see we have width which is our variable containing the width of the screen divided by 2 and then subtract the previous score with divided by 2 for height in here what should our height be let's see we'll just give it a height static of 200 we can play around with that after now we also want to blit our new score which we still haven't even defined yet so we'll do that in a second so let's just define here new score and that's equal to again we're gonna render this and I'm just gonna copy this say me at a time like that we'll do score cuz it's your current score again we're gonna add to that in a second to what the score actually is and then it will copy this one more time so that we can bleed it onto the screen change the name of course to new score and then we'll change the height to let's say like 320 maybe all right there we go now we just need to update this screen so pygame display dot update like so and that should be about it for what we're putting in our what we're drawing onto the thing now I still haven't defined what our score is gonna be and how we're actually gonna score now the way we're gonna do that is based off speed so pretty much every time we go up by 10 in speed we're gonna go up by one and score that's the way I want to do it or we can do it maybe by five so that the scores are higher let's let's do that actually so at the top of my function here or not function loop I'm just gonna say score is equal to and then speed integer division 5 we remember what integer division does right and then we're just gonna subtract 6 from it now the reason we're subtracting 6 is because because we start at a speed of 30 and I don't want you to start with a score of 6 because if we do 30 divided by 5 we get 6 so it has to be relative to what we've started out so by subtracting 6 as we're at 30 we're gonna start with the scores you okay we have the score now and then up here we can now just add plus str score like that now I'm also just gonna throw score up here in our global so we can change it and then at the bottom outside of our while loop so once we've exit the while loop we're gonna reset the score equal to zero just by doing that okay everything should be good now score equals speed and then we also need to draw the score on to the screen right so just like you saw on my other program we were just bleeding the score in our redraw window here so what I'm gonna do is just make another font objects let's say font equals PI game don't font dot sys font again Comic Sans my go-to I will make it about 30 big this time and I will render it out so let's say text is equal to what render in this case we're just gonna use the string score and before that we want to put like a little thing before that it says score plus sign of course and then 1 and color white again and then after that we need to of course put this on the screen so we'll just say wind up Blitz text and where do we want to put it let's put it about 700 and then 10 okay and that should be good let's now run the program and see if we run into any errors there we go okay so we can see we have score now up at the top of the screen and every frot every time we increment by 5 we get the score going up by one so we can see here everything looks to be working fine if I hit an object I fall over we wait the delay and then we have previous score and we have score awesome everything should be everything is looking good here now the only thing that CSU is we need to fix what previous scores and we need to fix the position of score the reason scores like that is just I just realized I messed up on one thing here so I copied and pasted this but I didn't change the name of previous score changes to new score and then we should have that working and we also need to figure out what our previous score is going to be now what I'm gonna do for our previous score is I'm some I'm gonna write it into a text file so in Python it's really cool and really simple you can write stuff into text files really easily and that's what we're gonna save our score you could save other attributes in there as well say if you have like characters that you're unlocking or something and you want to save which ones were there so what I'm gonna do here is I'm just gonna simply open up the folder one which I have the game in you can see I already have a text file that I've called scores text if you don't know how to create a new text file it's really easy to just go new and then find text file here text document and go ahead and there you go you can name it and you don't even need to put anything in it so just name your scores don't text like that all right I'm just gonna delete this one because I don't need any more now again it's really important to make sure that the scores dot text is in the same directory as your python file so you can see I've got two files here this is the one I'm working from and scores is in the same directory it's not inside images is not inside another folder it's right beside the starter file so make sure that's like that hey guys sorry for the interruption here so while I was editing this video I realized I made a really quick mistake so I just wanted to fix that really quickly for you guys so all you need to do to fix this mistake and make your program run properly i's going to scores here and just type 0 and then save it that's all its gonna make it work it's because we're gonna run into an error otherwise I just make sure you go in scores the text file you made type 0 type a number control ctrl s save it and there you go so let's get back with the video and then we're gonna Inc just include a little function that I created here I'm just gonna copy and paste in to save a bit of time as this videos already gone fairly long and you guys can just copy it it's really simple and I'll go through how it works so I'm gonna create another function here it's gonna say update file now what this one does is we use f to open up and save our file as scores text this mode here is our which means we're reading in the file our file now so the content of our file is equal to f dot read lines the last score is just going to be the only line in the file because we're only using that one line so we say file 0 that's the first line because this just puts it into a list for us and then we say if last which is going to be the score that we use here is less than the integer of our score then we say F dot close we're gonna close this file because we're actually gonna reopen the same file except we're gonna clear the contents by putting a W here and we're gonna rewrite our score in it like that so that if we get a higher score we're rewriting it if it's a lower score it's gonna stay the same and then if this is true we're gonna return what our current current score was otherwise we're returning the last score that was in the text file which is gonna be higher than what we just got okay so we have this update file just copy this down and then in our end screen here where we have previous score we're just gonna say plus string of update five because not only this update file will update our file it also returns the value of our score that we need to be putting in the text file okay there we are now let's go ahead and run the program and this should be everything that we need again the hitboxes if you feel that the game is too difficult that you're running into objects just go ahead and shrink those down a little bit personally I might make the size of the saw hitbox a little bit smaller so you see those little corners that popping up where the saw actually isn't we don't end up running into those cuz you can see the jump is fairly difficult to do on its own as we continue running our score goes up oh no we fell our score is nine score nine previous score nine there we go because that's the best score so yeah you can see we ran into a glitch here we're just continued to fall that's because I needed to actually fix one thing good job we didn't wrap put the video quite yet once we get in this while loop here we're just gonna underneath here and go runner dog falling equals false and that should hopefully fix things let's make sure so if we jump up and hit the saw again like I said we're always running into bugs here and I'm sure you guys are probably gonna find a lot more if you have any questions about them again just leave them in the comments down below let's jump in the air we run into it and let's see what happens now okay we start you can see there we are and we followed that back down that's just the way that it works there if you want them to again start on the bottom then you guys can figure that out we run into the soil let's see there we go we fall in and as we start back up we fall back down okay very good so that's been it for this video of the fourth and final video and my such roller series if you guys enjoyed the series please help me out by leaving a like on the video and subscribing to the channel if there's any other videos or games that you'd like to learn how to make don't hesitate to leave a comment down below as I'm always looking for ideas for next videos [Music]
Original Description
Hey guys and welcome to the last video in my side-scrolling game series. In this video I will be showing you how to implement a end screen and save your scores using a text file.
CODE: https://techwithtim.net/tutorials/game-development-with-python/side-scroller-pygame/scoring-end-screen/
How to install pygame: https://www.youtube.com/watch?v=AdUZArA-kZw
Twitter: https://twitter.com/TechWithTimm
Please leave a LIKE and SUBSCRIBE for more content!
Tags:
- Tech With Tim
- Python
- Pygame
- Side scroller pygame
- Pygame tutorial
- Pygame tutorial python 3
- Pygame tutorial for beginners
- pygame side scroller
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Tech With Tim · Tech With Tim · 56 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
▶
57
58
59
60
A* Path Finding Algorithm(Visualization)
Tech With Tim
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
Python Programming Tutorial #3 - Conditions
Tech With Tim
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
Python Programming Tutorial #6 - For Loops
Tech With Tim
Python Programming Tutorial #7 - While Loops
Tech With Tim
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
Python Programming Tutorial #10 - String Methods
Tech With Tim
How to Overclock a NVIDIA GPU
Tech With Tim
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
Python Programming Tutorial #12 - Functions
Tech With Tim
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
Cool VBS Script to Prank Your Friends!
Tech With Tim
How to Overclock an AMD GPU
Tech With Tim
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
Recursion and Memoization Tutorial Python
Tech With Tim
Ethereum Mining Rig - Hardware Guide
Tech With Tim
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
How to Install Pygame (Windows 8/10)
Tech With Tim
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
Linear Search Algorithm - Python Example and Code
Tech With Tim
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
Binary Search Algorithm - Python Example & Code
Tech With Tim
Pygame Tutorial #5 - Projectiles
Tech With Tim
Pygame Game - Mini Golf
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
Pygame Tutorial #6 - Enemies
Tech With Tim
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
How to Create A Message Box in Python - Tkinter
Tech With Tim
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim
More on: Tool Use & Function Calling
View skill →Related Reads
📰
📰
📰
📰
Why AI Makes Legacy Modernization More Feasible But Not More Automatic
Forbes Innovation
AI Stack Online Training | New Batch Starts Soon!
Medium · Python
I Started Tracking How Much Time I Spent Correcting AI Output. The Number Changed Everything.
Dev.to · Avery
The Top 5 AI Tools You Can Use Every Day to Save Hours
Medium · ChatGPT
🎓
Tutor Explanation
DeepCamp AI