我最近想找一下博客里的python贪吃蛇 发现不是要用图片就是运行不起来
气得我连夜搞出来个不要图片的贪吃蛇
我是真的有在认真搞
今天我用python使用turtle不用图片只要32行(以下代码段为准)做一个贪吃蛇
为了懒的人 先把代码放这了 #没登录往下刷 我放了可以不用登录就可以复制的代码
爱学习的宝子往下看
import turtle,random,time;turtle.setup(610,610);a=turtle.Screen();a.bgcolor('white');turtle.tracer(False);bg=turtle.Turtle();bg.color('gray')
for i in range(31):bg.pu();bg.goto(-300,-300+i*20);bg.pd();bg.fd(600)
bg.left(90)
for j in range(31):bg.pu();bg.goto(-300+j*20,300);bg.pd();bg.bk(600)
turtle.update();bg.ht();turtle.tracer(False);snakeList,f=[],5
def eggtf():
global eggx,eggy,egg;egg=turtle.Turtle();egg.pu();eggx,eggy=random.randint(-14,14),random.randint(-14,14);egg.goto(eggx*20,eggy*20);egg.shape('square')
for i in range(5):t=turtle.Turtle();t.shape('square');t.pu();t.goto(i*20,0);snakeList.append(t)
word=turtle.Turtle();word.ht()
def score(tf):
if tf:global word;word.ht();word.clear()
word=turtle.Turtle();word.pu();word.goto(-270,270);word.write('得分:'+str(f),font=('微软雅黑',18));word.ht()
def go(x1,y1):
global f,g
try:t=snakeList.pop();t.ht()
except:pass
t1=turtle.Turtle();t1.shape('square');snakeList.insert(0,t1);t1.pu();t1.goto(x1,y1);x2,y2=snakeList[0].pos()
if snakeList[0].xcor()==eggx*20 and snakeList[0].ycor()==eggy*20:
f+=1;score(True);snakeList.append('square');egg.ht();eggtf();t.shape('square&