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

Simple Python Snake Game Code

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)
153 views4 pages

Simple Python Snake Game Code

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

# Simple Snake Game in Python 3 for Beginners

# By @TokyoEdTech

import turtle
import time
import random

delay = 0.1

# Score
score = 0
high_score = 0

# Set up the screen


wn = [Link]()
[Link]("Snake Game by @TokyoEdTech")
[Link]("green")
[Link](width=600, height=600)
[Link](0) # Turns off the screen updates

# Snake head
head = [Link]()
[Link](0)
[Link]("square")
[Link]("black")
[Link]()
[Link](0,0)
[Link] = "stop"

# Snake food
food = [Link]()
[Link](0)
[Link]("circle")
[Link]("red")
[Link]()
[Link](0,100)

segments = []

# Pen
pen = [Link]()
[Link](0)
[Link]("square")
[Link]("white")
[Link]()
[Link]()
[Link](0, 260)
[Link]("Score: 0 High Score: 0", align="center", font=("Courier", 24, "normal"))

# Functions
def go_up():
if [Link] = "down":
[Link] = "up"

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

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

def go_right():
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)

# Keyboard bindings
[Link]()
[Link](go_up, "w")
[Link](go_down, "s")
[Link](go_left, "a")
[Link](go_right, "d")

# Main game loop


while True:
[Link]()

# Check for a collision with the border


if [Link]()>290 or [Link]()<-290 or [Link]()>290 or [Link]()<-290:
[Link](1)
[Link](0,0)
[Link] = "stop"

# Hide the segments


for segment in segments:
[Link](1000, 1000)

# Clear the segments list


[Link]()

# Reset the score


score = 0

# Reset the delay


delay = 0.1

[Link]()
[Link]("Score: {} High Score: {}".format(score, high_score), align="center", font=("Courier", 24,
"normal"))

# Check for a collision with the food


if [Link](food) < 20:
# Move the food to a random spot
x = [Link](-290, 290)
y = [Link](-290, 290)
[Link](x,y)

# Add a segment
new_segment = [Link]()
new_segment.speed(0)
new_segment.shape("square")
new_segment.color("grey")
new_segment.penup()
[Link](new_segment)

# Shorten the delay


delay -= 0.001

# Increase the score


score += 10

if score > high_score:


high_score = score

[Link]()
[Link]("Score: {} High Score: {}".format(score, high_score), align="center", font=("Courier", 24,
"normal"))

# Move the end segments first in 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 segment 0 to where the head is


if len(segments) > 0:
x = [Link]()
y = [Link]()
segments[0].goto(x,y)

move()

# Check for head collision with the body segments


for segment in segments:
if [Link](head) < 20:
[Link](1)
[Link](0,0)
[Link] = "stop"

# Hide the segments


for segment in segments:
[Link](1000, 1000)

# Clear the segments list


[Link]()

# Reset the score


score = 0

# Reset the delay


delay = 0.1

# Update the score display


[Link]()
[Link]("Score: {} High Score: {}".format(score, high_score), align="center", font=("Courier",
24, "normal"))

[Link](delay)

[Link]()

You might also like