[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Pausing a function



On Sep 21, 2007, at 12:25 PM, Samuel Mankins wrote:

What I decided to do was:
Each animation is a sprite, with the values of image, frames, repeats, and centerPoint. The animation sprite has an update function that updates it's frame and repeat, and if it's repeat is big enough, kills it. The function calls this sprite, and sets an event to happen that calls the update, but I don't know, is there a way to make a new event every time a function is called?

You can post events, but that feels like the wrong approach to me. I would think it would be easier just have each sprite object know when it needs to update (but saving the next update time in an attribute) and check if it's time to update at the top of each call (similar to what DROID suggested):

(in your sprite class, where game_time is the time value inside the game which could be ticks, frames, millis, whatever)

def update(self):
	if game_time > self.next_frame_time:
		# update image
		self.next_frame_time = game_time + self.frame_delay

Then you can call this every frame regardless of whether it needs updating. I realize this might not seem as efficient, but I wouldn't worry about optimizing it unless you had a ton of these active at one time and profiling showed this was consuming too much time.

-Casey


Samuel Mankins wrote:
Hm... That might work, but it would require restructuring my code quite a bit--Right now I have a function that refreshes the screen and redraws all the sprites, and then a function the continuosly calls it, but can be paused so that updates take place during functions. Thanks anyway, I can give it a try.

Casey Duncan wrote:
You will need a loop that continuously calls methods to update, draw and refresh the screen. At the top of the loop you could check for events to see if you need to do anything. Basically something like:

clock = pygame.time.Clock()
while playing:
    for event in pygame.event.get():
        handle_event(event)
sprites.update() # update all sprites, including ambient animation
    dirty = sprites.draw(screen)
    pygame.display.update(dirty)
pygame.display.flip() # May not be necessary depending on display config
    clock.tick(40) # Limit to 40 fps (set this to whatever you like)

You can also be kinder to the system by inserting a short time.sleep() in there, but I'll leave that as an exercise for the reader ;)

-Casey

On Sep 21, 2007, at 11:19 AM, Samuel Mankins wrote:

Hi.
Is there a way to pause one function, but to keep all the others going? I'm building a 2D RPG, and my current animation function uses pygame.time.wait(), which is good, but means I can't have "ambient" animations (Things that move around randomly, like plants waving in the wind), and I can only have on going at a time. Does anyone know a way around this?

Thanks!