WEBVTT

0
00:00.330 --> 00:01.650
So to start off,

1
00:01.740 --> 00:06.210
go ahead and create a new project called snake_game in PyCharm.

2
00:06.870 --> 00:11.340
And then inside this project, we're going to create a new main.py file.

3
00:12.450 --> 00:16.500
And inside our file, we're going to need our normal imports.

4
00:16.860 --> 00:21.860
So I'm going to say from turtle import the screen class and also the turtle

5
00:24.120 --> 00:25.890
class. Now,

6
00:25.920 --> 00:29.880
the first thing we're going to do is we're going to create a new screen,

7
00:29.940 --> 00:32.820
which is basically going to be the window that shows up.

8
00:33.390 --> 00:38.390
And then we're going to set up our screen so that it is 600 by 600.

9
00:40.380 --> 00:45.380
So the width is going to be 600 pixels and the height is also going to be 600

10
00:47.310 --> 00:51.780
pixels. And notice how I'm using these keyword arguments

11
00:51.810 --> 00:55.530
so that it's a little bit clearer to the next person looking at this code

12
00:55.770 --> 00:58.290
what's actually happening here. Now,

13
00:58.320 --> 01:01.770
the other thing I want to change is the screen's background color.

14
01:02.160 --> 01:06.570
So there's a method called bgcolor and inside here

15
01:06.570 --> 01:11.310
we're going to write the text black so that we end up with a black screen.

16
01:11.880 --> 01:12.300
Now,

17
01:12.300 --> 01:17.300
another thing that you might not have seen before is how to set the title of

18
01:17.340 --> 01:21.600
the window that shows up. And we do that through a method called title,

19
01:21.870 --> 01:26.430
and you can name your program here. So I'm going to call it 'My Snake Game',

20
01:26.790 --> 01:30.870
but you can of course call it whatever it is you want. Now finally,

21
01:30.870 --> 01:35.550
at the very bottom of the file, I'm going to say screen.exitonclick.

22
01:35.790 --> 01:40.050
So this way, when I actually run it, it doesn't just disappear straight away

23
01:40.770 --> 01:44.640
and it waits for me and I can look at what's actually happened so far.

24
01:45.090 --> 01:49.230
So I'm going to click run and then select my main file.

25
01:49.680 --> 01:51.000
And once it pops up,

26
01:51.030 --> 01:56.030
this is what you should see, a 600 by 600 square window

27
01:56.520 --> 01:59.880
and then at the top of the window, you'll see the name of your program,

28
01:59.910 --> 02:04.620
which is My Snake Game. And the background is now black.

29
02:05.640 --> 02:07.500
Now that we've set up our screen,

30
02:07.530 --> 02:11.040
we're ready to tackle the first of our seven steps,

31
02:11.340 --> 02:13.500
which is to create a snake body.

32
02:14.160 --> 02:17.520
And we're going to create three squares

33
02:17.610 --> 02:19.590
which are each going to be turtles,

34
02:19.830 --> 02:24.090
and they're going to be lined up next to each other along the horizontal axis.

35
02:24.210 --> 02:28.020
So let's think about this. When we create a new turtle,

36
02:28.290 --> 02:33.290
it has a dimension of 20 pixels wide by 20 pixels tall.

37
02:34.530 --> 02:38.100
And if we want three of them lined up next to each other,

38
02:38.370 --> 02:43.050
then we have to think about how we create their coordinates. As a challenge,

39
02:43.170 --> 02:46.260
I want you to think about how we can create this. Here

40
02:46.260 --> 02:48.990
we have three separate squares

41
02:49.050 --> 02:54.050
which are each a turtle object and they're shaped like a square and they have a

42
02:54.330 --> 02:57.420
white color. Now, they're positioned

43
02:57.450 --> 03:00.130
so that the first one is (0, 0),

44
03:00.490 --> 03:04.810
the next one is 20 pixels to the left and the next one is again

45
03:04.870 --> 03:08.920
20 pixels to the left so that none of the squares overlap with each other,

46
03:09.100 --> 03:12.940
but there's also no gaps. So it looks like a whole snake.

47
03:13.510 --> 03:14.980
This is your challenge

48
03:15.160 --> 03:18.820
and you might have to think about a few of the things that we've done before in

49
03:18.820 --> 03:22.450
order to achieve this. So browse the documentation,

50
03:22.810 --> 03:26.650
look back to the previous code that you've written and see if you can solve this

51
03:26.650 --> 03:28.240
challenge. Pause the video now.

52
03:32.590 --> 03:32.980
Alright.

53
03:32.980 --> 03:37.060
So the first thing we're going to need to do is to create some sort of turtle,

54
03:37.060 --> 03:37.660
right?

55
03:37.660 --> 03:42.660
So let's call this segment_1 and this is going to be a new turtle.

56
03:43.150 --> 03:45.880
And remember that when we initialize the turtle,

57
03:46.090 --> 03:48.310
we can already specify the shape.

58
03:48.580 --> 03:51.850
So we can either say shape = "square",

59
03:51.940 --> 03:56.740
or simply just leave it as square. So now at this point,

