1

I am a newbie about pygame, and my English is very poor.

I want to know why my code can't run as an animation without event.get().

import pygame
pygame.init()
screen = pygame.display.set_mode((500, 400))
pygame.display.set_caption('test')
i = 1
while i < 100:
    i += 1

    screen.fill((200, 200, 200))
    rect = pygame.Rect(1 + i, 1 + i, 50, 50)
    pygame.draw.rect(screen, (5, 5, 5), rect)
    # pygame.event.get() # if add this , it works

    pygame.display.flip()
furas
  • 134,197
  • 12
  • 106
  • 148
  • It works without `event.get()` on Linux. Maybe it runs so fast that you don't see animation. You could add `pygame.time.Clock` to slow down. – furas Nov 18 '17 at 05:45
  • you should better describe problem - do you get rectangle in final place? Doesn't draw anything? Do you get error message when you run it in console/terminal ? – furas Nov 18 '17 at 05:51
  • thank you for answering my question, in fact I got the rect in final place, but I didn't see it moving . I tried the Clock with 5 fps, but it didn't work . When I use the event.get() I can see the rect moving. My computer is Mac. – huangyiqianlin Nov 18 '17 at 06:44
  • Maybe this problem exists only on Mac - but I would't bother this because program without `event.get()` is useless - you can't control object, you can't use key `ESC` to leave program, on some systems you can't even close window, etc. As I remeber in documentation you can see that you should always use `event.get()` because system sends to program mouse position and pressed keys all the time and PyGame keeps it in special queue which has limited size - you may have other problems if you don't get events from queue. – furas Nov 18 '17 at 06:59
  • thanks a lot , I'm learning the book writen by AI Sweigart.I got this issue when I run the game code of Memory Puzzle. There is a part of the function called start_animation, it used no event.get but used update for just one second animation. I tried the code but failed until I add the event.get in the function ,even though I didn't use the event.get to do anything.Anyway, THANK YOU so much for helping me , it's my first time to ask question in stackoverflow, you makes me so happy to keep on programing. – huangyiqianlin Nov 18 '17 at 07:15
  • If you want to know why you need `event.get()` or `event.pump()`, take a look at [these answers](https://stackoverflow.com/q/44254458/6220679). – skrx Nov 18 '17 at 07:39
  • 1
    thanks. it helps a lot. (smile) – huangyiqianlin Nov 18 '17 at 07:56

0 Answers0