Xoryy
Xoryy

Reputation: 121

Very simple pygame window lags a lot

I've got a problem with pygame, I've only added a background image and an icon to the window but its lagging whenever I run it and I have no clue why, I've added the convert function but it did not help.

Code:

import pygame

pygame.init()

# generate game window
pygame.display.set_caption("Space Under Attack")
screen = pygame.display.set_mode((1200, 600))
# import background
background = pygame.image.load('assets/resize.jpg').convert()
icon=pygame.image.load('assets/alienicon.png').convert()

running = True

# loop that will execute while the condition is true
while running:
    # apply background to the window
    screen.blit(background, (0,0))
    #apply icon to window
    pygame.display.set_icon(icon)
    # update screen
    pygame.display.flip()
    # if the gamer closes the window
for event in pygame.event.get():
    # check if event is event of closing window
    if event.type == pygame.QUIT:
        running = False
        pygame.quit()
        print("Quitting the game...")

enter image description here

Upvotes: 0

Views: 282

Answers (1)

gnight
gnight

Reputation: 472

'lags' = window becomes sluggish? The while loop is hogging all available resources.

You have an indentation problem. Try indenting the whole for event in pygame.event.get(): and all it contains so that it is part of while running.

A event loop usually needs a sleeper operation which is what pygame.event.get() is for.

while running:
    screen.blit(background, (0,0))
    pygame.display.set_icon(icon)
    pygame.display.flip()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            pygame.quit()
            print("Quitting the game...")

Upvotes: 2

Related Questions