100 Days of Code: The Complete Python Pro Bootcamp for 2022 - Day 20 (Snake Game Part 1)
Published: 2022-06-07 05:11:40

- Create A Snake Game

**_Day 1 Process_**
1. Create a snake
2. Move the snake
3. Create a snake class and move to OOP
3. Control the snake

**_Day 2 Process_**
4. Detect collision with food
5. Create scoreboard
6. Detect collision with wall
7. Detect collision with tail

- 1. Create a snake
````python
from turtle import Turtle, Screen

# Screen setup
screen = Screen()
screen.setup(width=600, height=600)
screen.bgcolor("black")
screen.title("SnakeGame")


# Variables
starting_positions = [(0, 0), (-20, 0), (-40, 0)]


# Snake class and objects
for position in starting_positions:
    new_snake = Turtle(shape="square")
    new_snake.color("white")
    new_snake.goto(position)


screen.exitonclick()
```
- 2. Move the snake

Add Import time,
Add screen.tracer,
Add game_is_on = True,
Add while game_is_on loop,
Add for loop for position,
Add screen.update and time.sleep,

````python
from turtle import Turtle, Screen
import time

# Screen setup
screen = Screen()
screen.setup(width=600, height=600)
screen.bgcolor("black")
screen.title("SnakeGame")
screen.tracer(0)

# Variables
starting_positions = [(0, 0), (-20, 0), (-40, 0)]
all_snake = []
game_is_on = True

# Snake class and objects
for position in starting_positions:
    new_snake = Turtle(shape="square")
    new_snake.color("white")
    new_snake.penup()
    new_snake.goto(position)
    all_snake.append(new_snake)

while game_is_on:
    screen.update()
    time.sleep(0.1)

    for snake_num in range(len(all_snake) - 1, 0, -1):
        new_x = all_snake[snake_num - 1].xcor()
        new_y = all_snake[snake_num - 1].ycor()
        all_snake[snake_num].goto(new_x, new_y)
    all_snake[0].forward(20)
    all_snake[0].left(90)

screen.exitonclick()
```
- 3. Create a snake class and move to OOP

3.1 Main.py
````python
from turtle import Screen
from snake import Snake
import time

# Screen setup
screen = Screen()
screen.setup(width=600, height=600)
screen.bgcolor("black")
screen.title("SnakeGame")
screen.tracer(0)

snake = Snake()

game_is_on = True
while game_is_on:
    screen.update()
    time.sleep(0.1)

    snake.move()

screen.exitonclick()
```
3.2 snake.py
````python
from turtle import Turtle


STARTING_POSITIONS = [(0, 0), (-20, 0), (-40, 0)]
MOVE_DISTANCE = 20


class Snake:

    def __init__(self):
        self.all_snake = []
        self.create_snake()

    def create_snake(self):
        for position in STARTING_POSITIONS:
            new_snake = Turtle("square")
            new_snake.color("white")
            new_snake.penup()
            new_snake.goto(position)
            self.all_snake.append(new_snake)

    def move(self):
        for seg_num in range(len(self.all_snake) - 1, 0, -1):
            new_x = self.all_snake[seg_num - 1].xcor()
            new_y = self.all_snake[seg_num - 1].ycor()
            self.all_snake[seg_num].goto(new_x, new_y)
        self.all_snake[0].forward(MOVE_DISTANCE)
```
- 4. Control the snake

4.1 main.py - Add screen.listen and screen.onkey to use the kb arrow keys

````python
from turtle import Screen
from snake import Snake
import time

# Screen setup
screen = Screen()
screen.setup(width=600, height=600)
screen.bgcolor("black")
screen.title("SnakeGame")
screen.tracer(0)

snake = Snake()

screen.listen()
screen.onkey(snake.up, "Up")
screen.onkey(snake.down, "Down")
screen.onkey(snake.left, "Left")
screen.onkey(snake.right, "Right")

game_is_on = True
while game_is_on:
    screen.update()
    time.sleep(0.1)

    snake.move()

screen.exitonclick()
```
4.2 snake.py 
Add Global variables UP,DOWN,LEFT,RIGHT
Add self.head for heading
Under def move change to self.head.forward(MOVE_DISTANCE)
Add def up, down, left, right
Add if statements to disallow back tracking (double back on the same line opposite direction)
````python
from turtle import Turtle


STARTING_POSITIONS = [(0, 0), (-20, 0), (-40, 0)]
MOVE_DISTANCE = 20
UP = 90
DOWN = 270
LEFT = 180
RIGHT = 0


class Snake:

    def __init__(self):
        self.all_snake = []
        self.create_snake()
        self.head = self.all_snake[0]

    def create_snake(self):
        for position in STARTING_POSITIONS:
            new_snake = Turtle("square")
            new_snake.color("white")
            new_snake.penup()
            new_snake.goto(position)
            self.all_snake.append(new_snake)

    def move(self):
        for seg_num in range(len(self.all_snake) - 1, 0, -1):
            new_x = self.all_snake[seg_num - 1].xcor()
            new_y = self.all_snake[seg_num - 1].ycor()
            self.all_snake[seg_num].goto(new_x, new_y)
        self.head.forward(MOVE_DISTANCE)

    def up(self):
        if self.head.heading() != DOWN:
            self.head.setheading(UP)

    def down(self):
        if self.head.heading() != UP:
            self.head.setheading(DOWN)

    def left(self):
        if self.head.heading() != RIGHT:
            self.head.setheading(LEFT)

    def right(self):
        if self.head.heading() != LEFT:
            self.head.setheading(RIGHT)
```