Hi,

I'm not sure if you got your question answered already, but here is my 
suggestion.

The basic problem with your code is with this line:

    clock.tick(1)

That tells pygame that after doing whatever code runs in your loop, it should 
wait until a total of 1 second elapses before moving on.  I have typically 
placed this line *after* the pygame.display.update.   That is, do all your code 
to draw, update to the screen, then allow pygame to wait for as long as it 
needs to, to keep the requested frame rate, before proceeding onto the next 
frame.  

Here is an updated version.  I moved a few things around and added a few print 
statements.  I also changed your loop to a basic "while True' loop and added 
'break' statements to exit your loop at the appropriate places. The final frame 
of animation shows up, then it waits for 4 seconds (because of your two calls 
to delay), then quits.

import pygame, sys

def main():
   pygame.init()
   screen = pygame.display.set_mode((600, 400))

   frame_count = 0

   # The clock helps us manage the frames per second of the animation
   clock = pygame.time.Clock()

   square = pygame.Rect((0,100),(50,50))
  
   while True:
       # Process events
      for event in pygame.event.get():
         if event.type == pygame.QUIT:
            break  # user pressed close button
       # The main snake code
      square.move_ip((50,0))
      print(square)

      frame_count += 1
      print('Showing frame', frame_count)
       
      # Erase the screen
      screen.fill((50, 50, 50))
      pygame.draw.rect(screen, (200,50,50), square)

      # Bring drawn changes to the front
      pygame.display.update()

      # set fps  
      clock.tick(1)

      if frame_count >= 5:
         print('Exiting loop')
         break

   print("Out of loop")
   pygame.time.delay(2000)
# This also works to bring up the last frame.    
#    pygame.event.clear()
#    pygame.event.wait()
   pygame.quit()
   print("After quit")
   pygame.time.delay(2000)
   sys.exit()

main()



Hope this helps,

Irv




 
> On Jan 16, 2018, at 9:37 AM, dejohnso <dejoh...@cs.utah.edu> wrote:
> 
> I have having trouble with a basic game/animation loop. 
> 
> It seems like the final frame doesn't show up until after the game loop is 
> done and pygame.quit() is called. An example is below - if it is run, I see 
> the "Out of loop" output, then a pause, then the final frame, then the final 
> pause. 
> 
> Am I doing something wrong? Is this specific to my setup/machine? 
> 
> import pygame, sys
> 
> def main():
>    pygame.init()
>    screen = pygame.display.set_mode((600, 400))
> 
>    frame_count = 0
> 
>    # The clock helps us manage the frames per second of the animation
>    clock = pygame.time.Clock()
> 
>    square = pygame.Rect((0,100),(50,50))
>    done = False
>    while not done:
>        # Erase the screen
>        screen.fill((50, 50, 50))
>        # Process events
>        for event in pygame.event.get():
>            if event.type == pygame.QUIT:
>                done = True
>        # The main snake code
>        square.move_ip((50,0))
>        pygame.draw.rect(screen, (200,50,50), square)
>        print(square)
>        frame_count += 1
>        if frame_count >= 5:
>            done = True
>        print(done)
>        # set fps
>        clock.tick(1)
>        # Bring drawn changes to the front
>        pygame.display.update()
> 
>    print("Out of loop")
>    pygame.time.delay(2000)
> # This also works to bring up the last frame.    
> #    pygame.event.clear()
> #    pygame.event.wait()
>    pygame.quit()
>    print("After quit")
>    pygame.time.delay(2000)
>    sys.exit()
> 
> main()
> 
> 

Reply via email to