So to start off,
go ahead and create a new project called snake_game in PyCharm.
And then inside this project, we're going to create a new main.py file.
And inside our file, we're going to need our normal imports.
So I'm going to say from turtle import the screen class and also the turtle
class. Now,
the first thing we're going to do is we're going to create a new screen,
which is basically going to be the window that shows up.
And then we're going to set up our screen so that it is 600 by 600.
So the width is going to be 600 pixels and the height is also going to be 600
pixels. And notice how I'm using these keyword arguments
so that it's a little bit clearer to the next person looking at this code
what's actually happening here. Now,
the other thing I want to change is the screen's background color.
So there's a method called bgcolor and inside here
we're going to write the text black so that we end up with a black screen.
Now,
another thing that you might not have seen before is how to set the title of
the window that shows up. And we do that through a method called title,
and you can name your program here. So I'm going to call it 'My Snake Game',
but you can of course call it whatever it is you want. Now finally,
at the very bottom of the file, I'm going to say screen.exitonclick.
So this way, when I actually run it, it doesn't just disappear straight away
and it waits for me and I can look at what's actually happened so far.
So I'm going to click run and then select my main file.
And once it pops up,
this is what you should see, a 600 by 600 square window
and then at the top of the window, you'll see the name of your program,
which is My Snake Game. And the background is now black.
Now that we've set up our screen,
we're ready to tackle the first of our seven steps,
which is to create a snake body.
And we're going to create three squares
which are each going to be turtles,
and they're going to be lined up next to each other along the horizontal axis.
So let's think about this. When we create a new turtle,
it has a dimension of 20 pixels wide by 20 pixels tall.
And if we want three of them lined up next to each other,
then we have to think about how we create their coordinates. As a challenge,
I want you to think about how we can create this. Here
we have three separate squares
which are each a turtle object and they're shaped like a square and they have a
white color. Now, they're positioned
so that the first one is (0, 0),
the next one is 20 pixels to the left and the next one is again
20 pixels to the left so that none of the squares overlap with each other,
but there's also no gaps. So it looks like a whole snake.
This is your challenge
and you might have to think about a few of the things that we've done before in
order to achieve this. So browse the documentation,
look back to the previous code that you've written and see if you can solve this
challenge. Pause the video now.
Alright.
So the first thing we're going to need to do is to create some sort of turtle,
right?
So let's call this segment_1 and this is going to be a new turtle.
And remember that when we initialize the turtle,
we can already specify the shape.
So we can either say shape = "square",
or simply just leave it as square. So now at this point,
we should end up with a square turtle, but because it's black,
when we run the code, we won't actually see it on screen.
So let's change this segment color to white.
And all we have to do is write the word for the color
and now when we rerun our code,
you can see a white square at the center,
which is at position (0, 0).
Now the next step is to create our other two white squares.
So let's go ahead and copy these bits of code,
and I'm going to create three of them in total.
So segment_2 and segment_3.
Now here, we're going to create the same thing,
but we obviously have to change the positioning because otherwise we end up with
three squares all on top of each other,
and we can only see one square because this is a 2-D image.
So let's go ahead and make segment_2 go to a different position.
So the first thing we have to specify is the X,
which you remember is the horizontal axis here.
And if we want another square to the left of it,
then we're going to have to go to -20 from zero.
That way it will be shifted to the left by 20 pixels.
Let's say go to -20
and then the Y-axis is going to be zero because it's not going to move up or
down.
Now let's take a look at the result of this and you can see we've now got two
squares lining side by side,
but the third square is still at the center.
So the third segment is going to be shifted a further 20 pixels.
This is centered on (0, 0), this is at (-20, 0),
then this has to be at (-40, 0).
So let's tell our segment_3 to go to (-40, 0).
And this is what we see.
Did you manage to achieve this or did you do it in a different way? Now,
a much easier way of creating this would have been to use a for loop.
For example, we
could have created a list of
starting positions
and this could contain some tuples.
So we know that in order to specify where the segment should go to,
we have to provide a tuple, which is a X and a Y value.
And we know that we can create a tuple just by using a set of parentheses and
then adding values in there, separated by commas.
So if the first segment has to be at the center, then that's going to be
(0, 0), and then the second piece is going to be at (-20, 0),
and the third piece is going to be at (-40, 0).
Now we can create
a for loop. So for
position in starting_positions,
let's loop through this list and create a new segment for each of those
positions. So now we're going to create a new segment
that's a square turtle, that's going to be white,
and we're going to get the segment to go to the position that we're currently
looping on. Now we can delete all of that code
and when we run our code again, you can see we have the same effect.
We have all three pieces. So if you can't remember how tuples work,
be sure to go back to the lessons on tuples in Day 18,
where we talked about how to write them and how they work.
But that completes our first step.
We've managed to create three segments that will comprise our snake
and in the next lesson,
we're going to figure out how to get our snake to start moving by itself on the
screen. So for all of that and more, I'll see you on the next lesson.