0% found this document useful (0 votes)
16 views6 pages

Import Required Modules

gg

Uploaded by

rehanriyaz3705
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views6 pages

Import Required Modules

gg

Uploaded by

rehanriyaz3705
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

# Import required modules

import turtle

import time

import random

# Set initial parameters

delay = 0.1

score = 0

high_score = 0

# Creating a window screen

wn = turtle.Screen()

wn.title("Snake Game")

wn.bgcolor("blue")

wn.setup(width=600, height=600)

wn.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()

food.speed(0)

food.shape("circle")

food.color("red")

food.penup()
food.goto(0, 100)

# Pen for displaying scores

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=("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"

# Move the snake

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)

# Keyboard bindings

wn.listen()

wn.onkeypress(go_up, "w")

wn.onkeypress(go_down, "s")

wn.onkeypress(go_left, "a")

wn.onkeypress(go_right, "d")

segments = []

# Main Gameplay

while True:

wn.update()

# Check for collisions 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"

# Clear the segments and reset the score

for segment in segments:

segment.goto(1000, 1000)

segments.clear()

score = 0

delay = 0.1

# Update the high score on the screen

pen.clear()

pen.write(

"Score: {} High Score: {}".format(score, high_score),

align="center",

font=("candara", 24, "bold"),

# Check for collision with food

if head.distance(food) < 20:

x = random.randint(-270, 270)

y = random.randint(-270, 270)

food.goto(x, y)

# Add a new segment to the snake

new_segment = turtle.Turtle()

new_segment.speed(0)

new_segment.shape("square")
new_segment.color("orange")

new_segment.penup()

segments.append(new_segment)

# 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=("candara", 24, "bold"),

# Move the segments of the snake

for index in range(len(segments) - 1, 0, -1):

x = segments[index - 1].xcor()

y = segments[index - 1].ycor()

segments[index].goto(x, y)

# Move the first segment to where the head is

if len(segments) > 0:

x = head.xcor()

y = head.ycor()

segments[0].goto(x, y)

# Check for collision with the body segments

for segment in segments:

if segment.distance(head) < 20:

time.sleep(1)
head.goto(0, 0)

head.direction = "Stop"

# Clear the segments and reset the score

for seg in segments:

seg.goto(1000, 1000)

segments.clear()

score = 0

delay = 0.1

# Update the high score on the screen

pen.clear()

pen.write(

"Score: {} High Score: {}".format(score, high_score),

align="center",

font=("candara", 24, "bold"),

# Move the snake

move()

# Add a delay

time.sleep(delay)

# This code creates a functional Snake game using the Turtle graphics library in Python. Players
control the snake with the "w," "s," "a," and "d" keys to collect red food while avoiding collisions with
the screen borders and the snake's own body. The game keeps track of the player's score and high
score. When the snake collides with the screen border or itself, the game resets.

Please note that this code snippet does not include a game over condition when the player decides
to quit the game. You may want to add a mechanism to allow players to exit the game gracefully if
needed.

You might also like