0% found this document useful (0 votes)
9 views4 pages

Python Snake Game Code Example

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

Python Snake Game Code Example

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

# import required modules

import turtle
import time
import random

delay = 0.1
score = 0
high_score = 0

# Creating a window screen


wn = [Link]()
[Link]("Snake Game")
[Link]("blue")
# the width and height can be put as user's choice
[Link](width=600, height=600)
[Link](0)

# head of the snake


head = [Link]()
[Link]("square")
[Link]("white")
[Link]()
[Link](0, 0)
[Link] = "Stop"

# food in the game


food = [Link]()
colors = [Link](['red', 'green', 'black'])
shapes = [Link](['square', 'triangle', 'circle'])
[Link](0)
[Link](shapes)
[Link](colors)
[Link]()
[Link](0, 100)

pen = [Link]()
[Link](0)
[Link]("square")
[Link]("white")
[Link]()
[Link]()
[Link](0, 250)
[Link]("Score : 0 High Score : 0", align="center",
font=("candara", 24, "bold"))# import required modules
import turtle
import time
import random

delay = 0.1
score = 0
high_score = 0

# Creating a window screen


wn = [Link]()
[Link]("Snake Game")
[Link]("blue")
# the width and height can be put as user's choice
[Link](width=600, height=600)
[Link](0)

# head of the snake


head = [Link]()
[Link]("square")
[Link]("white")
[Link]()
[Link](0, 0)
[Link] = "Stop"

# food in the game


food = [Link]()
colors = [Link](['red', 'green', 'black'])
shapes = [Link](['square', 'triangle', 'circle'])
[Link](0)
[Link](shapes)
[Link](colors)
[Link]()
[Link](0, 100)

pen = [Link]()
[Link](0)
[Link]("square")
[Link]("white")
[Link]()
[Link]()
[Link](0, 250)
[Link]("Score : 0 High Score : 0", align="center",
font=("candara", 24, "bold"))

# assigning key directions


def group():
if [Link] != "down":
[Link] = "up"

def godown():
if [Link] != "up":
[Link] = "down"

def goleft():
if [Link] != "right":
[Link] = "left"

def goright():
if [Link] != "left":
[Link] = "right"

def move():
if [Link] == "up":
y = [Link]()
[Link](y+20)
if [Link] == "down":
y = [Link]()
[Link](y-20)
if [Link] == "left":
x = [Link]()
[Link](x-20)
if [Link] == "right":
x = [Link]()
[Link](x+20)

[Link]()
[Link](group, "w")
[Link](godown, "s")
[Link](goleft, "a")
[Link](goright, "d")

segments = []

# Main Gameplay
while True:
[Link]()
if [Link]() > 290 or [Link]() < -290 or [Link]() > 290 or [Link]()
< -290:
[Link](1)
[Link](0, 0)
[Link] = "Stop"
colors = [Link](['red', 'blue', 'green'])
shapes = [Link](['square', 'circle'])
for segment in segments:
[Link](1000, 1000)
[Link]()
score = 0
delay = 0.1
[Link]()
[Link]("Score : {} High Score : {} ".format(
score, high_score), align="center", font=("candara", 24, "bold"))
if [Link](food) < 20:
x = [Link](-270, 270)
y = [Link](-270, 270)
[Link](x, y)

# Adding segment
new_segment = [Link]()
new_segment.speed(0)
new_segment.shape("square")
new_segment.color("orange") # tail colour
new_segment.penup()
[Link](new_segment)
delay -= 0.001
score += 10
if score > high_score:
high_score = score
[Link]()
[Link]("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 = [Link]()
y = [Link]()
segments[0].goto(x, y)
move()
for segment in segments:
if [Link](head) < 20:
[Link](1)
[Link](0, 0)
[Link] = "stop"
colors = [Link](['red', 'blue', 'green'])
shapes = [Link](['square', 'circle'])
for segment in segments:
[Link](1000, 1000)
[Link]()

score = 0
delay = 0.1
[Link]()
[Link]("Score : {} High Score : {} ".format(
score, high_score), align="center", font=("candara", 24, "bold"))
[Link](delay)

[Link]()

You might also like