WEBVTT

0
00:00.420 --> 00:04.110
First, let's start by improving our snake game,

1
00:04.650 --> 00:09.150
getting it to be able to keep track of the high score that we attain each time

2
00:09.150 --> 00:14.150
we play the game so that we can see what our high score is and keep striving to

3
00:14.310 --> 00:15.150
better ourselves.

4
00:16.200 --> 00:21.180
Open up the snake game in PyCharm that you created a few days back. Now,

5
00:21.180 --> 00:23.430
if you can't find your snake game project,

6
00:23.820 --> 00:28.170
or if you've made some significant changes to it where it might be confusing to

7
00:28.170 --> 00:32.490
follow along with the tutorial, then simply just head over to this URL

8
00:32.670 --> 00:34.770
which is in the course resources

9
00:35.220 --> 00:39.980
and then you can go ahead and hit download as a zip, unzip

10
00:39.980 --> 00:43.470
the downloaded file, and then open it inside PyCharm

11
00:43.860 --> 00:47.190
and you should end up with the same project as I have here.

12
00:47.940 --> 00:49.260
And once you've done that,

13
00:49.410 --> 00:53.880
then we can begin to start adding the functionality to keep track of the high

14
00:53.880 --> 00:57.060
score. Firstly, I'm going to add

15
00:57.060 --> 01:01.410
another attribute to my scoreboard, which is going to be called high_score.

16
01:01.920 --> 01:06.060
And again, it's going to start out as zero. Now,

17
01:06.090 --> 01:11.090
the next thing I'm going to do is I'm going to replace this game over method

18
01:11.910 --> 01:16.680
with a new method. We're not going to stop the game and write game over anymore.

19
01:17.130 --> 01:19.770
Instead, we're going to reset the scoreboard.

20
01:20.430 --> 01:23.730
So what happens when we reset the scoreboard? Well,

21
01:23.730 --> 01:28.730
we need to figure out if the current score that the user has attained is greater

22
01:29.640 --> 01:34.170
than the high score of all time. And if that is the case,

23
01:34.260 --> 01:38.850
then we're going to update the high score with the value of the current score.

24
01:39.420 --> 01:42.150
So the code for that would look something like this.

25
01:42.540 --> 01:47.250
If self.score is greater than self.high_score,

26
01:47.670 --> 01:52.670
well in that case then self.high_score is going to be equal to self.score.

27
01:53.910 --> 01:56.400
Now, this looks a little bit confusing at first,

28
01:56.730 --> 02:00.780
but if you just go through the logic and think about how you would create a high

29
02:00.780 --> 02:02.250
score saving mechanism,

30
02:02.700 --> 02:07.290
then you'll pretty quickly understand what's going on here. Now,

31
02:07.290 --> 02:12.270
once we've updated the high score, the next thing to do is to reset the score.

32
02:12.690 --> 02:17.340
So I'm going to reset it down to zero. Now, remember the order of your code

33
02:17.340 --> 02:21.780
matters a huge deal because if you first set the score to zero,

34
02:22.050 --> 02:25.350
then it's never going to be greater than the current high score.

35
02:25.560 --> 02:27.450
So this will never get triggered.

36
02:27.870 --> 02:32.550
So as always, be careful where you're writing your code. Now,

37
02:32.640 --> 02:36.990
after we've set the score, then we need to update the scoreboard.

38
02:37.740 --> 02:38.010
Now,

39
02:38.010 --> 02:42.870
one thing that I've changed about the scoreboard is previously when we increased

40
02:42.930 --> 02:43.950
the score,

41
02:44.340 --> 02:48.780
I've used the self.clear and then self.update_scoreboard.

42
02:49.230 --> 02:52.350
But it actually makes more sense for this clear to happen

43
02:52.380 --> 02:54.780
every single time we update the scoreboard.

44
02:55.230 --> 02:59.010
So I've added this method call here before we actually write the score.

45
03:00.160 --> 03:04.330
The reason why I couldn't do this previously is because when we wanted to show

46
03:04.330 --> 03:05.200
game over,

47
03:05.620 --> 03:10.000
we wanted to keep the score on the screen and then for

48
03:10.000 --> 03:15.000
the score board turtle to go to the center and write game over so that both of

49
03:15.280 --> 03:17.080
these things show up on screen.

50
03:17.740 --> 03:20.680
But now that we're getting rid of our game over method,

51
03:21.130 --> 03:26.130
then we can go ahead and move that clear to update scoreboard

52
03:26.590 --> 03:30.400
and then we don't have to call it twice, both in reset and increase_score.

