1
00:00:00,180 --> 00:00:02,160
All right. So just as previously,

2
00:00:02,220 --> 00:00:07,220
we've split up this large problem of building the game into five smaller

3
00:00:07,320 --> 00:00:09,090
sub-parts. Firstly,

4
00:00:09,120 --> 00:00:13,140
moving the turtle, controlling it with the keypress, creating and moving the

5
00:00:13,140 --> 00:00:15,540
cars automatically across the screen,

6
00:00:15,810 --> 00:00:19,740
detecting a collision with the car and detecting when the turtle reaches the

7
00:00:19,740 --> 00:00:21,300
other side. Finally,

8
00:00:21,300 --> 00:00:24,510
we create a scoreboard that keeps track of which level we're on

9
00:00:24,750 --> 00:00:27,090
and also that shows game over at the end.

10
00:00:27,810 --> 00:00:31,710
So the first step is to move the turtle using a keypress.

11
00:00:32,070 --> 00:00:36,480
The turtle can only go forwards, and every time we hit the Up key

12
00:00:36,690 --> 00:00:41,580
the turtle moves forward by a set amount until it reaches the other side of the

13
00:00:41,580 --> 00:00:45,000
screen. This is the first thing that we're going to tackle. Now,

14
00:00:45,030 --> 00:00:48,180
because this is related to the functionality of the player,

15
00:00:48,510 --> 00:00:51,870
let's go ahead and create this inside the player class.

16
00:00:52,800 --> 00:00:54,420
I'm going to delete that pass

17
00:00:54,540 --> 00:00:59,340
and I'm also going to import the turtle class from the turtle module.

18
00:01:00,090 --> 00:01:04,170
Now, our player is going to inherit from this turtle class.

19
00:01:04,709 --> 00:01:06,720
So inside our init,

20
00:01:06,930 --> 00:01:10,830
we're going to need to add the super.init 

21
00:01:10,860 --> 00:01:13,860
or you can add it automatically using the light bulb.

22
00:01:14,460 --> 00:01:19,460
So now this player class can do everything that a turtle class can do and we can

23
00:01:20,490 --> 00:01:21,870
make it do even more.

24
00:01:22,830 --> 00:01:27,630
The first thing we're going to do is we're going to set the shape of our player

25
00:01:27,960 --> 00:01:30,210
and we're gonna set it to a turtle.

26
00:01:30,810 --> 00:01:34,770
The next thing we're going to do is we're going to call penup so that this

27
00:01:34,770 --> 00:01:39,330
turtle just remains a shape and it doesn't draw. Finally,

28
00:01:39,360 --> 00:01:43,470
we need to get it to the starting position and get it to face North.

29
00:01:43,950 --> 00:01:48,950
We can do that by setting self.goto and then we can set it to go to the

30
00:01:50,370 --> 00:01:51,660
starting position,

31
00:01:52,350 --> 00:01:57,350
which you can see is a tuple because it's enclosed inside parentheses and you've

32
00:01:59,040 --> 00:02:03,300
got values separated by a comma. Finally,

33
00:02:03,330 --> 00:02:08,330
we're going to set the heading of our turtle so that it faces North,

34
00:02:09,300 --> 00:02:11,220
which is 90 degrees.

35
00:02:11,880 --> 00:02:16,800
So now all we have to do is go back into our main.py,

36
00:02:17,400 --> 00:02:19,410
after we've set up our screen,

37
00:02:19,650 --> 00:02:23,880
let's go ahead and create a new player from the player class.

38
00:02:25,290 --> 00:02:27,360
Let's go ahead and run this code.

39
00:02:28,160 --> 00:02:28,993
...

40
00:02:30,650 --> 00:02:35,650
And you can see that we've got our little turtle showing up here at the center

41
00:02:37,400 --> 00:02:40,850
of the bottom of the screen and it's facing North.

42
00:02:42,380 --> 00:02:47,270
So now the next thing we need to do is to get that turtle to move upwards

43
00:02:47,540 --> 00:02:49,460
every time we hit the Up key.

44
00:02:50,060 --> 00:02:53,450
That means we're going to need to get our screen to

45
00:02:53,480 --> 00:02:57,620
listen for events. After calling screen.listen,

46
00:02:57,680 --> 00:03:02,680
we're going to get the screen to listen to a keystroke. So we can use onkey to

47
00:03:04,720 --> 00:03:08,080
set the Up key as the key to listen to.

48
00:03:08,650 --> 00:03:13,390
And then when that happens, then we're going to call player.

49
00:03:14,110 --> 00:03:19,110
go_up. And remember that when we're calling methods inside the listener,

50
00:03:20,320 --> 00:03:24,970
we don't want to add the parentheses because this will trigger it at the point

51
00:03:25,030 --> 00:03:28,900
where it evaluates this line of code. Instead,

52
00:03:28,930 --> 00:03:33,550
we want to trigger this function only when this Up key is detected.

53
00:03:34,240 --> 00:03:39,240
So now all that's left to do is to go into our player.py and define that

54
00:03:40,000 --> 00:03:41,680
function go_up.

55
00:03:42,850 --> 00:03:46,630
So how can we move our turtle up? Well,

56
00:03:46,630 --> 00:03:50,080
we can get our turtle so self,

57
00:03:50,410 --> 00:03:53,710
and then we can get it to move forwards by a distance.

58
00:03:54,280 --> 00:03:58,930
And the distance is going to be the move distance that's set in this constant

59
00:03:58,930 --> 00:04:03,070
here. This means that later on when we want to change the move distance

60
00:04:03,070 --> 00:04:06,970
if we want it to go further each time, we can just edit it at the top of the

61
00:04:06,970 --> 00:04:09,130
file instead of digging through the code.

62
00:04:10,540 --> 00:04:14,350
Now let's run our code again and let's just make sure that it works.

63
00:04:14,830 --> 00:04:16,660
So now every time I hit the Up key

64
00:04:16,690 --> 00:04:21,690
my turtle moves up and it keeps on going until it reaches the other side of the

65
00:04:21,730 --> 00:04:26,500
screen. So that's the first step completed. Now,

66
00:04:26,530 --> 00:04:28,990
if you think you can complete the next step by yourself,

67
00:04:29,290 --> 00:04:34,290
then head back to that list of problems broken down and see if you can tackle

68
00:04:34,390 --> 00:04:39,010
the next one by yourself. If you can't or if you need some extra help,

69
00:04:39,130 --> 00:04:42,460
then you can always come back to the videos and I'll walk you through the steps

70
00:04:42,490 --> 00:04:43,120
one by one.

