首頁 > 軟體

Python繪製時鐘的範例程式碼

2022-02-11 10:03:16

匯入需要的包設定變數

from datetime import datetime
from pygame.locals import *
import sys, math, pygame


def print_text(font, x, y, text, color=(255, 255, 255)):
    img_text = font.render(text, True, color)
    screen.blit(img_text, (x, y))


def wrap_angle(angle):
    return angle % 360


pygame.init()
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption("線上時鐘")

# 設定字型
font1 = pygame.font.SysFont("方正粗黑宋簡體", 24)
orange = 220, 180, 0
white = 255, 255, 255
yellow = 255, 255, 0
pink = 255, 100, 100

pos_x = 300
pos_y = 250
radius = 250
angle = 360

這裡繪製一個600*500的螢幕,設定圓心位置及其半徑。根據時間的不同,計算弧度,通過三角函數計算出對應的座標,從圓心的位置繪製出時針的線條。

寫數位

通過迴圈1到12,將數位寫在對應的位置上,-10 和對應的字型微調使其更加對正

    for n in range(1, 13):
        angle = math.radians(n * (360 / 12) - 90)
        x = math.cos(angle) * (radius - 20) - 10
        y = math.sin(angle) * (radius - 20) - 10
        print_text(font1, pos_x + x, pos_y + y, str(n))

繪製時針

這裡通過時間計算出對應的角度,然後從圓心到指定位置畫時針。

    hour_angle = wrap_angle(hours * (360/12) - 90) + wrap_angle(minutes * (360/60) - 90) / 60
    hour_angle = math.radians(hour_angle)
    hour_x = math.cos(hour_angle) * (radius - 80)
    hour_y = math.sin(hour_angle) * (radius - 80)
    target = (pos_x + hour_x, pos_y + hour_y)
    pygame.draw.line(screen, pink, (pos_x, pos_y), target, 25)

完整程式碼

from datetime import datetime
from pygame.locals import *
import sys, math, pygame


def print_text(font, x, y, text, color=(255, 255, 255)):
    img_text = font.render(text, True, color)
    screen.blit(img_text, (x, y))


def wrap_angle(angle):
    return angle % 360


pygame.init()
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption("線上時鐘")

# 設定字型
font1 = pygame.font.SysFont("方正粗黑宋簡體", 24)
orange = 220, 180, 0
white = 255, 255, 255
yellow = 255, 255, 0
pink = 255, 100, 100

pos_x = 300
pos_y = 250
radius = 250
angle = 360

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    keys = pygame.key.get_pressed()
    if keys[K_ESCAPE]:
        pygame.quit()
        sys.exit()

    screen.fill((154, 205, 255))
    pygame.draw.circle(screen, white, (pos_x, pos_y), radius, 6)

    for n in range(1, 13):
        angle = math.radians(n * (360 / 12) - 90)
        x = math.cos(angle) * (radius - 20) - 10
        y = math.sin(angle) * (radius - 20) - 10
        print_text(font1, pos_x + x, pos_y + y, str(n))

    today = datetime.today()
    hours = today.hour % 12
    minutes = today.minute
    seconds = today.second

    hour_angle = wrap_angle(hours * (360/12) - 90) + wrap_angle(minutes * (360/60) - 90) / 60
    hour_angle = math.radians(hour_angle)
    hour_x = math.cos(hour_angle) * (radius - 80)
    hour_y = math.sin(hour_angle) * (radius - 80)
    target = (pos_x + hour_x, pos_y + hour_y)
    pygame.draw.line(screen, pink, (pos_x, pos_y), target, 25)

    min_angle = wrap_angle(minutes * (360/60) - 90)
    min_angle = math.radians(min_angle)
    min_x = math.cos(min_angle) * (radius - 60)
    min_y = math.sin(min_angle) * (radius - 60)
    target = (pos_x + min_x, pos_y + min_y)
    pygame.draw.line(screen, orange, (pos_x, pos_y), target, 12)

    sec_angle = wrap_angle(seconds * (360/60) - 90)
    sec_angle = math.radians(sec_angle)
    sec_x = math.cos(sec_angle) * (radius - 40)
    sec_y = math.sin(sec_angle) * (radius - 40)
    target = (pos_x + sec_x, pos_y + sec_y)
    pygame.draw.line(screen, yellow, (pos_x, pos_y), target, 6)

    pygame.draw.circle(screen, white, (pos_x, pos_y), 20)
    print_text(font1, 0, 0, str(hours) + ":" + str(minutes) + ":" + str(seconds))

    pygame.display.update()

以上就是Python繪製時鐘的範例程式碼的詳細內容,更多關於Python繪製時鐘的資料請關注it145.com其它相關文章!


IT145.com E-mail:sddin#qq.com