Home > Net >  c curses loop is stuck
c curses loop is stuck

Time:12-03

I'm working on a snake game in c curses and this part of code will be the foundation of the game, but it seems somehow stuck, the box shows up and there is only a single # in it when realistically there should be a lot more since I commented the delay.

    WINDOW *game_win = newwin(height, width, ym/4 - height/4, xm/4 - width/4);
    box(game_win, 0, 0);
    mvwprintw(game_win, height/2, width/2, "#");
    wrefresh(game_win);
    noecho();
    nodelay(game_win, TRUE);

    int position[] = {height / 2, width / 2};
    int direction = KEY_RIGHT;
    //clock_t delay;
    while(1){
        //delay = clock();
        //while(clock() < delay   100000){
            direction = wgetch(game_win);
        //}
        
        if(direction == KEY_UP) position[0]  ;
        if(direction == KEY_DOWN) position[0]--;
        if(direction == KEY_RIGHT) position[1]  ;
        if(direction == KEY_LEFT) position[1]--;

        mvwprintw(game_win, position[0], position[1], "#");
        wrefresh(game_win);
    }

I thought that the wgetch() was pausing the program so I added nodelay, also tried some clearing and redrawing the whole thing but that didn't help either. At this point I really only need this to cycle infinitely with the user having one second to pick a direction until the snake moves (in this case im not erasing the previous position so instead of moving it just get's longer in the given direction).

CodePudding user response:

Passed your question to ChatGpt, hope this helps :)

It looks like the problem is that the wgetch() function is not returning any input, so the direction variable is never being updated and the snake is not moving. One possible reason for this is that you are using the nodelay() function to prevent the wgetch() function from blocking, but you are passing the game_win window as the argument instead of stdscr.

The nodelay() function is used to set the stdscr window to be non-blocking, which means that it will not wait for user input before continuing. In your code, you are passing the game_win window to the nodelay() function, but this will not have any effect because the game_win window is not the stdscr window.

To fix this problem, you can remove the call to the nodelay() function and instead use the timeout() function to set a timeout for the wgetch() function. This will allow the wgetch() function to return immediately if there is no user input within the specified timeout.

Here is an example of how you could modify your code to use the timeout() function:

Copy code
#include <curses.h>

int main(void) {
  // Set up the screen and create the game window
  initscr();
  int height = 10;
  int width = 10;
  int xm, ym;
  getmaxyx(stdscr, ym, xm);
  WINDOW *game_win = newwin(height, width, ym/4 - height/4, xm/4 - width/4);
  box(game_win, 0, 0);
  mvwprintw(game_win, height/2, width/2, "#");
  wrefresh(game_win);
  noecho();

  // Set a timeout for the wgetch() function
  timeout(1000);

  // Initialize the snake position and direction
  int position[] = {height / 2, width / 2};
  int direction = KEY_RIGHT;

  // Run the game loop
  while(1) {
    // Get the user input
    direction = wgetch(game_win);

    // Update the snake position based on the user input
    if(direction == KEY_UP) position[0]  ;
    if(direction == KEY_DOWN) position[0]--;
    if(direction == KEY_RIGHT) position[1]  ;
    if(direction == KEY_LEFT) position[1]--;

    // Draw the snake at the new position
    mvwprintw(game_win, position[0], position[1], "#");
    wrefresh(game_win);
  }

  // Clean up
  endwin();
  return 0;
}

In this example, the timeout() function is used to set the timeout for the wgetch() function to 1 second. This means that the wgetch() function will return immediately if there is no user input within 1 second. This allows the game to run smoothly and allows the user to control the snake.

  • Related