python turtle贪吃蛇
代码如下:
from turtle import *
import time
from winsound import PlaySound,SND_ASYNC
import random
tracer(10000000,0)
size=200
snakeSpeed=12
stepNum=20
stepSize=size/stepNum
points=0
direction=0
snakeLen=4
snake="0,0|-1,0|-2,0|-3,0|"
fruit=[1,1]
getFruit=False
sleepTime=1/snakeSpeed
setup(size+stepSize*10,size+stepSize*10)
screensize(size,size)
penup()
goto(size/2+stepSize/2,size/2+stepSize/2)
pendown()
goto(size/2+stepSize/2,-size/2-stepSize/2)
goto(-size/2-stepSize/2,-size/2-stepSize/2)
goto(-size/2-stepSize/2,size/2+stepSize/2)
goto(size/2+stepSize/2,size/2+stepSize/2)
penup()
def getSnake(barNum):
barCount=0
STR=""
x=0;y=0
for i in range(len(snake)):
if barCount==barNum:
j=i
while snake[j]!=',':
STR+=snake[j]
j+=1
x=int(STR)
j+=1
STR=""
while snake[j]!='|':
STR+=snake[j]
j+=1
y=int(STR)
break
if snake[i]=='|':
barCount+=1
return [x,y]
def move():
#print("move!!!")
#print(getFruit)
newSnake=""
if direction==<