WEBVTT

0
00:00.330 --> 00:01.200
In the last lesson,

1
00:01.260 --> 00:05.640
we figured out how to detect when our turtle reaches the other side of the

2
00:05.640 --> 00:08.340
screen. And when this happens,

3
00:08.370 --> 00:12.480
we return the turtle player to its original position

4
00:12.990 --> 00:17.990
and we also increase the speed of the cars by the move increment that we

5
00:18.690 --> 00:22.500
defined as a constant. So in this lesson,

6
00:22.560 --> 00:24.720
what we're going to do is the final step,

7
00:24.750 --> 00:29.750
which is to create a scoreboard that keeps track of which level the turtle

8
00:30.690 --> 00:35.190
player is on, and also when the turtle hits one of the

9
00:35.190 --> 00:36.720
cars to display

10
00:36.750 --> 00:40.680
the words game over in the center of the screen. To do that,

11
00:40.710 --> 00:45.210
we're of course going to go inside our scoreboard class and again

12
00:45.240 --> 00:48.240
we're going to need the help of the turtle class.

13
00:48.660 --> 00:53.660
So let's import that from the turtle module and I'm going to make my scoreboard

14
00:54.210 --> 00:57.300
a subclass of the turtle class.

15
00:57.600 --> 01:00.270
So it's now inheriting from the turtle class.

16
01:00.720 --> 01:04.950
And once we've defined our init and managed to get it

17
01:04.950 --> 01:07.470
to inherit everything from the superclass,

18
01:07.860 --> 01:12.720
then our scoreboard is now able to do everything a turtle class can do.

19
01:13.710 --> 01:15.510
What do we want it to do? Well,

20
01:15.570 --> 01:19.950
we have to initialize it with a couple of things first. For example,

21
01:19.950 --> 01:22.650
we probably want to start out by hiding the turtle,

22
01:22.680 --> 01:27.000
we just want to use it as a pen to draw. And in addition,

23
01:27.030 --> 01:31.590
we don't want it to move and draw. So we're going to pull the pen up.

24
01:32.040 --> 01:35.250
We're going to be using the write method instead.

25
01:35.880 --> 01:38.940
And what we want to write is the level, right?

26
01:39.300 --> 01:43.620
So the current level that the player is on. In order to do that,

27
01:43.620 --> 01:46.230
we also have to keep track of the level.

28
01:46.650 --> 01:49.440
So let's go ahead and create a new attribute

29
01:49.710 --> 01:52.980
which I'll call level and let's start at level 1.

30
01:53.700 --> 01:58.320
So then we can insert this right here with self.level

31
01:59.010 --> 02:01.710
and in addition to writing this text,

32
02:01.950 --> 02:04.950
we can define what we want the alignment to be.

33
02:05.370 --> 02:10.370
So I want this to be on the left and also what the font to be.

34
02:10.920 --> 02:15.920
So I'm going to use this font that was declared up here as a constant. Now,

35
02:17.070 --> 02:22.070
inside our main.py next to where we've defined our player and our car_

36
02:22.070 --> 02:22.903
manager

37
02:22.920 --> 02:27.920
I'm going to create this new scoreboard object and that's going to be created

38
02:29.040 --> 02:33.870
from the scoreboard class. Now notice when I run the code as it is,

39
02:34.230 --> 02:37.890
you can see that our level is left-aligned,

40
02:38.280 --> 02:42.300
it's got the font that we defined and it's writing the text that we want,

41
02:42.630 --> 02:46.800
but it's not in the right position. To define the position

42
02:46.830 --> 02:51.750
we have to do that right before we tell the scoreboard to write,

43
02:52.110 --> 02:55.950
so right here, but after where we've got our penup.

44
02:56.580 --> 02:59.830
So this way we don't draw a path to where we're going to.

45
03:00.400 --> 03:03.220
So now we're going to define self.goto,

46
03:03.730 --> 03:07.870
and I'm just going to get it to go to probably the top left corner.

47
03:08.200 --> 03:13.200
So that's going to be probably -280 and then it's going to be 

48
03:14.560 --> 03:18.640
+280 on the Y. Now, if we just check the positioning,

49
03:18.910 --> 03:22.630
you can see that it's a little bit too far up on the Y-axis.

50
03:22.990 --> 03:27.940
So it lets move it down a little bit and you can tweak these things until you

51
03:27.940 --> 03:30.670
get to the point where you're happy with its positioning.

52
03:31.300 --> 03:34.900
So I think this looks pretty good. Now,

53
03:34.930 --> 03:37.540
in addition to writing the level,

54
03:37.570 --> 03:41.710
we actually have to update it every time the player levels up, right?

55
03:42.220 --> 03:45.340
And they do that when there's a successful crossing.

56
03:45.850 --> 03:47.500
So at some point here,

57
03:47.500 --> 03:51.550
we should be able to call scoreboard and we should be able to get the scoreboard

58
03:51.610 --> 03:53.830
to increase the level.

59
03:54.550 --> 03:59.440
So let's go into scoreboard and let's define that function, increase_

