1
00:00:00,420 --> 00:00:04,110
First, let's start by improving our snake game,

2
00:00:04,650 --> 00:00:09,150
getting it to be able to keep track of the high score that we attain each time

3
00:00:09,150 --> 00:00:14,150
we play the game so that we can see what our high score is and keep striving to

4
00:00:14,310 --> 00:00:15,150
better ourselves.

5
00:00:16,200 --> 00:00:21,180
Open up the snake game in PyCharm that you created a few days back. Now,

6
00:00:21,180 --> 00:00:23,430
if you can't find your snake game project,

7
00:00:23,820 --> 00:00:28,170
or if you've made some significant changes to it where it might be confusing to

8
00:00:28,170 --> 00:00:32,490
follow along with the tutorial, then simply just head over to this URL

9
00:00:32,670 --> 00:00:34,770
which is in the course resources

10
00:00:35,220 --> 00:00:39,980
and then you can go ahead and hit download as a zip, unzip

11
00:00:39,980 --> 00:00:43,470
the downloaded file, and then open it inside PyCharm

12
00:00:43,860 --> 00:00:47,190
and you should end up with the same project as I have here.

13
00:00:47,940 --> 00:00:49,260
And once you've done that,

14
00:00:49,410 --> 00:00:53,880
then we can begin to start adding the functionality to keep track of the high

15
00:00:53,880 --> 00:00:57,060
score. Firstly, I'm going to add

16
00:00:57,060 --> 00:01:01,410
another attribute to my scoreboard, which is going to be called high_score.

17
00:01:01,920 --> 00:01:06,060
And again, it's going to start out as zero. Now,

18
00:01:06,090 --> 00:01:11,090
the next thing I'm going to do is I'm going to replace this game over method

19
00:01:11,910 --> 00:01:16,680
with a new method. We're not going to stop the game and write game over anymore.

20
00:01:17,130 --> 00:01:19,770
Instead, we're going to reset the scoreboard.

21
00:01:20,430 --> 00:01:23,730
So what happens when we reset the scoreboard? Well,

22
00:01:23,730 --> 00:01:28,730
we need to figure out if the current score that the user has attained is greater

23
00:01:29,640 --> 00:01:34,170
than the high score of all time. And if that is the case,

24
00:01:34,260 --> 00:01:38,850
then we're going to update the high score with the value of the current score.

25
00:01:39,420 --> 00:01:42,150
So the code for that would look something like this.

26
00:01:42,540 --> 00:01:47,250
If self.score is greater than self.high_score,

27
00:01:47,670 --> 00:01:52,670
well in that case then self.high_score is going to be equal to self.score.

28
00:01:53,910 --> 00:01:56,400
Now, this looks a little bit confusing at first,

29
00:01:56,730 --> 00:02:00,780
but if you just go through the logic and think about how you would create a high

30
00:02:00,780 --> 00:02:02,250
score saving mechanism,

31
00:02:02,700 --> 00:02:07,290
then you'll pretty quickly understand what's going on here. Now,

32
00:02:07,290 --> 00:02:12,270
once we've updated the high score, the next thing to do is to reset the score.

33
00:02:12,690 --> 00:02:17,340
So I'm going to reset it down to zero. Now, remember the order of your code

34
00:02:17,340 --> 00:02:21,780
matters a huge deal because if you first set the score to zero,

35
00:02:22,050 --> 00:02:25,350
then it's never going to be greater than the current high score.

36
00:02:25,560 --> 00:02:27,450
So this will never get triggered.

37
00:02:27,870 --> 00:02:32,550
So as always, be careful where you're writing your code. Now,

38
00:02:32,640 --> 00:02:36,990
after we've set the score, then we need to update the scoreboard.

39
00:02:37,740 --> 00:02:38,010
Now,

40
00:02:38,010 --> 00:02:42,870
one thing that I've changed about the scoreboard is previously when we increased

41
00:02:42,930 --> 00:02:43,950
the score,

42
00:02:44,340 --> 00:02:48,780
I've used the self.clear and then self.update_scoreboard.

43
00:02:49,230 --> 00:02:52,350
But it actually makes more sense for this clear to happen

44
00:02:52,380 --> 00:02:54,780
every single time we update the scoreboard.

45
00:02:55,230 --> 00:02:59,010
So I've added this method call here before we actually write the score.

46
00:03:00,160 --> 00:03:04,330
The reason why I couldn't do this previously is because when we wanted to show

47
00:03:04,330 --> 00:03:05,200
game over,

48
00:03:05,620 --> 00:03:10,000
we wanted to keep the score on the screen and then for

49
00:03:10,000 --> 00:03:15,000
the score board turtle to go to the center and write game over so that both of

50
00:03:15,280 --> 00:03:17,080
these things show up on screen.

51
00:03:17,740 --> 00:03:20,680
But now that we're getting rid of our game over method,

52
00:03:21,130 --> 00:03:26,130
then we can go ahead and move that clear to update scoreboard

53
00:03:26,590 --> 00:03:30,400
and then we don't have to call it twice, both in reset and increase_score.

