Snake Game using Turtle in Python โ
Source Code ๐๐
# import required modules
import turtle
import time
import random
delay = 0.1
score = 0
high_score = 0
# Creating a window screen
wn = turtle.Screen()
wn.title("Snake Game")
wn.bgcolor("blue")
# the width and height can be put as user's choice
wn.setup(width=600, height=600)
wn.tracer(0)
# head of the snake
head = turtle.Turtle()
head.shape("square")
head.color("white")
head.penup()
head.goto(0, 0)
head.direction = "Stop"
# food in the game
food = turtle.Turtle()
colors = random.choice(['red', 'green', 'black'])
shapes = random.choice(['square', 'triangle', 'circle'])
food.speed(0)
food.shape(shapes)
food.color(colors)
food.penup()
food.goto(0, 100)
pen = turtle.Turtle()
pen.speed(0)
pen.shape("square")
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 250)
pen.write("Score : 0ย High Score : 0", align="center",
ย ย ย ย ย ย ย ย ย font=("candara", 24, "bold"))
# assigning key directions
def group():
ย ย ย if head.direction != "down":
ย ย ย ย ย ย ย head.direction = "up"
def godown():
ย ย ย if head.direction != "up":
ย ย ย ย ย ย ย head.direction = "down"
def goleft():
ย ย ย if head.direction != "right":
ย ย ย ย ย ย ย head.direction = "left"
def goright():
ย ย ย if head.direction != "left":
ย ย ย ย ย ย ย head.direction = "right"
def move():
ย ย ย if head.direction == "up":
ย ย ย ย ย ย ย y = head.ycor()
ย ย ย ย ย ย ย head.sety(y+20)
ย ย ย if head.direction == "down":
ย ย ย ย ย ย ย y = head.ycor()
ย ย ย ย ย ย ย head.sety(y-20)
ย ย ย if head.direction == "left":
ย ย ย ย ย ย ย x = head.xcor()
ย ย ย ย ย ย ย head.setx(x-20)
ย ย ย if head.direction == "right":
ย ย ย ย ย ย ย x = head.xcor()
ย ย ย ย ย ย ย head.setx(x+20)
wn.listen()
wn.onkeypress(group, "w")
wn.onkeypress(godown, "s")
wn.onkeypress(goleft, "a")
wn.onkeypress(goright, "d")
segments = []
# By -
@pythonnotes1# Main Gameplay
while True:
ย ย ย wn.update()
ย ย ย if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290:
ย ย ย ย ย ย ย time.sleep(1)
ย ย ย ย ย ย ย head.goto(0, 0)
ย ย ย ย ย ย ย head.direction = "Stop"
ย ย ย ย ย ย ย colors = random.choice(['red', 'blue', 'green'])
ย ย ย ย ย ย ย shapes = random.choice(['square', 'circle'])
ย ย ย ย ย ย ย for segment in segments:
ย ย ย ย ย ย ย ย ย ย ย segment.goto(1000, 1000)
ย ย ย ย ย ย ย segments.clear()
ย ย ย ย ย ย ย score = 0
ย ย ย ย ย ย ย delay = 0.1
ย ย ย ย ย ย ย pen.clear()
ย ย ย ย ย ย ย pen.write("Score : {} High Score : {} ".format(
ย ย ย ย ย ย ย ย ย ย ย score, high_score), align="center", font=("candara", 24, "bold"))
ย ย ย if head.distance(food) < 20:
ย ย ย ย ย ย ย x = random.randint(-270, 270)
ย ย ย ย ย ย ย y = random.randint(-270, 270)
ย ย ย ย ย ย ย food.goto(x, y)
ย ย ย ย ย ย ย # Adding segment
ย ย ย ย ย ย ย new_segment = turtle.Turtle()
ย ย ย ย ย ย ย new_segment.speed(0)
ย ย ย ย ย ย ย new_segment.shape("square")
ย ย ย ย ย ย ย new_segment.color("orange")ย # tail colour
ย ย ย ย ย ย ย new_segment.penup()
ย ย ย ย ย ย ย segments.append(new_segment)
ย ย ย ย ย ย ย delay -= 0.001
ย ย ย ย ย ย ย score += 10
ย ย ย ย ย ย ย if score > high_score:
ย ย ย ย ย ย ย ย ย ย ย high_score = score
ย ย ย ย ย ย ย pen.clear()
ย ย ย ย ย ย ย pen.write("Score : {} High Score : {} ".format(
ย ย ย ย ย ย ย ย ย ย ย score, high_score), align="center", font=("candara", 24, "bold"))
ย ย ย # Checking for head collisions with body segments
ย ย ย for index in range(len(segments)-1, 0, -1):
ย ย ย ย ย ย ย x = segments[index-1].xcor()
ย ย ย ย ย ย ย y = segments[index-1].ycor()
ย ย ย ย ย ย ย segments[index].goto(x, y)
ย ย ย if len(segments) > 0:
ย ย ย ย ย ย ย x = head.xcor()
ย ย ย ย ย ย ย y = head.ycor()
ย ย ย ย ย ย ย segments[0].goto(x, y)
ย ย ย move()
ย ย ย for segment in segments:
ย ย ย ย ย ย ย if segment.distance(head) < 20:
ย ย ย ย ย ย ย ย ย ย ย time.sleep(1)
ย ย ย ย ย ย ย ย ย ย ย head.goto(0, 0)
ย ย ย ย ย ย ย ย ย ย ย head.direction = "stop"
ย ย ย ย ย ย ย ย ย ย ย colors = random.choice(['red', 'blue', 'green'])
ย ย ย ย ย ย ย ย ย ย ย shapes = random.choice(['square', 'circle'])
ย ย ย ย ย ย ย ย ย ย ย for segment in segments:
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย segment.goto(1000, 1000)
ย ย ย ย ย ย ย ย ย ย ย segments.clear()
ย ย ย ย ย ย ย ย ย ย ย score = 0
ย ย ย ย ย ย ย ย ย ย ย delay = 0.1
ย ย ย ย ย ย ย ย ย ย ย pen.clear()
ย ย ย ย ย ย ย ย ย ย ย pen.write("Score : {} High Score : {} ".format(
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย score, high_score), align="center", font=("candara", 24, "bold"))
ย ย ย time.sleep(delay)
wn.mainloop()