Python Pygame Tutorial - Episode 6! Creating Restart Code and Previous Run and High Score Tracking!

LeMaster Tech
181 followers
14 Video Views·Apr 23, 2025
Learn to develop video games using python and pygame!! This is episode 6 in an intro to Pygame series from LeMaster Tech. This episode covers creating restart logic after game over scenarios and tracking code for high score and previous score tracking!
Be sure to check out our channel for tons of additional python tutorials and practice problems and be sure to like and subscribe if you find our content useful!!
https://www.youtube.com/channel/UCV5Ab39YnXvTZ6Grar9URxQ
If you want to see anything specific on the channel or have technical questions about anything leave a comment on our of our videos and we will get back as soon as we can!
Thanks for watching and supporting the channel!
hello and welcome back to the channel
uh today's video will be episode six in
our intro to pie game series uh with
python
and um if you haven't seen the videos
leading up to this video we will be
starting with the code we've created so
far
so if you'd like to follow along then i
recommend going back and checking those
out otherwise if you're just here for
the concepts covered in this video
then
you know feel free to ask any questions
in the comments but understand that
there's a lot of code already existing
today
so a quick recap of what we've already
built we have a bouncing ball that
you try to avoid as the player in the
orange rectangle
and we have game over conditions where
the player and the ball stop moving when
they collide but what we haven't done
yet is handled restart conditions or any
kind of like high score
tracking or keeping track of the history
so we're going to take a look at doing
some of that today and without further
ado let's get into it
so first thing we're going to do is
we're going to create a bit to actually
track whether or not game over has
occurred
and so we're going to create a boolean
variable
game over and obviously to initialize it
up at the beginning of the game we're
going to say it's false
and then once we have collision which is
when game over runs we're going to go
ahead and say
this is when game over
becomes true
and this is actually gonna become pretty
relevant because we're going to add um
some controls that can restart the game
but only if game over is true
so this does mean we're going to
want to pass in
the global
game over variable
so that way this is referring to the
uh overarching one
and then we're going to add some
instruction that's only up when game
over is being called that tells them to
uh how to restart the game
and so
i'm going to make this smaller it
doesn't need to be the same huge sizes
doesn't need to be the same huge size as
game over and i'm going to call this
display
restart
and what we're going to do with this is
we're going to tell them
hey
press
let's say space
to
restart
and we'll go ahead and make this white
and we're gonna throw this down
um we're gonna go ahead and throw this
down at like
170 because we're going to be much
smaller font
but then a little further back because
it's longer text
okay
so or a little further down sorry
and then uh press space to restart
display restart so um what we're going
to do though since that text is only
coming up
when game over is true we need to
actually create the logic so that when
you press space
um in in this when you release space
actually that's when uh game over
conditions reset but only if game over
is true
so we're going to go ahead and put that
code in now and we're going to say
if event dot key
equals
pygame dot
space and i'm putting this in
k space um i'm putting this in the key
up condition because to me it makes a
little more sense
that the game would restart once you've
released the space bar not necessarily
when you've pushed it down um
it would be valid to do it the other way
if you want this is how i'm choosing to
do it and the other thing we want to do
is actually say and
game over so we actually want to check
that the game over variable is also true
and then
so in here we need to put all of the
code in place to reset the game
so does that mean we're going to move
circle x and circle y back to their
initial positions and we started that
guy at the
middle of the screen so 300 300
if you remember in game over what we did
is we stopped the direction variables
for everybody so you weren't able to
move we need to go ahead and
reinitialize those back to what we gave
them which was four and five
and then we're going to put our player
back where he was as well so
300 it could be a girl it's just a
rectangle but
so player x 300 player y 500 and then um
let's go ahead and reset the score back
to zero
and let's
make the game over variable false again
so this really should act as a full
restart let's go ahead and see
how we look if we've got that done so
let's try to get hit here
oh what have i done
153
check collisions
now that should all be okay
46
oh
okay we're gonna have to name our game
over variable something else we've
already used game over with a space
underscore
we'll get rid of the underscore that
should solve our problems
so tried to create a
try to create a variable oops
that one needs to
try to create a variable with the same
name as a function that's going to give
you all kinds of issues so
that should take care of that let's go
ahead and run that again take a look
here
let's go get hit
okay now i'm gonna try pressing space to
restart we see we've got score four the
position's out of whack
and
well i'm pressing space and i'm not
getting anything so
let's get into the troubleshooting so
game over starts out false
check for collision collision game over
ah
forgot one underscore
there we go
that can be frustrating all right here
we go all right train space there we go
so the player resets it doesn't put you
in an impossible position to get out of
the ball resets your score resets so
that's pretty cool
one more thing i'd like to add in this
video is let's actually keep track of
the previous score and your high score
for this play through because as of now
you press space and it's really like
restarting there's no way of knowing if
you did better if you did worse if if
you're getting any better
so
let's go ahead and do a little bit of
this tracking this is actually pretty
easy
we're just going to go down to
the game over stuff
and we will go ahead and say
actually let's go up into our variables
and define a couple new ones so we're
gonna right where we put score
here we go
let's add two new variables that we're
going to use for this and we'll say
previous score and set equal zero and
let's make a high score
and set equals zero okay and that's just
so
after um the game resets we're going to
do a few things so
no matter what in game over we're going
to go ahead and before we change the
score to zero
we're gonna say previous score equals
score
and then what we'll say in here is if
score is greater than high score
then let's make our high score equal to
score and then we clear out the score so
what we just did is we made the last run
equal to
whatever when we reset
whatever we just scored and then
if our score is greater than the highest
score we've had then it'll overwrite it
but only if it's a new high so now we
just have to copy our display score
um
code and make a few new ones for display
let's say
previous score
and display
high score we'll we'll call it and uh
i'm gonna update that in both places
and let's move them down the screen by
20 pixels so they should be pretty
tightly packed in the top left corner um
display high score
and
now let's change the text to say
high score and make that a string
of our variable high score and then this
string will say last score previous is
kind of a long word
and we will say previous score for the
variable
and i did that kind of quickly i don't
want this to chew up too much time but
let's see now if we have a previous
score and a high score tracker
okay so we're counting two three four
all right now when i press space both
last score and high score should update
to five and score should go to zero all
right so let's try getting something
that's not a high score we got three so
last score should update to three but
the five should maintain
and it does so that's pretty cool
that's how you handle restart conditions
you know clearing out all of your
variables and resetting them to starting
positions
as well as keeping track of a last score
and a high score and things like that
so still to come we'll be doing some
difficulty increases because for now
you're just gonna have kind of a slow
moving ball forever and obviously this
is kind of a boring game it's not too
challenging
you could probably pretty easily rack up
a high score but you'd be real bored
with it real fast
so in the future videos we'll work on
increasing difficulty
as well as potentially taking a look at
like some player boosts and speed
upgrades things like that
uh so for now hope you enjoyed this
video found it useful and kind of fun
and if you have any questions or things
you'd like to see in the future feel
free to let me know in the comments
below and you found this or any of the
content on the channel useful i really
appreciate uh liking a subscribe helps
channel out a lot
and as always thanks for watching and
good luck with your code thanks bye
