Last active
May 5, 2019 04:32
-
-
Save royalPanic/38045262a42cb87551e351bf906d04a3 to your computer and use it in GitHub Desktop.
The only remaining problem is that *sometimes* the game freezes when a food is collected for no explicable reason.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
xCoords = [-240, -220, -200, -180, -160, -140, -120, -100, -80, -60, -40, -20, 0, 20, 40, 60, 80, 100, 120, 140, 160, 180, 200, 220, 240,] | |
yCoords = [-240, -220, -200, -180, -160, -140, -120, -100, -80, -60, -40, -20, 0, 20, 40, 60, 80, 100, 120, 140, 160, 180, 200, 220, 240,] | |
# Screen setup | |
w.title("Snake") | |
w.bgcolor("White") | |
w.setup(width=600, height=700) | |
w.tracer(0) | |
p.speed(1) | |
p.hideturtle() | |
# Functions: | |
# 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=xCoords[r.randint(0,len(xCoords))], y=yCoords[r.randint(0,len(yCoords))]) | |
# 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=xCoords[r.randint(0,len(xCoords))], y=yCoords[r.randint(0,len(yCoords))]) | |
# 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