How can I load a huge image with pygame as a background?
I'm making a fighting game in pygame but whenever I load a background (940x680 PNG) the program starts to lag really badly, here is my code:
#import section import os import pygame import time from pygame.locals import * #end of import section #initiazing pygame pygame.init() print "Loaded Pygame 100%" #lengths width, height = 940,680 #opening window screen = pygame.display.set_mode((width, height)) #position background = [0,0] #loading images back = pygame.image.load("resources/image/Back.png") print "Loaded graphics 100%" while 1: #clear to reload screen.fill(0) #drawing background screen.blit(back, background ) #update pygame.display.flip()
This may be a newbie question but I just started so yeah...
In pygame you need to use this code you wrote.
#clear to reload screen.fill(0) #drawing background screen.blit(back, background ) #update pygame.display.flip()
If you add a player to the screen and then move him. You will notice that unless you redraw the background, you will see the character more than once.
Pygame doesn't just let you add an image to the screen and move it around, you must redraw the background to get rid of a previous frame.
Another thing you should add to your code is a wait.
clock = pygame.time.Clock() FPS = 30 while 1: clock.tick(FPS)
This will set the frame rate. I'd say you would generally want this at 30 - 60 frames, depending on the game / hardware.
Your current program is probably redrawing the frame a several hundred times, so this will definately add lag.
When I checked this on my pc Ubuntu 12.04 without a background. i.e. A Black screen.
My frame rate was between 1000 - 2000 FPS.