How to Code a Snake and Turtle Game in Python?

Estimated read time 3 min read

To code a Snake and Turtle game in Python, you can use the Turtle graphics library. Turtle provides a simple way to create graphics and animations. Here’s an example of how you can implement a basic Snake and Turtle game:

import turtle
import time
import random

delay = 0.1

# Set up the screen
window = turtle.Screen()
window.title("Snake and Turtle Game")
window.bgcolor("black")
window.setup(width=600, height=600)
window.tracer(0)

# Snake head
snake_head = turtle.Turtle()
snake_head.speed(0)
snake_head.shape("square")
snake_head.color("white")
snake_head.penup()
snake_head.goto(0, 0)
snake_head.direction = "Stop"

# Turtle food
food = turtle.Turtle()
food.speed(0)
food.shape("circle")
food.color("red")
food.penup()
food.goto(0, 100)

segments = []

# Functions
def go_up():
    if snake_head.direction != "down":
        snake_head.direction = "up"

def go_down():
    if snake_head.direction != "up":
        snake_head.direction = "down"

def go_left():
    if snake_head.direction != "right":
        snake_head.direction = "left"

def go_right():
    if snake_head.direction != "left":
        snake_head.direction = "right"

def move():
    if snake_head.direction == "up":
        y = snake_head.ycor()
        snake_head.sety(y + 20)

    if snake_head.direction == "down":
        y = snake_head.ycor()
        snake_head.sety(y - 20)

    if snake_head.direction == "left":
        x = snake_head.xcor()
        snake_head.setx(x - 20)

    if snake_head.direction == "right":
        x = snake_head.xcor()
        snake_head.setx(x + 20)

# Keyboard bindings
window.listen()
window.onkeypress(go_up, "w")
window.onkeypress(go_down, "s")
window.onkeypress(go_left, "a")
window.onkeypress(go_right, "d")

# Main game loop
while True:
    window.update()

    # Check for collision with the border
    if (
        snake_head.xcor() > 290
        or snake_head.xcor() < -290
        or snake_head.ycor() > 290
        or snake_head.ycor() < -290
    ):
        time.sleep(1)
        snake_head.goto(0, 0)
        snake_head.direction = "Stop"

        for segment in segments:
            segment.goto(1000, 1000)

        segments.clear()

    # Check for collision with the food
    if snake_head.distance(food) < 20:
        x = random.randint(-270, 270)
        y = random.randint(-270, 270)
        food.goto(x, y)

        new_segment = turtle.Turtle()
        new_segment.speed(0)
        new_segment.shape("square")
        new_segment.color("grey")
        new_segment.penup()
        segments.append(new_segment)

    # Move the end segments first
    for i in range(len(segments) - 1, 0, -1):
        x = segments[i - 1].xcor()
        y = segments[i - 1].ycor()
        segments[i].goto(x, y)

    # Move segment 0 to where the snake head is
    if len(segments) > 0:
        x = snake_head.xcor()
        y = snake_head.ycor()
        segments[0].goto(x, y)

    move()

    # Check for head collision with the body
    for segment in segments:
        if segment.distance(snake_head) < 20:
            time.sleep(1)
            snake_head.goto(0, 0)
            snake_head.direction = "Stop"

            for segment in segments:
                segment.goto(1000, 1000)

            segments.clear()

    time.sleep(delay)

window.mainloop()

This code sets up a basic Snake and Turtle game using the Turtle graphics library. The snake moves around the screen and eats the turtle food, increasing its length. The game ends if the snake hits the screen border or collides with its own body.

You can further enhance the game by adding features such as score tracking, levels, and more complex gameplay mechanics. Experiment with the code and add your own customizations to create an enjoyable game experience.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply