1
00:00:00,330 --> 00:00:01,650
So to start off,

2
00:00:01,740 --> 00:00:06,210
go ahead and create a new project called snake_game in PyCharm.

3
00:00:06,870 --> 00:00:11,340
And then inside this project, we're going to create a new main.py file.

4
00:00:12,450 --> 00:00:16,500
And inside our file, we're going to need our normal imports.

5
00:00:16,860 --> 00:00:21,860
So I'm going to say from turtle import the screen class and also the turtle

6
00:00:24,120 --> 00:00:25,890
class. Now,

7
00:00:25,920 --> 00:00:29,880
the first thing we're going to do is we're going to create a new screen,

8
00:00:29,940 --> 00:00:32,820
which is basically going to be the window that shows up.

9
00:00:33,390 --> 00:00:38,390
And then we're going to set up our screen so that it is 600 by 600.

10
00:00:40,380 --> 00:00:45,380
So the width is going to be 600 pixels and the height is also going to be 600

11
00:00:47,310 --> 00:00:51,780
pixels. And notice how I'm using these keyword arguments

12
00:00:51,810 --> 00:00:55,530
so that it's a little bit clearer to the next person looking at this code

13
00:00:55,770 --> 00:00:58,290
what's actually happening here. Now,

14
00:00:58,320 --> 00:01:01,770
the other thing I want to change is the screen's background color.

15
00:01:02,160 --> 00:01:06,570
So there's a method called bgcolor and inside here

16
00:01:06,570 --> 00:01:11,310
we're going to write the text black so that we end up with a black screen.

17
00:01:11,880 --> 00:01:12,300
Now,

18
00:01:12,300 --> 00:01:17,300
another thing that you might not have seen before is how to set the title of

19
00:01:17,340 --> 00:01:21,600
the window that shows up. And we do that through a method called title,

20
00:01:21,870 --> 00:01:26,430
and you can name your program here. So I'm going to call it 'My Snake Game',

21
00:01:26,790 --> 00:01:30,870
but you can of course call it whatever it is you want. Now finally,

22
00:01:30,870 --> 00:01:35,550
at the very bottom of the file, I'm going to say screen.exitonclick.

23
00:01:35,790 --> 00:01:40,050
So this way, when I actually run it, it doesn't just disappear straight away

24
00:01:40,770 --> 00:01:44,640
and it waits for me and I can look at what's actually happened so far.

25
00:01:45,090 --> 00:01:49,230
So I'm going to click run and then select my main file.

26
00:01:49,680 --> 00:01:51,000
And once it pops up,

27
00:01:51,030 --> 00:01:56,030
this is what you should see, a 600 by 600 square window

28
00:01:56,520 --> 00:01:59,880
and then at the top of the window, you'll see the name of your program,

29
00:01:59,910 --> 00:02:04,620
which is My Snake Game. And the background is now black.

30
00:02:05,640 --> 00:02:07,500
Now that we've set up our screen,

31
00:02:07,530 --> 00:02:11,039
we're ready to tackle the first of our seven steps,

32
00:02:11,340 --> 00:02:13,500
which is to create a snake body.

33
00:02:14,160 --> 00:02:17,520
And we're going to create three squares

34
00:02:17,610 --> 00:02:19,590
which are each going to be turtles,

35
00:02:19,830 --> 00:02:24,090
and they're going to be lined up next to each other along the horizontal axis.

36
00:02:24,210 --> 00:02:28,020
So let's think about this. When we create a new turtle,

37
00:02:28,290 --> 00:02:33,290
it has a dimension of 20 pixels wide by 20 pixels tall.

38
00:02:34,530 --> 00:02:38,100
And if we want three of them lined up next to each other,

39
00:02:38,370 --> 00:02:43,050
then we have to think about how we create their coordinates. As a challenge,

40
00:02:43,170 --> 00:02:46,260
I want you to think about how we can create this. Here

41
00:02:46,260 --> 00:02:48,990
we have three separate squares

42
00:02:49,050 --> 00:02:54,050
which are each a turtle object and they're shaped like a square and they have a

43
00:02:54,330 --> 00:02:57,420
white color. Now, they're positioned

44
00:02:57,450 --> 00:03:00,130
so that the first one is (0, 0),

45
00:03:00,490 --> 00:03:04,810
the next one is 20 pixels to the left and the next one is again

46
00:03:04,870 --> 00:03:08,920
20 pixels to the left so that none of the squares overlap with each other,

47
00:03:09,100 --> 00:03:12,940
but there's also no gaps. So it looks like a whole snake.

48
00:03:13,510 --> 00:03:14,980
This is your challenge

49
00:03:15,160 --> 00:03:18,820
and you might have to think about a few of the things that we've done before in

50
00:03:18,820 --> 00:03:22,450
order to achieve this. So browse the documentation,

51
00:03:22,810 --> 00:03:26,650
look back to the previous code that you've written and see if you can solve this

52
00:03:26,650 --> 00:03:28,240
challenge. Pause the video now.

53
00:03:32,590 --> 00:03:32,980
Alright.

54
00:03:32,980 --> 00:03:37,060
So the first thing we're going to need to do is to create some sort of turtle,

55
00:03:37,060 --> 00:03:37,660
right?

56
00:03:37,660 --> 00:03:42,660
So let's call this segment_1 and this is going to be a new turtle.

57
00:03:43,150 --> 00:03:45,880
And remember that when we initialize the turtle,

58
00:03:46,090 --> 00:03:48,310
we can already specify the shape.

59
00:03:48,580 --> 00:03:51,850
So we can either say shape = "square",

60
00:03:51,940 --> 00:03:56,740
or simply just leave it as square. So now at this point,

