This video shows how to take critical data from your Python program, and save it into an external text file, so when you load the program next you have saved user data!
We begin with an example of how to save a high score while playing a game, then discuss the various additional applications this method could be applied to from gathering user data to performed advanced mathematical operations!
The game being demonstrated in this video was our custom built doodle jump which we have also provided a tutorial for previously on the channel!
If you have any questions about what you saw in this or any other video, or want to request something specific in the future, let me know in the comments below! And be sure to like the video and subscribe to the channel to stay up to date with all the great content coming out every week from LeMaster Tech!
Thanks for watching and good luck with your code!
hello welcome back to the channel and
today's video will be learning how to
save high scores to an external text
file and load them in at the beginning
of playing your game so that previous
runs high scores save in between
gameplay
[Music]
okay so the basic concept of this is um
a standard python game which i'm
assuming you came to this video because
you have a game you have a program that
you built and you want to save some data
at the end of a run and load it in the
next time you come into the game so this
isn't
specifically going to be a tutorial on
how to make this game in particular i
did do a tutorial on it this is a home
built doodle jumper and if you want to
learn how to build this game it's a good
one i'll put a link to it in the video
but the actual
flaw of this game is so here you get a
high score of 11
you could keep playing get real high
high scores but then if i close the
program if i were to close my code
editor and then reopen it every time i
restart the game it's gonna say high
score zero that's pretty frustrating
because obviously the high score it
should track you know however long
you've been playing this game whatever
your progressive high score is so today
we're going to take a look at how to do
that
and uh i've previously shown on this
channel how to read and write from
uh text files but in those videos that
was all i did in the video so it was
kind of like how do you incorporate this
into a greater program or into a
full-blown
game so let's go ahead and start taking
a look at that and let's begin with the
right logic because i think that that'll
be a little bit easier we'll program in
writing the high score to the file
and the first thing to note is drop an
empty text file into your game folder so
so into wherever you have your program
drop a text file give it a name that
will mean something to you i've called
mine player info because you could add
high score you could add like a currency
if there was a shop or something you
could add player name or user
credentials whatever you want into this
text file so just make sure you have a
text file mine's blank as you can see
but you're going to need that because
now what we're gonna do um i'm thinking
in order to not overburden the system
let's only save when the game ends so
instead of you know every time you're
jumping if you are getting a high score
well every new platform you get to is
going to be a new high score and the if
you put it in just any time that for
example anytime the high score updated
you also had it saving and overwriting
the file well technically that would be
true it would always capture whatever
the current high score is
but then it's going to be trying to save
every single increment of the score i
think when the game ends
that's when we'll save it um we'll check
to see if the high score uh was was high
enough and we'll save it at that point
and so to do that the first thing we're
going to do is we're going to need to
make a new variable but we'll be
checking to see if the high score
was greater than the last saved high
score so we'll come up a little bit
later and we'll make a saved high score
variable
but now here's the actual code for
accessing that file and putting data
into it
okay so it's going to be file equals
open
and then
the name of your text file
so python
especially like the good ides like
pycharm and there are others
they will auto populate with the names
of files so all you have to do is file
equals open
and then it'll search for any files in
your assets in your folder
so it's just file.open and then the name
of your file and then a comma and if
you're gonna write it's
you put a w in parentheses not
parentheses uh quotations
okay so
that's how you access the file and now
we have a variable called file that's
open and we can write data to it
so what are we going to do we're going
to do file.write
and now we're going to put what we want
to get written into that file and we
could just straight up write the score
that would work um but
on the off chance this becomes a file
where you want to store a lot of
character data like if you were in an
advanced game where you had you know
character attributes like strength or
stamina or anything like that you would
want to label each entry
to make it clear when you're accessing
that data at the start of the game that
you're getting the right info so we're
going to do high score colon
and then space just like that
and then we'll do plus and then uh to
concatenate strings you should know this
part you have to have
everything in there be a string so you
could either do an f string which is a
formatted string and put the variables
in curly brackets
or you can just do it this way with an
str which converts integer to string
but in any case this is going to write
one line of text to our text file
that will say high score
and then string of of the high score
actual variable okay wow that was hard
to get out
all right and then important thing file
dot close or it won't save okay
and then
we want to down here we actually want
the saved high score to update to the
high score
so this is how we keep track that we
just saved it
so that's what you do on the back end to
save it now let's kind of take a look at
what you would do in the beginning when
we're so we'll go up here to where score
and high score are initially called
zeros
and now let's take a look at it
um what we have to do right when the
game starts is it well it's
similar to start it's file equals open
and then
player info text just like before but
now you guessed it it's an r instead of
a w so we're reading from it
and what we're going to do in this one
is for line in file
it's also not a bad idea to give this a
different name
then you're giving the one that you
close at especially if there's a lot of
data
because when you do a write it's going
to
it's going to overwrite it's not just
going to update and like add another
line to it it's going to overwrite
what's in there so having all of this
info stored
and then when you do a write you write
all of this info just updated however it
got updated during the game that would
be a good way to do it
but okay so what we're going to check is
for line in the file so it's going to go
through every
line of your file because that's how
this open works by default it takes each
line of your text file and it puts it in
a list of like csv values where every
line is separated by a column
a comma
in the text format but then it comes in
with a backslash n in it
when you bring it in but
that's you can look into the videos i
did that are specifically just how to
read and write from text files
if you need more background info we're
going to focus on how to save the high
score for this one so
okay what we're checking is for line in
file so it's incrementing through every
line in that file and then we're going
to check if the sub string
high score
is in that line so
um
python makes that really easy it's just
if and then the string you're checking
for and then is in and then the string
you're checking against
this is checking to see if a substring
is in a string and that's super easy
okay so then what we're going to do
because remember this is at the very
beginning of the program so this code's
only running once it's not running every
loop of your game
well then we want the high score to be
and we need to use an integer converter
because remember on the back end we took
an integer number and we turned it into
a string so we could save it well now we
need to take that string value and we
need to pull it out as an integer
so we use int
and then what i'm going to do there's a
few ways you could do this but what i'm
going to do is i'm going to replace all
of the text that said hi space whoops
hi
and then spay
high score
and then colon and then space i'm going
to take all that text and then you do a
comma and then you say what you're going
to replace it with and i'm going to
replace it with nothing so essentially
that entire line of code and the reason
i did this is you don't know how many
characters your high score could be
so you certainly could check i mean you
could
write a program that's going to take
from the very end of that line and go
backwards until you hit the colon
and then pull that in as an integer i'm
going to do it this way where i just
take high score and i remove that text
and then i move it into my high score
variable
and then what i'm going to do
because this will take care of updating
it on the screen and everything but i am
going to make that saved high score
variable
saved high score equal to zero
and we need um before this thing runs we
need to make that variable just like we
have with every game variable so we have
to make it initially zero or whatever
value you want
on the off chance there is no high score
data yet like the first time you play
this game there won't be
and on the off chance something in the
formatting has gotten off in your text
file so let's go ahead and run it that
was a lot talking there was a lot of
code let's go ahead and see if it
actually works
so i'll just get like a high score of i
don't know four i'll use my air jumps
those are sweet yeah if you didn't check
out the game construction tutorial uh
this game is pretty fun um but my
microphone did goof up during it so i do
apologize it's not the most polished
video i've ever put out
but we did do like uh you have these
special super jumps if you hit space bar
and um color changing backgrounds stuff
like that so it's pretty fun but okay so
we got a high score 41 let's go ahead
and die if we can i'm just so good at
the game
all right i'm gonna hit space and you
see the high score keeps there but if i
close this program now i'm gonna go look
at the
so here we go that's pretty cool we have
high score 43 there was nothing there a
second ago the second big test is when
we run this and open it up so there we
go high score 43 is uh is saved in there
um that's pretty awesome because before
any time you would have closed
and reopened your program it would reset
to zero
so that's the basics of it guys i mean
you can expand the concepts that we just
did you can expand those out kind of
infinitely for additional player
information this is also how you would
actually
use user information if you made a
widget or a utility that was
doing a service for any kind of
apis or anything
there are ways to encrypt the data so
that's not so easy to modify because
obviously i could come in here it's just
a text file i could say i had some
ridiculously high score and i booted up
and you know the high scores off the
screen when in reality nobody's gonna
break a thousand on
my python doodle jump game
so uh there are ways to increase the
security enhance the security but for
basic purposes most people who play
games don't even know that most of their
player data is saved in local files and
so they would never go looking for it
anyways i hope you found this useful um
i if there is anything specific you want
to see on the channel in the future be
sure to let me know about in the
comments below if you did like this
content um be sure to subscribe to the
channel like the video to catch up on
all the future content go ahead and
check out the channel for tons of other
tutorials and as always good luck with
your code and thanks for watching thanks
bye