Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pygame window goes unresponsive randomly after a few clicks

Tags:

python

pygame

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()
like image 890
CashRegister Avatar asked Nov 21 '25 18:11

CashRegister


1 Answers

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
like image 197
Rabbid76 Avatar answered Nov 24 '25 09:11

Rabbid76



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!