0% found this document useful (0 votes)
13 views11 pages

Cs Project Snake Game

Uploaded by

viwok86688
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)
13 views11 pages

Cs Project Snake Game

Uploaded by

viwok86688
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/ 11

cs

PROJECT

“SNAKE GAME”
IN
PYTHON

BY- YASH JOSHI &


VIVEK CHINNAM
INDEX

 ACKNOWLEDGEMENT

 CERTIFICATE

 LOGIC

 PROGRAM CODE

 TOPIC COVERED IN PROJECT

 REFERENCES
ACKNOWLEDGEMENT

We would like to express our


sincere gratitude to our
teacher, Mrs. Karishma kapoor
for her invaluable guidance and
support throughout this project.
Her encouragement and
expertise have been
instrumental in shaping our
understanding. Additionally, we
extend our thanks to our
classmates for their
collaborative spirit, contributing
to a fulfilling learning
experience. We also thank our
principal Mr. Sharukh Khan for
giving us this opportunity to
enhance our skill
CERTIFICATE

The project recorded in this file


has been satisfactorily performed
by Tristan Tate and Andrew Tate,
studying in Class XI Science of
DPS Bhopal Public School during
the academic year 2023-24.

PRINCIPAL’S SIGNATURE

TEACHER’S SIGNATURE
DATE:
SCHOOL STAMP

LOGIC

To play, use the arrow keys to


guide the snake as it grows
longer with each consumed
"food" pixel. The challenge lies
in avoiding collisions with the
snake's own body or the screen
edges. Winning involves
strategic navigation, quick
reflexes, and careful planning
to maximize points without
reaching a dead-end. As you
progress, the game intensifies,
testing your skills and
concentration. Enjoy the
nostalgia and aim for the
highest score!

Program Code
import turtle
import time
import random

delay = 0.1
score = 0
high_score = 0

wn = turtle.Screen()
wn.title("Snake Game")
wn.bgcolor("blue")
wn.setup(width=600, height=600)
wn.tracer(0)

head = turtle.Turtle()
head.shape("square")
head.color("white")
head.penup()
head.goto(0, 0)
head.direction = "Stop"
food = turtle.Turtle()
food.speed(0)
food.penup()
food.goto(0, 100)

pen = turtle.Turtle()
pen.speed(0)
pen.hideturtle()
pen.goto(0, 250)
pen.write("Score : 0 High Score : 0", align="center", font=("candara", 24,
"bold"))

segments = []

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":
head.sety(head.ycor() + 20)
elif head.direction == "down":
head.sety(head.ycor() - 20)
elif head.direction == "left":
head.setx(head.xcor() - 20)
elif head.direction == "right":
head.setx(head.xcor() + 20)

def check_collision():
if (
head.xcor() > 290 or head.xcor() < -290
or head.ycor() > 290 or head.ycor() < -290
):
game_over()

def game_over():
global score, high_score, delay
time.sleep(1)
head.goto(0, 0)
head.direction = "Stop"

for segment in segments:


segment.goto(1000, 1000)
segments.clear()

score = 0
delay = 0.1
update_score()
def update_score():
pen.clear()
pen.write("Score : {} High Score : {} ".format(score, high_score),
align="center", font=("candara", 24, "bold"))

def spawn_food():
x = random.randint(-270, 270)
y = random.randint(-270, 270)
food.goto(x, y)

def eat_food():
global score, high_score, delay
x, y = head.xcor(), head.ycor()
if head.distance(food) < 20:
spawn_food()
new_segment = turtle.Turtle()
new_segment.speed(0)
new_segment.shape("square")
new_segment.color("orange")
new_segment.penup()
segments.append(new_segment)
delay -= 0.001
score += 10
if score > high_score:
high_score = score
update_score()

def move_segments():
for i in range(len(segments) - 1, 0, -1):
x, y = segments[i - 1].xcor(), segments[i - 1].ycor()
segments[i].goto(x, y)

def update_segments_position():
if len(segments) > 0:
x, y = head.xcor(), head.ycor()
segments[0].goto(x, y)

wn.listen()
wn.onkeypress(go_up, "r")
wn.onkeypress(go_down, "f")
wn.onkeypress(go_left, "d")
wn.onkeypress(go_right, "g")

while True:
wn.update()
move()
check_collision()
eat_food()
move_segments()
update_segments_position()
time.sleep(delay)

wn.mainloop()

FLOWCHART
l

You might also like