2

I have a simple animation to handle where I draw a rectangle and when I click on the surface, the rectangle slid left and wait for 2 seconds and then redraw it with a slid motion to the right, but if I reduce or move the window and make a try the window freezes on the waiting moment.

Here is the code:

import sys, pygame
from pygame.locals import *

pygame.init()

FPS       = 30
BGCOLOR   = (255, 255, 255)
RECTCOLOR = (10, 0, 199)
DS        = pygame.display.set_mode( (200,200) )
CLOCK     = pygame.time.Clock()
pygame.display.set_caption('Demo')

def main():
    DS.fill(BGCOLOR)
    pygame.draw.rect(DS, RECTCOLOR, (50, 50, 100, 100))
    pygame.display.update()

    while 1:
        ev = pygame.event.wait()
        if ev.type == QUIT:
            pygame.quit()
            sys.exit()
        elif ev.type == MOUSEBUTTONUP:
            animation()


def animation():
    pygame.event.set_blocked(MOUSEBUTTONUP)
    for x in range(10, 101, 10):
        pygame.draw.rect(DS, BGCOLOR, (150 - x,50,x,100))
        pygame.display.update()
        CLOCK.tick(FPS)
    pygame.time.wait(2000) # 2 seconds
    for x in range(10, 101, 10):
        pygame.draw.rect(DS, RECTCOLOR, (50,50,x,100))
        pygame.display.update()
        CLOCK.tick(FPS)
    pygame.event.set_allowed(MOUSEBUTTONUP)

if __name__ == '__main__':
    main()

Why we have this sort of behavior ?, maybe the system don't synchronize between the pause moment and the ticks in the second for loop

PS: i'm on windows 7 and sorry for my english it's not my native language

user9879283
  • 99
  • 4
  • 9

3 Answers3

1

Two things:

When you run a loop like this:

for x in range(10, 101, 10):
        pygame.draw.rect(DS, BGCOLOR, (150 - x,50,x,100))
        pygame.display.update()

you draw your animation, but pygame has no opportunity to handle the messages it gets from your operation system. To avoid that, you should call pygame.event.pump() or pygame.event.get() inside the loop.

From the docs:

pygame.event.pump()

For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system. If you are not using other event functions in your game, you should call pygame.event.pump() to allow pygame to handle internal actions.

This function is not necessary if your program is consistently processing events on the queue through the other pygame.event functions.

There are important things that must be dealt with internally in the event queue. The main window may need to be repainted or respond to the system. If you fail to make a call to the event queue for too long, the system may decide your program has locked up.

Also, while moving your window around, your game will freeze. This is a known limitation of SDL (remember that pygame is just a thin wrapper around SDL) and caused by the windows message loop: when you move or resize a window, windows uses a modal loop, which will block the thread your game runs in.

Community
  • 1
  • 1
sloth
  • 99,095
  • 21
  • 171
  • 219
  • From your answer it seems a bit clearer now, I've tested both `pump()` and `get()` inside the loop but the same freezing persists – user9879283 Sep 09 '13 at 19:39
  • @Vutz Yes, when you move an SDL window, it will freeze. You can't get rid of that. Just google `sdl window drag freeze` or something like that. You'll see that this issue is not limited to pygame, but to all SDL applications. – sloth Sep 10 '13 at 07:52
  • Thank you for your reply, I'm aware with the dragging issue, I would like to say though that if I run the program without touching the window there isn't freezing at all on the **pause time** but as soon as I move or reduce the window (not being on animation) and retry the freezing occurs, give it a try to see. – user9879283 Sep 10 '13 at 17:37
1

I recommend you to use only one loop and use variables or user events to keep track of game state.

Example:

game_state = 0

def main():
    global game_state
    rectangle_ticks = 0

    while 1:
        events = pygame.event.get()   # Get all pending events

        for ev in events:
            # Process events
            if ev.type == QUIT:
                pygame.quit()
                sys.exit()

            elif ev.type == MOUSEBUTTONUP:
                game_state = 1
                pygame.time.set_timer(pygame.time.set_timer(pygame.USEREVENT + 1, 1000)

            elif ev.type == pygame.USEREVENT + 1:
                pygame.time.set_timer(pygame.time.set_timer(pygame.USEREVENT + 1, 0)
                game_state = 2

        # Clear screen
        DS.fill(BGCOLOR)

        # Draw game state
        pygame.draw.rect(DS, RECTCOLOR, (50, 50, 100, 100))
        animation()

        # Update screen
        pygame.display.flip()

        CLOCK.tick(FPS)

def animation():
    if game_state == 1:
        for x in range(10, 101, 10):
            pygame.draw.rect(DS, BGCOLOR, (150 - x,50,x,100))

    if game_state == 2:
        for x in range(10, 101, 10):
            pygame.draw.rect(DS, RECTCOLOR, (50,50,x,100))

It may look weird to draw the whole screen in every tick, but it's the usual way to work in pygame in order to get rid of older objects you don't want to keep drawing.

pmoleri
  • 4,238
  • 1
  • 15
  • 25
1
pygame.time.wait(1000) # 2 seconds

This causes code to pause here, for 2 whole seconds. So you will not get any events till after 2 seconds.

You need to use get_ticks() for animation or a timer.

ninMonkey
  • 7,211
  • 8
  • 37
  • 66