The Snake Game is a classic arcade game where the player manoeuvres a line (the snake) that grows in length with each piece of food eaten. The player loses if the snake runs into the screen border or itself. This project implements the Snake Game using Python’s turtle graphics library, which provides a simple and flexible way to draw shapes and create animations.
To run this project on your machine, ensure you have Python installed. Follow these steps:
import turtle
import random
w = 500
h = 500
fs = 10
d = 100 # milliseconds
offsets = {
"up": (0, 20),
"down": (0, -20),
"left": (-20, 0),
"right": (20, 0)
}
def r():
global saap, kata, khanaT, pen
saap = [[0, 0], [0, 20], [0, 40], [0, 60], [0, 80]]
kata = "up"
khanaT = nun()
food.goto(khanaT)
hall()
def hall():
global kata
new_head = saap[-1].copy()
new_head[0] = saap[-1][0] + offsets[kata][0]
new_head[1] = saap[-1][1] + offsets[kata][1]
if new_head in saap[:-1]:
r()
else:
saap.append(new_head)
if not khana():
saap.pop(0)
if saap[-1][0] > w / 2:
saap[-1][0] -= w
elif saap[-1][0] < - w / 2:
saap[-1][0] += w
elif saap[-1][1] > h / 2:
saap[-1][1] -= h
elif saap[-1][1] < -h / 2:
saap[-1][1] += h
pen.clearstamps()
#clears all the stamps
for segment in saap:
pen.goto(segment[0], segment[1])
pen.stamp()
screen.update()
#updates the turtle.screen screen
turtle.ontimer(hall, d)
def khana():
global khanaT
if dist(saap[-1], khanaT) < 20:
khanaT = nun()
food.goto(khanaT)
return True
return False
def nun():
x = random.randint(- w / 2 + fs, w / 2 - fs)
y = random.randint(- h / 2 + fs, h / 2 - fs)
return (x, y)
def dist(poos1, poos2):
x1, y1 = poos1
x2, y2 = poos2
distance = ((y2 - y1) * 2 + (x2 - x1) * 2) ** 0.5
return distance
def mathi():
global kata
if kata != "down":
kata = "up"
def go_right():
global kata
if kata != "left":
kata = "right"
def go_down():
global kata
if kata != "up":
kata = "down"
def go_left():
global kata
if kata != "right":
kata = "left"
screen = turtle.Screen()
screen.setup(w, h)
screen.title("saap")
screen.bgcolor("green")
screen.setup(500, 500)
screen.tracer(0)
pen = turtle.Turtle("square")
pen.penup()
food = turtle.Turtle()
food.shape("circle")
food.color("white")
food.shapesize(fs / 20)
food.penup()
screen.listen()
screen.onkey(mathi, "Up")
screen.onkey(go_right, "Right")
screen.onkey(go_down, "Down")
screen.onkey(go_left, "Left")
r()
turtle.done()
import turtle
import random
w = 500
h = 500
fs = 10
d = 100 # milliseconds
offsets = {
"up": (0, 20),
"down": (0, -20),
"left": (-20, 0),
"right": (20, 0)
}
def r():
global saap, kata, khanaT, pen
saap = [[0, 0], [0, 20], [0, 40], [0, 60], [0, 80]]
kata = "up"
khanaT = nun()
food.goto(khanaT)
hall()
def hall():
global kata
new_head = saap[-1].copy()
new_head[0] = saap[-1][0] + offsets[kata][0]
new_head[1] = saap[-1][1] + offsets[kata][1]
if new_head in saap[:-1]:
r()
else:
saap.append(new_head)
if not khana():
saap.pop(0)
if saap[-1][0] > w / 2:
saap[-1][0] -= w
elif saap[-1][0] < - w / 2:
saap[-1][0] += w
elif saap[-1][1] > h / 2:
saap[-1][1] -= h
elif saap[-1][1] < -h / 2:
saap[-1][1] += h
pen.clearstamps()
#clears all the stamps
for segment in saap:
pen.goto(segment[0], segment[1])
pen.stamp()
screen.update()
#updates the turtle.screen screen
turtle.ontimer(hall, d)
def khana():
global khanaT
if dist(saap[-1], khanaT) < 20:
khanaT = nun()
food.goto(khanaT)
return True
return False
def nun():
x = random.randint(- w / 2 + fs, w / 2 - fs)
y = random.randint(- h / 2 + fs, h / 2 - fs)
return (x, y)
def dist(poos1, poos2):
x1, y1 = poos1
x2, y2 = poos2
distance = ((y2 - y1) * 2 + (x2 - x1) * 2) ** 0.5
return distance
def mathi():
global kata
if kata != "down":
kata = "up"
def go_right():
global kata
if kata != "left":
kata = "right"
def go_down():
global kata
if kata != "up":
kata = "down"
def go_left():
global kata
if kata != "right":
kata = "left"
screen = turtle.Screen()
screen.setup(w, h)
screen.title("saap")
screen.bgcolor("green")
screen.setup(500, 500)
screen.tracer(0)
pen = turtle.Turtle("square")
pen.penup()
food = turtle.Turtle()
food.shape("circle")
food.color("white")
food.shapesize(fs / 20)
food.penup()
screen.listen()
screen.onkey(mathi, "Up")
screen.onkey(go_right, "Right")
screen.onkey(go_down, "Down")
screen.onkey(go_left, "Left")
r()
turtle.done()
Initialize screen with dimensions and settings
Initialize snake starting position and direction
Create food object
Function to reset the game:
Set initial snake position
Set initial direction
Place food at random position
Start the main loop
Main loop function:
Calculate new head position based on current direction
Check if the new head position collides with the snake's body:
If collision occurs, reset the game
Otherwise:
Add new head position to the snake
If the snake eats food:
Generate new food position
Else:
Remove the tail segment of the snake
If the snake goes out of bounds, wrap it to the opposite side
Clear the screen and redraw the snake
Set a timer to repeat the main loop
Function to check if the snake eats food:
Calculate the distance between snake's head and food
If distance is less than threshold:
Return true
Else:
Return false
Function to generate new food position:
Choose random x and y within screen bounds
Return position
Function to calculate distance between two points:
Use the Pythagorean theorem to calculate the distance
Return distance
Functions to change snake direction based on user input:
Change direction if the new direction is not opposite to the current direction
Initialize the game
Initialize screen with dimensions and settings
Initialize snake starting position and direction
Create food object
Function to reset the game:
Set initial snake position
Set initial direction
Place food at random position
Start the main loop
Main loop function:
Calculate new head position based on current direction
Check if the new head position collides with the snake's body:
If collision occurs, reset the game
Otherwise:
Add new head position to the snake
If the snake eats food:
Generate new food position
Else:
Remove the tail segment of the snake
If the snake goes out of bounds, wrap it to the opposite side
Clear the screen and redraw the snake
Set a timer to repeat the main loop
Function to check if the snake eats food:
Calculate the distance between snake's head and food
If distance is less than threshold:
Return true
Else:
Return false
Function to generate new food position:
Choose random x and y within screen bounds
Return position
Function to calculate distance between two points:
Use the Pythagorean theorem to calculate the distance
Return distance
Functions to change snake direction based on user input:
Change direction if the new direction is not opposite to the current direction
Initialize the game
Why is my snake not moving?
Office:- 660, Sector 14A, Vasundhara, Ghaziabad, Uttar Pradesh - 201012, India