ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2020-02-03 09:02:57  阅读:350  来源: 互联网

标签:angle 自定义 python px float int pygame math


1.效果图

2.完整代码

#第1步:导出模块
import sys, random, math, pygame
from pygame.locals import *
from datetime import datetime, 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, 255
yellow = 255, 255, 0
pink = 255, 100, 100
caogreen=0, 100, 100  #草绿色
deepskyblue=30,144,255
black=0,0,0

#第5步:定义全局变量
px=700 #px=pos_x=初始的x位置,也就是水平移动700,从左往中间(右→)移动
py=250 #250就是时钟的圆点距离顶格水平线的距离=半径radius=250
radius = 250
angle = 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步:定义绑定的角度
def wrap_angle(angle):
    return angle % 360

#第8步:循环定义,必须必的
while True:
    #退出循环的方法有2种
    #方法一
    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, 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() #循环内的显示要不断地刷新,因为是动态的
View Code

 

标签:angle,自定义,python,px,float,int,pygame,math
来源: https://www.cnblogs.com/ysysbky/p/12254600.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有