61
00:03:56,740 --> 00:04:01,150
we should end up with a square turtle, but because it's black,

62
00:04:01,180 --> 00:04:04,630
when we run the code, we won't actually see it on screen.

63
00:04:05,140 --> 00:04:09,970
So let's change this segment color to white.

64
00:04:10,570 --> 00:04:14,920
And all we have to do is write the word for the color

65
00:04:15,370 --> 00:04:18,279
and now when we rerun our code,

66
00:04:18,339 --> 00:04:22,330
you can see a white square at the center,

67
00:04:22,360 --> 00:04:24,790
which is at position (0, 0).

68
00:04:25,690 --> 00:04:30,690
Now the next step is to create our other two white squares.

69
00:04:32,290 --> 00:04:35,560
So let's go ahead and copy these bits of code,

70
00:04:36,070 --> 00:04:39,340
and I'm going to create three of them in total.

71
00:04:39,340 --> 00:04:43,300
So segment_2 and segment_3.

72
00:04:45,130 --> 00:04:47,860
Now here, we're going to create the same thing,

73
00:04:48,040 --> 00:04:52,600
but we obviously have to change the positioning because otherwise we end up with

74
00:04:52,630 --> 00:04:55,000
three squares all on top of each other,

75
00:04:55,030 --> 00:04:58,960
and we can only see one square because this is a 2-D image.

76
00:04:59,530 --> 00:05:04,530
So let's go ahead and make segment_2 go to a different position.

77
00:05:05,320 --> 00:05:09,070
So the first thing we have to specify is the X,

78
00:05:09,220 --> 00:05:12,790
which you remember is the horizontal axis here.

79
00:05:13,030 --> 00:05:15,550
And if we want another square to the left of it,

80
00:05:15,970 --> 00:05:20,970
then we're going to have to go to -20 from zero.

81
00:05:21,550 --> 00:05:25,240
That way it will be shifted to the left by 20 pixels.

82
00:05:25,840 --> 00:05:28,750
Let's say go to -20

83
00:05:29,110 --> 00:05:32,650
and then the Y-axis is going to be zero because it's not going to move up or

84
00:05:32,650 --> 00:05:33,483
down.

85
00:05:33,640 --> 00:05:38,200
Now let's take a look at the result of this and you can see we've now got two

86
00:05:38,200 --> 00:05:40,210
squares lining side by side,

87
00:05:40,540 --> 00:05:43,990
but the third square is still at the center.

88
00:05:44,620 --> 00:05:49,030
So the third segment is going to be shifted a further 20 pixels.

89
00:05:49,390 --> 00:05:54,310
This is centered on (0, 0), this is at (-20, 0),

90
00:05:54,610 --> 00:05:57,100
then this has to be at (-40, 0).

91
00:05:57,350 --> 00:06:01,700
So let's tell our segment_3 to go to (-40, 0).

92
00:06:02,150 --> 00:06:03,800
And this is what we see.

93
00:06:04,190 --> 00:06:08,810
Did you manage to achieve this or did you do it in a different way? Now,

94
00:06:08,900 --> 00:06:13,700
a much easier way of creating this would have been to use a for loop.

95
00:06:14,090 --> 00:06:14,990
For example, we

96
00:06:15,250 --> 00:06:17,340
could have created a list of

97
00:06:17,480 --> 00:06:18,940
starting positions

98
00:06:20,860 --> 00:06:24,370
and this could contain some tuples.

99
00:06:24,850 --> 00:06:29,850
So we know that in order to specify where the segment should go to,

100
00:06:30,280 --> 00:06:34,240
we have to provide a tuple, which is a X and a Y value.

101
00:06:34,840 --> 00:06:39,220
And we know that we can create a tuple just by using a set of parentheses and

102
00:06:39,220 --> 00:06:42,130
then adding values in there, separated by commas.

103
00:06:42,610 --> 00:06:46,990
So if the first segment has to be at the center, then that's going to be 

104
00:06:46,990 --> 00:06:51,910
(0, 0), and then the second piece is going to be at (-20, 0),

105
00:06:52,510 --> 00:06:55,720
and the third piece is going to be at (-40, 0).

106
00:06:56,410 --> 00:06:57,780
Now we can create

107
00:06:57,940 --> 00:07:00,100
a for loop. So for

108
00:07:00,370 --> 00:07:03,040
position in starting_positions,

109
00:07:03,490 --> 00:07:08,490
let's loop through this list and create a new segment for each of those

110
00:07:09,490 --> 00:07:13,870
positions. So now we're going to create a new segment

111
00:07:13,870 --> 00:07:16,360
that's a square turtle, that's going to be white,

112
00:07:16,750 --> 00:07:21,750
and we're going to get the segment to go to the position that we're currently

113
00:07:21,940 --> 00:07:25,570
looping on. Now we can delete all of that code

114
00:07:25,900 --> 00:07:30,370
and when we run our code again, you can see we have the same effect.

115
00:07:30,400 --> 00:07:35,380
We have all three pieces. So if you can't remember how tuples work,

116
00:07:35,620 --> 00:07:38,890
be sure to go back to the lessons on tuples in Day 18,

117
00:07:39,220 --> 00:07:42,040
where we talked about how to write them and how they work.

118
00:07:42,760 --> 00:07:45,070
But that completes our first step.

119
00:07:45,280 --> 00:07:50,280
We've managed to create three segments that will comprise our snake

120
00:07:50,650 --> 00:07:51,910
and in the next lesson,

121
00:07:51,940 --> 00:07:55,690
we're going to figure out how to get our snake to start moving by itself on the

122
00:07:55,690 --> 00:07:59,380
screen. So for all of that and more, I'll see you on the next lesson.