53
03:31.390 --> 03:34.600
Now, in addition to writing the score,

54
03:34.900 --> 03:37.330
I'm now also going to write the high score,

55
03:37.960 --> 03:42.490
and this is going to be using an f-string to insert self.high_score.

56
03:43.420 --> 03:45.910
Now, if we run this game as it is,

57
03:45.970 --> 03:48.580
you can see it says score 0, high score

58
03:48.580 --> 03:51.430
0 using these initial values.

59
03:52.180 --> 03:54.550
But as soon as it hits the wall,

60
03:54.580 --> 03:57.430
it ends because in our main.py

61
03:57.760 --> 04:02.760
we change this game_is_on to false and that ends our while loop.

62
04:04.060 --> 04:04.840
Instead,

63
04:04.840 --> 04:09.840
what I'm going to do is I'm going to delete this game_is_on equals false and

64
04:10.660 --> 04:13.390
also delete it when we collide with the tail.

65
04:14.020 --> 04:18.250
And I'm also going to delete the part where we call game over from the score

66
04:18.250 --> 04:23.020
board. Instead, I'm going to get the scoreboard to reset itself.

67
04:24.490 --> 04:29.490
So that way the score gets reset to zero and we're ready with the next round.

68
04:30.730 --> 04:35.080
So now if I run this game again, you can see it says, score is 0,

69
04:35.380 --> 04:40.000
and then as soon as we get a higher score and we end up dying, like

70
04:40.000 --> 04:43.930
so, then our high score goes to 2.

71
04:44.560 --> 04:49.560
But our snake kind of just disappears because it continues moving in the same

72
04:50.560 --> 04:54.850
direction and it's now probably somewhere, all the way down to the left.

73
04:55.420 --> 05:00.100
What we do instead? Well, our snake also needs to be reset.

74
05:00.610 --> 05:04.990
So let's create a reset method inside the snake class as well.

75
05:05.680 --> 05:10.680
And all we're going to do is we're going to get all of the segments to clear.

76
05:11.770 --> 05:13.510
And when I hover over this clear,

77
05:13.510 --> 05:18.510
you can see what it does is it's going to remove all of the items from that

78
05:18.760 --> 05:19.360
list.

79
05:19.360 --> 05:24.360
So all of the segments that we've added already to the list of segments is

80
05:24.490 --> 05:28.780
going to be deleted. And then once we've gotten rid of all the segments,

81
05:28.810 --> 05:31.540
we're going to call self.create_snake again,

82
05:31.810 --> 05:36.490
so that we create another three-segment snake in the starting position.

83
05:37.660 --> 05:38.650
Now, in addition,

84
05:38.680 --> 05:43.680
I'm also going to set the self.head as the zeroth element from that list

85
05:44.920 --> 05:45.753
of segments.

86
05:46.150 --> 05:50.620
So basically we're doing everything that is in the init because we're going to

87
05:50.620 --> 05:53.620
initialize the snake again, back at the center.

88
05:54.220 --> 05:59.120
So now if I go back to main.py, and in addition to resetting the scoreboard,

89
05:59.450 --> 06:02.000
I also get my snake to reset,

90
06:02.770 --> 06:03.603
<v 1>Right?</v>

91
06:06.160 --> 06:07.930
<v 0>At this point, when I run the code,</v>

92
06:07.990 --> 06:12.990
you'll notice that it's not behaving quite the way you expect it to. So I can go

93
06:13.990 --> 06:17.830
ahead and score, and when I die, I get the high score,

94
06:18.250 --> 06:22.060
but my old snake is still lingering on screen.

95
06:22.480 --> 06:25.480
So even though we've cleared the segments,

96
06:25.690 --> 06:30.400
we don't actually get rid of the turtles that are still onscreen.

97
06:31.030 --> 06:32.710
And in order to get rid of them,

98
06:32.740 --> 06:37.740
we actually have to send them to a different location. In our snake reset method,

99
06:40.000 --> 06:42.610
just before we clear our segments,

100
06:42.940 --> 06:45.340
so while we still have access to all of them,

101
06:45.850 --> 06:50.850
we're going to use a for loop to loop through all of the segments in the list of

102
06:51.100 --> 06:56.100
segments and tell each of them to go to a place that is off the screen.

103
06:57.940 --> 07:01.420
So our screen is only 600 by 600

104
07:01.840 --> 07:05.590
and I'm going to tell it to go to 1000 X, 1000 Y.

105
07:05.950 --> 07:10.420
So it's gonna disappear off the screen. So this way,

