0

I'm coding a snake game... yes, I know, how original.

My problem is the following: I enabled nodelay() by writing stdscr.nodelay(1) and then I did set the timeout() to 100 miliseconds by writing stdscr.timeout(100). Happens what I expected. Everything fine.

But when I comment the line where I enabled nodelay() by adding a # right at the beginning of the line to see what will happen, nothing changes when I execute my program. Why???

import curses
from curses import textpad
from constants import *

def main(stdscr):
    curses.curs_set(0)
#    stdscr.nodelay(1)
    stdscr.timeout(int(1000 / 15))

    screen_height, screen_width = stdscr.getmaxyx()

    if screen_height < 40 or screen_width < 168:
        raise ValueError("Your terminal screen must be 40 rows and 168 columns minimum. ")

    snake = [
        [screen_height // 2, screen_width // 2]
    ]

    stdscr.addch(snake[0][0], snake[0][1], "#")    

    direction = None

    while True:
        key = stdscr.getch()

        if direction == curses.KEY_RIGHT or key == curses.KEY_RIGHT:
            snake[0][1] += 1
            direction = curses.KEY_RIGHT

        if direction == curses.KEY_LEFT or key == curses.KEY_LEFT:
            snake[0][1] -= 1
            direction = curses.KEY_LEFT

        if direction == curses.KEY_UP or key == curses.KEY_UP:
            snake[0][0] -= 1
            direction = curses.KEY_UP

        if direction == curses.KEY_DOWN or key == curses.KEY_DOWN:
            snake[0][0] += 1
            direction = curses.KEY_DOWN

        stdscr.clear()
        stdscr.addch(snake[0][0], snake[0][1], "#")    

curses.wrapper(main)

What I expected to happen is that the program now will wait for user's input everytime I do a stdscr.getch()

But what actually happens is that the program acts like nodelay is still enabled, so it doesnt wait for any input

Martín Nieva
  • 476
  • 1
  • 5
  • 13

1 Answers1

1

timeout does something similar to nodelay:

The timeout and wtimeout routines set blocking or non-blocking read for a given window.

versus

The nodelay option causes getch to be a non-blocking call.

(you may not notice the difference between something less than a tenth of a second and no delay at all).

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105