我使用pygame和python 3.7创建了一个游戏,其中可以将中心(250,250)的图像拖动到屏幕周围并与半径碰撞,在这种情况下会发生断裂,并且下一个循环也会在第一个图像所在的确切中心产生。尽管游戏按照我的意图进行工作,但从原理上讲,它对于快速的鼠标速度还是很奇怪的。在我的最小示例代码中,对于每个while循环,彩色圆圈应该重新出现在确切的中心,但是,它们以某种方式无法正确更新,因此在大多数情况下都不会出现在屏幕的中心(只有在我真的很早/适时地释放鼠标按钮时才这样做)。我在Windows和Mac上测试了游戏,发现在Mac上,“滞后”似乎更加严重。如果有人知道我该如何解决,我将非常感激。同样,游戏开始滞后并立即跳到下一个循环,以实现真正快速的鼠标移动,而我通过更改外部鼠标的速度来解决。 pygame中鼠标移动太快有固有的解决方法吗?感谢您提出的所有建议,以及可能对我的代码思想进行的其他改进。
import pygame
import time
from time import sleep
import math
pygame.init()
gameDisplay = pygame.display.set_mode((500,500))
clock = pygame.time.Clock()
background_surface = pygame.Surface((500,500))
background_surface.fill((255,255,255))
colors = [(0,0),(253,45,(249,253,(32,201,5),(0,210,235)]
for color in colors:
done = False
a,b = 250,250
u,v = 250,250
while not done:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.MOUSEMOTION:
if event.buttons[0]:
a += event.rel[0]
b += event.rel[1]
rect = pygame.Rect(u,v,0.001,0.001)
radius = 200
corners = [rect.bottomleft,rect.bottomright,rect.topleft,rect.topright]
dist = [math.sqrt((p[0] - a) ** 2 + (p[1] - b) ** 2) for p in corners]
p_out = [i for i,d in enumerate(dist) if d > radius]
if any(p_out):
break
gameDisplay.blit(background_surface,0))
pygame.draw.circle(gameDisplay,color,(a,b),50,0)
pygame.draw.circle(gameDisplay,(250,250),200,2)
pygame.display.flip()
sleep(0.7)
pygame.quit()
quit()