```python
import pygame
import math
# 初始化pygame
pygame.init()
# 设置窗口大小和标题
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Compass")
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
CYAN = (0, 255, 255)
MAGENTA = (255, 0, 255)
GRAY = (128, 128, 128)
# 计算圆的半径和内圆、外圆的中心点坐标
radius = 120/2 # 将直径转换为半径
center_x = 400
center_y = 300
inner_radius = radius * 0.97 # 将半径乘以系数0.97得到内圆半径
outer_radius = radius * 1.03 # 将半径乘以系数1.03得到外圆半径
inner_center_x = center_x + inner_radius * math.cos(math.pi/12)
inner_center_y = center_y + inner_radius * math.sin(math.pi/12)
outer_center_x = center_x + outer_radius * math.cos(math.pi/12)
outer_center_y = center_y + outer_radius * math.sin(math.pi/12)
# 绘制外圆和内圆并标上刻度和数字
for i in range(36): # 每隔π/6画一个刻度线和对应的数字
theta = i * math.pi/6
x = outer_center_x + outer_radius * math.cos(theta) # 根据公式计算外圆切线段的端点x坐标
y = outer_center_y + outer_radius * math.sin(theta) # 根据公式计算外圆切线段的端点y坐标
pygame.draw.line(screen, YELLOW, (int(x), int(y)), (int(x+outer_radius*2), int(y))) # 用黄色画出外圆的切线段作为刻度线
text = str(int(round((i+1)*6))) # 将角度转化为数字并转换为字符串类型
label = font.render(text, True, GRAY) # 在屏幕上渲染文本标签
label_rect = label.get_rect() # 获取文本标签的位置和大小信息
label_rect.centerx = x + outer_radius * math.cos(theta) # 将文本标签的中心点移动到切线段的端点上
label_rect.centery = y + outer_radius * math.sin(theta) # 将文本标签的中心点移动到切线段的端点上
screen.blit(label, label_rect) # 将文本标签绘制到屏幕上指定位置上
for i in range(30): # 每隔π/12画一个刻度线和对应的数字
theta = i * math.pi/12
x = inner_center_x + inner_radius * math.cos(theta) # 根据公式计算内圆切线段的端点x坐标
y = inner_center_y + inner_radius * math.sin(theta) # 根据公式计算内圆切线段的端点y坐标
pygame.draw.line(screen, GRAY, (int(x), int(y)), (int(x+inner_radius*2), int(y))) # 用灰色画出内圆的切线段作为刻度线
text = str(int(round((i+1)*12))) # 将角度转化为数字并转换为字符串类型
label = font.render(text, True, GRAY) # 在屏幕上渲染文本标签
label_rect = label.get_rect() # 获取文本标签的位置和大小信息
label_rect.centerx = x + inner_radius * math.cos(theta) # 将文本标签的中心点移动到切线段的端点上
label_rect.centery = y + inner_radius * math.sin(theta) # 将文本标签的中心点移动到切线段的端点上
screen.blit(label, label_rect) # 将文本标签绘制到屏幕上指定位置上
# 填充背景色并显示窗口内容
screen.fill(WHITE) # 用白色填充整个窗口区域
pygame.display.flip() # 将所有内容更新到屏幕上显示出来
```
不知道你这个问题是否已经解决, 如果还没有解决的话:同问
在最后加上这段代码就好了
# 主事件循环
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# 退出游戏
pygame.quit()
fill应该放在开头调用,而不是末尾,否则将覆盖之前所有的绘制内容。
还有就是,尽量在循环中反复绘制刷新(如果你的界面是动态的)
while True:
... # draw something here
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
pygame.display.flip()
Python pygame(GUI编程)模块最完整教程(1)_pygame模块详解_Python-ZZY的博客-CSDN博客