1
00:00:00,420 --> 00:00:02,580
Guys, we've already come really, really far.

2
00:00:03,090 --> 00:00:08,090
We've done all of this and now we're on this stage, creating a scoreboard.

3
00:00:08,760 --> 00:00:13,760
So what we want is to be able to write some text in our program that keeps track

4
00:00:13,950 --> 00:00:18,390
of the score, of how many pieces of food we've actually managed to eat. Now,

5
00:00:18,390 --> 00:00:22,620
the score should update every single time we hit a new piece of food and it's

6
00:00:22,620 --> 00:00:25,560
going to stay there and keep updating itself

7
00:00:25,620 --> 00:00:27,480
every time we hit a new piece of food.

8
00:00:28,710 --> 00:00:32,729
Now this scoreboard is also going to be a turtle.

9
00:00:33,330 --> 00:00:38,160
So one of the things that you can do with your turtle is you can get it to write

10
00:00:38,250 --> 00:00:39,330
a piece of text.

11
00:00:40,140 --> 00:00:43,620
And this is what the method looks like in the documentation.

12
00:00:44,040 --> 00:00:48,570
You can tell it what it should write, what kind of alignment you want,

13
00:00:48,570 --> 00:00:50,250
do you want it to be in the center of the screen,

14
00:00:50,250 --> 00:00:54,570
on the left or right side of the screen? And then what kind of font you want?

15
00:00:55,110 --> 00:00:58,860
So the font name, font size and font type,

16
00:00:58,870 --> 00:01:02,820
so normal or heavy or bold or underlined,

17
00:01:03,510 --> 00:01:05,940
and we would call it more or less like this.

18
00:01:06,900 --> 00:01:08,520
So here's a challenge for you.

19
00:01:08,820 --> 00:01:13,820
I want you to go ahead and create a new file called a scoreboard.py.

20
00:01:15,420 --> 00:01:20,370
And inside this file, I want you to create a new scoreboard class.

21
00:01:20,850 --> 00:01:21,060
Now,

22
00:01:21,060 --> 00:01:26,060
this scoreboard class is going to inherit from the turtle class,

23
00:01:26,430 --> 00:01:28,560
just as we did with the food class.

24
00:01:29,190 --> 00:01:31,950
And then the scoreboard is going to be a turtle

25
00:01:32,130 --> 00:01:37,130
which knows how to keep track of the score and how to display it in our program.

26
00:01:38,670 --> 00:01:42,090
Feel free to choose whatever font you want, whatever size you want,

27
00:01:42,360 --> 00:01:46,740
but the end outcome we're looking for is something that looks a bit like this

28
00:01:47,160 --> 00:01:51,210
and the score is going to need to be tracked inside that scoreboard class.

29
00:01:51,540 --> 00:01:55,530
And it needs to be increased by one every single time the snake eats a piece of

30
00:01:55,530 --> 00:01:59,550
food. You're going to need the help of the documentation,

31
00:01:59,550 --> 00:02:04,470
you're going to need to read up on how this turtle.write method works,

32
00:02:04,860 --> 00:02:09,509
and you're also probably going to need this turtle.clear so that you clear

33
00:02:09,509 --> 00:02:11,910
the writing every time you update the score.

34
00:02:12,510 --> 00:02:16,110
Have a think about how you would solve this and then go ahead,

35
00:02:16,170 --> 00:02:17,820
pause the video and give it a go.

36
00:02:18,740 --> 00:02:22,070
[inaudible]

37
00:02:22,760 --> 00:02:23,090
All right.

38
00:02:23,090 --> 00:02:27,230
So the first thing I'm going to do is I'm going to create my scoreboard class.

39
00:02:27,860 --> 00:02:29,030
And as I mentioned,

40
00:02:29,030 --> 00:02:32,990
this class needs to inherit from the turtle class.

41
00:02:33,230 --> 00:02:34,760
So from the turtle module,

42
00:02:34,790 --> 00:02:39,790
let's get hold of the actual turtle class so we can use it inside this file.

43
00:02:40,640 --> 00:02:45,440
And then I'm going to add it to my scoreboard as the superclass.

44
00:02:46,040 --> 00:02:48,140
Now, if I create my inits,

45
00:02:48,230 --> 00:02:53,230
I can go ahead and use this light bulb to insert the superclass call.

46
00:02:54,290 --> 00:02:59,290
So now my scoreboard is a class that can do everything a turtle class do.

47
00:03:00,460 --> 00:03:05,460
And one of the things I want it to do is I want it to keep track of the score.

48
00:03:06,760 --> 00:03:08,830
So let's say it starts out at zero,

49
00:03:09,130 --> 00:03:14,080
and we want to be able to write this onto the screen. So we can say self.

