吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 274|回复: 14
收起左侧

[学习记录] 关于Python的罗盘代码

[复制链接]
yangxuan 发表于 2025-3-19 16:24
本帖最后由 yangxuan 于 2025-3-19 16:33 编辑

有二个版本,但是还是显示不全,可能是我的显示器的原因

其中需要的模块,自己看import头部需要导入的模块

第一个代码是原始版
CSDN计算机那,套用的


以下为初代

[Python] 纯文本查看 复制代码
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import math
import pygame
import sys
import tkinter
from datetime import datetime
 
tk = tkinter.Tk()
width = tk.winfo_screenwidth()
height = tk.winfo_screenheight()
tk.quit()
 
 
def print_text(font, x, y, text, angle, color=(255, 255, 255)):
    """定义一个用于输出指定位置和角度文本的函数"""
    img_text = font.render(text, True, color)
    img_text = pygame.transform.rotate(img_text, angle)
    screen.blit(img_text, (x, y))
 
 
def cycle_text(cirText, bins, today_xx, cirRadius, font, color=(255, 255, 255)):
    """
    定义一个输出循环文本的函数
    cirText: 环形循环文本,如日期和时间的中文
    bins: 圆需要分为多少等分,如秒需要分成60等分
    today_xx: 接收当前时间(月日周时分秒)的具体数值
    cirRadius: 指定环形文本的半径
    font: 指定使用的字体
    """
    for i in range(1, len(cirText) + 1):
        # c_angle: 旋转一次的角度
        c_angle = math.radians(360 / bins) * (today_xx - i)
        # t_angle: 环上每个独立文本的角度
        t_angle = 0 - (360 / bins) * (today_xx - i)
        # add_x: 环上每个独立文本的横坐标距离pos_x的距离
        add_x = math.cos(c_angle) * cirRadius
        # add_x: 环上每个独立文本的横坐标距离pos_x的距离
        add_y = math.sin(c_angle) * cirRadius
        # print_text(): 调用上面定义的函数,输出换上每个文本
        print_text(font, pos_x + add_x, pos_y + add_y, str(cirText[i - 1]), angle=t_angle, color=color)
 
 
# 初始化一个界面
pygame.init()
# 屏幕大小
screen = pygame.display.set_mode((width, height), pygame.FULLSCREEN)
 
# 标题
pygame.display.set_caption("Python Clock")
# 月,日,时,分,秒字体
font1 = pygame.font.Font("C:/Windows/Fonts/msyh.ttc", 16)
# 年字体
font2 = pygame.font.Font("C:/Windows/Fonts/msyh.ttc", 25)
# 周字体
font3 = pygame.font.Font("C:/Windows/Fonts/msyh.ttc", 22)
# 时钟中心
pos_x = width / 2
pos_y = (height / 2) - 30
r = 395
# 年月日周时分秒对应中文字符
secondsText = ["零壹秒", "零贰秒", "零叁秒", "零肆秒", "零伍秒", "零陆秒", "零柒秒", "零捌秒", "零玖秒", "壹拾秒", "拾壹秒", "拾贰秒", "拾叁秒",
               "拾肆秒", "拾伍秒", "拾陆秒", "拾柒秒", "拾捌秒", "拾玖秒", "贰拾秒", "贰拾壹秒", "贰拾贰秒", "贰拾叁秒", "贰拾肆秒", "贰拾伍秒",
               "贰拾陆秒", "贰拾柒秒", "贰拾捌秒", "贰拾玖秒", "叁拾秒", "叁拾壹秒", "叁拾贰秒", "叁拾叁秒", "叁拾肆秒", "叁拾伍秒", "叁拾陆秒",
               "叁拾柒秒", "叁拾捌秒", "叁拾玖秒", "肆拾秒", "肆拾壹秒", "肆拾贰秒", "肆拾叁秒", "肆拾肆秒", "肆拾伍秒", "肆拾陆秒", "肆拾柒秒",
               "肆拾捌秒", "肆拾玖秒", "伍拾秒", "伍拾壹秒", "伍拾贰秒", "伍拾叁秒", "伍拾肆秒", "伍拾伍秒", "伍拾陆秒", "伍拾柒秒", "伍拾捌秒",
               "伍拾玖秒", "零   秒"]
