1
\$\begingroup\$

Relevant background information

I'm developing a game for a high school computer science performance task in python & pygame. I'm using objects and all the code is working well for me.

The game consists of a top down (bird's eye view) 2D shooter of a player that rotates and shoots at zombies that spawn and make their way to him. Kind of like dead ops arcade from the original black ops.

Though I'm developing the levels using a list string type designer (there's probably a name for the common technique I'm unaware of), than I load it and I have an array of rects. Here is some background code.

            x = 0 # Start X at 0
                    y = 0 # Start Y at 0
                    for row in levelData: # run a for loop for all the rows in level data
        for col in row: # run a for loop for all the colums in the row
            if col == "1": # check if the column section is labeled 1
                self.Rects.append(pygame.Rect((x, y),(self.tileWidth, self.tileHeight))) # append a rectangle
            if col == "2": # check if the column section is labeled 2
                self.Rects.append(pygame.Rect((x, y),(self.tileWidth, self.tileHeight))) # append a rectangle
            if col == "3": # check if the column section is labeled 3
                self.zombieSpawners.append(ZombieSpawner(x, y, self.zombieManager, int(self.maxZombies / self.levelDesigns.getSpawnerAmount(self.level)), self.zombieSpawnTimeMin, self.zombieSpawnTimeMax, self.zombieVelMin, self.zombieVelMax, self.zombieHealth)) # Initalize a zombie spawner with given data
            x += 64 # adjust x
        y += 64 # adjust y
        x = 0 # reset row
    self.levelLoaded = True


            def draw(self, canvas, playerRect):
    canvas.blit(self.backgroundImage, (0, 0)) # draw background image
    for rect in range(len(self.Rects)): # for loop to run all rects
        canvas.blit(self.blockImage, (self.Rects[rect].x, self.Rects[rect].y)) # draw a block
    for spawner in range(len(self.zombieSpawners)):
        canvas.blit(self.spawnerImage, (self.zombieSpawners[spawner].getX(), self.zombieSpawners[spawner].getY()))

The indenting is all screwed up but you should understand what I'm doing.

The problem

The code above works perfectly. The game ran really smooth with just the player and zombies. Though when I run the background image above, it becomes a huge performance decrease, the fps becomes a solid 3-5. Drawing the blocks and the spawners cause some performance issue's as well, not as bad as the background image but still unplayable.

I think the background image does so much performance damage because it's an actual picture and not a little cartoon like the blocks and spawer's.

What I'm trying to do

Is there anything I'm doing wrong or something I should do that can get my performance back? Like some sort of spritebatch for pygame or something. I can't figure out anything online other than pygame doesn't have that good of a performance.

\$\endgroup\$
  • \$\begingroup\$ can you share where you declare the background image please? \$\endgroup\$ – benh Dec 15 '16 at 14:18
  • \$\begingroup\$ self.backgroundImage = backgroundImage \$\endgroup\$ – Kyle Dec 15 '16 at 14:31
  • \$\begingroup\$ @benh Inside the level constructor, so it just gets loaded in once when the level is called in the main. \$\endgroup\$ – Kyle Dec 15 '16 at 14:31
  • \$\begingroup\$ @benh the original is initalized in the main class, out of the game loop and right before the level object is created. \$\endgroup\$ – Kyle Dec 15 '16 at 14:33
  • \$\begingroup\$ Do you resize the image to the appropriate resolution in the game loop? I've had this cause me problems in the past as its quite expensive \$\endgroup\$ – benh Dec 15 '16 at 14:44
2
\$\begingroup\$

I resolved the issue

I was facing severe fps drops using my background image and blocks. Though I changed it so when I loaded them into my program, I used .convert() at the end. The .convert() made a great performance different and my fps is back up to 60.

\$\endgroup\$
  • 1
    \$\begingroup\$ Don't forget to come back in a couple of days to mark this answer as accepted :) \$\endgroup\$ – Vaillancourt Dec 15 '16 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.