Lone Creation
Lone Creation

Reputation: 69

Why is pygame lagging and how to fix it?

pretty straightforward question. I have the code below that I just made from a pygame tutorial, and it is lagging absurdly. The jump takes an eternity, holding the arrow keys doesn't move them quite often, basically the framerate is absurdly low. Any idea on how to fix this?

pygame.init()

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

pygame.display.set_caption("First Game")

x = 50
y = 50
width = 40
height = 60
vel = 15

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 -= vel
        if keys[pygame.K_RIGHT] and x < 1000 - width:
            x += vel
        if not isJump:
            if keys[pygame.K_UP] and y > vel:
                y -= vel
            if keys[pygame.K_DOWN] and y < 1000 - height - vel:
                y += vel
            if keys[pygame.K_SPACE]:
                isJump = True
        else:
            if jumpCount >= -10:
                neg = 1
                if jumpCount < 0:
                    neg = -1
                y -= (jumpCount ** 2) * 0.5 * neg
                jumpCount -= 1
            else:
                isJump = False
                jumpCount = 10

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

pygame.quit()

Upvotes: 1

Views: 1813

Answers (1)

Rabbid76
Rabbid76

Reputation: 210880

It's a matter of Indentation. Your game lags, because you do the drawing in the event loop rather than the application loop. Note, the event loop is executed once for each event that occurs (e.g. when a button is pressed or the mouse is moved). The application loop is executed continuously.
Shift the code which updates the objects and draws the scene from the event loop to the application, to solve the issue:

while run:
    pygame.time.delay(100)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    # INDENTATION !
    # move from event loop to application loop
    #<--|

    keys = pygame.key.get_pressed()

    if keys[pygame.K_LEFT] and x > vel:
        x -= vel
    if keys[pygame.K_RIGHT] and x < 1000 - width:
        x += vel
    if not isJump:
        if keys[pygame.K_UP] and y > vel:
            y -= vel
        if keys[pygame.K_DOWN] and y < 1000 - height - vel:
            y += vel
        if keys[pygame.K_SPACE]:
            isJump = True
    else:
        if jumpCount >= -10:
            neg = 1
            if jumpCount < 0:
                neg = -1
            y -= (jumpCount ** 2) * 0.5 * neg
            jumpCount -= 1
        else:
            isJump = False
            jumpCount = 10

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

Upvotes: 1

Related Questions