单击几下后,pygame窗口随机发出反应症

发布于 2025-01-17 15:39:53 字数 2773 浏览 0 评论 0原文

我正在尝试制作一个类似于 https://aimtrainer.io 的游戏作为个人项目,并且我的 pygame 窗口一直在运行没有反应。我的程序将启动,但在单击 1-15 个圆圈后,它停止工作。只有当我单击一个圆圈时它才会无响应,窗口的其他部分都很好。我不知道该怎么做,任何建议都会受到赞赏。

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

I am trying to make a game similar to https://aimtrainer.io as a personal project and my pygame window keeps going unresponsive. My program will start, but after 1-15 circle clicks it stops working. It only goes unresponsive if I click a circle, other parts of the window are fine. I'm not sure what to do, any advice is appreciated.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

留蓝 2025-01-24 15:39:53

交叉点= 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

intersection = False must be set at the beginning of the loop, instead of before the loop, otherwise this will result in an endless loop once 2 circles intersect:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文