Create the Snake

Dark green cells make the snake (23:54)

Let’s create the snake. To represent the snake we have to draw some of the cells of the screen dark green.

The body of the snake is going to be a just a Python list of dark cells.

To start, let’s make the snake three cells long. In our code, the snake will be represented as a list of three cells.

snake image

But we won’t store the cells in the list, only their coordinates. The easiest way to do it is to use a Vector2 object to store the x and y coordinates of each cell of the snake body. The first item in the list is going to be the “head” (6,9) of the snake and the remaining ones, the tail (5,9) and (4,9).

First of all let’s create a Snake class and the init function:

class Snake:
    def __init__(self): 

As we said the body of the snake is going to be a list of 3 Vector2 objects. So we type three empty Vector2() list items:

class Snake:
    def __init__(self): 
        self.body = [Vector2(), Vector2(), Vector2()] 

Now, we have to enter some coordinates for the snake body. Let’s enter 6,9 for the head and 5,9 and 4,9 for the tail.

class Snake:
    def __init__(self): 
        self.body = [Vector2(6,9), Vector2(5,9), Vector2(4,9)] 

That’s it, our representation of the snake in code is ready. Now let’s draw the snake on the screen.

Let’s create a draw() method which will be responsible for drawing the snake body.

def draw(self): 

Draw three Segments for Snake to Begin (25:21)

We need to draw each segment of the snake body.

Our snake has 3 segments, 3 cells to be drawn so we are going to use a for loop to draw them all.

def draw(self):
    for segment in self.body: 

Now as we did before in the food class we will simply draw a square for each segment.

Let’s create a rectangle for each segment.

We need the x and y coordinate, the width and height of the rect:

def draw(self):
    for segment in self.body: 
    segment_rect = (segment.x * cell_size, segment.y * cell_size, cell_size, cell_size) 

Now we need to draw this rect on the display surface and give it a DARK_GREEN color.

def draw(self):
    for segment in self.body: 
    segment_rect = (segment.x * cell_size, segment.y * cell_size, cell_size, cell_size) 
    pygame.draw.rect(screen, DARK_GREEN, segment_rect) 

Create Snake Object (26:23)

That's it. Now let’s create a Snake object and call its draw method.

The snake object goes above the while True: game loop, after food = Food() code

Find this:

food = Food() #return here and type Snake object
food_surface = pygame.image.load("graphics/food.png")

while True: 
    for event in pygame.event.get():

After adding the snake object it should look like this:

food = Food()
snake = Snake() 
food_surface = pygame.image.load("graphics/food.png")

while True: 
    for event in pygame.event.get():

Next you will need to type snake.draw() in the game loop after

food.draw():

#Drawing
screen.fill(GREEN)
food.draw()
snake.draw()

Let’s now run the game to see our snake for the first time!

The snake is visible! Each segment of the snake’s body is a square, so the snake appears like a rectangle.

Make Snake Cells Rounded Rectangle (26:51)

Let’s make each segment of the snake body a rounded rectangle to be more pleasing to the eye and to be able to see the discrete segments of its body. So, in the draw method of the snake class, we change the draw line here like this.

We add, 0, 7. The 0, is that we want the rectangle to be filled with color, and the value 7 is the border radius of the rectangle. If we now run the game once more, we can see, our snake has rounded segments!

We can move on to the next step which is to make our snake move.