时空猎人@vemax 2023-07-27 08:31 采纳率: 40%
浏览 9
已结题

俺的这个代码为啥不显示,找不出原因



```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() # 将所有内容更新到屏幕上显示出来


```

  • 写回答

4条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2023-07-27 10:03
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    本回答被专家选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 8月8日
  • 专家已采纳回答 7月31日
  • 创建了问题 7月27日