Skip to content

Instantly share code, notes, and snippets.

@royalPanic
Created May 6, 2019 11:49
Show Gist options
  • Save royalPanic/62a4c6087c6f3fa70a75bb4e1c12e83e to your computer and use it in GitHub Desktop.
Save royalPanic/62a4c6087c6f3fa70a75bb4e1c12e83e to your computer and use it in GitHub Desktop.
This should fix all errors related to regular crashing upon picking up food due to an index error. This uses a modulus to make the coordinates conform to the grid instead of a list.
import turtle as t
import random as r
import time
# Death delay
delay = 0.1
# Score
score = 0
high_score = 0
# Var defs
w = t.Screen()
p = t.Turtle()
x = -290
y = -290
# Screen setup
w.title("Snake")
w.bgcolor("White")
w.setup(width=600, height=700)
w.tracer(0)
p.speed(1)
p.hideturtle()
# Functions:
# funtion for grid modulizing
def gridModulo(n):
return(n-(n%20))
# Function for grid lines
def vertical_line(x):
p.penup()
p.goto(x, 290)
p.pendown()
p.goto(x, -290)
def horizontal_line(y):
p.penup()
p.goto(-290, y)
p.pendown()
p.goto(290, y)
# Loop to make grid lines
while x < 300:
vertical_line(x)
x += 20
p.hideturtle()
while y < 300:
horizontal_line(y)
y += 20
# Snake movement & distance
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)
# Functions for snake to go up down left right and not go back in on itself
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 start_text():
penstart.write("Press Any Arrow Key to Start", align="center", font=("arial", 24, "normal"))
w.onkey(start, "Up" or "Down" or "Left" or "Right")
def start():
penstart.clear()
def death():
pendeath.write("Oh no! You died with a score of {}".format(score), align="center", font=("arial", 24, "normal"))
if head.direction != "stop":
pendeath.clear()
# Snake head config
head = t.Turtle()
head.speed(0)
head.shape("square")
head.color("green")
head.penup()
head.goto(0, 0)
head.direction = "stop"
# Food config
food = t.Turtle()
food.speed(0)
food.shape("circle")
food.color("red")
food.penup()
food.goto(x=gridModulo(r.randint(-240,240)),y=gridModulo(r.randint(-240,240)))
# Segment
segments = []
# Pen for score text
penscore = t.Turtle()
penscore.speed(0)
penscore.shape("square")
penscore.color("black")
penscore.penup()
penscore.hideturtle()
penscore.goto(0, 300)
penscore.write("Score: 0 High Score: 0", align="center", font=("arial", 24, "normal"))
# Pen for start text
penstart = t.Turtle()
penstart.speed(0)
penstart.shape("square")
penstart.color("black")
penstart.penup()
penstart.hideturtle()
penstart.goto(0, -325)
# Pen for death text
pendeath = t.Turtle()
pendeath.speed(0)
pendeath.shape("square")
pendeath.color("black")
pendeath.penup()
pendeath.hideturtle()
pendeath.goto(0, -325)
# Keyboard bindings
w.listen()
w.onkeypress(go_up, "Up")
w.onkeypress(go_down, "Down")
w.onkeypress(go_left, "Left")
w.onkeypress(go_right, "Right")
# Main game loop
while True:
w.update()
if head.direction == "stop":
start_text()
if head.direction != "stop":
penstart.clear()
# Check for collision with border
if head.xcor() > 280 or head.xcor() < -280 or head.ycor() > 280 or head.ycor() < -280:
time.sleep(1)
head.goto(0, 0)
death()
time.sleep(2.3)
head.direction = "stop"
# Hide segments (body parts)
for segment in segments:
segment.goto(1000, 1000)
# Post death segment reset
segments.clear()
# Post death score reset
score = 0
penscore.clear()
penscore.write("Score: {} High Score: {}".format(score, high_score), align="center", font=("arial", 24,'normal'))
# Check for collision with food:
if head.distance(food) < 20:
# Move food randomly
food.goto(x=gridModulo(r.randint(-240,240)),y=gridModulo(r.randint(-240,240)))
# Add segment to body
new_segment = t.Turtle()
new_segment.speed(0)
new_segment.shape("square")
new_segment.color("light green")
new_segment.penup()
segments.append(new_segment)
# Shorten delay to reduce lag appearance
delay -= 0.001
# Increase score
score += 10
# Update high score
if score > high_score:
high_score = score
penscore.clear()
penscore.write("Score: {} High Score: {}".format(score, high_score), align="center", font=("arial", 24,'normal'))
# Moving end 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)
# Move 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 body collision
for segment in segments:
if segment.distance(head) < 20:
time.sleep(1)
head.goto(0, 0)
death()
head.direction = "stop"
# Hide segments (body parts)
for segment in segments:
segment.goto(1000, 1000)
# Post death segment reset
segments.clear()
# Post death score reset
score = 0
penscore.clear()
penscore.write("Score: {} High Score: {}".format(score, high_score), align="center", font=("arial", 24,'normal'))
# Slows down whole program
time.sleep(delay)
w.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment