Search code examples
pythonpygamedelaylag

Starter query about lag


Okay. My problem is, when i run my code it delays everything else than the moving function when you click a or d. Like the ball that flys from the sky, that just stops moving for while. How do i fix it? Here is my making the screen code:

import pygame as py
import os
import time

NÄYTTÖX = 900
NÄYTTÖY = 675

Here are colors:

SKY_BLUE = (0, 128, 255)
GREEN = (0, 255, 0)

Here is making players part:

PELAAJA_HALKAISIJA = 100
syötävä_halkaisija = 20

SCREEN = py.display.set_mode((NÄYTTÖX, NÄYTTÖY))
PELAAJA = py.image.load(os.path.join("Assets", "packman.png"))
PELAAJA = py.transform.rotate(py.transform.scale(PELAAJA, (PELAAJA_HALKAISIJA, 
PELAAJA_HALKAISIJA)), 90)

Here i make my background:

TAUSTA = py.transform.scale(py.image.load(os.path.join("Assets", "space.png")), (NÄYTTÖX, 
NÄYTTÖY))

Here i draw everything on the screen:

def piirto(syötävärect, pelaajarect):
    SCREEN.blit(TAUSTA, (0, 0))
    SCREEN.blit(PELAAJA, (pelaajarect.x, pelaajarect.y))
    py.draw.circle(SCREEN, GREEN, (syötävärect.x, syötävärect.y), syötävä_halkaisija)
    py.display.update()

This is the main function:

def main():

    pelaajarect = py.Rect(NÄYTTÖX / 2 - PELAAJA_HALKAISIJA / 2, NÄYTTÖY - PELAAJA_HALKAISIJA * 
1.7, PELAAJA_HALKAISIJA, PELAAJA_HALKAISIJA)
    syötävärect = py.Rect(NÄYTTÖX / 2, 0, syötävä_halkaisija, syötävä_halkaisija)

    clock = py.time.Clock()
    run = True
    while run:
        clock.tick(60)
        for event in py.event.get():
            if event.type == py.QUIT:
                run = False

Here is my key codes:

            if event.type == py.KEYDOWN:
                if event.key == py.K_a:
                    if pelaajarect.x == 400 or pelaajarect.x == 600:
                        for i in range(100):
                            pelaajarect.x -= 2
                            piirto(syötävärect, pelaajarect)
                            time.sleep(0.0007)
                elif event.key == py.K_d:
                    if pelaajarect.x == 400 or pelaajarect.x == 200:
                        for i in range(100):
                            pelaajarect.x += 2
                            piirto(syötävärect, pelaajarect)
                            time.sleep(0.0007)

This makes the ball on the sky falling forever:

        if syötävärect.y < NÄYTTÖY:
            syötävärect.y += 2
        else:
            syötävärect.y = 0

Here is everything else:

        piirto(syötävärect, pelaajarect)
    py.quit

if __name__ == "__main__":
    main()

So it doesn't work. I use VScode studios


Solution

  • Never try to control the application with a loop inside the application loop. Use the application loop and change the position of object in each frame:

    Add a variable target_x. Set the variable in the event loop and move the object to the target in the application loop:

    def main():
    
        pelaajarect = py.Rect(NÄYTTÖX / 2 - PELAAJA_HALKAISIJA / 2, NÄYTTÖY - PELAAJA_HALKAISIJA * 
    1.7, PELAAJA_HALKAISIJA, PELAAJA_HALKAISIJA)
        syötävärect = py.Rect(NÄYTTÖX / 2, 0, syötävä_halkaisija, syötävä_halkaisija)
    
        # init target None
        target_x = None
    
        clock = py.time.Clock()
        run = True
        while run:
            clock.tick(60)
            for event in py.event.get():
                if event.type == py.QUIT:
                    run = False
    
    
                if event.type == py.KEYDOWN:
                    if event.key == py.K_a:
                        if pelaajarect.x == 400 or pelaajarect.x == 600:
                            # set target position
                            target_x = pelaajarect.x + 200
                            
                    elif event.key == py.K_d:
                        if pelaajarect.x == 400 or pelaajarect.x == 200:
                            # set target position
                            target_x = pelaajarect.x - 200
                   
            # move to target position       
            if target_x != None:
                if pelaajarect.x < target_x:
                    pelaajarect.x += 2
                elif pelaajarect.x > target_x:
                    pelaajarect.x -= 2
                            
            if syötävärect.y < NÄYTTÖY:
                syötävärect.y += 2
            else:
                syötävärect.y = 0
    
             piirto(syötävärect, pelaajarect)
        py.quit()