[Solved] What’s the error in this pygame program? [closed]


for event in pygame.event.get():
    if event.type==QUIT:
        pygame.quit()
        sys.exit()

pygame.event.get() gives you all the events that occurred since the last time you called it. This loop uses all the events that are currently available. Therefore, when interface() is called, and it tries to pygame.event.get() again, there are no more events left to check for key presses (unless they happen to occur while the janela.blit calls are being made, but those should be very fast; not sure if Pygame will process events during those calls anyway).

Another problem is that you will only get KEYDOWN events when the key is pressed, not continuously while it is held (unless you configure it with pygame.key.set_repeat(), but this may interfere with the rest of your logic). So you have code set up to try to move the paddle continuously while the key is held down, but it won’t actually do anything since .subir or .descer only gets called once per key press.

There are a lot of ways to fix this, but in any case you will have to re-structure things a bit. My suggestion is to have only one loop in the program that handles events, and translates them into some kind of data structure (e.g., it could have a set of which keys are currently pressed down, and which keys changed since last time). You might be able to simplify this with pygame.key.get_pressed(), but beware that you can miss very quick key taps this way.

solved What’s the error in this pygame program? [closed]