python制作罗盘时钟效果
更新时间:2024年04月16日 10:01:53 作者:朝阳1
这篇文章主要介绍了python制作罗盘时钟效果,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧
背景图
import sys import math import pygame from datetime import datetime from pygame import display, image, font, transform # 创建罗盘时钟的窗口 def createWindow(): pygame.init() screen = display.set_mode((800, 700), pygame.RESIZABLE) display.set_caption("炫酷星空版罗盘时钟") return screen # 年月日转换中文字符 def timeConvertText(year, month, day): # 制作转换字符集 timeSet = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十", "二十一", "二十二", "二十三", "二十四", "二十五", "二十六", "二十七", "二十八", "二十九", "三十"] yearText = "" timeText = "" # 进行字符的拼接 while year > 0: num = int(year % 10) yearText = yearText + timeSet[num] year = year // 10 for i in range(yearText.__len__()): timeText += yearText[yearText.__len__() - i - 1] timeText += "年" timeText += timeSet[month] timeText += "月" timeText += timeSet[day] timeText += "日" return timeText # 制作罗盘时钟的表盘 def createClockDial(screen): # 设置时钟背景 background = image.load("./black.jpg") screen.blit(background, (0, 0)) # 坐标左上 # 导入字体文件 clockFont = font.Font('C:\Windows\Fonts\simhei.ttf', 18) # 设置时钟中心 clockCenterX = 380 clockCenterY = 320 # 时分秒数据集 hoursSet = ["一 点", "二 点", "三 点", "四 点", "五 点", "六 点", "七 点", "八 点", "九 点", "十 点", "十一点", "十二点", "十三点", "十四点", "十五点", "十六点", "十七点", "十八点", "十九点", "二十点", "二一点", "二二点", "二三点", "二四点"] minutesSet = ["一 分", "二 分", "三 分", "四 分", "五 分", "六 分", "七 分", "八 分", "九 分", "十 分", "十一分", "十二分", "十三分", "十四分", "十五分", "十六分", "十七分", "十八分", "十九分", "二十分", "二一分", "二二分", "二三分", "二四分", "二五分", "二六分", "二七分", "二八分", "二九分", "三十分", "三一分", "三二分", "三三分", "三四分", "三五分", "三六分", "三七分", "三八分", "三九分", "四十分", "四一分", "四二分", "四三分", "四四分", "四五分", "四六分", "四七分", "四八分", "四九分", "五十分", "五一分", "五二分", "五三分", "五四分", "五五分", "五六分", "五七分", "五八分", "五九分", "六十分"] secondsSet = ["一 秒", "二 秒", "三 秒", "四 秒", "五 秒", "六 秒", "七 秒", "八 秒", "九 秒", "十 秒", "十一秒", "十二秒", "十三秒", "十四秒", "十五秒", "十六秒", "十七秒", "十八秒", "十九秒", "二十秒", "二一秒", "二二秒", "二三秒", "二四秒", "二五秒", "二六秒", "二七秒", "二八秒", "二九秒", "三十秒", "三一秒", "三二秒", "三三秒", "三四秒", "三五秒", "三六秒", "三七秒", "三八秒", "三九秒", "四十秒", "四一秒", "四二秒", "四三秒", "四四秒", "四五秒", "四六秒", "四七秒", "四八秒", "四九秒", "五十秒","五一秒", "五二秒", "五三秒", "五四秒", "五五秒", "五六秒", "五七秒", "五八秒", "五九秒", "六十秒"] # 获取当前时间 year = datetime.today().year month = datetime.today().month day = datetime.today().day hour = datetime.today().hour minute = datetime.today().minute second = datetime.today().second # 时间字符转换 yearMonthDayText = timeConvertText(year, month, day) # 设置样式颜色 yearMonthDayText = clockFont.render(yearMonthDayText, True, (255, 0, 0)) # 指定位置绘制 screen.blit(yearMonthDayText, (clockCenterX - 60, clockCenterY)) # 绘制小时罗盘 for i in range(hoursSet.__len__()): if i == 0: hourText = clockFont.render(hoursSet[hour], True, (255, 0, 0)) else: hourText = clockFont.render(hoursSet[hour], True, (255, 255, 255)) hourText = transform.rotate(hourText, 0 - (15 * i)) hourTextX = int(math.cos(math.radians(15 * i)) * 120) hourTextY = int(math.sin(math.radians(15 * i)) * 120) screen.blit(hourText, (clockCenterX + hourTextX, clockCenterY + hourTextY)) hour += 1 if hour == 24: hour = 0 # 绘制分钟罗盘 for j in range(minutesSet.__len__()): if j == 0: minuteText = clockFont.render(minutesSet[minute], True, (255, 0, 0)) else: minuteText = clockFont.render(minutesSet[minute], True, (255, 255, 255)) minuteText = transform.rotate(minuteText, 0 - (6 * j)) minuteTextX = int(math.cos(math.radians(6 * j)) * 200) minuteTextY = int(math.sin(math.radians(6 * j)) * 200) screen.blit(minuteText, (clockCenterX + minuteTextX, clockCenterY + minuteTextY)) minute += 1 if minute == 60: minute = 0 # 绘制秒种罗盘 for k in range(secondsSet.__len__()): if k == 0: secondText = clockFont.render(secondsSet[second], True, (255, 0, 0)) else: secondText = clockFont.render(secondsSet[second], True, (255, 255, 255)) secondText = transform.rotate(secondText, 0 - (6 * k)) secondTextX = int(math.cos(math.radians(6 * k)) * 280) secondTextY = int(math.sin(math.radians(6 * k)) * 280) screen.blit(secondText, (clockCenterX + secondTextX, clockCenterY + secondTextY)) second += 1 if second == 60: second = 0 display.update() if __name__ == "__main__": screen = createWindow() while True: # 设置监听事件 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() keys = pygame.key.get_pressed() if keys[pygame.K_ESCAPE]: pygame.quit() sys.exit() screen.fill((0,0,0)) createClockDial(screen)
到此这篇关于python制作罗盘时钟效果的文章就介绍到这了,更多相关python罗盘时钟内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
将tf.batch_matmul替换成tf.matmul的实现
这篇文章主要介绍了将tf.batch_matmul替换成tf.matmul的实现,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2020-06-06Python查找不限层级Json数据中某个key或者value的路径方式
今天小编就为大家分享一篇Python查找不限层级Json数据中某个key或者value的路径方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2020-02-02
最新评论