• pygame实现时钟


    pygame实现的逻辑

    1.时钟的刻度通过python的math函数cos,sin来获取,x,y,在通过pygame的字体对象进行书写

    2.时钟怎么动,通过pygame的帧率实现动态的加载

    3.秒,分在12点时会异常丢失

    原因是因为i的取值在(1,61)之间,而获取系统时间的时候,只有(0,59)
    应该在i=60的时候进行标识,从而在秒,分的时候可以识别出来
    
    • 1
    • 2

    代码

    import math
    import random
    import pygame
    import time
    from pygame.locals import *
    # 随机颜色
    def color():
        r = random.randint(0, 255)
        g = random.randint(0, 255)
        b = random.randint(0, 255)
        return (r,g,b)
    # 初始化
    pygame.init()
    # 创建窗口
    window=pygame.display.set_mode((600,600))
    pygame.display.set_caption("zengtian")
    cc=color()
    window.fill(cc)
    # 常量
    R_X=300
    R_Y=300
    R=200
    # 创建字体
    font = pygame.font.Font(None, 20)
    def font(text,x,y,font=font,color=(255,255,255)):
        te=font.render(text,True,color)
        window.blit(te,(x,y))
    # 帧率
    fps=pygame.time.Clock()
    
    # 刷新
    while True:
        # 获取当前时间
        wood = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
        window.fill(cc)
        font(wood,225,25)
        r = random.randint(100, 255)
        g = random.randint(100, 200)
        b = random.randint(200, 255)
        pygame.draw.circle(window, color(), (R_X, R_Y), R, 2)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()
        for i in range(1,61):
            # 获取每秒的弧长,从而计算出,x,y,
            an=math.radians((360/60)*i-90)
            x = math.cos(an) * R * 0.92
            y = math.sin(an) * R * 0.92
            if i % 5 == 0:
                font(str(i // 5), R_X + x, R_Y + y)
            # 解决 i==60时,在图中显示异常问题,原因是因为获取时间时,没有0
            j=i
            if i==60:
                j=0
            # 秒
            if j == int(wood[-2:]):
                pygame.draw.aaline(window, (b,g,r), [R_X, R_Y], [R_X + x * 0.95, R_Y + y * 0.95], 1)
            if j == int(wood[-5:-3]):
                pygame.draw.line(window, (g,r,b), [R_X, R_Y], [R_X + x * 0.85, R_Y + y * 0.85], 3)
    
            if int(wood[-8:-6])<=12:
                if i  == int(wood[-8:-6])*5:
                    pygame.draw.line(window,(r,g,b) , [R_X, R_Y],[round((R_X + x * 0.5)), round((R_Y + y * 0.5))], 5)
            if int(wood[-8:-6])>=12:
                if i == (int(wood[-8:-6]) -12)* 5:
                    pygame.draw.line(window, (r, g, b), [R_X, R_Y], [round((R_X + x * 0.5)), round((R_Y + y * 0.5))], 5)
    
                # if i-12  == int(wood[-8:-6])*5:
                #     pygame.draw.line(window,(r,g,b) , [R_X, R_Y],[round((R_X + x * 0.5)), round((R_Y + y * 0.5))], 5)
        fps.tick(1)
        pygame.display.update()
    
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
  • 相关阅读:
    第52节:cesium 3DTiles模型特效+选中高亮(含源码+视频)
    无线终端掉线问题专题
    《六月集训》(第二十二天)——有序集合
    Microsoft兼容性遥测是什么?Microsoft兼容性遥测占用高磁盘
    泰拉瑞亚EasyBuildMod便捷建造模组开发详细过程
    机器故障预测:未来24小时的决胜时刻!!!
    Spring Cloud Alibaba Nacos 配置中心 (配置持久化与动态刷新) 实战
    知识蒸馏算法汇总
    每日一考-9.11
    如何轻松学习Java
  • 原文地址:https://blog.csdn.net/weixin_48192346/article/details/127786283