Snake Game Using turtle library
How about lets use the same turtle library to make a simple game. Today we will be making the snake game using python.
All that the game does is there will be the food placed at random co-ordinates and the snake will have to move using the keys 'W', 'A', 'S' and 'D' and every time it eats the length of the snake is increased . Game will be over when it hits itself or the border.
We need import turtle random and time module for this game.
import turtle
import time
import random
delay = 0.1
# Score
score = 0
high_score = 0
# Set up the screen
win = turtle.Screen()
win.title("Snake Game--->Codeuniq")
win.bgcolor("blue")
win.setup(width=600, height=600)
win.tracer(0) # Turns off the screen updates
# Snake head
head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("yellow")
head.penup()
head.goto(0,0)
head.direction = "stop"
# Snake food
food = turtle.Turtle()
food.speed(0)
food.shape("circle")
food.color("red")
food.penup()
food.goto(0,100)
section = []
# Pen
pen = turtle.Turtle()
pen.speed(0)
pen.shape("square")
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 260)
pen.write("Score: 0 High Score: 0", align="center", font=("Arial", 24, "normal"))
# Functions
def move_up():
if head.direction != "down":
head.direction = "up"
def move_down():
if head.direction != "up":
head.direction = "down"
def move_left():
if head.direction != "right":
head.direction = "left"
def move_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)
# setting up the Keyboard configuration
win.listen()
win.onkeypress(move_up, "w")
win.onkeypress(move_down, "s")
win.onkeypress(move_left, "a")
win.onkeypress(move_right, "d")
# Main game loop
while True:
win.update()
# Check for a collision with the border
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"
# Hide the section
for s in sections:
s.goto(1000, 1000)
# Clear the sections list
sections.clear()
# Reset the score
score = 0
# Reset the delay
delay = 0.1
pen.clear()
pen.write("Score: {} High Score: {}".format(score, high_score), align="center", font=("Courier", 24, "normal"))
code for the game is given below
# Check for a collision with the food
if head.distance(food) < 20:
# Move the food to a random spot
x = random.randint(-290, 290)
y = random.randint(-290, 290)
food.goto(x,y)
new_section = turtle.Turtle()
new_section.speed(0)
new_section.shape("square")
new_section.color("grey")
new_section.penup()
sections.append(new_section)
delay -= 0.001
# Increase the score
score += 10
if score > high_score:
high_score = score
pen.clear()
pen.write("Score: {} High Score: {}".format(score, high_score), align="center", font=("Courier", 24, "normal"))
# Move the end sections first in reverse order
for index in range(len(sections)-1, 0, -1):
x = sections[index-1].xcor()
y = sections[index-1].ycor()
sections[index].goto(x, y)
# Move section 0 to where the head is
if len(sections) > 0:
x = head.xcor()
y = head.ycor()
sections[0].goto(x,y)
move()
# Check for head collision with the body sections
for s in sections:
if s.distance(head) < 20:
time.sleep(1)
head.goto(0,0)
head.direction = "stop"
# Hide the sections
for s in sections:
s.goto(1000, 1000)
# Clear the sections list
sections.clear()
# Reset the score
score = 0
# Reset the delay
delay = 0.1
# Update the score display
pen.clear()
pen.write("Score: {} High Score: {}".format(score, high_score), align="center", font=("Courier", 24, "normal"))
time.sleep(delay)
win.mainloop()
You can see the output of the program in the above mentioned video.
Hope you liked this content stay tuned for more
Thankyou.......
Comments
Post a Comment