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 if t.rect.collidepoint(mse)]
if not tilesatmouse:
if sel==4:
tiles.append(Tile(((mse[0]/32)*32,(mse[1]/32)*32),tilelist[sel],'res'))
else:
tiles.append(Tile(((mse[0]/32)*32,(mse[1]/32)*32),tilelist[sel],'tile'))
elif pygame.mouse.get_pressed()==(0,0,1):
for t in tiles:
if t.rect.collidepoint(mse):
tiles.remove(t)
if e.type==KEYUP:
if e.key==K_e:
sel+=1
if sel==len(tilelist):
sel=0
if e.key==K_q:
sel-=1
if sel==-1:
sel=len(tilelist)-1
if e.type==MOUSEBUTTONUP:
if e.button==1:
tilesatmouse=[t for t in tiles if t.rect.collidepoint(mse)]
if not tilesatmouse:
if sel==4:
tiles.append(Tile(((mse[0]/32)*32,(mse[1]/32)*32),tilelist[sel],'res'))
else:
tiles.append(Tile(((mse[0]/32)*32,(mse[1]/32)*32),tilelist[sel],'tile'))
if e.button==3:
for t in tiles:
if t.rect.collidepoint(mse):
tiles.remove(t)
for t in tiles:
if t.spr==water1 or t.spr==water2:
if waterframe>999:
t.spr=water1
else:
t.spr=water2
if t.typ=='res':
if t.adv>=0:
t.adv+=1
if t.adv==2000:
t.spr=house1_0
if t.adv==4000:
t.spr=house1_1
pop+=choice([1,2,3,4])
t.adv=-1
screen.blit(t.spr,t.pos)
waterframe-=1
if waterframe==0:
waterframe=2000
clock.tick(999)
screen.blit(curspr, ((mse[0]/32)*32,(mse[1]/32)*32))
screen.blit(moneydraw, (2,2))
screen.blit(namedraw, (2,18))
pygame.display.flip()
#if any(t.rect.colliderect(x.rect) for x in tiles if x is not t):
Will it hold this as I add a lot more objects? Or will it continue to drop?
... well, have you tried that and observed what happens? \$\endgroup\$((mse[x]/32)*32)
into(mse[x] & 0x7fffffe0)
. \$\endgroup\$&0xfffffe0
?? \$\endgroup\$12345 / 1000
is 12345 shifted right 3 times (1000 is the 3rd power of 10), which (in integer terms truncates the decimal) is12
. So,(12345/1000)*1000
is 12000. Which is the same as just setting the units/tens/hundreds digits to0
. Michael, in binary instead of decimal, has just pointed out that a binary number divided then multiplied by 32 is the same as setting the low-5 bits to 0, and that is what the& 0xffffffe0
does \$\endgroup\$