对于pygame中的新循环,变量的值无法正确更新

我使用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()
yehuan19840620 回答:对于pygame中的新循环,变量的值无法正确更新

  

[...]它们某种程度上无法正确更新,因此在大多数情况下不会重新出现在屏幕中央(仅当我确实非常早/适当地释放鼠标按钮时才出现)。 ..]

当然,请调查您的代码。代码中发生的第一件事是修改了ab

while not done:
   # [...]

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]

如果pygame.MOUSEMOTION事件在队列中,则在绘制圆之前对其进行处理。因此,圆的位置被修改,因此不会出现在中心。
请注意,事件不能反映鼠标的当前状态。它是一个通知,发生时会存储在队列中,但是以后可以进行事件处理。您实际上要做的是处理可能在过去发生的事件。

使用状态dragging,该状态在每个循环开始时为False。在按下按钮时设置状态(pygame.MOUSEBUTTONDOWN事件),在释放按钮时复位状态(pygame.MOUSEBUTTONUP事件):

for color in colors:

    # [...]

    dragging = False
    while not done:

        clock.tick(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    dragging = True
            elif event.type == pygame.MOUSEBUTTONUP:
                if event.button == 1:
                    dragging = False
            elif event.type == pygame.MOUSEMOTION:
                if dragging:
                    a += event.rel[0]
                    b += event.rel[1]

        # [...]
本文链接:https://www.f2er.com/2954921.html

大家都在问