1

I have a program in pygame. I have many different parts, two of which are shown below. In the second one, the game is going at a normal pace, but on the first one, it is very laggy, even though it has the same tick speed. Is there something I'm missing? By the way, only one of these are being executed each game loop cycle. Note that lines ending in # are repeated between the two.

for event in pygame.event.get():#
    if event.type==pygame.QUIT:#
        pygame.quit()#
        sys.exit()#
    if event.type==pygame.KEYDOWN:#
        if event.key==pygame.K_ESCAPE:#
            pygame.quit()#
            sys.exit()#
for ball in balls:#
    ball.update(winrect, walls)#
window.fill(WHITE)#
for box in boxes:#
    pygame.draw.rect(window, box[1], box[0])#
for wall in walls:#
    if wall.orientation==0:#
        pygame.draw.rect(window, BLACK, (wall.left, wall.top, wall.ttopleft, wall.height))#
        pygame.draw.rect(window, BLACK, (wall.bbottomright, wall.top, wall.right-wall.bbottomright, wall.height))#
    else:#
        pygame.draw.rect(window, BLACK, (wall.left, wall.top, wall.width, wall.ttopleft))#
        pygame.draw.rect(window, BLACK, (wall.left, wall.bbottomright, wall.width, wall.bottom-wall.holesize))#
for ball in balls:#
    pygame.draw.circle(window, ball.color, ball.center, round(ball.width/2))#
    pygame.draw.circle(window, BLACK, ball.center, round(ball.width/2), 2)#
window.blit(coverso, winrect)
window.blit(texts['complete'][0], texts['complete'][1])
window.blit(stuff[0], stuff[1])
pygame.display.update()#
pygame.time.Clock().tick(100)#

and the second one:

    #event loop
    for event in pygame.event.get():#
        if event.type==pygame.QUIT:#
            pygame.quit()#
            sys.exit()#
        if event.type==pygame.KEYDOWN:#
            if event.key==pygame.K_ESCAPE:#
                mode='pause'#
    #updates
    updates=[]
    for wall in walls:
        wall.update()
    for ball in balls:#
        updates.append(ball.update(winrect, walls))#similar
    #Seeing if won
    won=True
    for update in updates:
        if not update:
            won=False
    if won:
        if levels[loadinglevel][4]==0:
            levels[loadinglevel][4]=1
        levels[loadinglevel-1][4]=2
        mode='complete'
        stuff=getcomplete(loadinglevel, coins, bigfont, texts['complete'][1].bottom+100, winrect.centerx)
        for wall in walls:
            wall.bbottomright=100000
            wall.ttopleft=90000
        coins+=loadinglevel
    #blitting
    window.fill(WHITE)#
    for box in boxes:#
        pygame.draw.rect(window, box[1], box[0])#
    for wall in walls:#
        if wall.orientation==0:#
            pygame.draw.rect(window, BLACK, (wall.left, wall.top, wall.ttopleft, wall.height))#
            pygame.draw.rect(window, BLACK, (wall.bbottomright, wall.top, wall.right-wall.bbottomright, wall.height))#
        else:#
            pygame.draw.rect(window, BLACK, (wall.left, wall.top, wall.width, wall.ttopleft))#
            pygame.draw.rect(window, BLACK, (wall.left, wall.bbottomright, wall.width, wall.bottom-wall.holesize))#
    for ball in balls:#
        pygame.draw.circle(window, ball.color, ball.center, round(ball.width/2))#
        pygame.draw.circle(window, BLACK, ball.center, round(ball.width/2), 2)#
    pygame.display.update()#
    pygame.time.Clock().tick(100)#
    if mode=='pause':
        window.blit(coverso, winrect)
ThisIsAQuestion
  • 1,887
  • 14
  • 20
  • Have you tried running `cProfile` to pinpoint the function(s) of issue? –  Feb 10 '13 at 03:51
  • @Mike I've never heard of that before. Could you put what I would have to do to into one of the programs? Thanks. – ThisIsAQuestion Feb 10 '13 at 15:43
  • Impossible to tell what's going on. Have a look at [How can you profile a Python script?](http://stackoverflow.com/questions/582336/how-can-you-profile-a-python-script) and [RunSnakeRun](http://www.vrplumber.com/programming/runsnakerun/) to find the bottleneck. – sloth Feb 10 '13 at 18:46

2 Answers2

0

The only lines in the first part that don't repeat are these:

window.blit(coverso, winrect)
window.blit(texts['complete'][0], texts['complete'][1])
window.blit(stuff[0], stuff[1])

If you're not using alpha it's recomended that you use surface.convert() when you create the surface.

If you're using alpha if can use colorkeys because they're much faster that alpha surfaces.

To speed up calculations, I recommend using Psyco.

http://www.psyco.sourceforge.net/

alexpinho98
  • 909
  • 8
  • 14
  • It turns out that I was printing an extremally large rect at the same time accidentally. It was taking a long time to do it because of the large size. Thanks though. – ThisIsAQuestion Apr 11 '13 at 16:48
0

Adding to what alexpinho98 said, you don't have to use colorkeys with alpha surfaces. You can use surface.convert_alpha() instead.

Using the following code saves you some time and keep you from having to type .convert_alpha() repeatedly:

def loadify(imgname):
    return pygame.image.load(imagename).convert_alpha()