## DEV Community

Mike Kameta

Posted on • Updated on

# 100 Days of Code: The Complete Python Pro Bootcamp for 2022 - Day 21 (Snake Game Part 2)

• Day 2 Process

Inheritance

Continuation of the snake game

1. Detect collision with food
2. Create scoreboard and keep score
3. Detect collision with wall
4. Detect collision with tail
• Inheritance
class Animal:
def __init__(self):
self.num_eyes = 2

def breathe(self):
print("Inhale, Exhale. ")

class Fish(Animal):
def __init__(self):
super().__init__()

def breathe(self):
super(Fish, self).breathe()
print("doing this under water")

def swim(self):
print("moving in water")

nemo = Fish()
nemo.swim()
nemo.breathe()
print(nemo.num_eyes)
• 1. Detect collision with food Create food.py
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("blue")
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)

from food import Food
food = Food()
food.refresh()

Complete main.py

from turtle import Screen
from snake import Snake
from food import Food
import time

# Screen setup
screen = Screen()
screen.setup(width=600, height=600)
screen.bgcolor("black")
screen.title("SnakeGame")
screen.tracer(0)

snake = Snake()
food = Food()

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)
snake.move()

food.refresh()

screen.exitonclick()
• 2. Create scoreboard and keep score

Create scoreboard.py

from turtle import Turtle

ALIGNMENT = "center"
FONT = ("Courier", 20, "normal")

class Scoreboard(Turtle):

def __init__(self):
super().__init__()
self.hideturtle()
self.score = 0
self.penup()
self.setx(0)
self.sety(260)
self.pendown()
self.color("white")
self.update_scoreboard()

def update_scoreboard(self):
self.write(f"Score: {self.score}", align=ALIGNMENT, font=FONT)

def increase_score(self):
self.score += 1
self.clear()
self.update_scoreboard()

from scoreboard import Scoreboard
score = Scoreboard()

from turtle import Screen
from snake import Snake
from food import Food
from scoreboard import Scoreboard
import time

# Screen setup
screen = Screen()
screen.setup(width=600, height=600)
screen.bgcolor("black")
screen.title("SnakeGame")
screen.tracer(0)

snake = Snake()
food = Food()
score = Scoreboard()

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)
snake.move()

food.refresh()
score.increase_score()

screen.exitonclick()
• 3 + 4 Detect collision with wall and tail snake.py
class Snake:

def __init__(self):
self.all_snake = []
self.create_snake()

def create_snake(self):
for position in STARTING_POSITIONS:

new_snake = Turtle("square")
new_snake.color("white")
new_snake.penup()
new_snake.goto(position)
self.all_snake.append(new_snake)

def extend(self):

main.py

# Detect collision with tail
for segment in snake.all_snake: