-2

I have been making an ascii based game. It uses curses and timeout to make the game not wait for user input. But pressing a key repeatedly will make the game much more faster. I was wondering if there was a way to stop this.

screen.timeout(300)

while run:

    screen.clear()

    print_map()

    curses.flushinp()
    action = screen.getch()

    # Movement
    if action == curses.KEY_UP:
        player.xy["y"] -= 1
    if action == curses.KEY_DOWN:
        player.xy["y"] += 1
    if action == curses.KEY_LEFT:
        player.xy["x"] -= 1
    if action == curses.KEY_RIGHT:
        player.xy["x"] += 1

While holding down a key, enemies and NPCs move faster. I have tried many things and searched the internet for a way to fix this. But I can't find a way to fix this.

kible
  • 31
  • 5
  • 2
    You want a timeout of zero, so that the player movement handling takes the same amount of time whether a key is pressed or not. The game speed would be set by a `time.sleep()` call in your main loop, perhaps a tenth of a second at most (so as not to introduce a noticeable delay in user input response). – jasonharper Jul 31 '22 at 02:21

1 Answers1

4

Use stdscr.nodelay(1). Currently, screen.getch is blocking while waiting for input, which results in slower execution when said input is not provided.

Amit Eyal
  • 459
  • 3
  • 11