54
00:03:31,390 --> 00:03:34,600
Now, in addition to writing the score,

55
00:03:34,900 --> 00:03:37,330
I'm now also going to write the high score,

56
00:03:37,960 --> 00:03:42,490
and this is going to be using an f-string to insert self.high_score.

57
00:03:43,420 --> 00:03:45,910
Now, if we run this game as it is,

58
00:03:45,970 --> 00:03:48,580
you can see it says score 0, high score

59
00:03:48,580 --> 00:03:51,430
0 using these initial values.

60
00:03:52,180 --> 00:03:54,550
But as soon as it hits the wall,

61
00:03:54,580 --> 00:03:57,430
it ends because in our main.py

62
00:03:57,760 --> 00:04:02,760
we change this game_is_on to false and that ends our while loop.

63
00:04:04,060 --> 00:04:04,840
Instead,

64
00:04:04,840 --> 00:04:09,840
what I'm going to do is I'm going to delete this game_is_on equals false and

65
00:04:10,660 --> 00:04:13,390
also delete it when we collide with the tail.

66
00:04:14,020 --> 00:04:18,250
And I'm also going to delete the part where we call game over from the score

67
00:04:18,250 --> 00:04:23,020
board. Instead, I'm going to get the scoreboard to reset itself.

68
00:04:24,490 --> 00:04:29,490
So that way the score gets reset to zero and we're ready with the next round.

69
00:04:30,730 --> 00:04:35,080
So now if I run this game again, you can see it says, score is 0,

70
00:04:35,380 --> 00:04:40,000
and then as soon as we get a higher score and we end up dying, like

71
00:04:40,000 --> 00:04:43,930
so, then our high score goes to 2.

72
00:04:44,560 --> 00:04:49,560
But our snake kind of just disappears because it continues moving in the same

73
00:04:50,560 --> 00:04:54,850
direction and it's now probably somewhere, all the way down to the left.

74
00:04:55,420 --> 00:05:00,100
What we do instead? Well, our snake also needs to be reset.

75
00:05:00,610 --> 00:05:04,990
So let's create a reset method inside the snake class as well.

76
00:05:05,680 --> 00:05:10,680
And all we're going to do is we're going to get all of the segments to clear.

77
00:05:11,770 --> 00:05:13,510
And when I hover over this clear,

78
00:05:13,510 --> 00:05:18,510
you can see what it does is it's going to remove all of the items from that

79
00:05:18,760 --> 00:05:19,360
list.

80
00:05:19,360 --> 00:05:24,360
So all of the segments that we've added already to the list of segments is

81
00:05:24,490 --> 00:05:28,780
going to be deleted. And then once we've gotten rid of all the segments,

82
00:05:28,810 --> 00:05:31,540
we're going to call self.create_snake again,

83
00:05:31,810 --> 00:05:36,490
so that we create another three-segment snake in the starting position.

84
00:05:37,660 --> 00:05:38,650
Now, in addition,

85
00:05:38,680 --> 00:05:43,680
I'm also going to set the self.head as the zeroth element from that list

86
00:05:44,920 --> 00:05:45,753
of segments.

87
00:05:46,150 --> 00:05:50,620
So basically we're doing everything that is in the init because we're going to

88
00:05:50,620 --> 00:05:53,620
initialize the snake again, back at the center.

89
00:05:54,220 --> 00:05:59,120
So now if I go back to main.py, and in addition to resetting the scoreboard,

90
00:05:59,450 --> 00:06:02,000
I also get my snake to reset,

91
00:06:02,770 --> 00:06:03,603
Right?

92
00:06:06,160 --> 00:06:07,930
At this point, when I run the code,

93
00:06:07,990 --> 00:06:12,990
you'll notice that it's not behaving quite the way you expect it to. So I can go

94
00:06:13,990 --> 00:06:17,830
ahead and score, and when I die, I get the high score,

95
00:06:18,250 --> 00:06:22,060
but my old snake is still lingering on screen.

96
00:06:22,480 --> 00:06:25,480
So even though we've cleared the segments,

97
00:06:25,690 --> 00:06:30,400
we don't actually get rid of the turtles that are still onscreen.

98
00:06:31,030 --> 00:06:32,710
And in order to get rid of them,

99
00:06:32,740 --> 00:06:37,740
we actually have to send them to a different location. In our snake reset method,

100
00:06:40,000 --> 00:06:42,610
just before we clear our segments,

101
00:06:42,940 --> 00:06:45,340
so while we still have access to all of them,

102
00:06:45,850 --> 00:06:50,850
we're going to use a for loop to loop through all of the segments in the list of

103
00:06:51,100 --> 00:06:56,100
segments and tell each of them to go to a place that is off the screen.

104
00:06:57,940 --> 00:07:01,420
So our screen is only 600 by 600

105
00:07:01,840 --> 00:07:05,590
and I'm going to tell it to go to 1000 X, 1000 Y.

106
00:07:05,950 --> 00:07:10,420
So it's gonna disappear off the screen. So this way,

