Python使用Pygame绘制时钟
作者:浩瀚蓝天dep 时间:2022-08-04 14:10:07
本文实例为大家分享了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()
运行结果:
来源:https://blog.csdn.net/LSW_JAVADP/article/details/110295573
标签:python,pygame,时钟
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python web框架 django wsgi原理解析
2021-11-21 02:44:59
python 如何快速复制序列
2022-12-04 05:20:36
![](https://img.aspxhome.com/file/2023/5/81845_0s.png)
python实现接口并发测试脚本
2023-04-20 05:06:15
python 实现图片裁剪小工具
2022-04-27 18:15:15
![](https://img.aspxhome.com/file/2023/9/104999_0s.png)
PyTorch 如何将CIFAR100数据按类标归类保存
2023-01-10 06:01:03
![](https://img.aspxhome.com/file/2023/6/89416_0s.png)
ASP初学者常犯的几个错误
2007-09-07 10:19:00
![](https://img.aspxhome.com/file/UploadPic/2007-1/2007130163317843s.gif)
ASP把数字用逗号每3位隔开显示代码
2008-01-02 13:11:00
![](https://img.aspxhome.com/file/UploadPic/20081/2/200812132551808s.gif)
PHP实现抓取HTTPS内容
2023-11-14 12:45:14
![](https://img.aspxhome.com/file/2023/4/80514_0s.png)
python实现决策树分类算法
2022-08-10 12:15:56
![](https://img.aspxhome.com/file/2023/2/99652_0s.jpg)
python抓取搜狗微信公众号文章
2021-10-25 17:56:08
![](https://img.aspxhome.com/file/2023/3/81653_0s.jpg)
python二维图制作的实例代码
2021-09-17 21:29:35
![](https://img.aspxhome.com/file/2023/5/99245_0s.jpg)
解决numpy矩阵相减出现的负值自动转正值的问题
2021-10-07 19:49:16
利用pyecharts读取csv并进行数据统计可视化的实现
2023-07-15 07:18:03
![](https://img.aspxhome.com/file/2023/6/84046_0s.png)
Python中处理字符串之endswith()方法的使用简介
2023-02-25 09:05:36
简单了解什么是神经网络
2023-10-11 22:26:34
![](https://img.aspxhome.com/file/2023/8/89318_0s.png)
PHP基于phpqrcode生成带LOGO图像的二维码实例
2023-11-23 23:47:18
Python 列表排序方法reverse、sort、sorted详解
2021-10-18 10:11:52
支持在线写SQL的Oracle学习免费网站推荐!(个人常使用)
2023-07-24 09:06:51
![](https://img.aspxhome.com/file/2023/6/64476_0s.jpg)
神经网络理论基础及Python实现详解
2023-04-01 20:48:23
![](https://img.aspxhome.com/file/2023/1/90791_0s.png)
OpenCV-Python实现图像梯度与Sobel滤波器
2021-04-07 05:23:20
![](https://img.aspxhome.com/file/2023/5/82535_0s.png)