猫史档案馆


【Python作品分享】时钟

用户:淘气的炸弹人K3Pe淘气的炸弹人K3Pe查看:0 回复:0 评论:0 创建时间:2020-04-30T16:20:35


【作品展示】

center_image

 

【作品介绍】

时钟

 

【作品源代码】

from arcade import *
# 设置窗体宽和高
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 600
# 设置窗体标题
SCREEN_TITLE = "我的时钟"
#窗体中心坐标
CENTER_X = SCREEN_WIDTH//2
CENTER_Y = SCREEN_HEIGHT//2
class Pointer():
    def __init__(self,name,value,length,color,line_width):
        self.name=name
        self.value=value
        self.length=length
        self.color=color
        self.line_width=line_width
    def draw(self):
        if self.name=="hour":
            angle=360*self.value/12
        else:
            angle=360*self.value/60
        x,y=CENTER_X+self.length*math.sin(math.radians(angle)),CENTER_Y+self.length*math.cos(math.radians(angle))
        draw_line(CENTER_X,CENTER_Y,x,y,self.color,self.line_width)
class MyClock(arcade.Window):
    def __init__(self, width, height, title):
        super().__init__(width, height, title)
        #窗口初始化
        self.setup()
    def setup(self):
        #设置背景颜色
        set_background_color(color.WHITE)
    def on_draw(self):
        start_render()
        self.mydraw()
    def mydraw(self):
        #表盘
        r=200
        draw_circle_outline(CENTER_X, CENTER_Y,r,color.SKY_BLUE,20)
        #数字
        per=360/12
        for i in range(1,13):
            angle=per*i
            x,y,=CENTER_X + (r-20)*math.sin(math.radians(angle)),CENTER_Y+(r-20)*math.cos(math.radians(angle))
            draw_text(str(i),x,y,color.BLACK,20,font_name=("simhei","PingFang"),anchor_x="center",anchor_y="center")
        #指针
        import datetime
        d=datetime.datetime.now()
        h,m,s=d.hour,d.minute,d.second
        hour=Pointer("hour",h,70,color.BLUE,10)
        hour.draw()
        minute=Pointer("minute",m,120,color.BLUE,10)
        minute.draw()
        second=Pointer("second",s,150,color.RED,5)
        second.draw()
        #轴心
        draw_circle_filled(CENTER_X,CENTER_Y,10,color.BLACK)
        #年月日星期
        year,month,day,week=d.year,d.month,d.day,d.weekday()
        weeks=["一","二","三","四","五","六","日"]
        date=f"{year}年{month}月{day}日 星期{weeks[week]}" 
        draw_text(date,CENTER_X,CENTER_Y-250,color.BLACK,20,font_name=("simhei","PingFang"),anchor_x="center",anchor_y="center")
if __name__ == '__main__':
    game = MyClock(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
    run()

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页