minuteText = ["零壹分", "零贰分", "零叁分", "零肆分", "零伍分", "零陆分", "零柒分", "零捌分", "零玖分", "壹拾分", "拾壹分", "拾贰分", "拾叁分",
              "拾肆分", "拾伍分", "拾陆分", "拾柒分", "拾捌分", "拾玖分", "贰拾分", "贰拾壹分", "贰拾贰分", "贰拾叁分", "贰拾肆分", "贰拾伍分",
              "贰拾陆分", "贰拾柒分", "贰拾捌分", "贰拾玖分", "叁拾分", "叁拾壹分", "叁拾贰分", "叁拾叁分", "叁拾肆分", "叁拾伍分", "叁拾陆分",
              "叁拾柒分", "叁拾捌分", "叁拾玖分", "肆拾分", "肆拾壹分", "肆拾贰分", "肆拾叁分", "肆拾肆分", "肆拾伍分", "肆拾陆分", "肆拾柒分",
              "肆拾捌分", "肆拾玖分", "伍拾分", "伍拾壹分", "伍拾贰分", "伍拾叁分", "伍拾肆分", "伍拾伍分", "伍拾陆分", "伍拾柒分", "伍拾捌分",
              "伍拾玖分", "零   分"]
hourText = ["零壹点", "零两点", "零叁点", "零肆点", "零伍点", "零陆点", "零柒点", "零捌点", "零玖点", "壹拾点", "拾壹点", "拾贰点",
            "拾叁点", "拾肆点", "拾伍点", "拾陆点", "拾柒点", "拾捌点", "拾玖点", "贰拾点", "贰拾壹点", "贰拾贰点", "贰拾叁点", "零   点"]
weekText = ["星期壹", "星期贰", "星期叁", "星期肆", "星期伍", "星期陆", "星期日"]
dayText = ["零壹日", "零贰日", "零叁日", "零肆日", "零伍日", "零陆日", "零柒日", "零捌日", "零玖日", "壹拾日", "拾壹日", "拾贰日",
           "拾叁日", "拾肆日", "拾伍日", "拾陆日", "拾柒日", "拾捌日", "拾玖日", "贰拾日", "贰拾壹日", "贰拾贰日", "贰拾叁日",
           "贰拾肆日", "贰拾伍日", "贰拾陆日", "贰拾柒日", "贰拾捌日", "贰拾玖日", "叁拾日", "叁拾壹日"]
monthText = ["零壹月", "零贰月", "零叁月", "零肆月", "零伍月", "零陆月", "零柒月", "零捌月", "零玖月", "拾  月", "拾壹月", "拾贰月"]
# 为了更方便的提取年对应的中文字符,先自动生成一个数字列表,将第2020-2050个元素更换为2020年-2050年对应的中分字符
yearText = list(range(1, 3000))
yearText[2020:2051] = ["贰零贰零年", "贰零贰壹年", "贰零贰贰年", "贰零贰叁年", "贰零贰肆年", "贰零贰伍年", "贰零贰陆年", "贰零贰柒年",
                       "贰零贰捌年", "贰零贰玖年", "贰零叁零年", "贰零叁壹年", "贰零叁贰年", "贰零叁叁年", "贰零叁肆年", "贰零叁伍年",
                       "贰零叁陆年", "贰零叁柒年", "贰零叁捌年", "贰零叁玖年", "贰零肆零年", "贰零肆壹年", "贰零肆贰年", "贰零肆叁年",
                       "贰零肆肆年", "贰零肆伍年", "贰零肆陆年", "贰零肆柒年", "贰零肆捌年", "贰零肆玖年", "贰零伍零年"]
 
while True:
    # 鼠标点击x或按下键盘esc键时退出
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
    keys = pygame.key.get_pressed()
    if keys[pygame.K_ESCAPE]:
        sys.exit()
    # 填充背景颜色
    screen.fill(color=(0, 115, 120))
    # screen.fill(color=(0, 0, 0))
 
    # 绘制一条表明当前日期时间的红线
    # pygame.draw.circle(screen, color="red", center=(pos_x, pos_x), radius=10, width=5,)
    pygame.draw.line(screen, color="red", start_pos=(pos_x+15, pos_y + 25), end_pos=(pos_x+r+60, pos_y + 25), width=2)
 
    # 获取当前年份
    year = datetime.today().year
    # 输出当前年份
    print_text(font2, pos_x - 35, pos_y - 15, text=yearText[year], angle=0, color="red")
 
    # 获取当前月份
    months = datetime.today().month
    # 输出月环(会根据月份的变化,顺时针方向旋转,红线处为当前月份)
    cycle_text(cirText=monthText, bins=12, today_xx=months, cirRadius=r - 290, font=font1, color="white")
    # 天数这里需要考虑闰年和非闰年以及每月天数不一致的情况
    days = datetime.today().day
    if months in [1, 3, 5, 7, 8, 10, 12]:
        cycle_text(cirText=dayText, bins=31, today_xx=days, cirRadius=r - 225, font=font1, color="white")
    elif months in [4, 6, 9, 11]:
        cycle_text(cirText=dayText[0:-1], bins=30, today_xx=days, cirRadius=r - 225, font=font1, color="white")
    elif months == 2:
        if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
            '''如果是闰年'''
            cycle_text(cirText=dayText[0:-2], bins=29, today_xx=days, cirRadius=r - 225, font=font1, color="white")
        else:
            '''如果是非闰年'''
            cycle_text(cirText=dayText[0:-3], bins=28, today_xx=days, cirRadius=r - 225, font=font1, color="white")
    # 以下周,时,分,秒与月环相似
    weeks = datetime.today().isoweekday()
    print_text(font3, pos_x - 8, pos_y + 30, text=weekText[weeks-1], angle=0, color="blue")
    # cycle_text(cirText=weekText, bins=7, today_xx=weeks, cirRadius=r - 185, font=font1, color="white")
    hours = datetime.today().hour % 24
    cycle_text(cirText=hourText, bins=24, today_xx=hours, cirRadius=r - 150, font=font1, color="white")
    minutes = datetime.today().minute
    cycle_text(cirText=minuteText, bins=60, today_xx=minutes, cirRadius=r - 75, font=font1, color="white")
    seconds = datetime.today().second
    cycle_text(cirText=secondsText, bins=60, today_xx=seconds, cirRadius=r, font=font1, color="white")
    # 最后别忘记刷新一下界面
    pygame.display.update()





