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

这篇博客展示了一个使用Python turtle模块编写的趣味游戏。游戏中,玩家控制角色跳跃避开障碍物,随着时间推移得分增加。游戏界面包含得分显示、随机生成的跑道以及按键响应等功能,代码实现详细且完整。

前文:【2】

好的基本内容完成了,我们来做一些优化。

【1】

计分系统

我打算还是用time,活一段时间就算一分!

那么可以在前面加上一句:

st=time.time()

yes.Perfect.

好然后我们来做一个写字的画笔,程序【全文】我直接写在这里了:

#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
st=time.time()
d=turtle.Pen()
d.penup()
d.hideturtle()
d.goto(-150,200)
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
nw=0
d.write("得分:"+str(0)+"分",font=("宋体",30,"normal"))
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
        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)
                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()

92lines,very long but perfect.

行。

---------------------------------未完下文:【4】---------------------------------------------------------

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Unconquerable p

给点吧~

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

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

打赏作者

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

抵扣说明:

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

余额充值