1

this is my code idk why it runs so slow why is my code running so slow plz help me! thank you in advance

import pygame
from rgb import *
from pygame.constants import *
pygame.init()

height=700
width=1500
center=(780, 450)
ball_pos=[500,550]
sky_pos=[0,-1000]


screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
pygame.display.update()

running=True
while running:
    for event in pygame.event.get():
        keys = pygame.key.get_pressed()
        if keys[K_SPACE]:
            running=False
            pygame.display.update()
    sky=pygame.image.load("sky.jpg")



    if keys[K_RIGHT]:
        sky_pos[0]-=1
    screen.blit(sky, sky_pos)

    class Circle:
        pygame.draw.circle(screen, green, ball_pos, 50)
    pygame.display.update()
    screen.fill(black)

pygame.quit()

I tried doing everything but nothing is working i want the sky to move to the left with smoothness though i dont know how!

1 Answers1

1

There are some issues in your code, but the most obvious thing is that you are loading the image into the application loop. pygame.image.load is very time consuming, because it has to read the images from the data store.

Load the image once before the application loop, rather than continuously in the loop:

sky=pygame.image.load("sky.jpg")      # <--- ADD

running=True
while running:
    # [...]

    # sky=pygame.image.load("sky.jpg")  <--- DELETE
Rabbid76
  • 202,892
  • 27
  • 131
  • 174