改良版

窗口显示改良

[Asm] 纯文本查看 复制代码
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import math
import pygame
import sys
import tkinter
from datetime import datetime
 
# 获取屏幕尺寸
tk = tkinter.Tk()
width = tk.winfo_screenwidth()
height = tk.winfo_screenheight()
tk.quit()
 
def print_text(font, x, y, text, angle, color=(255, 255, 255)):
    """定义一个用于输出指定位置和角度文本的函数"""
    img_text = font.render(text, True, color)
    img_text = pygame.transform.rotate(img_text, angle)
    screen.blit(img_text, (x, y))
 
def cycle_text(cirText, bins, today_xx, cirRadius, font, color=(255, 255, 255)):
    """
    定义一个输出循环文本的函数
    cirText: 环形循环文本,如日期和时间的中文
    bins: 圆需要分为多少等分,如秒需要分成60等分
    today_xx: 接收当前时间(月日周时分秒)的具体数值
    cirRadius: 指定环形文本的半径
    font: 指定使用的字体
    """
    for i in range(1, len(cirText) + 1):
        c_angle = math.radians(360 / bins) * (today_xx - i)
        t_angle = 0 - (360 / bins) * (today_xx - i)
        add_x = math.cos(c_angle) * cirRadius
        add_y = math.sin(c_angle) * cirRadius
        print_text(font, pos_x + add_x, pos_y + add_y, str(cirText[i - 1]), angle=t_angle, color=color)
 
def get_gan_zhi(year, month, day):
    """计算天支地干"""
    # 天干地支的起始年份为1900年
    base_year = 1900
    gan = ["甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"]
    zhi = ["子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"]
 
    # 计算从1900年到当前年的总年数
    total_years = year - base_year
    # 计算天干地支索引
    gan_index = (total_years - 1) % 10
    zhi_index = (total_years - 1) % 12
 
    return f"{gan[gan_index]}{zhi[zhi_index]}年"
 
# 初始化一个界面
pygame.init()
screen = pygame.display.set_mode((width, height), pygame.RESIZABLE)
pygame.display.set_caption("Python Clock")
 
# 字体设置
font1 = pygame.font.Font("C:/Windows/Fonts/msyh.ttc", 16)
font2 = pygame.font.Font("C:/Windows/Fonts/msyh.ttc", 25)
font3 = pygame.font.Font("C:/Windows/Fonts/msyh.ttc", 22)
 
# 时钟中心
pos_x = width / 2
pos_y = (height / 2) - 30
r = 395
 
# 中文字符列表
secondsText = [f"零{i}秒" if i < 10 else f"{i}秒" for i in range(60)]
minuteText = [f"零{i}分" if i < 10 else f"{i}分" for i in range(60)]
hourText = [f"零{i}点" if i < 10 else f"{i}点" for i in range(24)]
weekText = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"]
dayText = [f"零{i}日" if i < 10 else f"{i}日" for i in range(1, 32)]
monthText = [f"零{i}月" if i < 10 else f"{i}月" for i in range(1, 13)]
 
# 动态生成年份的中文表示
yearText = {year: f"{year}年" for year in range(1900, 2051)}
 
