1
00:00:00,330 --> 00:00:01,200
In the last lesson,

2
00:00:01,260 --> 00:00:05,640
we figured out how to detect when our turtle reaches the other side of the

3
00:00:05,640 --> 00:00:08,340
screen. And when this happens,

4
00:00:08,370 --> 00:00:12,480
we return the turtle player to its original position

5
00:00:12,990 --> 00:00:17,990
and we also increase the speed of the cars by the move increment that we

6
00:00:18,690 --> 00:00:22,500
defined as a constant. So in this lesson,

7
00:00:22,560 --> 00:00:24,720
what we're going to do is the final step,

8
00:00:24,750 --> 00:00:29,750
which is to create a scoreboard that keeps track of which level the turtle

9
00:00:30,690 --> 00:00:35,190
player is on, and also when the turtle hits one of the

10
00:00:35,190 --> 00:00:36,720
cars to display

11
00:00:36,750 --> 00:00:40,680
the words game over in the center of the screen. To do that,

12
00:00:40,710 --> 00:00:45,210
we're of course going to go inside our scoreboard class and again

13
00:00:45,240 --> 00:00:48,240
we're going to need the help of the turtle class.

14
00:00:48,660 --> 00:00:53,660
So let's import that from the turtle module and I'm going to make my scoreboard

15
00:00:54,210 --> 00:00:57,300
a subclass of the turtle class.

16
00:00:57,600 --> 00:01:00,270
So it's now inheriting from the turtle class.

17
00:01:00,720 --> 00:01:04,950
And once we've defined our init and managed to get it

18
00:01:04,950 --> 00:01:07,470
to inherit everything from the superclass,

19
00:01:07,860 --> 00:01:12,720
then our scoreboard is now able to do everything a turtle class can do.

20
00:01:13,710 --> 00:01:15,510
What do we want it to do? Well,

21
00:01:15,570 --> 00:01:19,950
we have to initialize it with a couple of things first. For example,

22
00:01:19,950 --> 00:01:22,650
we probably want to start out by hiding the turtle,

23
00:01:22,680 --> 00:01:27,000
we just want to use it as a pen to draw. And in addition,

24
00:01:27,030 --> 00:01:31,590
we don't want it to move and draw. So we're going to pull the pen up.

25
00:01:32,040 --> 00:01:35,250
We're going to be using the write method instead.

26
00:01:35,880 --> 00:01:38,940
And what we want to write is the level, right?

27
00:01:39,300 --> 00:01:43,620
So the current level that the player is on. In order to do that,

28
00:01:43,620 --> 00:01:46,230
we also have to keep track of the level.

29
00:01:46,650 --> 00:01:49,440
So let's go ahead and create a new attribute

30
00:01:49,710 --> 00:01:52,980
which I'll call level and let's start at level 1.

31
00:01:53,700 --> 00:01:58,320
So then we can insert this right here with self.level

32
00:01:59,010 --> 00:02:01,710
and in addition to writing this text,

33
00:02:01,950 --> 00:02:04,950
we can define what we want the alignment to be.

34
00:02:05,370 --> 00:02:10,370
So I want this to be on the left and also what the font to be.

35
00:02:10,919 --> 00:02:15,920
So I'm going to use this font that was declared up here as a constant. Now,

36
00:02:17,070 --> 00:02:22,070
inside our main.py next to where we've defined our player and our car_

37
00:02:22,070 --> 00:02:22,903
manager

38
00:02:22,920 --> 00:02:27,920
I'm going to create this new scoreboard object and that's going to be created

39
00:02:29,040 --> 00:02:33,870
from the scoreboard class. Now notice when I run the code as it is,

40
00:02:34,230 --> 00:02:37,890
you can see that our level is left-aligned,

41
00:02:38,280 --> 00:02:42,300
it's got the font that we defined and it's writing the text that we want,

42
00:02:42,630 --> 00:02:46,800
but it's not in the right position. To define the position

43
00:02:46,830 --> 00:02:51,750
we have to do that right before we tell the scoreboard to write,

44
00:02:52,110 --> 00:02:55,950
so right here, but after where we've got our penup.

45
00:02:56,580 --> 00:02:59,830
So this way we don't draw a path to where we're going to.

46
00:03:00,400 --> 00:03:03,220
So now we're going to define self.goto,

47
00:03:03,730 --> 00:03:07,870
and I'm just going to get it to go to probably the top left corner.

48
00:03:08,200 --> 00:03:13,200
So that's going to be probably -280 and then it's going to be 

49
00:03:14,560 --> 00:03:18,640
+280 on the Y. Now, if we just check the positioning,

50
00:03:18,910 --> 00:03:22,630
you can see that it's a little bit too far up on the Y-axis.

51
00:03:22,990 --> 00:03:27,940
So it lets move it down a little bit and you can tweak these things until you

52
00:03:27,940 --> 00:03:30,670
get to the point where you're happy with its positioning.

53
00:03:31,300 --> 00:03:34,900
So I think this looks pretty good. Now,

54
00:03:34,930 --> 00:03:37,540
in addition to writing the level,

55
00:03:37,570 --> 00:03:41,710
we actually have to update it every time the player levels up, right?

56
00:03:42,220 --> 00:03:45,340
And they do that when there's a successful crossing.

57
00:03:45,850 --> 00:03:47,500
So at some point here,

58
00:03:47,500 --> 00:03:51,550
we should be able to call scoreboard and we should be able to get the scoreboard

59
00:03:51,610 --> 00:03:53,830
to increase the level.

60
00:03:54,550 --> 00:03:59,440
So let's go into scoreboard and let's define that function, increase_

