Search code examples
pythonpygamelag

What causes lag in the game?


I made a very simple game in python with pygame, and I don't know why it is lagging, can somebody help me?

I tried to fix it myself, but I couldn't and I couldn't find a way to fix it online.

And it is not because of my PC, it has 16 gigabytes of RAM, SSD 512 GB of storage, processor: Intel Core i7-7600U 2.90 GHz.

This is the code:

import pygame

pygame.init()

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

pygame.display.set_caption("Platformer")

colour = (255, 0, 0) # Colour red
x = 50
y = 50
width = 40
height = 60
vel = 5

isJump = False
jumpCount = 10

run = True

while run:
    pygame.time.delay(100)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    keys = pygame.key.get_pressed()

    if keys[pygame.K_LEFT] and x > vel:
        x = x - vel

    if keys[pygame.K_RIGHT] and x < 500 - width - vel:
        x = x + vel

    if not(isJump):

        if keys[pygame.K_UP] and y > vel:
            y = y - vel

        if keys[pygame.K_DOWN] and y < 500 - height - vel:
            y = y + vel

        if keys[pygame.K_SPACE]:
            isJump = True

    else:
        if jumpCount >= -10:
            neg = 1
            if jumpCount < 0:
                neg = -1
            y = y - (jumpCount ** 2) * 0.5 * neg
            jumpCount = jumpCount - 1
            
        else:
            isJump = False
            jumpCount = 10
            

    win.fill((0, 0, 0))
    pygame.draw.rect(win, colour, (x, y, width, height))
    pygame.display.update()
    

pygame.quit()


Solution

  • The application lags, because of the large delay in the application loop. Remove the delay:

    pygame.time.delay(100)


    Use pygame.time.Clock to control the frames per second and thus the game speed.

    The method tick() of a pygame.time.Clock object, delays the game in that way, that every iteration of the loop consumes the same period of time. See pygame.time.Clock.tick():

    This method should be called once per frame.

    That means that the loop:

    clock = pygame.time.Clock()
    run = True
    while run:
       clock.tick(60)
    

    runs 60 times per second.


    Complete example:

    import pygame
    
    pygame.init()
    win = pygame.display.set_mode((500, 500))
    pygame.display.set_caption("Platformer")
    
    colour = (255, 0, 0) # Colour red
    x = 50
    y = 50
    width = 40
    height = 60
    vel = 5
    
    isJump = False
    jumpCount = 10
    
    clock = pygame.time.Clock()
    run = True
    while run:
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
    
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT] and x > vel:
            x = x - vel
        if keys[pygame.K_RIGHT] and x < 500 - width - vel:
            x = x + vel
    
        if not(isJump):
            if keys[pygame.K_UP] and y > vel:
                y = y - vel
            if keys[pygame.K_DOWN] and y < 500 - height - vel:
                y = y + vel
            if keys[pygame.K_SPACE]:
                isJump = True
    
        else:
            if jumpCount >= -10:
                neg = 1
                if jumpCount < 0:
                    neg = -1
                y = y - (jumpCount ** 2) * 0.5 * neg
                jumpCount = jumpCount - 1
                
            else:
                isJump = False
                jumpCount = 10
                
    
        win.fill((0, 0, 0))
        pygame.draw.rect(win, colour, (x, y, width, height))
        pygame.display.update()
    
    pygame.quit()