60
03:56.740 --> 04:01.150
we should end up with a square turtle, but because it's black,

61
04:01.180 --> 04:04.630
when we run the code, we won't actually see it on screen.

62
04:05.140 --> 04:09.970
So let's change this segment color to white.

63
04:10.570 --> 04:14.920
And all we have to do is write the word for the color

64
04:15.370 --> 04:18.280
and now when we rerun our code,

65
04:18.340 --> 04:22.330
you can see a white square at the center,

66
04:22.360 --> 04:24.790
which is at position (0, 0).

67
04:25.690 --> 04:30.690
Now the next step is to create our other two white squares.

68
04:32.290 --> 04:35.560
So let's go ahead and copy these bits of code,

69
04:36.070 --> 04:39.340
and I'm going to create three of them in total.

70
04:39.340 --> 04:43.300
So segment_2 and segment_3.

71
04:45.130 --> 04:47.860
Now here, we're going to create the same thing,

72
04:48.040 --> 04:52.600
but we obviously have to change the positioning because otherwise we end up with

73
04:52.630 --> 04:55.000
three squares all on top of each other,

74
04:55.030 --> 04:58.960
and we can only see one square because this is a 2-D image.

75
04:59.530 --> 05:04.530
So let's go ahead and make segment_2 go to a different position.

76
05:05.320 --> 05:09.070
So the first thing we have to specify is the X,

77
05:09.220 --> 05:12.790
which you remember is the horizontal axis here.

78
05:13.030 --> 05:15.550
And if we want another square to the left of it,

79
05:15.970 --> 05:20.970
then we're going to have to go to -20 from zero.

80
05:21.550 --> 05:25.240
That way it will be shifted to the left by 20 pixels.

81
05:25.840 --> 05:28.750
Let's say go to -20

82
05:29.110 --> 05:32.650
and then the Y-axis is going to be zero because it's not going to move up or

83
05:32.650 --> 05:33.483
down.

84
05:33.640 --> 05:38.200
Now let's take a look at the result of this and you can see we've now got two

85
05:38.200 --> 05:40.210
squares lining side by side,

86
05:40.540 --> 05:43.990
but the third square is still at the center.

87
05:44.620 --> 05:49.030
So the third segment is going to be shifted a further 20 pixels.

88
05:49.390 --> 05:54.310
This is centered on (0, 0), this is at (-20, 0),

89
05:54.610 --> 05:57.100
then this has to be at (-40, 0).

90
05:57.350 --> 06:01.700
So let's tell our segment_3 to go to (-40, 0).

91
06:02.150 --> 06:03.800
And this is what we see.

92
06:04.190 --> 06:08.810
Did you manage to achieve this or did you do it in a different way? Now,

93
06:08.900 --> 06:13.700
a much easier way of creating this would have been to use a for loop.

94
06:14.090 --> 06:14.990
For example, we

95
06:15.250 --> 06:17.340
could have created a list of

96
06:17.480 --> 06:18.940
starting positions

97
06:20.860 --> 06:24.370
and this could contain some tuples.

98
06:24.850 --> 06:29.850
So we know that in order to specify where the segment should go to,

99
06:30.280 --> 06:34.240
we have to provide a tuple, which is a X and a Y value.

100
06:34.840 --> 06:39.220
And we know that we can create a tuple just by using a set of parentheses and

101
06:39.220 --> 06:42.130
then adding values in there, separated by commas.

102
06:42.610 --> 06:46.990
So if the first segment has to be at the center, then that's going to be 

103
06:46.990 --> 06:51.910
(0, 0), and then the second piece is going to be at (-20, 0),

104
06:52.510 --> 06:55.720
and the third piece is going to be at (-40, 0).

105
06:56.410 --> 06:57.780
Now we can create

106
06:57.940 --> 07:00.100
a for loop. So for

107
07:00.370 --> 07:03.040
position in starting_positions,

108
07:03.490 --> 07:08.490
let's loop through this list and create a new segment for each of those

109
07:09.490 --> 07:13.870
positions. So now we're going to create a new segment

110
07:13.870 --> 07:16.360
that's a square turtle, that's going to be white,

111
07:16.750 --> 07:21.750
and we're going to get the segment to go to the position that we're currently

112
07:21.940 --> 07:25.570
looping on. Now we can delete all of that code

113
07:25.900 --> 07:30.370
and when we run our code again, you can see we have the same effect.

114
07:30.400 --> 07:35.380
We have all three pieces. So if you can't remember how tuples work,

115
07:35.620 --> 07:38.890
be sure to go back to the lessons on tuples in Day 18,

116
07:39.220 --> 07:42.040
where we talked about how to write them and how they work.

117
07:42.760 --> 07:45.070
But that completes our first step.

118
07:45.280 --> 07:50.280
We've managed to create three segments that will comprise our snake

119
07:50.650 --> 07:51.910
and in the next lesson,

120
07:51.940 --> 07:55.690
we're going to figure out how to get our snake to start moving by itself on the

121
07:55.690 --> 07:59.380
screen. So for all of that and more, I'll see you on the next lesson.