# 背景颜色和背景图
background_color = (0, 0, 0)
background_image = None  # 可以提供背景图链接并加载
 
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.VIDEORESIZE:
            screen = pygame.display.set_mode(event.size, pygame.RESIZABLE)
            width, height = event.size
            pos_x = width / 2
            pos_y = (height / 2) - 30
 
    keys = pygame.key.get_pressed()
    if keys[pygame.K_ESCAPE]:
        sys.exit()
 
    screen.fill(background_color)
 
    if background_image:
        screen.blit(background_image, (0, 0))
 
    # 绘制一条表明当前日期时间的红线
    pygame.draw.line(screen, color="red", start_pos=(pos_x + 15, pos_y + 25), end_pos=(pos_x + r + 60, pos_y + 25), width=2)
 
    # 获取当前时间
    now = datetime.now()
    year = now.year
    month = now.month
    day = now.day
    hour = now.hour
    minute = now.minute
    second = now.second
    week = now.isoweekday()
 
    # 输出当前年份(包括天支地干)
    gan_zhi = get_gan_zhi(year, month, day)
    print_text(font2, pos_x - 35, pos_y - 15, text=f"{yearText.get(year, f'{year}年')} ({gan_zhi})", angle=0, color="white")
 
    # 输出月环
    cycle_text(cirText=monthText, bins=12, today_xx=month, cirRadius=r - 290, font=font1, color="white")
 
    # 输出天环
    if month in [1, 3, 5, 7, 8, 10, 12]:
        cycle_text(cirText=dayText, bins=31, today_xx=day, cirRadius=r - 225, font=font1, color="white")
    elif month in [4, 6, 9, 11]:
        cycle_text(cirText=dayText[:-1], bins=30, today_xx=day, cirRadius=r - 225, font=font1, color="white")
    elif month == 2:
        if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
            cycle_text(cirText=dayText[:-2], bins=29, today_xx=day, cirRadius=r - 225, font=font1, color="white")
        else:
            cycle_text(cirText=dayText[:-3], bins=28, today_xx=day, cirRadius=r - 225, font=font1, color="white")
 
    # 输出周
    print_text(font3, pos_x - 8, pos_y + 30, text=weekText[week - 1], angle=0, color="white")
 
    # 输出小时环
    cycle_text(cirText=hourText, bins=24, today_xx=hour, cirRadius=r - 150, font=font1, color="white")
 
    # 输出分钟环(缩小距离)
    cycle_text(cirText=minuteText, bins=60, today_xx=minute, cirRadius=r - 80, font=font1, color="white")
 
    # 输出秒环(缩小距离)
    cycle_text(cirText=secondsText, bins=60, today_xx=second, cirRadius=r - 50, font=font1, color="white")
 
    pygame.display.update()






发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

chaleaoch 发表于 2025-3-19 17:09
感觉发错了板块. 应该去, 编程语言求助区.
gilliugen 发表于 2025-3-19 17:37
pygame.init()这里

改手动试试
width = 1024
height = 768

还有你发错板块了,大佬不太爱来这里
 楼主| yangxuan 发表于 2025-3-19 18:46
chaleaoch 发表于 2025-3-19 17:09
感觉发错了板块. 应该去, 编程语言求助区.

不是,我咋,谁给我干这儿来了。
原本就是要让那些同志,思考问题,需要导入那些模块的
 楼主| yangxuan 发表于 2025-3-19 19:28
gilliugen 发表于 2025-3-19 17:37
pygame.init()这里

改手动试试

emmm,转过来,就回不去了
 楼主| yangxuan 发表于 2025-3-19 19:29
gilliugen 发表于 2025-3-19 17:37
pygame.init()这里

改手动试试

在这里怎么添加
imcuer 发表于 2025-3-20 08:44
这里能够显示完整的转动表盘
微信截图_20250320084448.png
 楼主| yangxuan 发表于 2025-3-20 09:00
imcuer 发表于 2025-3-20 08:44
这里能够显示完整的转动表盘

我的笔记本屏幕显示不全,上下位置
gilliugen 发表于 2025-3-20 09:09

width = 1024
height = 768
screen = pygame.display.set_mode((width, height), pygame.RESIZABLE)

根据你的笔记本需求改吧
gilliugen 发表于 2025-3-20 09:16
而且这一段很别扭
# 中文字符列表
secondsText = [f"零{i}秒" if i < 10 else f"{i}秒" for i in range(60)]
minuteText = [f"零{i}分" if i < 10 else f"{i}分" for i in range(60)]
hourText = [f"零{i}点" if i < 10 else f"{i}点" for i in range(24)]
weekText = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"]
dayText = [f"零{i}日" if i < 10 else f"{i}日" for i in range(1, 32)]
monthText = [f"零{i}月" if i < 10 else f"{i}月" for i in range(1, 13)]

为什么加个前导零呢,是这种罗盘有什么讲究吗
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-3-30 21:06

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表