Python 은 Pygame 을 사용 하여 시 계 를 그립 니 다.
전제 조건:
pygame 설치 필요
기능:
1.화면 초기 화 시계 화면 표시
2.현재 시간 에 따라 시침,분침,초침 의 이동 실현
import pygame, sys, random, math
from datetime import datetime
from pygame.locals import *
def print_text(font, x, y, text, color=(255, 255, 255)):
img_text = font.render(text, True, color)
screen.blit(img_text, (x, y))
pygame.init()
#
screen = pygame.display.set_mode((600, 500))
#
pygame.display.set_caption(" ")
#
font1 = pygame.font.Font(None, 24)
#
pos_x = 300
pos_y = 250
#
radius = 250
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
keys = pygame.key.get_pressed()
if keys[K_ESCAPE]:
sys.exit()
screen.fill((0, 0, 100))
color = r, g, b
pygame.draw.circle(screen, color, (pos_x, pos_y), radius, 6)
# 1-12
for i in range(1, 13):
angle = math.radians((360 / 12) * i - 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(i))
#
hour = datetime.today().hour % 12 #
hour_angle = math.radians((360 / 12) * hour - 90)
hour_x = math.cos(hour_angle) * (radius - 90)
hour_y = math.sin(hour_angle) * (radius - 90)
pygame.draw.line(screen, (255, 0, 0), (pos_x, pos_y), (pos_x + hour_x, pos_y + hour_y), 12)
#
minutes = datetime.today().minute #
minutes_angle = math.radians((360 / 60) * minutes - 90)
minutes_x = math.cos(minutes_angle) * (radius - 70)
minutes_y = math.sin(minutes_angle) * (radius - 70)
pygame.draw.line(screen, (0, 255, 0), (pos_x, pos_y), (pos_x + minutes_x, pos_y + minutes_y), 8)
#
seconds = datetime.today().second #
seconds_angle = math.radians((360 / 60) * seconds - 90)
seconds_x = math.cos(seconds_angle) * (radius - 30)
seconds_y = math.sin(seconds_angle) * (radius - 30)
pygame.draw.line(screen, (0, 0, 255), (pos_x, pos_y), (pos_x + seconds_x, + pos_y + seconds_y), 4)
#
pygame.draw.circle(screen, (255, 255, 255), (pos_x, pos_y), 10)
pygame.display.update()실행 결과:
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.