0

I am not sure if it is my code, or my IDE issue. Vscode windows will always become unresponsive and frozen when it plays the following animation/code mid-way.

It wont complete the whole animation as it crashes at around go("3", 255, 0, 0, 8)

The code are actually running fine in the background as the frame is still counting in the terminal.

I am curious as to how this can be fixed, is this software issue or my code was wrongly written that caused memory leak.



def go(t, x, y, z, s):

    if FRAME_NUM == s*FPS:
        txt = t
        showtext = pygame.font.SysFont(None, int(WIDTH/12))
        showtext = showtext.render(txt, True, (100,100,100))
        screen.fill((x, y, z))
        screen.blit(showtext, (int(WIDTH/8), int(WIDTH/8)))
        
        

def main():
    pygame.init()

    while True:
        pygame.event.pump()
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.display.quit()
                pygame.quit()
                exit()
        
        for i in range(0, 20*FPS):
            #screen.fill((0, 0, 0))
            go("1", 255, 255, 255, 2)
            go("2", 255, 255, 0, 4)
            go("3", 255, 0, 0,  8)
            go("4", 255, 255, 255, 12)
            go("5", 255, 255, 0, 16)
                
            pygame.display.update()
            global FRAME_NUM
            FRAME_NUM += 1
            print(FRAME_NUM)
            clock.tick(FPS)



        pygame.display.quit()
        pygame.quit()
        exit()

adrian li
  • 457
  • 8
  • 19
  • Do not try to use a loop inside the application loop to animate something. Use the application loop. You system is not responding, because you do not handel the events in the inner `for`-loop (the loop in the `go` function). You can "hide" the problem by calling `pygame.event.pump()` in the for-loop. But this does not solve the actual problem of the wrong design. – Rabbid76 Dec 06 '22 at 22:02
  • @Rabbid76 oh no, becuz my professor did the same `for-loop` in the function to control animation in his example so i thought it was the right way. to be fair his example also crashes on my pc so i thought it's software issue. i tried adding or replace `event.get()` with `pygame.event.pump():`, but return error `'NoneType' object is not iterable`. i will update my code there without a loop inside `go()` , the result was the same? – adrian li Dec 07 '22 at 00:22
  • No. Why did you replace `event.get()`? Just put `pygame.event.pump()` in the `for`-loop. You need additional event handling in the `for`-loop. (I suggest finding a better teacher) – Rabbid76 Dec 07 '22 at 13:35

0 Answers0