python制作动态时钟_python+pygame制作一个可自定义的动态时钟和详解

#第1步:导出模块

importsys, random, math, pygamefrom pygame.locals import *

from datetime importdatetime, date, time#第2步:初始化和窗扣大小的设置

pygame.init() #这一步必须放在模块之后,对pygame很重要#screen = pygame.display.set_mode((800, 600)) #定义窗口大小

screen = pygame.display.set_mode((1500, 1000),RESIZABLE,0,32)'''set_mode(size =(0,0),flags = 0,depth = 0)

默认flags = 0,depth = 32

size:(0,0)参数是一对表示宽度和高度的数字

depth:参数表示用于颜色的位数(通常最好不要传递深度参数。它将默认为系统的最佳和最快颜色深度),默认32,一般8~32

flags:参数控制您想要的显示类型。

有几种可供选择,您甚至可以使用按位或运算符(管道“|”字符)组合多种类型。

如果传递0或没有flags参数,它将默认为软件驱动的窗口。

FULLSCREEN=全屏,小程序不建议,大型游戏可以考虑;RESIZABLE=窗口可调节大小,不设置就是不能调节窗口大小'''pygame.display.set_caption("动态时钟DIY") #定义标题

#第3步:字体设置#font = pygame.font.Font(None, 36) #定义字体,注意如果是中文字体,需要额外设置#如果游戏界面内有中文,比如‘分数’等显示时,需要设置中文,可以指定字体和路径,如下本机设置

font = pygame.font.Font('/home/xgj/Desktop/simsun/simsun.ttf', 36) #显示中文的设置和字体,及路径

#第4步:颜色设置#pygame中的颜色采用RGB数值,故定义颜色名称=具体数值,会使代码简洁#当然也可以直接写数值,比如caogreen=0, 100, 100

orange = 220, 180, 0

white= 255, 255, 255yellow= 255, 255, 0

pink= 255, 100, 100caogreen=0, 100, 100 #草绿色

deepskyblue=30,144,255black=0,0,0#第5步:定义全局变量

px=700 #px=pos_x=初始的x位置,也就是水平移动700,从左往中间(右→)移动

py=250 #250就是时钟的圆点距离顶格水平线的距离=半径radius=250

radius = 250angle= 360 #一圈正好360°

'''注意:曾在本机python3.8报错,下面有注释和改进后的说明

DeprecationWarning: an integer is required (got type float).

Implicit conversion to integers using __int__ is deprecated,

and may be removed in a future version of Python.

报错的bug1、2、3的原因是target元组里的数是float,需要转换成int整数'''

#第6步:定义打印文字print_text,可多次使用,依次放置,注意位置,后面有举例#def print_text(font, x, y, text, color=(255, 255, 255)):#这里设置了打印字体的颜色了,后面再单独设置就不行,除非自己再单独定义函数print_text2

def print_text(font, x, y, text, color=white):

imgText=font.render(text, True, white)#screen.blit(imgText,x,y) #报错,修改前

screen.blit(imgText, (int(float(x)),int(float(y)))) #bug0,修改后,将float转换成int,改进了,搞定了

#第7步:定义绑定的角度

defwrap_angle(angle):return angle % 360

#第8步:循环定义,必须必的

whileTrue:#退出循环的方法有2种

#方法一

for event inpygame.event.get():if event.type ==QUIT:

sys.exit()#方法二

keys =pygame.key.get_pressed()ifkeys[K_ESCAPE]:

sys.exit()#定义屏幕背景颜色=填充颜色

#screen.fill((0, 100, 100)) #直接数值法,代码显得繁琐

screen.fill(deepskyblue)#画圈,屏幕上,粉红色圈线,位置,半径,粗细为6

pygame.draw.circle(screen, pink, (px, py), radius, 6)for n in range(1, 13): #定义时钟上面的1~12的小时刻度

angle = math.radians(n * (360 / 12) - 90) #角度

x = math.cos(angle) * (radius - 20) - 10 #x坐标

y = math.sin(angle) * (radius - 20) - 10 #y坐标

print_text(font, px + x, py + y, str(n)) #打印出来

#获取今天的时间

today =datetime.today()#定义年月日

years=today.year

months=today.month

days=today.day#定义时分秒

#hours = today.hour % 12 #显示12h制

hours = today.hour % 24 #显示24h制

minutes =today.minute

seconds=today.second#定义时针

hour_angle = wrap_angle(hours * (360 / 12) - 90)

hour_angle=math.radians(hour_angle)

hour_x= math.cos(hour_angle) * (radius - 80)

hour_y= math.sin(hour_angle) * (radius - 80)#target = (px + hour_x, py + hour_y) #修改前的bug

target = (int(float(px + hour_x)), int(float(py + hour_y))) #修改后,将float转换成int

pygame.draw.line(screen, pink, (px, py), target, 12) #本来bug1,后来不提示;时针设置,粉红色

#定义分针

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 = (px + min_x, py + min_y) #修改前的bug

target = (int(float(px + min_x)), int(float(py + min_y))) #修改后,将float转换成int

pygame.draw.line(screen, orange, (px, py), target, 6) #本来bug2,后来不提示,秒针设置,橘色

#定义秒针

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 = (px + sec_x, py + sec_y) #修改前的bug

target = (int(float(px + sec_x)), int(float(py + sec_y))) #修改后,将float转换成int

pygame.draw.line(screen, black, (px, py), target, 3) #本来提示bug3,后来不提示;分针设置,黄色

pygame.draw.circle(screen, white, (px, py),20) #钟面中心的圆点设置,白色,粗20

#显示当前时间,x=400,y=550,为坐标

print_text(font, 400, 550, '当前时间:'+str(years) +'年'+str(months) +'月'+str(days)+'日'+str(hours) +'时'+ ":" + str(minutes) + '分'+":" + str(seconds)+'秒')

print_text(font,400,600,'上联:火神山、雷神山、钟南山,三座大山镇魔妖')

print_text(font,400,650,'下联:军民心、医护心、爱国心,万众一心战新冠')

print_text(font,400,700,'横批:庚子新冠')

pygame.display.update()#循环内的显示要不断地刷新,因为是动态的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值