patternpythonMinor
Virtual sandbox game
Viewed 0 times
sandboxgamevirtual
Problem
I am working on an app: a virtual sandbox of sorts, a lot like Powder Game.
The problem is, when working with an app, you must deal with hardware limitations. Don't get me wrong, the speed isn't like 2 FPS or anything, but I would just like for someone to look over the code and point out any spots that could use optimization, to speed things up. I just want max performance for the first stable build, and I might learn something.
I am using pgs4a to package it for Android.
This code is a bit lengthy, so if you'd like for me to post it as a zip file, just let me know.
```
import pygame
from random import choice
from pygame.locals import *
try:
import android
except ImportError:
android = None
pygame.init()
end=0
if android:
android.init()
android.map_key(android.KEYCODE_BACK, pygame.K_ESCAPE)
menuscreen=pygame.display.set_mode((480,320))
done=0
phone=pygame.image.load('phone.bmp').convert()
tablet=pygame.image.load('tablet.bmp').convert()
prect=pygame.Rect(70,50,117,41)
trect=pygame.Rect(70,180,117,41)
stype=1
while done==0:
menuscreen.fill((0,0,0))
menuscreen.blit(phone,(70,50))
menuscreen.blit(tablet,(70,180))
if android:
if android.check_pause():
android.wait_for_resume()
for e in pygame.event.get():
if e.type==MOUSEBUTTONDOWN:
if prect.collidepoint(pygame.mouse.get_pos()[0],pygame.mouse.get_pos()[1]):
stype=1
done=1
elif trect.collidepoint(pygame.mouse.get_pos()[0],pygame.mouse.get_pos()[1]):
stype=2
done=1
if not android:
if e.type==QUIT:
end=1
done=1
if e.type == pygame.KEYDOWN and e.key == pygame.K_ESCAPE:
end=1
done=1
pygame.display.flip()
if stype==1:
screen = pygame.display.set_mode((480,320))
partdraw=pygame.Surface((480,320))
else:
screen = pygame.display.set_mode((800,480))
partdraw=pygame.Surface
The problem is, when working with an app, you must deal with hardware limitations. Don't get me wrong, the speed isn't like 2 FPS or anything, but I would just like for someone to look over the code and point out any spots that could use optimization, to speed things up. I just want max performance for the first stable build, and I might learn something.
I am using pgs4a to package it for Android.
This code is a bit lengthy, so if you'd like for me to post it as a zip file, just let me know.
```
import pygame
from random import choice
from pygame.locals import *
try:
import android
except ImportError:
android = None
pygame.init()
end=0
if android:
android.init()
android.map_key(android.KEYCODE_BACK, pygame.K_ESCAPE)
menuscreen=pygame.display.set_mode((480,320))
done=0
phone=pygame.image.load('phone.bmp').convert()
tablet=pygame.image.load('tablet.bmp').convert()
prect=pygame.Rect(70,50,117,41)
trect=pygame.Rect(70,180,117,41)
stype=1
while done==0:
menuscreen.fill((0,0,0))
menuscreen.blit(phone,(70,50))
menuscreen.blit(tablet,(70,180))
if android:
if android.check_pause():
android.wait_for_resume()
for e in pygame.event.get():
if e.type==MOUSEBUTTONDOWN:
if prect.collidepoint(pygame.mouse.get_pos()[0],pygame.mouse.get_pos()[1]):
stype=1
done=1
elif trect.collidepoint(pygame.mouse.get_pos()[0],pygame.mouse.get_pos()[1]):
stype=2
done=1
if not android:
if e.type==QUIT:
end=1
done=1
if e.type == pygame.KEYDOWN and e.key == pygame.K_ESCAPE:
end=1
done=1
pygame.display.flip()
if stype==1:
screen = pygame.display.set_mode((480,320))
partdraw=pygame.Surface((480,320))
else:
screen = pygame.display.set_mode((800,480))
partdraw=pygame.Surface
Solution
The comment from fernando.reyes is 100% correct: if you haven't measured things, you can't tell what's slow, or if it gets faster. That said, there are two main things I see that I know from prior exposure are likely to be slow. Since I've not used python on android, much less pgs4a, even that is quite possibly not applicable or not relevant. So, again, if you don't measure, you cannot prove whether these are actually meaningful in your case, or if changing them helps.
(By the way, seriously, clean up the code before asking for reviews. In the nearly 300 lines of code, the only comment (or docstring) is
Avoid eval. Its use can prevent normal optimizations that python does for local variables in a function. For example:
Prefer working with callables as actual objects.
Avoid looking up globals or deep attribute lookups in loops. For example:
could well be this, to avoid half of the lookups:
or this, to avoid the other half:
Similarly you can consider capturing the globals (or better yet their deep attribute names) as locals to avoid excess lookups:
(By the way, seriously, clean up the code before asking for reviews. In the nearly 300 lines of code, the only comment (or docstring) is
# This isn't run on Android., there are multiple unhelpful—such as one-character—variable names, the use of horizontal whitespace is irregular, and vertical whitespace is missing.)Avoid eval. Its use can prevent normal optimizations that python does for local variables in a function. For example:
sel='SandPart'
...
if sel is not 'SandPart':
...
sand.append(eval(sel)((mse[0]/8)*8,(mse[1]/8)*8))Prefer working with callables as actual objects.
sel = SandPart
...
if sel is not SandPart:
...
sand.append(sel((mse[0]/8)*8, (mse[1]/8)*8))Avoid looking up globals or deep attribute lookups in loops. For example:
if e.type==MOUSEBUTTONDOWN:
if prect.collidepoint(pygame.mouse.get_pos()[0],pygame.mouse.get_pos()[1]):could well be this, to avoid half of the lookups:
if e.type==MOUSEBUTTONDOWN:
if prect.collidepoint(*pygame.mouse.get_pos()):or this, to avoid the other half:
if e.type==MOUSEBUTTONDOWN:
if prect.collidepoint(*e.pos):Similarly you can consider capturing the globals (or better yet their deep attribute names) as locals to avoid excess lookups:
def main():
Rect = pygame.Rect
get_pressed = pygame.mouse.get_pressed
draw_rect = pygame.draw.rect
...Code Snippets
sel='SandPart'
...
if sel is not 'SandPart':
...
sand.append(eval(sel)((mse[0]/8)*8,(mse[1]/8)*8))sel = SandPart
...
if sel is not SandPart:
...
sand.append(sel((mse[0]/8)*8, (mse[1]/8)*8))if e.type==MOUSEBUTTONDOWN:
if prect.collidepoint(pygame.mouse.get_pos()[0],pygame.mouse.get_pos()[1]):if e.type==MOUSEBUTTONDOWN:
if prect.collidepoint(*pygame.mouse.get_pos()):if e.type==MOUSEBUTTONDOWN:
if prect.collidepoint(*e.pos):Context
StackExchange Code Review Q#39185, answer score: 2
Revisions (0)
No revisions yet.