codeInjection
codeInjection

Reputation: 31

Why is my Pygame window only displaying for only a few seconds?

I'm new to PyGame (and python in general) and I am just trying to get a window to pop up. That's all I'm after for now. Here's my code:

import pygame
pygame.init()

win = pygame.display.set_mode((600, 600))

pygame.display.set_caption('First Game')

I am using Python 3.7.0 in Pycharm and PyGame 1.9.4.

Upvotes: 3

Views: 1085

Answers (2)

bunbun
bunbun

Reputation: 2676

Following Python's PyGame tutorial, your next step is to start a while loop to handle the game frames:

import pygame
pygame.init()

win = pygame.display.set_mode((600, 600))

pygame.display.set_caption('First Game')

clock = pygame.time.Clock()    # Determine FPS (frames-per-second)

crashed = False

# Game loop
while not crashed:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True

        print(event)

    pygame.display.update()
    clock.tick(60)

Upvotes: 3

dawn
dawn

Reputation: 1342

Because you need to first put it inside a loop (so it refreshes whatever is inside it), that way it stays on until something alters the condition of that loop.

# Event loop (HERE YOU PUT IT).
    while 1:
        for event in pygame.event.get():
            if event.type == QUIT:
                return

        screen.blit(background, (0, 0))
        pygame.display.flip()


if __name__ == '__main__': main()

Upvotes: 2

Related Questions