61
00:04:01,000 --> 00:04:01,833
level.

62
00:04:02,410 --> 00:04:07,410
And the first thing to do when we're increasing the level is of course getting

63
00:04:07,420 --> 00:04:12,420
hold of this self.level and then adding one to it each time.

64
00:04:13,420 --> 00:04:17,529
In addition, we're going to need the level to be rewritten again.

65
00:04:17,980 --> 00:04:22,980
So let's cut this out of the init and let's instead define a custom method

66
00:04:23,260 --> 00:04:25,750
which we'll call update_scoreboard.

67
00:04:28,140 --> 00:04:28,410
Yeah.

68
00:04:28,410 --> 00:04:33,300
Inside this update_scoreboard, we can write the current level.

69
00:04:33,930 --> 00:04:35,460
So now inside the init

70
00:04:35,490 --> 00:04:39,180
we can call self.update_scoreboard

71
00:04:39,570 --> 00:04:41,820
and also when we increase the level,

72
00:04:41,850 --> 00:04:45,030
we can call self.update_scoreboard.

73
00:04:45,780 --> 00:04:47,790
Now at the moment, as it is,

74
00:04:47,940 --> 00:04:52,140
it's going to overwrite what used to be on the scoreboard.

75
00:04:52,620 --> 00:04:55,110
So at the moment it's on level 1,

76
00:04:56,130 --> 00:04:58,800
but once I make a successful crossing,

77
00:04:59,070 --> 00:05:03,240
you can see that level 2 is going to be overwritten over level 1.

78
00:05:03,870 --> 00:05:06,510
To prevent that when we update the scoreboard,

79
00:05:06,780 --> 00:05:09,600
we have to get the scoreboard to clear itself

80
00:05:09,840 --> 00:05:13,740
so that it deletes all the previous stuff that it wrote. This way

81
00:05:13,830 --> 00:05:15,690
when we actually run our code

82
00:05:15,720 --> 00:05:20,720
you can see that our scoreboard will refresh and clear the previous text and

83
00:05:22,890 --> 00:05:24,690
write the new text each time.

84
00:05:25,500 --> 00:05:30,500
So now the final thing to do is to write the words game over in the middle of

85
00:05:30,510 --> 00:05:35,430
the screen when the game ends. To do that, I'm going to create 

86
00:05:35,440 --> 00:05:36,900
another method here

87
00:05:36,930 --> 00:05:41,460
which I'll call game_over. And inside this method,

88
00:05:41,790 --> 00:05:45,300
we're going to get our turtle to go to the center.

89
00:05:45,360 --> 00:05:50,360
So we're going to say self.goto and the center is, of course, at

90
00:05:51,120 --> 00:05:54,900
(0, 0), and then we're going to get it to write,

91
00:05:55,320 --> 00:06:00,140
but this time we're not going to write the level anymore. Instead,

92
00:06:00,170 --> 00:06:04,370
we're going to just write the words GAME OVER in all caps.

93
00:06:04,820 --> 00:06:09,820
And I want the alignment to be centered and I want the font to be the default

94
00:06:09,890 --> 00:06:14,690
font. Now, when we actually detect a collision,

95
00:06:14,780 --> 00:06:17,300
not only is game_is_on going to be false,

96
00:06:17,570 --> 00:06:21,950
but also we're going to get the scoreboard to show the game over sequence.

97
00:06:22,670 --> 00:06:27,670
So now we can run our code and you can see that when my turtle collides with a

98
00:06:28,730 --> 00:06:29,563
car,

99
00:06:31,010 --> 00:06:33,740
then it says game over in the center.

100
00:06:34,220 --> 00:06:39,050
And because when we wrote game over, we didn't clear any of the previous texts,

101
00:06:39,350 --> 00:06:42,650
the user can see at the highest level that they managed to reach.

102
00:06:43,730 --> 00:06:45,830
That's it. That's the entire game.

103
00:06:46,370 --> 00:06:50,870
Hopefully you've managed to build this entire game by yourself and you're just here

104
00:06:50,870 --> 00:06:55,610
to check a few niggling issues. But if you struggled with this code,

105
00:06:55,850 --> 00:07:00,290
then I really recommend to review the previous lessons where we created the

106
00:07:00,290 --> 00:07:03,110
snake game or when we created this turtle crossing game

107
00:07:03,440 --> 00:07:07,310
and try to see if you can create these two games from scratch by yourself

108
00:07:07,670 --> 00:07:11,870
by looking at how the game works. Because if you continue forward,

109
00:07:11,930 --> 00:07:14,120
things are only gonna get more complex.

110
00:07:14,420 --> 00:07:18,860
And I'm assuming that you're going to take the time to review and revise before

111
00:07:18,860 --> 00:07:19,693
you continue.

112
00:07:20,900 --> 00:07:24,590
So have fun playing with the turtle crossing game and be sure to let me know in

113
00:07:24,590 --> 00:07:29,590
the Q/A what your highest level is that you managed to reach and also

114
00:07:29,810 --> 00:07:32,780
remember to attach a picture or it didn't happen.

115
00:07:33,740 --> 00:07:36,590
And this is a great project for you to customize.

116
00:07:36,620 --> 00:07:39,110
So think about what you might want to change

117
00:07:39,140 --> 00:07:44,140
like the colors or the shapes and make the game really your own and then take a

118
00:07:44,780 --> 00:07:48,830
screenshot of it and share it with us in the Q/A so that we can all

119
00:07:48,830 --> 00:07:51,500
appreciate and congratulate you on your work.

