Professional Documents
Culture Documents
NO CONTENTS
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
import turtle
import time
import random
delay = 0.2
# SCORE
score = 0
high_score = 0
wn = turtle.Screen()
wn.bgcolor("green")
wn.setup(width=600, height=600)
# 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)
# 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()
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"
segment.goto(1000, 1000)
segments.clear()
score = 0
delay = 0.1
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)
delay -= 0.001
# INCREASE THE SCORE
score += 10
high_score = score
pen.clear()
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()
time.sleep(1)
head.goto(0, 0)
head.direction = "stop"
segment.goto(1000, 1000)
segments.clear()
score = 0
delay = 0.1
pen.clear()
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: