一个弧线跟随我的鼠标,但停留在ci上

2024-10-17 08:25:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图让弧跟随我的鼠标,而停留在圆的路径

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

当我运行它时,它只创建一个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()

PicturePicture2

我真正想要的是下图 谢谢


Tags: fromimporteventdisplaymathscreenpygamerunning
1条回答
网友
1楼 · 发布于 2024-10-17 08:25:16

我想下面的就可以了。我解决了两个问题:

  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()

下面是跑步的样子

screenshot of script running showing arc and mouse position

相关问题 更多 >