0
\$\begingroup\$

I want a shape in pygame to fall down slowly. The user should be able to move the shape with the arrow keys. My code is given below:

while Running:
    Keys=pygame.key.get_pressed()
    if event.type == pygame.QUIT:
        Quit()
    elif event.type == pygame.KEYDOWN:
        if event.key ==pygame.K_LEFT:
            x=x-1
    y=y+1
    pygame.time.wait(3000)

However, it doesn't always take in the keyboard input if the shape is falling. e.g I will have to press the left arrow key 5 or 6 times for the shape to move. Any idea how I can sort this out?

update: I think the error is in the following code, but if I don't have it then the shape falls too quickly.

    pygame.time.wait(3000)
\$\endgroup\$
0
\$\begingroup\$

You don't want to halt your game for three seconds. Instead you want to look into more proper frame-rate limiting. See this answer to get you started. That is a much more sustainable and better way to move your object.

If you pause the game for three seconds, no events are captured and that's probably why you need to spam your key to get through the blocking wait call.

Also note, that you don't have to use integers here. You can just as well decrement x by 0.1 for an example. This would end up meaning that the block moves one pixel in ten frames.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.