Pygame window not responding when I try to exit the window in the middle of execution or when I spam click on the window

Roni

I'm trying to run the following code:

import pygame

WIDTH, HEIGHT = 800, 500
FPS = 60
DR_RUIN_WIDTH, DR_RUIN_HEIGHT = 800, 1200
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Dr. Ruin Rap Performance")
BG = pygame.transform.scale(pygame.image.load("files/bg.png"), (WIDTH, HEIGHT)).convert()
DR_RUIN_IMAGE = pygame.image.load("files/drruinsprite.png")
DR_RUIN = pygame.transform.scale(DR_RUIN_IMAGE, (DR_RUIN_WIDTH, DR_RUIN_HEIGHT))
DR_RUIN_BACK_IMAGE = pygame.image.load("files/drruinspriteback.png")
DR_RUIN_BACK = pygame.transform.scale(DR_RUIN_BACK_IMAGE, (DR_RUIN_WIDTH, DR_RUIN_HEIGHT))
BLUE = "#7c15ea"


def draw_window():
    WIN.blit(BG, (0, 0))
    WIN.blit(DR_RUIN, (15, -275))
    pygame.draw.rect(WIN, BLUE, pygame.Rect(0, 450, 800, 50))
    pygame.display.update()
    pygame.time.delay(14000)
    WIN.blit(DR_RUIN_BACK, (15, -275))
    pygame.display.update()
    pygame.time.delay(1000)
    WIN.blit(DR_RUIN, (15, -275))
    pygame.display.update()


def main():
    clock = pygame.time.Clock()
    run = True
    while run:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                break

        draw_window()
    pygame.quit()
    exit()


if __name__ == "__main__":
    pygame.mixer.init()
    song = pygame.mixer.Sound("files/ruinsong (2).mp3")
    pygame.mixer.music.set_volume(0.7)
    song.play()
    main()

When I try to exit the program while it's running, the window isn't responding and a message pops up informing me that the window isn't responding. It also happens when I spam click on the window. Can someone please help me fix this?

Rabbid76

how do I manage it so that the program waits 14 seconds after displaying the first image, and then displays the next image?

Use pygame.time.get_ticks() to return the number of milliseconds since pygame.init() was called. Calculate the time that has passed since the application loop started:

def main():
    clock = pygame.time.Clock()
    start_time = pygame.time.get_ticks()
    run = True
    while run:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
        
        current_time = pygame.time.get_ticks()
        elapsed_time = current_time - start_time
        if elapsed_time > 15000:
            start_time = current_time 
 
        draw_window(elapsed_time)
        
    pygame.quit()
    exit()

Draw the scene depending on the elapsed time:

def draw_window(elapsed_time):
    WIN.blit(BG, (0, 0))

    if elapsed_time > 14000:
        WIN.blit(DR_RUIN_BACK, (15, -275))
    else:
        WIN.blit(DR_RUIN, (15, -275))
    
    pygame.draw.rect(WIN, BLUE, pygame.Rect(0, 450, 800, 50))
    pygame.display.update()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Want jTextField to lose focus when I click on the main window

Prevent modal window scrolling up when I click into ace editor

Pygame Python window stops responding

Pygame: window not responding

C#: Window hangs when I try to move it, click outside or minimize

pygame screen only works when i move the window off the screen

I keep receiving an attribute error in Pygame when attempting to create a window

When I try pygame, window closes when I open it

Pygame window freezes when it opens

Tkinker window not responding when clicked on the "Exit"

Pygame window not responding error when i try to run a code

When I click my JButton, the JLabel doesn't show on the window,

How to create a new window when I click a button in QT?

pygame window not responding when clicked

How can I make a window appear on top of another window, without closing when I click the second?

How to set an action when I click outside of the popup window?

How do I lock a window to part of the screen without it disappearing when I click on the window underneath?

window freezes when I try to show an evolving value in a Qt window using threads with python

how to open a new window and close the current window when I click the button on java GUI?

pygame window keeps not responding

App crashes when I click the button that opens a Popup window

When I run my Python Turtle game, the Graphics Window stops responding

How to open a new window when i click one button?

How resize whole window when I drag window with mouse

How to make tkinter's window not show "not responding" when click at window

Pygame is opening a not responding window

Pygame window crashes when I use pygame.image.load()

My Pygame window only updates when i am moving my cursor around on the window

Tkinter button executes when the window opens, and not when I click it