HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonModerate

SimCity clone performance

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
simcityperformanceclone

Problem

I am working on a SimCity clone, and I am noticing a drop in frame rate as I add more objects to my map, now this is expected, but, when I fill the whole screen, the game maintains about 300 FPS.

Will it hold this as I add a lot more objects, or will it continue to drop?

```
import pygame
from pygame.locals import *
from random import choice

#inits
pygame.init()
font=pygame.font.Font(None, 18)
screen=pygame.display.set_mode((640,480))
pygame.display.set_caption('City Game | Pre-Alpha')
clock=pygame.time.Clock()

#sprites
curspr=pygame.image.load('curs.png').convert()
curspr.set_alpha(100)
grassspr=pygame.image.load('grass.png').convert()
roadspr=pygame.image.load('road.png').convert()
forestspr=pygame.image.load('forest.png').convert()
water1=pygame.image.load('water1.png').convert()
water2=pygame.image.load('water2.png').convert()
res=pygame.image.load('res.png').convert()
house1_0=pygame.image.load('house1_0.png').convert()
house1_1=pygame.image.load('house1_1.png').convert()
res.set_alpha(215)

#vars and lists
tilelist=[grassspr,roadspr,forestspr,water1,res]
namelist=['Grass','Road','Forest','Water','Residental']
tiles=[]
sel=0
money=10000
moneydraw=font.render('Funds: '+str(money), 1, (255,255,255))
namedraw=font.render(namelist[sel],1,(255,255,255))
mse=(0,0)
waterframe=2000
pop=0

class Tile(object):
def __init__(self,pos,spr,typ):
self.typ=typ
self.spr=spr
self.pos=pos
self.rect=pygame.rect.Rect(pos[0],pos[1],32,32)
self.adv=0

while True:
pygame.display.set_caption(str(clock.get_fps()))
namedraw=font.render(namelist[sel],1,(255,255,255))
screen.fill((2,110,200))
for e in pygame.event.get():
if e.type==QUIT:
exit()
if e.type==MOUSEMOTION:
mse=pygame.mouse.get_pos()
key=pygame.key.get_pressed()
if key[K_LSHIFT] or key[K_RSHIFT]:
if pygame.mouse.get_pressed()==(1,0,0):
tilesatmouse=[t for t in tiles i

Solution

As you asked in your question, I'm going to try and address your performance issues here. Some of this will be a style review though, so bear with me.

Style

-
First off, no offense, but this code looks, not so great. You have many major style issues, so here's a list of some of the major ones I see.

  • You have no spaces between operators. You should have space between all operators. For example, if I wanted to assign x to 1, it should look like this, x = 1, and not this. x=1



  • Your variable naming is just horrendous. For example, you named a variable sel. I have no idea what this variable does, or what it's purpose is. Good variable names should not be too short, or too long, and should reflect clearly on that variable's purpose.



  • This code needs to be separated into various different functions/classes. Right now there's some top-level variables, one class, and a while loop doing most of the work. Try and see if there are ways to separate this into separate functions, e.g, a function for checking key actions, or another for rendering the tiles.



You can find these and many other recommendations in PEP8,
the Python coding style guide.

Performance

  • First off, from what I can see, you're doing a lot of looping through the list storing the tile data. This is where your problem is. The more you loop through the data, and the larger the data gets, the slower your program will run.



  • If you have any loops that are looping through the data and checking for an item with a specific value, it's better to use if..in instead.



  • As mentioned in the comments, you can also shorten some of your expressions. For example, the following expression, ((mse[x]/32)*32), can be shortened into (mse[x] & 0x7fffffe0). While shortening expressions is something that isn't easy to do, (even I can't do it very well), see where you can do things like this.



Anyways, I hope that this review helped! If you want me to add on anything else, just ask about it in the comments, and I'll see what I can do.

Context

StackExchange Code Review Q#35914, answer score: 11

Revisions (0)

No revisions yet.