下面是关于用Pygame实现时钟效果的完整攻略,包含了步骤、代码示例和详细说明。
步骤
- 导入Pygame库。因为本文所讲的内容涉及到窗口绘图操作,所以需要用到Pygame库。
python
import pygame
- 初始化Pygame。在继续之前,需要对Pygame进行初始化。
python
pygame.init()
- 设定窗口大小。根据自己的需要,设定窗口的大小。
python
screen_width = 400
screen_height = 400
screen = pygame.display.set_mode((screen_width, screen_height))
- 设定字体样式。在后续绘制时钟时,需要指定字体的样式和字体大小。
python
font_size = 50
font = pygame.font.Font(None, font_size)
- 设定颜色。为时钟和文字指定颜色。
python
bg_color = (255, 255, 255) # 背景颜色
clock_color = (0, 0, 0) # 时钟的颜色
text_color = (255, 0, 0) # 文字的颜色
- 获取当前时间。可以使用Python的time模块获取当前时间。
python
current_time = time.strftime('%H:%M:%S')
- 在屏幕上绘制时钟。使用Pygame的draw模块绘制时钟。
python
pygame.draw.circle(screen, clock_color, (int(screen_width/2), int(screen_height/2)), int(screen_width/3), 3) # 画一个圆
for i in range(12):
angle = i*30*math.pi/180
pos = (int(screen_width/2+math.sin(angle)*screen_width/5), int(screen_height/2-math.cos(angle)*screen_width/5))
pygame.draw.circle(screen, clock_color, pos, 10, 0) # 画12个刻度线
pygame.draw.line(screen, text_color, (int(screen_width/2), int(screen_height/2)), (int(screen_width/2+math.sin(hour_angle)*screen_width/8), int(screen_height/2-math.cos(hour_angle)*screen_width/8)), int(screen_width/80)) # 时针
pygame.draw.line(screen, text_color, (int(screen_width/2), int(screen_height/2)), (int(screen_width/2+math.sin(minute_angle)*screen_width/6), int(screen_height/2-math.cos(minute_angle)*screen_width/6)), int(screen_width/160)) # 分针
pygame.draw.line(screen, text_color, (int(screen_width/2), int(screen_height/2)), (int(screen_width/2+math.sin(second_angle)*screen_width/5), int(screen_height/2-math.cos(second_angle)*screen_width/5)), int(screen_width/320)) # 秒针
- 在屏幕上绘制文字。为了更好地显示当前时间,在时钟下面绘制当前时间的文字。
python
text = font.render(current_time, True, text_color)
text_rect = text.get_rect()
text_rect.centerx = int(screen_width/2)
text_rect.top = int(screen_height/2)+int(screen_width/3)
screen.blit(text, text_rect) # 在屏幕上绘制文字
- 刷新屏幕。完成绘制之后,需要刷新屏幕。
python
pygame.display.flip()
- 监听事件。在程序最后,监听用户的事件并退出程序。
python
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
示例
示例一
import pygame
import math
import time
import sys
pygame.init()
# 设定窗口大小
screen_width = 400
screen_height = 400
screen = pygame.display.set_mode((screen_width, screen_height))
# 设定字体和大小
font_size = 50
font = pygame.font.Font(None, font_size)
# 设定颜色
bg_color = (255, 255, 255) # 背景颜色
clock_color = (0, 0, 0) # 时钟的颜色
text_color = (255, 0, 0) # 文字的颜色
while True:
# 获取当前时间
current_time = time.strftime('%H:%M:%S')
# 计算时针、分针、秒针的角度
hour_angle = int(current_time.split(':')[0])*30*math.pi/180
minute_angle = int(current_time.split(':')[1])*6*math.pi/180
second_angle = int(current_time.split(':')[2])*6*math.pi/180
# 绘制时钟和文字
screen.fill(bg_color)
pygame.draw.circle(screen, clock_color, (int(screen_width/2), int(screen_height/2)), int(screen_width/3), 3)
for i in range(12):
angle = i*30*math.pi/180
pos = (int(screen_width/2+math.sin(angle)*screen_width/5), int(screen_height/2-math.cos(angle)*screen_width/5))
pygame.draw.circle(screen, clock_color, pos, 10, 0)
pygame.draw.line(screen, text_color, (int(screen_width/2), int(screen_height/2)), (int(screen_width/2+math.sin(hour_angle)*screen_width/8), int(screen_height/2-math.cos(hour_angle)*screen_width/8)), int(screen_width/80))
pygame.draw.line(screen, text_color, (int(screen_width/2), int(screen_height/2)), (int(screen_width/2+math.sin(minute_angle)*screen_width/6), int(screen_height/2-math.cos(minute_angle)*screen_width/6)), int(screen_width/160))
pygame.draw.line(screen, text_color, (int(screen_width/2), int(screen_height/2)), (int(screen_width/2+math.sin(second_angle)*screen_width/5), int(screen_height/2-math.cos(second_angle)*screen_width/5)), int(screen_width/320))
text = font.render(current_time, True, text_color)
text_rect = text.get_rect()
text_rect.centerx = int(screen_width/2)
text_rect.top = int(screen_height/2)+int(screen_width/3)
screen.blit(text, text_rect)
# 刷新屏幕
pygame.display.flip()
# 监听退出事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
示例二
import pygame
import math
import time
import sys
pygame.init()
# 设定窗口大小
screen_width = 500
screen_height = 500
screen = pygame.display.set_mode((screen_width, screen_height))
# 设定字体和大小
font_size = 80
font = pygame.font.Font(None, font_size)
# 设定颜色
bg_color = (255, 255, 255) # 背景颜色
clock_color = (0, 0, 0) # 时钟的颜色
text_color = (255, 0, 0) # 文字的颜色
while True:
# 获取当前时间
current_time = time.strftime('%H:%M:%S')
# 计算时针、分针、秒针的角度
hour_angle = int(current_time.split(':')[0])*30*math.pi/180
minute_angle = int(current_time.split(':')[1])*6*math.pi/180
second_angle = int(current_time.split(':')[2])*6*math.pi/180
# 绘制时钟和文字
screen.fill(bg_color)
pygame.draw.circle(screen, clock_color, (int(screen_width/2), int(screen_height/2)), int(screen_width/3), 3)
for i in range(60):
angle = i*6*math.pi/180
pos_outer = (int(screen_width/2+math.sin(angle)*screen_width/3), int(screen_height/2-math.cos(angle)*screen_width/3))
if i % 5 == 0:
pygame.draw.circle(screen, clock_color, pos_outer, 10, 0)
else:
pos_inner = (int(screen_width/2+math.sin(angle)*screen_width/3.5), int(screen_height/2-math.cos(angle)*screen_width/3.5))
pygame.draw.circle(screen, clock_color, pos_inner, 5, 0)
pygame.draw.line(screen, text_color, (int(screen_width/2), int(screen_height/2)), (int(screen_width/2+math.sin(hour_angle)*screen_width/5), int(screen_height/2-math.cos(hour_angle)*screen_width/5)), int(screen_width/20))
pygame.draw.line(screen, text_color, (int(screen_width/2), int(screen_height/2)), (int(screen_width/2+math.sin(minute_angle)*screen_width/3.5), int(screen_height/2-math.cos(minute_angle)*screen_width/3.5)), int(screen_width/30))
pygame.draw.line(screen, text_color, (int(screen_width/2), int(screen_height/2)), (int(screen_width/2+math.sin(second_angle)*screen_width/3), int(screen_height/2-math.cos(second_angle)*screen_width/3)), int(screen_width/60))
text = font.render(current_time, True, text_color)
text_rect = text.get_rect()
text_rect.centerx = int(screen_width/2)
text_rect.top = int(screen_height/4)*3
screen.blit(text, text_rect)
# 刷新屏幕
pygame.display.flip()
# 监听退出事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
结束语
以上就是使用Pygame实现时钟效果的完整攻略,包括了步骤、代码示例和详细说明。希望大家可以参考本文修改自己的代码,达到更好的效果。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:pygame实现时钟效果 - Python技术站