做一个新的项目:滚动的小球[2]

前文:【1】

好的我们继续。

昨天的方案被推翻了:我又有了新方案。

就是:

让小球每按上一次,就可以增加高度值1,以此类推。

那么这就好办了:

先创建一个高度变量,再在函数里引用,把它加上1(当然要把它声明全局)

于是乎就这样:

gd=0
def jump():
    global gd
    gd+=1

对吧,木得毛病。

好的再让小球去到这个坐标就好了。

那么想要让小球去到这个坐标,我们就必须把小球也声明全局。所以我们把原来的global语句换成了这个:

global gb,t

然后我本人是不想用goto的:因为还要使用横坐标值。

于是乎我们就这么写(zuo)。不过,为了让跳起来的样子明显一点,我们要把gd变量乘上20.

    t.sety(gd*20)

木得毛病!

这是整个函数的代码:

gd=0
def jump():
    global gd,t
    gd+=1
    t.sety(gd*20)

好的程序现在67行了。继续干~

再就是调用函数了(捕捉键盘)

故我们要使用onkey或者onkeypress。

我用的是onkey。

【在While循环内】

turtle.onkey(jump,"Up")

不过我也不知道为什么,turtle必须先按一下<Tab>键才能捕获键盘动作。

反正现在能跳起来了!

但是,我们还要判断,当跳起来时即使有坑也不会掉进去。

套上一个if吧,以后再编起来还方便~

好的现在是这一整个程序:

#coding=utf-8
import turtle
import time
import random
turtle.title("project:ball")
turtle.bgcolor("skyblue")
turtle.setup(600,600,0,0)
t=turtle.Pen()
t.penup()
t.shape("circle")
t.goto(-300,0)
t.fillcolor("red")
g=turtle.Pen()
g.penup()
g.goto(-300,-10)
g.pendown()
g.forward(600)
g.hideturtle()
sp=5
t.speed(0)
g.speed(0)
gd=0
def jump():
    global gd,t
    gd+=1
    t.sety(gd*20)
def make_road():
    global g
    rd=["-","-","-"]
    for i in range(17):
        a=random.randint(1,2)
        if a==1 and not( rd[-1]==rd[-2]==rd[-3]==" "):
            rd.append(" ")
        else:
            rd.append("-")
    return rd
f=["-","-","-","-","-","-","-","-","-","-","-","-","-","-","-","-","-","-","-","-"]
timew=-1
while True:
    timew+=1
    if tuple(t.position())[0]>300:
        timew=-1
        t.goto(-300,0)
        g.clear()
        g.penup()
        g.goto(-300,-10)
        g.pendown()
        f=make_road()
        for i in f:
            if i=="-":
                g.forward(30)
            else:
                g.penup()
                g.forward(30)
                g.pendown()
    else:
        if gd==0:
            s=(timew+1)%20
            if f[int(timew/int(600/(20*sp)))]=="-":
                t.circle(0,-180)
                t.setheading(0)
                t.forward(sp)
            else:
                t.setheading(-90)
                t.forward(300)
                break
        else:
            t.circle(0, -180)
            t.setheading(0)
            t.forward(sp)
    turtle.onkey(jump,"Up")
    time.sleep(0.01)
turtle.mainloop()

啊啊啊73行了!加油继续干!!!

好的现在至少跳起来不会判断掉坑了~

继续……续……续续干!!!!!

首先有一件小事:

原来的第43行

t.goto(-300,0)

因为高度不能每次刷新都变成0,所以要改成:

t.goto(-300,gd*20)

那么目前我们还需要做到一个目标:要下落!(来自牛顿的笑容)

那么我们就在else(67行)里搞事情吧!

这是我的代码:

 对不起图配错了

这就是我最终的代码:::

#coding=utf-8
import turtle
import time
import random
turtle.title("project:ball game----make it interesting and make you interested.")
turtle.bgcolor("skyblue")
turtle.setup(600,600,0,0)
t=turtle.Pen()
t.penup()
t.shape("circle")
t.goto(-300,0)
t.fillcolor("red")
g=turtle.Pen()
g.penup()
g.goto(-300,-10)
g.pendown()
g.forward(600)
g.hideturtle()
sp=5
t.speed(0)
g.speed(0)
gd=0
ti=99999999999999999999999999999999999999999999999999
def jump():
    global gd,t,ti
    ti=int(time.time())
    gd+=1
    t.sety(gd*20)
def make_road():
    global g
    rd=["-","-","-"]
    for i in range(17):
        a=random.randint(1,2)
        if a==1 and not( rd[-1]==rd[-2]==rd[-3]==" "):
            rd.append(" ")
        else:
            rd.append("-")
    return rd
f=["-","-","-","-","-","-","-","-","-","-","-","-","-","-","-","-","-","-","-","-"]
timew=-1
while True:
    timew+=1
    t.sety(gd*20)
    if tuple(t.position())[0]>300:
        timew=-1
        t.goto(-300,gd*20)
        g.clear()
        g.penup()
        g.goto(-300,-10)
        g.pendown()
        f=make_road()
        for i in f:
            if i=="-":
                g.forward(30)
            else:
                g.penup()
                g.forward(30)
                g.pendown()
    else:
        if gd==0:
            s=(timew+1)%20
            if f[int(timew/int(600/(20*sp)))]=="-":
                t.circle(0,-180)
                t.setheading(0)
                t.forward(sp)
            else:
                t.setheading(-90)
                t.forward(300)
                print("over")
                break
        else:
            t.circle(0, -180)
            t.setheading(0)
            t.forward(sp)
            if int(time.time())-ti==1:
                gd-=1
    turtle.onkey(jump,"Up")
    time.sleep(0.01)
turtle.mainloop()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Unconquerable p

给点吧~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值