0% found this document useful (0 votes)
78 views

Project Ip

The document contains the project report for a Snake Game created using Python. It includes a certificate signed by the IP teacher certifying the project, acknowledgements, hardware/software requirements, coding details, and screenshots of the output. The project uses the Turtle and Random Python libraries to code the snake that moves around the screen eating food, with the goal of getting a high score. Key sections include setting up the game window, creating the snake head and food objects, defining movement functions, and coding the gameplay logic of growing the snake and resetting on collisions.
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)
78 views

Project Ip

The document contains the project report for a Snake Game created using Python. It includes a certificate signed by the IP teacher certifying the project, acknowledgements, hardware/software requirements, coding details, and screenshots of the output. The project uses the Turtle and Random Python libraries to code the snake that moves around the screen eating food, with the goal of getting a high score. Key sections include setting up the game window, creating the snake head and food objects, defining movement functions, and coding the gameplay logic of growing the snake and resetting on collisions.
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/ 24

Informatics Practices

Project

Submitted by :
Vibanshu
Vikas Raina
TUshar Badyal

Class: 12th D
Certificate
This is to certify that Mr. Vibanshu,Mr. Vikas
Raina, Mr. Tushar Badyal of Class XII D have
prepared the report on the project entitled
"Snake Game". The report is the result of their
efforts and endeavors. The report is found
worthy of acceptance as final project report
for the subject Informatics Practices of Class
XII they have prepared the report under my
guidance.

Mr. Rajesh Bhat


(IP teacher)
Kendriya Vidyalaya
No.1 Gandhi Nagar Jammu
Acknowledgement
We would like to express a deep sense of
thanks to my project guide Mr. Rajesh Bhat Sir
for guiding me immensely though the course of
the project. He always evinced keen interest in
my work. His constructive constant motivation
have been responsible for the successful
completion of this project Our sincere thanks
go to Mr. Amit Walter, our Principal sir, for His
coordination in extending every possible
support for the completion of this project. We
also thank to my parents for their motivation
and support Last but not the least; We would
like to thank all those helped directly or
indirectly toward the completion of this
project.
INDEX

--Certificate
--Acknowledgement
--Hardware and software requirements
--Python Libraries used
--Function used
--Files generated
--Working description
--Coding
--0utput screen
--Bibliography
Hardware and Software
Requirements

The platform used is python, Hence we


decided to use Idle Python version 3.10
For optional coding we used Microsoft Visual
Studio 2010 edition.

Henceforth for optimal usage of such


software a windows based operating system
preferably windows 7 or later must be there.
Also on the hardware part any system having
windows 7 or later installed will suffice.
Python Libraries Used

o Turtle: This is an installed Python library


that allows users to draw patterns and
images by providing the user with a virtual
canvas.
o

o Time: It is used in order to calculate the


number of seconds since the date of the
event.
o

o Random: This is a function utilized to create


random numbers in Python through the use
of the random module.
.
CODING

import turtle
import time
import random

delay = 0.1
score = 0
high_score = 0

# Creating a window screen


wn = turtle.Screen()
wn.title("Snake Game")
wn.bgcolor("blue")
# the width and height can be put as user's
choice
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()
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 goup():
if head.direction != "down":
head.direction = "up"

def godown():
if head.direction != "up":
head.direction = "down"

def goleft():
if head.direction != "right":
head.direction = "left"
def goright():
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)
wn.listen()
wn.onkeypress(goup, "w")
wn.onkeypress(godown, "s")
wn.onkeypress(goleft, "a")
wn.onkeypress(goright, "d")

segments = []

# Main Gameplay
while True:
wn.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)

wn.mainloop()

Code Explanation:
 The code starts by creating a window screen.
 The title of the window is “Snake Game”.
 The background color of the window is blue.
 Next, the code creates two turtle objects:
head and food.
 Head is used to control the snake, while food
will be used as the game’s object.
 The code first sets up head so that it looks
like a square with white color and starts at (0,
0).
 Next, it sets up food so that it looks like a
square with blue color and places it at (10,
10).
 Finally, head moves towards food using
direction=”Stop”.
 The code creates a window screen with the
title “Snake Game” and sets the background
color to blue.
 Next, a square head object is created and set
to have the color white.
 The pen up() method is called on the head
object, which causes it to rise up from the
bottom of the screen.
 Finally, the goTo() method is used to move
the head object towards (0, 0) in the x-
direction.
 Next, two turtle objects are created – one for
food and one for head.
 The food object has a shape of “square” and
will be placed at (0, 0) on the screen.
 The head object has no shape specified and
will be placed at (0, 0) in the y-direction
 The code starts by creating a few variables to
store information about the game.
 The first is food, which stores information
about the shapes and colors of the food that
will be displayed on the screen.
 Next, the code creates a function called
speed() that will control how quickly the food
moves across the screen.
 Finally, the shape() and color() functions are
used to create different types of food (square,
triangle, and circle) with corresponding colors
and speeds.
 The next section of code sets up an event
handler for when the user clicks on one of the
buttons in the toolbar.
 This handler calls a function called goto() that
takes two arguments: position (in pixels) and
direction (which can be “up”, “down”, “left”, or
“right”).
 goTo() then uses these values to move the
pen object to a specific location onscreen and
set its properties accordingly.
 The last section of code displays some text
onscreen and starts timing it using time().
 Then it prints out a score value for each
round played as well as high scores for both
players at once.
 The code will create a turtle that will move at
0 speed and have a square shape.
 The turtle will be white in color and will start
at the top left corner of the screen.
 Once the code has executed, it will output
“Score : 0 High Score : 0” in center
alignment.
 The code starts by creating three variables:
head, wn, and godown.
 The head variable stores information about
the player’s current position on the screen
(xcor and ycor), while wn is a listening object
that will be used to track keyboard input.
 Godown stores information about where the
player should go next (if they are moving left
or right), and goleft and goright store key
directions for moving up, down, or left/right
respectively.
 The code then starts to loop through each of
these variables in turn.
 If any of them change (head.direction,
wn.listen(), godown.direction), the code
updates itself accordingly and sets various
properties on head such as its xcor and ycor
coordinates, its heading direction (up, down,
left/right), as well as colors and shapes for
each segment in the game world according to
what was just inputted by the user via
keyboard presses.
 Finally, there is a delay timer set to 0.1
seconds which allows time for one frame of
animation before starting over again with
another round of gameplay; this is important
because it ensures that everything looks
smooth even when there are lots of objects
moving around on-screen at once!
 The code assigns key directions to the turtle,
listens for user input, updates the screen and
calculates the player’s score.
 It also creates a new square segment if
needed and moves it according to the key
presses.
 If the player is moving their turtle up or down,
then head.direction will be set to “up” or
“down”, respectively.
 If they are moving left or right, head.direction
will be set to “left” or “right”.
 The code also checks whether the player’s
current position falls within a food zone and if
not, then the appropriate x and y coordinates
are randomly generated and stored in food
and moved accordingly by calling
Turtle().goto() with those values.

OUTPUT
Bibliography
https://www.google,com

https://en.wikipedia.org

https://www.youtube.com

https://
www.geeksforgeeks.org/create-
a-snake-game-using-turtle-in-
python/

You might also like