processing源码分享(1)----时钟

这篇文章介绍了使用Python编程实现的一个时钟功能,包括三角函数绘制、时间组件显示、颜色渐变矩形、数字显示以及鼠标交互。代码展示了如何在图形界面中创建动态时间和地点信息,以及简单的图形元素控制。
摘要由CSDN通过智能技术生成

抽空做了个time(时钟)......

import math
def trianglefunction(x,y,r,an):
    xz=x+r*math.cos(an*3.141592653589793238462643383279502884797169399875105820474944/180   ) 
    yz=y+r*math.sin(an*3.141592653589793238462643383279502884797169399875105820474944/180   )
    return xz,yz
def time():
    return year(),month(),day(),hour(),minute(),second()
def colorrect(x,y,x2,y2,xx):
    if xx==1:
        co3=0
        co3j=255/(float(y2)-float(y))
    else:
        co3=255
        co3j=0-255/(float(y2)-float(y))
    yy=y
    while yy<y2:
        yy=yy+1
        co3=co3+co3j
        stroke(co3)
        line(x,yy,x2,yy)
    stroke(0)
def number():
    num=-2
    while num<10:
        textSize(40)
        xt,yt=trianglefunction(387.5,415,270,num*30)
        text(num+3,xt,yt)
        xk1,yk1=trianglefunction(400,400,299,num*30)
        xk2,yk2=trianglefunction(400,400,290,num*30)
        line(xk1,yk1,xk2,yk2)
        num=num+1
    num=1
    while num<61:
        strokeWeight(3)
        xk1,yk1=trianglefunction(400,400,299,num*6)
        xk2,yk2=trianglefunction(400,400,295,num*6)
        line(xk1,yk1,xk2,yk2)
        num=num+1
    num=1
    textSize(30)
    while num<25:
        if num<10:
            text(' '+str(num)+' h',730,num*30+20)
        else:
            text(str(num)+'h',730,num*30+20)
        num=num+1
        line(790,num*30-20,800,num*30-20)
        fill(0)
def Rect():
    y,mon,d,h,m,s=time()
    fill('#6CFFF4')
    noStroke()
    rect(300,250,200,90)
    fill(0)
    stroke(0)
    textSize(40)
    text('tcw',367.5,280)
    textSize(30)
    text(str(y)+'-'+str(mon)+'-'+str(d),310,320)
def Time():
    global xs,ys,tf
    fill(255)
    strokeWeight(5)
    y,mon,d,h,m,s=time()
    ellipse(400,400,600,600)
    fill(0)
    ellipse(400,400,10,10)
    Rect()
    yj()
    strokeWeight(2)
    xs,ys=trianglefunction(400,400,250,s*6-90)
    line(400,400,xs,ys)
    strokeWeight(5)
    xm,ym=trianglefunction(400,400,200,m*6-90+s*1/12)
    line(400,400,xm,ym)
    strokeWeight(7)
    xh,yh=trianglefunction(400,400,150,h*30-90+m*6/12)
    line(400,400,xh,yh)
def kdx():
    h=hour()
    m=minute()
    stroke('#FF0000')
    line(805,h*30+10+m/2,905,h*30+10+m/2)
    if h>=0 and h<6:
        fill('#EAEAEA')
        STR='Lin Chen'
    if h>=6 and h<9:
        fill('#FF0000')
        STR='Ru Chu'
    if h>=9 and h<11:
        fill('#FF903B')
        STR='Shang Wu'
    if h>=11 and h<13:
        fill('#FF766A')
        STR='Zhong Wu'
    if h>=13 and h<18:
        fill('#FF5E00')
        STR='Xia Wu'
    if h>=18 and h<21:
        fill('#D6AA09')
        STR='Wan Shang'
    if h>=21:
        fill('#FF0000')
        STR='Ri Luo'
    text(STR,810,h*30+m/2)
    stroke(0)
def yj():
    global xs,ys
    fill(255)
    strokeWeight(2)
    stroke(255,0,0)
    rect(xs-40,ys-30,80,35)
    stroke(0)
    fill(255,0,0)
    text('100$',xs-40,ys)
    fill(255)
    ellipse(250,400,100,100)
    ellipse(550,400,100,100)
    xe1=xe2=xs
    ye1=ye2=ys
    if xe1<225:
        xe1=225
    if xe1>275:
        xe1=275
    if xe2<525:
        xe2=525
    if xe2>575:
        xe2=575
#   -----------
    if ye1<375:
        ye1=375
    if ye1>425:
        ye1=425
    if ye2<375:
        ye2=375
    if ye2>425:
        ye2=425
    fill(0)
    ellipse(xe1,ye1,10,10)
    ellipse(xe2,ye2,10,10)
def setup():
    global run,yun,xs,ys
    xs=0
    ys=0
    yun=['Fu ru dong hai',
         'Shou bi nan shan',
         'Gong xi fa cai',
         'Sheng ti jian kang',
         'Wan shi ru yi',
         'Nian nian you yv',
         'Xiao kou chang kai',
         'Yi fan fong shun',
         'He jia huan le',
         'Bu bu gao shen',
         'Ji xiang ru yi',
         'Long ma jing sheng']
    run=True
    size(1000,800)
def draw():
    tc=time()
    background(255)
    global r
    Time()
    number()
    colorrect(805,10,905,210,1)
    colorrect(805,540,905,780,-1)
    kdx()
    if run:
        r=random(0,len(yun))
    fill(255,0,0)
    text(yun[int(r)],280,70)
def mousePressed():
    global run
    if run:
        run=False
    elif not(run):
        run=True

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值