106
07:10.450 --> 07:11.283
notice how

107
07:11.470 --> 07:16.470
when the snake hits one of the walls or when it hits its tail,

108
07:16.930 --> 07:18.940
instead of sort of just staying there,

109
07:18.970 --> 07:23.230
it actually disappears into a location that's off the screen.

110
07:23.890 --> 07:26.890
So that way we can keep track of our high scores,

111
07:27.190 --> 07:29.680
we can continue to get higher scores,

112
07:30.100 --> 07:34.000
and if we get a score that's not quite as high as the high score

113
07:34.480 --> 07:39.220
and we end up dying, then the high score stays as the previous value.

114
07:40.180 --> 07:44.560
Now, the only thing about this game is that in order to end it,

115
07:44.710 --> 07:49.570
we have to hit the stop button here. But once we've done that,

116
07:49.750 --> 07:53.140
it also reveals the crucial flaw in our code.

117
07:53.500 --> 07:57.580
Because if I run our program again, you can see that, wait,

118
07:57.610 --> 07:59.410
what happened to the high score, right?

119
07:59.620 --> 08:03.910
We had a high score of 3 previously. So, huh?

120
08:04.300 --> 08:08.470
What exactly is happening? Well, the reason is actually quite simple.

121
08:09.010 --> 08:13.450
We know that if we create a variable, let's say we create a variable called a

122
08:13.480 --> 08:15.070
set it to equal three,

123
08:15.550 --> 08:20.320
and then at some point we set a to be from the input.

124
08:20.710 --> 08:22.540
So now when I run this code,

125
08:22.570 --> 08:25.780
you can see that initially a is equal to three,

126
08:26.200 --> 08:31.030
but then the next line I asked the user, 'what do you want a to equals?'

127
08:31.030 --> 08:34.420
I'm going to make it equal to 12. Now at this point,

128
08:34.420 --> 08:38.500
if I go ahead and access this variable a inside the console

129
08:38.530 --> 08:42.880
just by typing a and then hitting enter, you can see it's equal to 12.

130
08:43.480 --> 08:46.630
Now, similarly, I can add some print statements here.

131
08:46.660 --> 08:48.370
I can print the value of a

132
08:48.700 --> 08:53.350
and then also print the value of a after we modify it. So again,

133
08:53.530 --> 08:57.810
you can see initially it's equal to 3, but then I decided to change it to 12.

134
08:58.020 --> 08:59.460
So now it's equal to 12.

135
09:00.300 --> 09:05.300
Now what happens if I rerun the code. A is equal to three

136
09:05.880 --> 09:06.300
aain.

137
09:06.300 --> 09:11.300
Its reset itself because the code is being rerun and the previous state is not

138
09:13.680 --> 09:18.450
being remembered. In our case, we've got this high score variable

139
09:18.840 --> 09:21.570
which initially starts out as zero.

140
09:22.170 --> 09:26.580
And at some point during the game as we progress,

141
09:26.820 --> 09:31.820
we modify this high score variable to equal a new value,

142
09:31.920 --> 09:32.850
let's say three.

143
09:33.480 --> 09:37.140
So now if I go ahead and print my high score,

144
09:37.620 --> 09:39.720
you can see that it's equal to 3.

145
09:40.290 --> 09:44.130
But as soon as I rerun the code and this goes through,

146
09:44.430 --> 09:46.980
then all of that progress gets reset.

147
09:47.280 --> 09:51.900
And now if I print the value of high score, you can see it's again,

148
09:51.990 --> 09:56.340
equal to zero. So this is what's happening in our program as well.

149
09:57.180 --> 10:02.180
So how might you keep track of the score if you weren't using Python?

150
10:03.150 --> 10:03.450
Well,

151
10:03.450 --> 10:08.400
you might open up a word document and then write down your high score in that

152
10:08.400 --> 10:11.970
document, close it down, and then the next time you play the game,

153
10:12.330 --> 10:16.170
open up that file again and see what your previous high score is.

154
10:16.710 --> 10:18.570
We're going to try and do the same thing,

155
10:18.870 --> 10:23.870
but using Python to read and write to a file so that we can save our progress

156
10:25.800 --> 10:29.280
and be able to retrieve our previous high scores

157
10:29.370 --> 10:34.370
each time we play the snake game. To find out how to do that and to solve this

158
10:35.100 --> 10:37.470
problem, head over to the next lesson

159
10:37.800 --> 10:42.270
and we'll talk about how to use Python to open, read, write,

160
10:42.360 --> 10:44.250
and close files on your system.