[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] Paralax snow flaks: Some tipps for enhancing the performance?



Hello!

I just started Programming in Python & Pygame and my first Experiment was a simple Paralax Starfield. This worked very well, so i added some stuff to simulate snow fall (with the sinus function). No Problem at all...

But when I tryed to extend the resolution of my programm (from 400x400 to 1024x768) i noticed obvious performance problems. In measure of the fps I gained around 15 fps compared to 40-50 fps in 400x400

I don't really understand this ... There are no more flake-positions which have to be calculated!

Some guys in the #pygame irc channel told me to implement the profile module, to figure out, which part of my code thwarts my programm, but it didn't actually work...

So, here are my questions:

- How can i implement this profile module
- What does this loss of fps in higher res. rest upon? My first "Programming Tool" was this Game-Basic-Dialect BlitzBasic, there the fps-difference isn't so clearly.
- Do you have any tipps to enhance the performace of my pygames in general? How would you code things like my example?

So, here is the Code:

------------------------------------------------------------------------ ---------------------------------------


#Schnefall 0.1

#Imports
import pygame, random, math
from pygame.locals import *

#Konstanten
MAX_FLOCKEN = 100
MAX_FLOCKEN_SPEED = 5
MAX_FLOCKEN_SIZE = 3
SCREEN_HEIGHT = 400
SCREEN_WIDTH = 400
SIN_FREQUENZ = 2

#Klassendefinitionen
class Flocke:
#Initialisierung jeder neuen Flocke
def __init__(self):
self.speed = random.randint(1,MAX_FLOCKEN_SPEED)
helligkeit = random.randint(20*self.speed,50*self.speed)
self.color = (helligkeit, helligkeit, helligkeit)
self.size = random.randint(1, MAX_FLOCKEN_SIZE)
self.ypos = random.randint(1,SCREEN_HEIGHT)
self.xpos = random.randint(1,SCREEN_WIDTH)
self.angle = random.randint(0,360)

#Methode zum Darstellen der Flocken
def draw(self):
screen.fill(self.color, (self.xpos, self.ypos, self.size, self.size))

#Eigenschaften einer Flocke updaten
def update(self):
self.ypos = self.ypos + self.speed
self.xpos = self.xpos + (math.sin(self.angle)*5)
self.angle = self.angle + 0.1

if self.angle >= 360:
self.angle=0

if self.ypos > SCREEN_HEIGHT:
self.ypos = 0



#Standardinitialisierungen
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), HWSURFACE|DOUBLEBUF)
pygame.display.set_caption('Paralaxer Schneefall')
pygame.mouse.set_visible(0)

#Hintergrund erschaffen
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((0,0,0))

#Erschaffen der Sterne
flockerl = []
for x in range(0,MAX_FLOCKEN):
flockerl.append(Flocke())


#Main Loop
def main():
frames=0
ticks = pygame.time.get_ticks()

while 1:
screen.blit(background, (0,0))

for jede_flocke in flockerl:
jede_flocke.update()
jede_flocke.draw()

pygame.display.flip()

frames=frames+1
keyinput = pygame.key.get_pressed ()
if keyinput [ K_ESCAPE] or pygame.event.peek(QUIT):
break
print "fps: %d" % ((frames*1000)/(pygame.time.get_ticks()-ticks))


if __name__=='__main__':
main()