60
04:01.000 --> 04:01.833
level.

61
04:02.410 --> 04:07.410
And the first thing to do when we're increasing the level is of course getting

62
04:07.420 --> 04:12.420
hold of this self.level and then adding one to it each time.

63
04:13.420 --> 04:17.530
In addition, we're going to need the level to be rewritten again.

64
04:17.980 --> 04:22.980
So let's cut this out of the init and let's instead define a custom method

65
04:23.260 --> 04:25.750
which we'll call update_scoreboard.

66
04:28.140 --> 04:28.410
<v 1>Yeah.</v>

67
04:28.410 --> 04:33.300
<v 0>Inside this update_scoreboard, we can write the current level.</v>

68
04:33.930 --> 04:35.460
So now inside the init

69
04:35.490 --> 04:39.180
we can call self.update_scoreboard

70
04:39.570 --> 04:41.820
and also when we increase the level,

71
04:41.850 --> 04:45.030
we can call self.update_scoreboard.

72
04:45.780 --> 04:47.790
Now at the moment, as it is,

73
04:47.940 --> 04:52.140
it's going to overwrite what used to be on the scoreboard.

74
04:52.620 --> 04:55.110
So at the moment it's on level 1,

75
04:56.130 --> 04:58.800
but once I make a successful crossing,

76
04:59.070 --> 05:03.240
you can see that level 2 is going to be overwritten over level 1.

77
05:03.870 --> 05:06.510
To prevent that when we update the scoreboard,

78
05:06.780 --> 05:09.600
we have to get the scoreboard to clear itself

79
05:09.840 --> 05:13.740
so that it deletes all the previous stuff that it wrote. This way

80
05:13.830 --> 05:15.690
when we actually run our code

81
05:15.720 --> 05:20.720
you can see that our scoreboard will refresh and clear the previous text and

82
05:22.890 --> 05:24.690
write the new text each time.

83
05:25.500 --> 05:30.500
So now the final thing to do is to write the words game over in the middle of

84
05:30.510 --> 05:35.430
the screen when the game ends. To do that, I'm going to create 

85
05:35.440 --> 05:36.900
another method here

86
05:36.930 --> 05:41.460
which I'll call game_over. And inside this method,

87
05:41.790 --> 05:45.300
we're going to get our turtle to go to the center.

88
05:45.360 --> 05:50.360
So we're going to say self.goto and the center is, of course, at

89
05:51.120 --> 05:54.900
(0, 0), and then we're going to get it to write,

90
05:55.320 --> 06:00.140
but this time we're not going to write the level anymore. Instead,

91
06:00.170 --> 06:04.370
we're going to just write the words GAME OVER in all caps.

92
06:04.820 --> 06:09.820
And I want the alignment to be centered and I want the font to be the default

93
06:09.890 --> 06:14.690
font. Now, when we actually detect a collision,

94
06:14.780 --> 06:17.300
not only is game_is_on going to be false,

95
06:17.570 --> 06:21.950
but also we're going to get the scoreboard to show the game over sequence.

96
06:22.670 --> 06:27.670
So now we can run our code and you can see that when my turtle collides with a

97
06:28.730 --> 06:29.563
car,

98
06:31.010 --> 06:33.740
then it says game over in the center.

99
06:34.220 --> 06:39.050
And because when we wrote game over, we didn't clear any of the previous texts,

100
06:39.350 --> 06:42.650
the user can see at the highest level that they managed to reach.

101
06:43.730 --> 06:45.830
That's it. That's the entire game.

102
06:46.370 --> 06:50.870
Hopefully you've managed to build this entire game by yourself and you're just here

103
06:50.870 --> 06:55.610
to check a few niggling issues. But if you struggled with this code,

104
06:55.850 --> 07:00.290
then I really recommend to review the previous lessons where we created the

105
07:00.290 --> 07:03.110
snake game or when we created this turtle crossing game

106
07:03.440 --> 07:07.310
and try to see if you can create these two games from scratch by yourself

107
07:07.670 --> 07:11.870
by looking at how the game works. Because if you continue forward,

108
07:11.930 --> 07:14.120
things are only gonna get more complex.

109
07:14.420 --> 07:18.860
And I'm assuming that you're going to take the time to review and revise before

110
07:18.860 --> 07:19.693
you continue.

111
07:20.900 --> 07:24.590
So have fun playing with the turtle crossing game and be sure to let me know in

112
07:24.590 --> 07:29.590
the Q/A what your highest level is that you managed to reach and also

113
07:29.810 --> 07:32.780
remember to attach a picture or it didn't happen.

114
07:33.740 --> 07:36.590
And this is a great project for you to customize.

115
07:36.620 --> 07:39.110
So think about what you might want to change

116
07:39.140 --> 07:44.140
like the colors or the shapes and make the game really your own and then take a

117
07:44.780 --> 07:48.830
screenshot of it and share it with us in the Q/A so that we can all

118
07:48.830 --> 07:51.500
appreciate and congratulate you on your work.