Noah Leuthold
Noah Leuthold

Reputation: 65

What can I do to make my PyGame window respond?

I am trying to make a window open every time I run my code, but every time I run the code, it shows the window as unresponsive.

My code is as follows:

import os
import time
import sys
pygame.font.init()

WIDTH, HEIGHT = 500, 400
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("EscapeGame")

BG = pygame.transform.scale(pygame.image.load(os.path.join("GameMap1.png")), (WIDTH, HEIGHT))


def main():
    fps = 60

    clock = pygame.time.Clock()

    def redraw_window():
        WIN.blit(BG, (0,0))
        pygame.display.update()

        while True:
            clock.tick(fps)
            redraw_window()
                    
            for event in pygame.event.get():
                if event.type == sys.exit:
                    sys.exit()
input()
main()

I've already tried a few thing with event.get and event.pump, I'm probably missing something very basic. Could someone tell me what else I could try?

Also, I'm new at pygame, so if anyone sees another mistake, please tell me about. I would greatly appreciate it.

Upvotes: 1

Views: 39

Answers (1)

sloth
sloth

Reputation: 101072

The window is unresponsive because you don't handle the events by calling pygame.event.get().

While pygame.event.get() is in your code, it's never executed because

a) it's in a function you never call
b) your code blocks on the input() call

You're code should look like this:

import os

pygame.init()

WIDTH, HEIGHT = 500, 400

def main():
    WIN = pygame.display.set_mode((WIDTH, HEIGHT))
    pygame.display.set_caption("EscapeGame")

    BG = pygame.transform.scale(pygame.image.load(os.path.join("GameMap1.png")).convert_alpha(), (WIDTH, HEIGHT))
    fps = 60
    clock = pygame.time.Clock()

    while True:
        clock.tick(fps)
                
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                    return
        
        WIN.blit(BG, (0,0))
        pygame.display.flip()

main()

Upvotes: 3

Related Questions