Snake Game Code (annotated)
from turtle import Screen
from time import sleep
from Snake import Snake
from Food import Food
from Scoreboard import Scoreboard
screen = Screen()
screen.setup(width=600, height=600)
screen.bgcolor('black')
screen.title('Snake Game')
screen.tracer(0)
jake = Snake()
food = Food()
scoreboard = Scoreboard()
screen.listen()
screen.onkey(key='Up', fun=jake.up)
screen.onkey(key='Down', fun=jake.down)
screen.onkey(key='Left', fun=jake.left)
screen.onkey(key='Right', fun=jake.right)
game_on = True
while game_on:
sleep(.06)
screen.update()
jake.move()
# detect collision with food
if jake.head.distance(food) < 15:
food.refresh()
scoreboard.add_one()
jake.extend()
# detect going outside of b
if jake.head.xcor() >= 310 or jake.head.xcor() <= -310 or jake.head.ycor() >= 310 or jake.head.ycor() <= -310:
scoreboard.reset()
jake.reset()
# detect snake eating itself
for x in jake.segments[1:]:
if jake.head.distance(x) < 10:
scoreboard.reset()
jake.reset()
screen.exitonclick()
Snake Class Code
from turtle import Turtle
from time import sleep
STARTING_CORDS = [(0, 0), (-20, 0), (-40, 0)]
MOVE_DISTANCE = 20
UP = 90
DOWN = 270
LEFT = 180
RIGHT = 0
class Snake:
def __init__(self):
self.segments = []
for x in STARTING_CORDS:
block1 = Turtle(shape='square')
block1.color('white')
block1.penup()
block1.goto(x=x[0],y=x[1])
self.segments.append(block1)
self.head = self.segments[0]
def create_snake(self):
for x in STARTING_CORDS:
block1 = Turtle(shape='square')
block1.color('white')
block1.penup()
block1.goto(x=x[0],y=x[1])
self.segments.append(block1)
self.head = self.segments[0]
def extend(self):
new_segment = Turtle(shape='square')
new_segment.color('white')
new_segment.penup()
x = self.segments[-1].xcor()
y = self.segments[-1].ycor()
new_segment.goto(x=x, y=y)
self.segments.append(new_segment)
def move(self):
for seg_num in range(len(self.segments) - 1, 0, -1):
x = self.segments[seg_num - 1].xcor()
y = self.segments[seg_num - 1].ycor()
self.segments[seg_num].goto(x, y)
self.head.forward(MOVE_DISTANCE)
def reset(self):
for segment in self.segments:
segment.goto(1000, 1000)
self.segments.clear()
self.create_snake()
sleep(1)
def up(self):
if self.head.heading() != DOWN:
self.head.setheading(UP)
def left(self):
if self.head.heading() != RIGHT:
self.head.setheading(LEFT)
def down(self):
if self.head.heading() != UP:
self.head.setheading(DOWN)
def right(self):
if self.head.heading() != LEFT:
self.head.setheading(RIGHT)
Scoreboard Class Code
from turtle import Turtle
alignment = 'center'
font = ('arial', 24)
class Scoreboard(Turtle):
def __init__(self):
super().__init__()
self.score = 0
with open('High_Score.txt') as file:
self.high_score = int(file.read())
self.hideturtle()
self.sety(275)
self.color('white')
self.write(arg=f'Score: {self.score}. High Score {self.high_score}', align=alignment, font=font)
def update_scoreboard(self):
self.clear()
self.write(arg=f'Score: {self.score}. High Score {self.high_score}', align=alignment, font=font)
def reset(self):
self.clear()
if self.score > self.high_score:
self.high_score = self.score
with open('High_Score.txt', mode='w') as file:
file.write(str(self.score))
self.score = 0
self.write(arg=f'Score: {self.score}. High Score {self.high_score}', align=alignment, font=font)
def add_one(self):
self.clear()
self.score += 1
self.update_scoreboard()