Hey guys welcome to another Visual Basic games programming tutorial. This is a
request made by one of our subscribers wanting to see a high score system
implemented as part of the game that I made a while ago
magic monk adventures so just to recap you guys on what I have made so far in
that game was so basically this is the main title screen and then you can click
start game and then you move your character around collecting the blue
gems dodging the red bugs and if you die three times so one two three it tells
you that the game is over and you can see a high score of five and then if you
play again and you beat that high score
so you beat that high score let's get to six seven ok so you if you die three
times then the high score turns into seven but the problem with that is if
you close down the game and you open up the game again you open up the game
again it resets the high score so the high score is not there anymore so what
we want to do is find a way so that we can
store the high score in a text file and then it's going to load up that
text file every time we play the game so that we can see what the previous high
score was so here are the steps required to get this high score system
into our game the first step is if your score beats the high score at the end of
the game then it's going to ask you to input your name into the
program and what we're going to do with that is store your name as well as the
score into the text file which is step two so we're going to store your high
score and your player name to a text file and then step three at the
beginning of the game we're going to load your high score and your name from
the text file now this will only occur saving your score to the to the file
if your score beats the previous high score all right
so otherwise it won't won't do it'll just have the previous high score are
saved in that file it won't modify that file any further ok so let's try to get
this to work now let's bring up Visual Basic and the first thing we're going to
do is declare a variable called player name and we're going to declare as a
string variable with nothing in it an empty string okay and then we're going
to also have in our form a label a label to display this to display this variable
so I'm going to create another label put it on the screen and instead of label
one I'm going to call it name label so I'm going to call this you know where
is the name property instead of label one I'm going to call this name label
and I'm going to change the font size to 14 so font size change that to 14 so
it's a bit bigger line it up with the other labels and the text the intro text
is going to be player name so player name so I should say best player right
because this is the best player so far okay so when you load up the game
actually I'm going to make it save the ask you to input your name after
at the end of the game so go back to form one go into the coding and go down
to the Timer1_Tick Event Handler so scroll all the way down to where it says
timer one tick and then go down to where it says go down to where it says if
lives equals zero so that means the game has ended right you're going to display
the message box game over and if your score is bigger than the high score we
also want your name so player name is equal to and we're going to get this via
an input box and it's just going to ask them to enter your name so we have their
name in the player name variable and then we're going to display it in our
label so the label is going to say best player
and it's going to also display the player name at the end of that string
now as you know my theory is do a little bit at a time make sure that it works so
let's click start debugging and we just want to see that after I died three
times it's going to ask me for my name so one two three game over ask for your
name so I'm going to say Ed and it says best player high score is four so if you
play this game again oops and you don't beat four let's say I only
get one or two and I die again and nothing happens but if I beat that high
score so three four five and I die now
game over then it's asking me for my name with my high score okay so that's
step one completed step 2 of this tutorial what we want to do now is store
the high score and the player name into the text file and we're only going to do
this after the game has ended and and that the player has scored a higher
score than the current high score so what we do is we declare a variable
called file num as integer and this is underneath the timer one tick event
handler and we scroll down to where it says if score is bigger than high score
and previously we've already asked for the player name so what we're
going to do is we're going to open the file file open a file num we're going
to open the file called score.txt and the open mode is going to be output
because we're saving into the file now notice how filenum hasn't been defined
yet so it's an integer but we don't know what number it is so we need to use the
free file function and that'll tell us what is the next file number available
which is going to be one because we don't have any other files open so and
then we're going to print line so we're going to print to the score file with
our player name and we're going to print to the file our high score and then
we're going to close the file so that's all pretty simple and let's just see if
it works so we're gonna play the game I'm gonna get a couple of gems die okay
game over ask me for my name John and my high score is three so I'm now going to
browse to that folder of my game which is located in documents Visual Studio
2010 projects my first game1 my first game1 being debug and you can see
that I have score.txt so I'm going to open this up and it has John three which
is the high score currently so we have successfully saved the high score to the
file step three of this tutorial we want the program to load up the name of the
player and the previous high score from the score.txt file at the beginning
of the game alright so we know what our goal is to accomplish when we're playing
what score we have to be ok so we're going to go up to the beginning of the
program where Form1_Loads what we're going to do so I'm going to
firstly declare file num again remember to do this every time you need to access
access the file and I'm going to use the free file function to tell us the file
number that is available and then I'm going to open up the file with that file
number and it's going to be score.txt that's the name of the file the open
mode is going to be input all right because we're only reading the file and
we're gonna store the name of the player which is going to be
the first line from the file into the player name variable and we're going to
score the store the high score in the
high score variable oops spelt this wrong
lucky there's a smile lucky there's a spellchecker thing so I have stored the
player name inside this variable the high score inside this variable and I'm
going to make the label display the player name from the file and I'm going
to make the high score label display ah
high score the high score from the file okay so if I play this game now
what's going to happen is if I start the game you can see that it loads up the
high score three from the previous game so if I get five and I die and I call
myself something else Ed
oops so make sure you jump out of that there was a mistake that I made earlier
which is at the beginning when I was programming it to load up the player
name I forgot one last line and that was we have to close the file file close
filenum and without that the file is still going to be open which won't allow
us to modify it later on in the game so we're going to try it again so we're
going to play it start game it's got John three so I'm
going to get five and I'm going to die three times okay game over I'm going to
call it a Eddie and then I've got Eddie five so if I
close this game down and I run it again it comes up with five Eddie in the
beginning all right and if you check if you check the text file all right go
into the folder here and then check the text file it says Eddie 5 as well okay thanks
for watching guys see you again next time.