50
00:03:14,080 --> 00:03:18,310
write, and let's use an f-string to say score,

51
00:03:18,790 --> 00:03:23,290
and then we can insert the self.score value in here.

52
00:03:24,010 --> 00:03:29,010
And then we can add whether if we want it to be aligned to the center,

53
00:03:29,410 --> 00:03:34,210
for example, and then we can also add a font if we need to.

54
00:03:34,600 --> 00:03:37,330
Let's use the same font as here, Arial,

55
00:03:37,660 --> 00:03:39,760
and then we're going to make the font size a bit bigger,

56
00:03:39,790 --> 00:03:41,350
and then we're going to keep it as normal.

57
00:03:42,100 --> 00:03:46,240
So I'm going to choose an Arial font, size 24,

58
00:03:46,540 --> 00:03:48,700
and then the style is going to be normal.

59
00:03:50,500 --> 00:03:51,820
And as you can see here,

60
00:03:51,820 --> 00:03:56,380
I'm getting a warning here and it's telling me that it expected a tuple,

61
00:03:56,590 --> 00:03:58,840
but instead it just got a single string.

62
00:03:59,110 --> 00:04:02,920
So let's just compare this against the documentation code

63
00:04:03,310 --> 00:04:07,690
and you can see that this is meant to be a tuple instead of just a single piece

64
00:04:07,690 --> 00:04:10,120
of string. So let me go ahead and fix that.

65
00:04:10,600 --> 00:04:13,300
Let's add some parentheses around this,

66
00:04:13,840 --> 00:04:16,839
and then this Arial is going to be a string,

67
00:04:17,140 --> 00:04:21,100
this 24 is going to be a number and the normal is going to be a string.

68
00:04:21,339 --> 00:04:23,170
And now our errors have gone away.

69
00:04:23,890 --> 00:04:26,770
So now going back to our main.py

70
00:04:27,130 --> 00:04:29,500
let's go ahead and create our scoreboard

71
00:04:31,330 --> 00:04:34,630
as an object from the scoreboard class,

72
00:04:34,900 --> 00:04:38,290
which of course means that we need to import it into this file.

73
00:04:38,530 --> 00:04:43,530
So from the scoreboard file import the Scoreboard class.

74
00:04:45,220 --> 00:04:48,760
And now if I run this code, somewhere in the middle

75
00:04:48,760 --> 00:04:50,560
there I've got a scoreboard,

76
00:04:51,130 --> 00:04:55,540
but because it's actually black, you can't really see it.

77
00:04:56,200 --> 00:05:01,200
It's really important that we change the color of the scoreboard turtle before

78
00:05:02,770 --> 00:05:04,270
we write the text,

79
00:05:04,510 --> 00:05:08,590
because if it was written as black and then we change it to white,

80
00:05:08,620 --> 00:05:10,330
you still won't notice a difference.

81
00:05:10,780 --> 00:05:15,780
So if I change the self.color right here to white and I hit run,

82
00:05:16,690 --> 00:05:19,000
then you can see that scoreboard showing up.

83
00:05:19,270 --> 00:05:23,530
But if I move this line of code to after we've written it,

84
00:05:23,830 --> 00:05:26,590
then it won't actually make a difference. It's still written in black

85
00:05:26,590 --> 00:05:29,770
somewhere on there. Now, in addition,

86
00:05:29,800 --> 00:05:34,800
we want to get rid of the turtle that shows up when we create our scoreboard

87
00:05:35,290 --> 00:05:37,990
because all we want it to do is we want it to write,

88
00:05:38,050 --> 00:05:42,850
we don't want it to actually show up a turtle. Self.hideturtle, and

89
00:05:43,210 --> 00:05:45,280
now that little arrow disappears.

90
00:05:45,790 --> 00:05:49,450
And we probably don't want this scoreboard to be bang in the middle.

91
00:05:49,540 --> 00:05:51,010
So let's move it.

92
00:05:51,340 --> 00:05:55,510
Let's tell it to go to a particular X and Y position.

93
00:05:56,560 --> 00:06:00,290
Now, in example, here, I've got it in the center, right at the top.

94
00:06:01,130 --> 00:06:05,960
So we can keep the X as zero, but let's move the Y further up to the top.

95
00:06:05,960 --> 00:06:10,700
So let's say something around 270. And now if we test it again,

96
00:06:10,970 --> 00:06:14,540
you can see that the turtle went to the top,

97
00:06:14,840 --> 00:06:19,490
but this happened after this line was already written. So again,

98
00:06:19,520 --> 00:06:22,520
this needs to happen before we write.

99
00:06:23,270 --> 00:06:26,390
And now if we refresh, you can see it's moved to the top,

100
00:06:26,840 --> 00:06:30,470
but it's also drawn on a path to do that. So instead of doing that,

