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

前文:【3】

增加难度系统(不能碰到黑线)

把空格换为红线(同样不能碰到)

整理程序

#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")
t.speed(0)
g=turtle.Pen()
g.penup()
g.goto(-300,-10)
g.pendown()
g.forward(600)
g.hideturtle()
g.speed(0)
d=turtle.Pen()
d.penup()
d.hideturtle()
d.goto(-150,200)
h=turtle.Pen()
h.penup()
h.goto(-300,300)
h.pendown()
h.hideturtle()
h.speed(0)
#变量设置
st=time.time()
sp=5
gd=0
ti=99999999999999999999999999999999999999999999999999
f=["-","-","-","-","-","-","-","-","-","-","-","-","-","-","-","-","-","-","-","-"]
timew=-1
nw=0
ld=[]

#函数
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
def hard():
    global ld
    for i in range(20):
        ld.append(random.randint(0,150))
#主窗口
d.write("得分:"+str(0)+"分",font=("宋体",30,"normal"))
hard()
for i in ld:
    h.sety(300-i)
    h.forward(30)
while True:
    #得分部分
    bf=int((time.time()-st)/2)
    if bf != nw:
        d.clear()
        d.write("得分:"+str(bf)+"分",font=("宋体",30,"normal"))
    nw=int((time.time()-st)/2)
    timew+=1
    #行动部分
    t.sety(gd*20)
    if tuple(t.position())[0]>300:
        timew=-1
        gd=0
        t.goto(-300,gd*20)
        g.clear()
        g.penup()
        g.goto(-300,-10)
        g.pendown()
        h.goto(-300,300)
        h.clear()
        hard()
        for i in ld:
            h.sety(300 - i)
            h.forward(30)
        f=make_road()
        for i in f:
            if i=="-":
                g.forward(30)
            else:
                g.color("red")
                g.forward(30)
                g.color("black")
    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)
                d.clear()
                d.write("得分:"+str(int((time.time()-st)/2))+"分,游戏结束",font=("宋体",30,"normal"))
                break
        else:
            s = (timew + 1) % 20
            if (300-ld[int(timew / int(600 / (20 * sp)))]) <= gd*20:
                t.setheading(-90)
                t.forward(1000)
                d.clear()
                d.write("得分:" + str(int((time.time() - st) / 2)) + "分,游戏结束", font=("宋体", 30, "normal"))
                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、付费专栏及课程。

余额充值