Logo Programming: Snake Game using Python
Hello, this week we will be exploring logo programming. Yes, using python is it can be done. Python is a very rich language it comes with a preinstalled library called a turtle. Python turtle library comes with a similar interactive feature that gives new programmers a taste of what itβs like to work with Python.
Let’s Dig into it
turtle contains all the methods and functions that you’ll need to create an image. To access it, we need to import into our program.
import turtle as tt
For the creation of any image, we need a window where one can see the output of the code. There will be a little black triangular shape in the middle of the screen is called the turtle. With two lines of code, we will create a polygon design. Let’s see how we can do this.
from turtle import *
for i in range(700):
forward(i)
left(91)
You can change the parameters, to have different shapes. As one can see in the output screen.
Let’s Create a Snake Game π
Everyone has played the snake game, the snake π grows longer as it eats π₯ food. If the snake runs into its own tail due to turning too sharply it loses one of three lives which was I think firstly introduced in the 90s Nokia mobiles. I used to play a lot. I was beating my own score. π
import turtle
import time
import random
delay = 0.1
score = 0
high_score = 0
# Creating a window screen
win = turtle.Screen()
win.title("Snake Game")
win.bgcolor("blue")
# the width and height can be put as user's choice
win.setup(width=600, height=600)
win.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 go_up():
if head.direction != "down":
head.direction = "up"
def go_down():
if head.direction != "up":
head.direction = "down"
def go_left():
if head.direction != "right":
head.direction = "left"
def go_right():
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)
win.listen()
win.onkeypress(go_up, "w")
win.onkeypress(go_down, "s")
win.onkeypress(go_left, "a")
win.onkeypress(go_right, "d")
segments = []
# Main Gameplay
while True:
win.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)
segment.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)
win.mainloop()
Copy and paste the code in the python file. Run using the command.
python [filename].py
Output
Tadaa!!! We have created a snake game.