Skip to content

Instantly share code, notes, and snippets.

@Jithender5913
Last active January 15, 2022 08:41
Show Gist options
  • Select an option

  • Save Jithender5913/87499690ca24c01ac55430b0f2b7a23a to your computer and use it in GitHub Desktop.

Select an option

Save Jithender5913/87499690ca24c01ac55430b0f2b7a23a to your computer and use it in GitHub Desktop.
Snake Game using Python OOP
from snake import Snake
import time
from turtle import Screen
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)
# Step 1 - Create a snake body
snake = Snake()
food = Food()
score = ScoreBoard()
# Step 4 - Control the 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)
# Step 2 - Move forward the snake - Animating the snake segment on screen
# Step 3 - Create a snake class and Move to OOP
snake.move()
# Step 5 - create food class and Detect collision with food
# Step 6 - Create a scoreboard and keep score
if snake.head.distance(food) < 15:
food.refresh()
# Step 9 - Extend snake length and detect collision with tail
snake.extend()
score.increase_score()
# Step 7 - Detect collision with wall
if snake.head.xcor() > 280 or snake.head.xcor() < -280 or snake.head.ycor() > 280 or snake.head.ycor() < -280:
game_is_on = False
score.game_over()
# Step 8 - Detect collision with snake body
for segment in snake.segments[1:]:
if snake.head.distance(segment) < 10:
game_is_on = False
score.game_over()
screen.exitonclick()
# Snake class
from turtle import Turtle
STARTING_POSITIONS = [(0, 0), (-20, 0), (-40, 0)] # this is a constant, and it should be in ALL CAPS.
MOVE_DISTANCE = 20
UP = 90
DOWN = 270
LEFT = 180
RIGHT = 0
class Snake:
def __init__(self):
self.segments = []
self.create_snake()
self.head = self.segments[0]
def create_snake(self):
for position in STARTING_POSITIONS:
self.add_segment(position)
def add_segment(self, position):
new_segment = Turtle(shape="square")
new_segment.color("white")
new_segment.penup()
new_segment.goto(position)
self.segments.append(new_segment)
def extend(self): # Step 7 - Extend snake length and detect collision with tail
self.add_segment(self.segments[-1].position())
def move(self):
for seg_num in range(len(self.segments) - 1, 0, -1): # range(start, stop, step)
new_x = self.segments[seg_num - 1].xcor()
new_y = self.segments[seg_num - 1].ycor()
self.segments[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)
# Scoreboard class
from turtle import Turtle
ALIGNMENT = "center"
FONT = ("Arial", 24, "normal")
class ScoreBoard(Turtle):
def __init__(self):
super().__init__()
self.score = 0
self.color("white")
self.penup()
self.goto(0, 270)
self.hideturtle()
self.update_scoreboard()
def update_scoreboard(self):
self.write(f"Score:{self.score}", align=ALIGNMENT, font=FONT)
def game_over(self):
self.goto(0, 0)
self.write("GAME OVER", align="Center", font=FONT)
def increase_score(self):
self.score += 1
self.clear()
self.update_scoreboard()
# Food class
from turtle import Turtle
import random
class Food(Turtle):
def __init__(self):
super().__init__()
self.shape("circle")
self.penup()
self.shapesize(stretch_len=0.5, stretch_wid=0.5)
self.color("white")
self.speed("fastest")
self.refresh()
def refresh(self):
random_x = random.randint(-280, 280)
random_y = random.randint(-280, 280)
self.goto(random_x, random_y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment