Pygame window 点击几次后随机无响应

Pygame window goes unresponsive randomly after a few clicks

我正在尝试制作一款类似于 https://aimtrainer.io 的游戏作为个人项目,但我的 pygame window 一直没有响应。我的程序将启动,但在 1-15 圈点击后它停止工作。如果我单击一个圆圈,它只会没有响应,window 的其他部分都很好。我不知道该怎么做,欢迎任何建议。

from random import randint
import pygame, math

WIDTH, HEIGHT = 750, 750
WINDOW = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Click the circles!")

BLACK = (25, 25, 25)
RED = (163, 0, 0)

RADIUS = 40
CIRCLE_COUNT = 5
targetList = []

FPS = 60

class Target:

    def __init__(self, x, y):
        self.x = x
        self.y = y

    def drawTarget(self):
        pygame.draw.circle(WINDOW, RED, (self.x, self.y), RADIUS)

    def inTarget(self, posx, posy):
        sqx = (posx - self.x) ** 2
        sqy = (posy - self.y) ** 2
        if math.sqrt(sqx + sqy) < RADIUS:
            return True

    def targetsDontIntersect(self, secondTarget):
        return math.sqrt((self.x - secondTarget.x) ** 2 + (self.y - secondTarget.y) ** 2) > RADIUS * 2 #True if they don't intersect

    def __str__(self) -> str:
        return "X is {} and Y is {}".format(self.x, self.y)

def addTarget():

    intersection = False
    while True:
        t = Target(randint(RADIUS, WIDTH - RADIUS), randint(RADIUS, HEIGHT - RADIUS))
        for t2 in targetList:
            if not t.targetsDontIntersect(t2):
                intersection = True
        
        if not intersection:
            targetList.append(t)
            break

def drawWindow():
    WINDOW.fill(BLACK)

    if len(targetList) < CIRCLE_COUNT:
        addTarget()
        
    for target in targetList:
        target.drawTarget()
    pygame.display.update()

# Add targets so that they don't intersect
def addTargets():
    targetList.append(Target(randint(RADIUS, WIDTH - RADIUS), randint(RADIUS, HEIGHT - RADIUS)))
    
    while len(targetList) < CIRCLE_COUNT:
        intersection = False
        t = Target(randint(RADIUS, WIDTH - RADIUS), randint(RADIUS, HEIGHT - RADIUS))

        for t2 in targetList:
            if (not t.targetsDontIntersect(t2)):
                intersection = True
                break
                
        if not intersection:
            targetList.append(t)
    
def main():
    global targetList
    clock = pygame.time.Clock()

    addTargets()
    drawWindow()

    while True:
        clock.tick(FPS)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
            elif event.type == pygame.MOUSEBUTTONUP:
                x, y = pygame.mouse.get_pos()
                targetList = [num for num in targetList if not num.inTarget(x, y)] #List of circles that weren't clicked
        drawWindow()

if __name__ == "__main__":
    main()

intersection = False 必须在循环开始时设置,而不是在循环之前设置,否则一旦 2 个圆相交将导致无限循环:

def addTarget():
   
    # intersection = False            <-- DELETE
    while True:
        intersection = False        # <-- INSERT
        
        t = Target(randint(RADIUS, WIDTH - RADIUS), randint(RADIUS, HEIGHT - RADIUS))
        for t2 in targetList:
            if not t.targetsDontIntersect(t2):
                intersection = True
        if not intersection:
            targetList.append(t)
            break