跟随我的鼠标但留在圆圈上的弧线

An arc that follows my mouse but stays on the circle

我试图让圆弧跟随我的鼠标,同时保持在圆形路径上。

我不知道为什么它不起作用。

当我 运行 它时,它只是创建一个 pygame 白屏,没有错误。

这是我的代码:

from __future__ import division 
from math import atan2, degrees, pi
import math
import pygame

pygame.init()
screen = pygame.display.set_mode((800, 800))
pygame.display.set_caption("globe")
CENTER = (400, 400)
RADIUS = 350

running = True

def drawCircleArc(screen,color,center,radius,startDeg,endDeg,thickness):

    (x,y) = center
    rect = (x-radius,y-radius,radius*2,radius*2)
    startRad = math.radians(startDeg)
    endRad = math.radians(endDeg)
    pygame.draw.arc(screen,color,rect,startRad,endRad,thickness)


while running:

  for event in pygame.event.get():
    if event.type == pygame.QUIT: 
        running = False

  mouse = pygame.mouse.get_pos()

  relx = mouse[0] - CENTER[0]
  rely = mouse[1] - CENTER[1]
  rad = atan2(-rely,relx)
  rad %= 2*pi
  degs = degrees(rad)


  screen.fill((152,206,231))

  drawCircleArc(screen,(243,79,79),CENTER,RADIUS, degs + 90,degs + 100 ,10)


  pygame.draw.circle(screen, (71,153,192), CENTER, RADIUS)
  pygame.display.update()

pygame.quit()

Picture Picture2

我真正想要的是下图 谢谢

我认为下面会做你想做的。我解决了两个问题:

  1. 你画的图顺序错了,把红色的短弧给遮住了(需要从后往前画),

  2. 您添加到计算的 degs 角度的两个文字值太大。

我还进行了其他一些并非严格需要的更改,包括按照 PEP 8 - Style Guide for Python Code 准则重新格式化代码并添加 pygame.time.Clock 以降低刷新率比较合理。

from __future__ import division
from math import atan2, degrees, pi
import math
import pygame

pygame.init()
screen = pygame.display.set_mode((800, 800))
pygame.display.set_caption("globe")
clock = pygame.time.Clock()
FPS = 60  # Frames per sec
CENTER = (400, 400)
RADIUS = 350

running = True

def draw_circle_arc(screen, color, center, radius, start_deg, end_deg, thickness):
    x, y = center
    rect = (x-radius, y-radius, radius*2, radius*2)
    start_rad = math.radians(start_deg)
    end_rad = math.radians(end_deg)
    pygame.draw.arc(screen, color, rect, start_rad, end_rad, thickness)

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    mouse = pygame.mouse.get_pos()
    relx = mouse[0] - CENTER[0]
    rely = mouse[1] - CENTER[1]
    rad = atan2(-rely, relx)
    degs = degrees(rad)

    screen.fill((152,206,231))
    pygame.draw.circle(screen, (71,153,192), CENTER, RADIUS)
    draw_circle_arc(screen, (243,79,79), CENTER, RADIUS, degs-10, degs+10, 10)
    pygame.display.update()
    clock.tick(FPS)

pygame.quit()

这是它的样子运行