When i try to blit images for a parallax background in my game i run into FPS issues.
I tried .convert(), .convert_alpha(), i made sure that the images are loaded and transformed just once - not in the loop. The only thing looping is movement processing and blitting the image.
About the images:
- format = .png
- resolution = 1940x1080x
- more than 50% transparent pixels
- 100 KB - 450 KB each.
I want to run the game with 120 FPS, however blitting just 4 of these images result in a drop to 80 FPS. I heard of dirtyRect updating, but since my images are screen-sized i have to update the whole screen anyway. Thanks for your help!
# IMPORT --------------------------------------------------------------------------------------------------------------#
import pygame
import random
from math import cos, sin, radians
# SETUP ---------------------------------------------------------------------------------------------------------------#
WIDTH, HEIGHT = 1940, 1080
SIZE = (WIDTH, HEIGHT)
scCenterX, scCenterY = WIDTH//2, HEIGHT//2
SC = pygame.display.set_mode(SIZE)
pygame.display.set_caption("Space and Void")
FPS = 120
CLOCK = pygame.time.Clock()
pygame.mixer.init()
pygame.init()
img_clouds_path = [f"assets/clouds/{i}.png" for i in range(0, 7)]
img_clouds = [pygame.image.load(i).convert_alpha() for i in img_clouds_path]
class PARALAX:
def __init__(self, i, j, x, y):
self.img1 = img_clouds[i]
self.img2 = img_clouds[j]
self.x1 = x
self.y1 = y
self.x2 = self.x1 + WIDTH/2
self.y2 = self.y1 + HEIGHT/2
def blit(self):
self.x1 -= scrollingBG.xScroll / 2
self.y1 += scrollingBG.yScroll / 2
self.x2 -= scrollingBG.xScroll / 4
self.y2 += scrollingBG.yScroll / 4
SC.blit(self.img1, (self.x1, self.y1))
SC.blit(self.img2, (self.x2, self.y2))
backgroundStart = PARALAX(5, 2, 0, 0)
backgroundStart2 = PARALAX(3, 5, 0-WIDTH, 0)
def main(): # MAIN --- MAIN --- MAIN --- MAIN --- MAIN --- MAIN --- MAIN --- MAIN --- MAIN --- MAIN --- MAIN --- MAIN #
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT: # QUITTING -----------------------------------------------------------------#
run = False
# MAIN GAME LOOP ---------------------------------------------------------------------------------------------#
SC.fill(BLACK)
backgroundStart.blit()
backgroundStart2.blit()
# UPDATE -----------------------------------------------------------------------------------------------------#
scrollingBG.update()
pygame.display.update()
CLOCK.tick(FPS)
########################################################################################################################
# EXECUTION -----------------------------------------------------------------------------------------------------------#
main()
pygame.quit()
exit()