WEBVTT

0
00:00.180 --> 00:02.160
All right. So just as previously,

1
00:02.220 --> 00:07.220
we've split up this large problem of building the game into five smaller

2
00:07.320 --> 00:09.090
sub-parts. Firstly,

3
00:09.120 --> 00:13.140
moving the turtle, controlling it with the keypress, creating and moving the

4
00:13.140 --> 00:15.540
cars automatically across the screen,

5
00:15.810 --> 00:19.740
detecting a collision with the car and detecting when the turtle reaches the

6
00:19.740 --> 00:21.300
other side. Finally,

7
00:21.300 --> 00:24.510
we create a scoreboard that keeps track of which level we're on

8
00:24.750 --> 00:27.090
and also that shows game over at the end.

9
00:27.810 --> 00:31.710
So the first step is to move the turtle using a keypress.

10
00:32.070 --> 00:36.480
The turtle can only go forwards, and every time we hit the Up key

11
00:36.690 --> 00:41.580
the turtle moves forward by a set amount until it reaches the other side of the

12
00:41.580 --> 00:45.000
screen. This is the first thing that we're going to tackle. Now,

13
00:45.030 --> 00:48.180
because this is related to the functionality of the player,

14
00:48.510 --> 00:51.870
let's go ahead and create this inside the player class.

15
00:52.800 --> 00:54.420
I'm going to delete that pass

16
00:54.540 --> 00:59.340
and I'm also going to import the turtle class from the turtle module.

17
01:00.090 --> 01:04.170
Now, our player is going to inherit from this turtle class.

18
01:04.710 --> 01:06.720
So inside our init,

19
01:06.930 --> 01:10.830
we're going to need to add the super.init 

20
01:10.860 --> 01:13.860
or you can add it automatically using the light bulb.

21
01:14.460 --> 01:19.460
So now this player class can do everything that a turtle class can do and we can

22
01:20.490 --> 01:21.870
make it do even more.

23
01:22.830 --> 01:27.630
The first thing we're going to do is we're going to set the shape of our player

24
01:27.960 --> 01:30.210
and we're gonna set it to a turtle.

25
01:30.810 --> 01:34.770
The next thing we're going to do is we're going to call penup so that this

26
01:34.770 --> 01:39.330
turtle just remains a shape and it doesn't draw. Finally,

27
01:39.360 --> 01:43.470
we need to get it to the starting position and get it to face North.

28
01:43.950 --> 01:48.950
We can do that by setting self.goto and then we can set it to go to the

29
01:50.370 --> 01:51.660
starting position,

30
01:52.350 --> 01:57.350
which you can see is a tuple because it's enclosed inside parentheses and you've

31
01:59.040 --> 02:03.300
got values separated by a comma. Finally,

32
02:03.330 --> 02:08.330
we're going to set the heading of our turtle so that it faces North,

33
02:09.300 --> 02:11.220
which is 90 degrees.

34
02:11.880 --> 02:16.800
So now all we have to do is go back into our main.py,

35
02:17.400 --> 02:19.410
after we've set up our screen,

36
02:19.650 --> 02:23.880
let's go ahead and create a new player from the player class.

37
02:25.290 --> 02:27.360
Let's go ahead and run this code.

38
02:28.160 --> 02:28.993
...

39
02:30.650 --> 02:35.650
<v 0>And you can see that we've got our little turtle showing up here at the center</v>

40
02:37.400 --> 02:40.850
of the bottom of the screen and it's facing North.

41
02:42.380 --> 02:47.270
So now the next thing we need to do is to get that turtle to move upwards

42
02:47.540 --> 02:49.460
every time we hit the Up key.

43
02:50.060 --> 02:53.450
That means we're going to need to get our screen to

44
02:53.480 --> 02:57.620
listen for events. After calling screen.listen,

45
02:57.680 --> 03:02.680
we're going to get the screen to listen to a keystroke. So we can use onkey to

46
03:04.720 --> 03:08.080
set the Up key as the key to listen to.

47
03:08.650 --> 03:13.390
And then when that happens, then we're going to call player.

48
03:14.110 --> 03:19.110
go_up. And remember that when we're calling methods inside the listener,

49
03:20.320 --> 03:24.970
we don't want to add the parentheses because this will trigger it at the point

50
03:25.030 --> 03:28.900
where it evaluates this line of code. Instead,

51
03:28.930 --> 03:33.550
we want to trigger this function only when this Up key is detected.

52
03:34.240 --> 03:39.240
So now all that's left to do is to go into our player.py and define that

53
03:40.000 --> 03:41.680
function go_up.

54
03:42.850 --> 03:46.630
So how can we move our turtle up? Well,

55
03:46.630 --> 03:50.080
we can get our turtle so self,

56
03:50.410 --> 03:53.710
and then we can get it to move forwards by a distance.

57
03:54.280 --> 03:58.930
And the distance is going to be the move distance that's set in this constant

58
03:58.930 --> 04:03.070
here. This means that later on when we want to change the move distance

59
04:03.070 --> 04:06.970
if we want it to go further each time, we can just edit it at the top of the

60
04:06.970 --> 04:09.130
file instead of digging through the code.

61
04:10.540 --> 04:14.350
Now let's run our code again and let's just make sure that it works.

62
04:14.830 --> 04:16.660
So now every time I hit the Up key

63
04:16.690 --> 04:21.690
my turtle moves up and it keeps on going until it reaches the other side of the

64
04:21.730 --> 04:26.500
screen. So that's the first step completed. Now,

65
04:26.530 --> 04:28.990
if you think you can complete the next step by yourself,

66
04:29.290 --> 04:34.290
then head back to that list of problems broken down and see if you can tackle

67
04:34.390 --> 04:39.010
the next one by yourself. If you can't or if you need some extra help,

68
04:39.130 --> 04:42.460
then you can always come back to the videos and I'll walk you through the steps

69
04:42.490 --> 04:43.120
one by one.