pythontimepygamekeypresstetris

Python Pygame Tetris, problems with handling of keypresses. implement delayed auto shift. how long a key has to be pressed before moving


I want to code my own Tetris. The goal is to get as close as possible to this game: https://jstris.jezevec10.com/?play=1&mode=1

I don't know if I google the wrong things but I couldn't find a satisfying answer.

The problem: the pieces can move left and right with the arrows. If the player presses the key once, the piece should move by 1 to the left or right. Now I want to implement that after a certain time (t > 133ms - default setting jstris) the piece should move directly to the side of the screen. It would be more of a keyhold than a keypress event.

1)

if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_LEFT:
        piece.x -= 1

If done like this the player is not able to hold the left arrow. He has to press it over and over again to get to the side of the screen.

2)

keys = pygame.key.get_pressed()
    
    if keys[pygame.K_LEFT]:
        piece.x -= 1

In this solution the goal to get a constantly moving piece to the side is achieved, BUT it is nearly impossible to only move the piece by 1. To handle this I would have to set a low fps to get control whether to move by 1 or more but that is a really bad solution. It slows down the game speed a lot and feels like lagging.

So here is the question: Is it possible to move the piece by 1 if the key is pressed but check live if it is pressed longer than t>133ms. If true the piece should move instantly to the wall. In tetris it is called Auto delayed shift (DAS). It would be a mix of 1) and 2) decided by a measured time.

Before posting here I read a lot about measuring time of the duration of a keypress or between events but here I don't have KEYDOWN and KEYUP to measure the time between them because the KEYUP event doesn't even exist when the game needs to decide what to do.

Do i maybe need to get out of pygame and search after other modules to get to my goal or am I missing something ?

Thanks for your help!


Solution

  • You can use pygame.key.set_repeat docs to make KEYDOWN events repeat while the key is held down.

    Try putting the following at the beginning of your program:

    KEY_DELAY = 100
    KEY_INTERVAL = 50
    pygame.key.set_repeat(KEY_DELAY, KEY_INTERVAL)
    

    The tetris piece will move a second time if you keep a key pressed for 100 milliseconds, then again after 50. Adjust the values as needed.