from turtle import *
import time
from random import randint
import math
import pygame
#screen settings
sc=Screen()
sc.setup(600,600)
sc.title('pySnake')
sc.tracer(2)
#border
mypen2=Turtle()
mypen2.hideturtle()
mypen=Turtle()
mypen.up()
mypen.pensize(3)
mypen.pencolor('black')
mypen.setpos(-270,-270)
mypen.down()
for i in range(2):
mypen.forward(535)
mypen.left(90)
mypen.forward(520)
mypen.left(90)
mypen.hideturtle()
#high score file
scfile=open('Score.txt','r')
n=scfile.read()
scfile.close()
n=int(n)
#scorep
score=0
#player
player=Turtle()
player.speed(0)
player.shape('square')
player.color('green')
player.shapesize(1,1)
player.up()
#other parts(pl)
pl=[]
body=Turtle()
body.shape('square')
body.color('green')
body.shapesize(1,1)
body.up()
pl.append(body)
#apple
apple=Turtle()
apple.shape('square')
apple.color('red')
apple.shapesize(0.7,0.7)
apple.up()
apple.setpos(randint(-260,255),randint(-260,240))
#other functions
def iscollision(o1,o2):
d=math.sqrt(math.pow(o1.xcor()-o2.xcor(),2)+math.pow(o1.ycor()-o2.ycor(),2))
if d <20:
return True
#player key functions
direction=None
def left():
global direction
direction='left'
def right():
global direction
direction='right'
def up():
global direction
direction='up'
def down():
global direction
direction='down'
def move():
if direction=='left':
x=player.xcor()
player.setx(x-20)
if direction=='right':
x=player.xcor()
player.setx(x+20)
if direction=='up':
y=player.ycor()
player.sety(y+20)
if direction=='down':
y=player.ycor()
player.sety(y-20)
#keys
listen()
onkeypress(left,'Left')
onkeypress(right,'Right')
onkeypress(up,'Up')
onkeypress(down,'Down')
#loop
while True:
sc.update()
move()
#body parts collision
for i in pl[1:]:
if i.distance(player)<20:
#sound
pygame.mixer.init()
pygame.mixer.music.load("Beep19.wav")
pygame.mixer.music.play()
#draw score
mypen.undo()
mypen.penup()
mypen.hideturtle()
mypen.setposition(0,50)
scoretesting='Your Score: %s' %score
mypen.write(scoretesting,False,align='center',font=('arial',30,'normal'))
#draw high score
mypen2.undo()
mypen2.penup()
mypen2.hideturtle()
mypen2.setposition(0,-5)
scoretesting='High Score: %s' %n
mypen2.write(scoretesting,False,align='center',font=('arial',30,'normal'))
sc.mainloop()
#border collision
if player.xcor()<-260 or player.xcor()>255:
#sound
pygame.mixer.init()
pygame.mixer.music.load("Beep19.wav")
pygame.mixer.music.play()
#draw score
mypen.undo()
mypen.penup()
mypen.hideturtle()
mypen.setposition(0,50)
scoretesting='Your Score: %s' %score
mypen.write(scoretesting,False,align='center',font=('arial',30,'normal'))
#draw high score
mypen2.undo()
mypen2.penup()
mypen2.hideturtle()
mypen2.setposition(0,-5)
scoretesting='High Score: %s' %n
mypen2.write(scoretesting,False,align='center',font=('arial',30,'normal'))
sc.mainloop()
if player.ycor()<-260 or player.ycor()>240:
#sound
pygame.mixer.init()
pygame.mixer.music.load("Beep19.wav")
pygame.mixer.music.play()
#draw score
mypen.undo()
mypen.penup()
mypen.hideturtle()
mypen.setposition(0,50)
scoretesting='Your Score: %s' %score
mypen.write(scoretesting,False,align='center',font=('arial',30,'normal'))
#draw high score
mypen2.undo()
mypen2.penup()
mypen2.hideturtle()
mypen2.setposition(0,-5)
scoretesting='High Score: %s' %n
mypen2.write(scoretesting,False,align='center',font=('arial',30,'normal'))
sc.mainloop()
#apple collision
if iscollision(player,apple):
apple.setpos(randint(-260,255),randint(-260,240))
score+=1
#sound
pygame.mixer.init()
pygame.mixer.music.load("Beep16.wav")
pygame.mixer.music.play()
#append body parts
body=Turtle()
body.shape('square')
body.color('green')
body.shapesize(1,1)
body.up()
pl.append(body)
#draw the score
mypen.undo()
mypen.penup()
mypen.hideturtle()
mypen.setposition(-270,262)
scoretesting='Score: %s' %score
mypen.write(scoretesting,False,align='left',font=('arial',14,'normal'))
#high score
if n<score:
scfile=open('Score.txt','w')
scfile.write(str(score))
scfile.close()
mypen2.undo()
mypen2.penup()
mypen2.hideturtle()
mypen2.setposition(-160,262)
n=score
scoretesting=' High Score: %s' %n
mypen2.write(scoretesting,False,align='left',font=('arial',14,'normal'))
else:
mypen2.undo()
mypen2.penup()
mypen2.hideturtle()
mypen2.setposition(-160,262)
scoretesting=' High Score: %s' %n
mypen2.write(scoretesting,False,align='left',font=('arial',14,'normal'))
#snake
for i in range(len(pl)-1,0,-1):
x=pl[i-1].xcor()
y=pl[i-1].ycor()
pl[i].setpos(x,y)
if len(pl)>0:
x=player.xcor()
y=player.ycor()
pl[0].setpos(x,y)
time.sleep(0.15)