107
00:07:10,450 --> 00:07:11,283
notice how

108
00:07:11,470 --> 00:07:16,470
when the snake hits one of the walls or when it hits its tail,

109
00:07:16,930 --> 00:07:18,940
instead of sort of just staying there,

110
00:07:18,970 --> 00:07:23,230
it actually disappears into a location that's off the screen.

111
00:07:23,890 --> 00:07:26,890
So that way we can keep track of our high scores,

112
00:07:27,190 --> 00:07:29,680
we can continue to get higher scores,

113
00:07:30,100 --> 00:07:34,000
and if we get a score that's not quite as high as the high score

114
00:07:34,480 --> 00:07:39,220
and we end up dying, then the high score stays as the previous value.

115
00:07:40,180 --> 00:07:44,560
Now, the only thing about this game is that in order to end it,

116
00:07:44,710 --> 00:07:49,570
we have to hit the stop button here. But once we've done that,

117
00:07:49,750 --> 00:07:53,140
it also reveals the crucial flaw in our code.

118
00:07:53,500 --> 00:07:57,580
Because if I run our program again, you can see that, wait,

119
00:07:57,610 --> 00:07:59,410
what happened to the high score, right?

120
00:07:59,620 --> 00:08:03,910
We had a high score of 3 previously. So, huh?

121
00:08:04,300 --> 00:08:08,470
What exactly is happening? Well, the reason is actually quite simple.

122
00:08:09,010 --> 00:08:13,450
We know that if we create a variable, let's say we create a variable called a

123
00:08:13,480 --> 00:08:15,070
set it to equal three,

124
00:08:15,550 --> 00:08:20,320
and then at some point we set a to be from the input.

125
00:08:20,710 --> 00:08:22,540
So now when I run this code,

126
00:08:22,570 --> 00:08:25,780
you can see that initially a is equal to three,

127
00:08:26,200 --> 00:08:31,030
but then the next line I asked the user, 'what do you want a to equals?'

128
00:08:31,030 --> 00:08:34,419
I'm going to make it equal to 12. Now at this point,

129
00:08:34,419 --> 00:08:38,500
if I go ahead and access this variable a inside the console

130
00:08:38,530 --> 00:08:42,880
just by typing a and then hitting enter, you can see it's equal to 12.

131
00:08:43,480 --> 00:08:46,630
Now, similarly, I can add some print statements here.

132
00:08:46,660 --> 00:08:48,370
I can print the value of a

133
00:08:48,700 --> 00:08:53,350
and then also print the value of a after we modify it. So again,

134
00:08:53,530 --> 00:08:57,810
you can see initially it's equal to 3, but then I decided to change it to 12.

135
00:08:58,020 --> 00:08:59,460
So now it's equal to 12.

136
00:09:00,300 --> 00:09:05,300
Now what happens if I rerun the code. A is equal to three

137
00:09:05,880 --> 00:09:06,300
aain.

138
00:09:06,300 --> 00:09:11,300
Its reset itself because the code is being rerun and the previous state is not

139
00:09:13,680 --> 00:09:18,450
being remembered. In our case, we've got this high score variable

140
00:09:18,840 --> 00:09:21,570
which initially starts out as zero.

141
00:09:22,170 --> 00:09:26,580
And at some point during the game as we progress,

142
00:09:26,820 --> 00:09:31,820
we modify this high score variable to equal a new value,

143
00:09:31,920 --> 00:09:32,850
let's say three.

144
00:09:33,480 --> 00:09:37,140
So now if I go ahead and print my high score,

145
00:09:37,620 --> 00:09:39,720
you can see that it's equal to 3.

146
00:09:40,290 --> 00:09:44,130
But as soon as I rerun the code and this goes through,

147
00:09:44,430 --> 00:09:46,980
then all of that progress gets reset.

148
00:09:47,280 --> 00:09:51,900
And now if I print the value of high score, you can see it's again,

149
00:09:51,990 --> 00:09:56,340
equal to zero. So this is what's happening in our program as well.

150
00:09:57,180 --> 00:10:02,180
So how might you keep track of the score if you weren't using Python?

151
00:10:03,150 --> 00:10:03,450
Well,

152
00:10:03,450 --> 00:10:08,400
you might open up a word document and then write down your high score in that

153
00:10:08,400 --> 00:10:11,970
document, close it down, and then the next time you play the game,

154
00:10:12,330 --> 00:10:16,170
open up that file again and see what your previous high score is.

155
00:10:16,710 --> 00:10:18,570
We're going to try and do the same thing,

156
00:10:18,870 --> 00:10:23,870
but using Python to read and write to a file so that we can save our progress

157
00:10:25,800 --> 00:10:29,280
and be able to retrieve our previous high scores

158
00:10:29,370 --> 00:10:34,370
each time we play the snake game. To find out how to do that and to solve this

159
00:10:35,100 --> 00:10:37,470
problem, head over to the next lesson

160
00:10:37,800 --> 00:10:42,270
and we'll talk about how to use Python to open, read, write,

161
00:10:42,360 --> 00:10:44,250
and close files on your system.

