python turtle模块 不要图片 32行 贪吃蛇 天地良心

我最近想找一下博客里的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')
    elif x2>300 or y2>300 or x2<-300 or y2<-300:
        for i in snakeList:i.reset()
    turtle.update()
eggtf();score(False)
def goLeft():
    while 1:time.sleep(0.1);x,y=snakeList[0].pos();go(x-20,y)
def goUp():
    while 1:time.sleep(0.1);x,y=snakeList[0].pos();go(x,y+20)
def goRight():
    while 1:time.sleep(0.1);x,y=snakeList[0].pos();go(x+20,y)
def goDown():
    while 1:time.sleep(0.1);x,y=snakeList[0].pos();go(x,y-20)
turtle.listen();turtle.onkeypress(goLeft,'Left');turtle.onkeypress(goRight,'Right');turtle.onkeypress(goDown,'Down');turtle.onkeypress(goUp,'Up')

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')
    elif x2>300 or y2>300 or x2<-300 or y2<-300:
        for i in snakeList:i.reset()
    turtle.update()
eggtf();score(False)
def goLeft():
    while 1:time.sleep(0.1);x,y=snakeList[0].pos();go(x-20,y)
def goUp():
    while 1:time.sleep(0.1);x,y=snakeList[0].pos();go(x,y+20)
def goRight():
    while 1:time.sleep(0.1);x,y=snakeList[0].pos();go(x+20,y)
def goDown():
    while 1:time.sleep(0.1);x,y=snakeList[0].pos();go(x,y-20)
turtle.listen();turtle.onkeypress(goLeft,'Left');turtle.onkeypress(goRight,'Right');turtle.onkeypress(goDown,'Down');turtle.onkeypress(goUp,'Up')

老规矩 天龙八步走:

第一步.先导入模块#不写趋势

import turtle,random,time#这个贪吃蛇比较复杂 模块都要三个

第二步.基础设置#不写趋势

turtle.setup(600,600)#设置一个 600像素*600像素 的窗口
f,snakeList=5,[]#小蛇的身体#初始为5分
a=turtle.Screen()#设置窗口“a”
a.bgcolor('white')#把背景颜色设置成白色(括号里可以改)
turtle.tracer(False)#从现在开始以最快速度运行speed也不管用

第三步.辅助线#视力好的可以不写(除了最后一行)

bg=turtle.Turtle()
bg.pencolor('gray')#把辅助线换成灰色(括号里可以改)
for i in range(31):#画横线的for循环
    bg.pu()#抬笔 写penup()也行
    bg.goto(-300,-300+i*20)#换个位置
    bg.pd()#落笔 写pendown()也行
    bg.fd(600)#前进 写forward()也行
bg.left(90)#换成画竖线的方向
for j in range(31):#画竖线的for循环
    bg.pu()
    bg.goto(-300+j*20,300)#换个位置
    bg.pd()
    bg.bk(600)
bg.ht()#隐藏画笔bg 写hideturtle也行
turtle.update()#刷新,并停止tracer的行为

 第四步.给蛇 蛋 吃#不写蛇长不高

def eggtf():#定义eggt(鸡蛋投放)f函数
    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)#确保是20的倍数
    egg.shape('square')#呈现方形的蛋

第五步. 画蛇 并给蛇一个温馨的列表#不写趋势

for i in range(5):#画5个身子
    t=turtle.Turtle()#这个身子的画笔名
    t.shape('square')#设置方形蛇身子
    t.pu()#不让蛇身子留下痕迹
    t.goto(i*20,0)#去下一个地方
    snakeList.append(t)#往列表里这个身子

第六步.得分展示#不想写的可以不写

word=turtle.Turtle()
def score(tf):
    if tf:#如果tf是True运行下列代码
        global word
        word.ht()
        word.clear()#删掉上次的得分
    word=turtle.Turtle()
    word.pu()
    word.goto(-270,270)#去指定位置
    word.write('得分:'+str(f),font=('微软雅黑',18))#写得分
    word.ht()
score(False)#没的删所以不删

第七步.跑路函数#一定要写

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')#方形身体
    elif x2>300 or y2>300 or x2<-300 or y2<-300:#侦测是否撞到墙
        for i in snakeList:#遍历身体
            i.reset()#回城
    turtle.update()#更新
def goLeft():#向左
    while 1:
        time.sleep(0.1)
        x,y=snakeList[0].pos()
        go(x-20,y)
def goUp():#向上
    while 1:
        time.sleep(0.1)
        x,y=snakeList[0].pos()
        go(x,y+20)
def goRight():#向右
    while 1:
        time.sleep(0.1)
        x,y=snakeList[0].pos()
        go(x+20,y)
def goDown():#向下
    while 1:
        time.sleep(0.1)
        x,y=snakeList[0].pos()
        go(x,y-20)

第八步.监听键盘

turtle.listen()#开始监听
turtle.onkeypress(goLeft,'Left')#左键
turtle.onkeypress(goRight,'Right')#右键
turtle.onkeypress(goDown,'Down')#下键(没特殊意义)
turtle.onkeypress(goUp,'Up')#上键

其实完全拆开有93行 有很多函数因为短所以占行 因此我并一起了

最终结果:1742字节

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值