You are on page 1of 16

S.

NO CONTENTS

1. OVERVIEW OF PYTHON AND MYSQL

2. ABSTRACT

3. SOURCE CODE

4. SAMPLE OUTPUT

5. CONCLUSION

6. BIBLIOGRAPHY
1. OVER VIEW OF PYTHON AND MYSQL
Python is an interpreter, high-level and general-purpose programming
language. Python's design philosophy emphasizes code readability with its
notable use of significant indentation. Python and its broad variety of libraries
are very well suited to develop customized machine learning tools which tackle
the complex challenges posed by financial time series. Python is a general-
purpose coding language—which means that, unlike HTML, CSS, and
JavaScript, it can be used for other types of programming and software
development besides web
development. That includes back
end development, software
development, data science and
writing system scripts among
other things.

MYSQL is an open-
source relational database
management system. Its
name is a combination of
“My”, the name of co-
founders Michael Widenius's daughter, and "SQL", the abbreviation
for Structured Query Language. Many of the world's largest and
fastest-growing organizations including Facebook, Google, Adobe,
Alcatel Lucent and Zappos rely on MySQL to save time and money
powering their high-volume Web sites, business-critical systems and
packaged software.
2. ABSTRACT

This is python version of the most popular mobile and computer


game named “snake”. The main objective of this python project is to
build a snake game. The game is to feed an increasing length of a
snake with food particles which are found at random positions. Every
time the snake eats the food, its length grows longer that makes the
game more difficult. Use the arrow keys to navigate and eat the food.
Avoid eating yourself or going out of bounds! The game is a
representation of the snake game which appears as an inbuilt game

feature in most of the leading mobile handsets like nokia.


3. SOURCE CODE

import turtle

import time

import random

delay = 0.2

# SCORE

score = 0

high_score = 0

# SETTING UP THE SCREEN

wn = turtle.Screen()

wn.title("Snake game by Meena Roshini.S")

wn.bgcolor("green")

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

wn.tracer(0) # turns off the screen updates

# SNAKE HEAD

head = turtle.Turtle()

head.speed(0)
head.shape("square")

head.color("orange")

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)

segments = []

# 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=("Courier", 24,


"normal"))

# FUNCTIONS

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)

# KEYBOARD BINDING

wn.listen()

wn.onkeypress(go_up, "Up")

wn.onkeypress(go_down, "Down")

wn.onkeypress(go_left, "Left")

wn.onkeypress(go_right, "Right")
# MAIN GAME LOOP

while True:

wn.update()

# CHECK FOR 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 SEGMENTS

for segment in segments:

segment.goto(1000, 1000)

# CLEAR THE SEGMENTS LIST

segments.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"))

# CHECK FOR THE COLLISION WITH 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)

# ADD A SEGMENT

new_segment = turtle.Turtle()

new_segment.speed(0)

new_segment.shape("square")

new_segment.color("grey")

new_segment.penup()

segments.append(new_segment)

# SHORTEN THE DELAY

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 SEGMENTS FIRST IN THE REVERSE ORDER

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 SEGMENT 0 TO WHERE THE HEAD IS

if len(segments) > 0:

x = head.xcor()

y = head.ycor()

segments[0].goto(x, y)

move()

# 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"

# HIDE THE SEGMENTS

for segment in segments:

segment.goto(1000, 1000)

# CLEAR THE SEGMENTS LIST

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

4. SAMPLE OUTPUT
5. CONCLUSION
We were successful in creating a python version of traditional snake game.
You have learned how to create the game “snake” in python along with
concepts such as

 Collision detection,
 Event handling,
 Using
i. List to increase the length,
ii. If conditions to control the game,
iii. Pygame to create game
 Learnt how to use
i. Turtle modules – to draw on the screen
ii. Random module – used to generate random numbers
iii. Time modules - inbuilt module in python provides the
functionality of time.
iv. Goto module - used to move the turtle at x and y
coordinates
6. BIBLIOGRAPHY

Website Reference:

http://www.grantjenks.com

https://wiki.python.org/moin/PythonGames

https://www.edureka.co/blog/snake-game-with-pygame

Book Reference:

Class 12th Computer science with PYTHON_by Preeti Arora

Class 12th Computer science with PYTHON_by Sumita Arora

You might also like