101
00:06:30,500 --> 00:06:35,500
we can tell it to self.penup before it moves to this location.

102
00:06:38,480 --> 00:06:43,250
There we have it. We've got our score showing up at the top. Now,

103
00:06:43,250 --> 00:06:48,250
all that's left to do is to keep track of the score and to increase it whenever

104
00:06:48,620 --> 00:06:50,390
the snake hits a new piece of food.

105
00:06:51,020 --> 00:06:54,500
So we know that that happens inside this if statement,

106
00:06:54,860 --> 00:06:59,000
when the snake head collides with the food, then we refresh the food,

107
00:06:59,240 --> 00:07:01,730
but we also want to increase the score.

108
00:07:02,330 --> 00:07:07,250
So let's go ahead and create a function inside our scoreboard class called

109
00:07:07,280 --> 00:07:08,990
increase_score.

110
00:07:09,830 --> 00:07:14,830
And this function is going to take the self.score and add one to it.

111
00:07:17,360 --> 00:07:20,750
And then it's going to call self.write.

112
00:07:22,220 --> 00:07:24,590
So now back inside our main.py,

113
00:07:24,920 --> 00:07:29,920
we can get whole of our scoreboard object and tell it to increase score

114
00:07:30,860 --> 00:07:35,000
whenever the snake collides with the food. So let's test this again.

115
00:07:35,660 --> 00:07:39,110
And if I manage to hit the food, 

116
00:07:42,730 --> 00:07:46,090
then you can see that the score is being updated,

117
00:07:46,330 --> 00:07:51,330
but what's happening is that the score is being written on top of the previous

118
00:07:52,210 --> 00:07:55,600
scores. So it's just all overlapping with each other.

119
00:07:56,980 --> 00:07:57,940
So instead,

120
00:07:57,970 --> 00:08:02,290
what we need to do is between each time we update the scoreboard,

121
00:08:02,620 --> 00:08:05,860
we actually have to delete what was previously on there.

122
00:08:06,910 --> 00:08:09,940
And because we've now got these two lines

123
00:08:09,970 --> 00:08:12,730
which are pretty much identical in two places,

124
00:08:13,240 --> 00:08:17,860
let's go ahead and create a function instead. We'll call update_scoreboard,

125
00:08:19,450 --> 00:08:23,560
and inside this function, we'll have our self.write.

126
00:08:28,720 --> 00:08:33,720
And we can call self.update_scoreboard here and also here.

127
00:08:39,880 --> 00:08:44,110
So now before we increase the score and call update scoreboard,

128
00:08:44,440 --> 00:08:49,440
we can call self.clear to clear the previous text that was written by this

129
00:08:50,620 --> 00:08:55,090
turtle, which is the scoreboard. Now, if we run this again

130
00:08:55,860 --> 00:08:57,840
you can see that when I do score,

131
00:08:58,140 --> 00:09:03,000
my scoreboard is wiped and then the new text code is written.

132
00:09:04,380 --> 00:09:07,560
So it doesn't overlap with the previous scoreboard.

133
00:09:08,730 --> 00:09:13,730
The final thing I want to do just as a finishing touch is I don't like having

134
00:09:13,830 --> 00:09:18,420
these hard-coded pieces of text inside the body of my programs.

135
00:09:18,810 --> 00:09:23,810
So instead it would be much better if we could take these pieces of texts out

136
00:09:24,240 --> 00:09:25,890
and create constants with them.

137
00:09:25,920 --> 00:09:30,920
So we could have one that's called ALIGNMENT set to center,

138
00:09:31,230 --> 00:09:33,120
and then another one called a FONT,

139
00:09:33,480 --> 00:09:36,240
which is going to be set to this tuple.

140
00:09:37,170 --> 00:09:40,350
And then I can use these constants down here,

141
00:09:40,350 --> 00:09:45,210
so align = ALIGNMENT, font = FONT. That way,

142
00:09:45,210 --> 00:09:49,380
when I decide that I want to change something about the alignment or the font,

143
00:09:49,620 --> 00:09:51,750
so for example instead of Arial,

144
00:09:51,780 --> 00:09:55,350
I might go for something that is a little bit more video-gamey

145
00:09:55,530 --> 00:09:57,420
sort of font like courier,

146
00:09:57,840 --> 00:10:01,560
then I don't have to dig through the body of my program to find out where it was

147
00:10:01,560 --> 00:10:04,170
defined. I can just look at the top

148
00:10:04,350 --> 00:10:09,350
which contains all the constants and then fix it right here. That shouldn't have

149
00:10:09,570 --> 00:10:14,570
changed any of the functionality. It's just made our scoreboard look a bit more

150
00:10:14,880 --> 00:10:18,480
video-gamey, and you can see how it works perfectly.

