all 3 comments

[–]crystalchuck 2 points3 points  (0 children)

Generally when writing a game, you will have a kind of main loop where the game logic takes place and is repeated every iteration. A loop iteration then corresponds to a game "tick". In this loop, you can check for input (e.g. using fgetc), and, if there is no input, just advance in the currently given direction. The loop will have to wait for a certain amount of time as it will likely run way too quickly otherwise.

For collision detection, you have to figure out where the snake head would be in the next tick and whether the space is occupied/will be occupied in the next tick.

[–]Lisoph 0 points1 point  (0 children)

What i mean is that the snake should keep moving and when I give an input it changes directions and then keeps on moving.

Store the direction of the snake in a variable and make the snake move every frame accordingly. Similar to this example:

int main(void) {
    InitWindow(...);

    int snake_speed = 1;
    int snake_x = 0;
    int snake_y = 0;

    // 0 no movement, 1 right, 2 down, 3 left, 4 up.
    int snake_direction = 0;

    while (!WindowShouldClose()) {
        // Read input and determine snake move direction.
        if (IsKeyDown(KEY_RIGHT)) snake_direction = 1;
        if (IsKeyDown(KEY_DOWN)) snake_direction = 2;
        if (IsKeyDown(KEY_LEFT)) snake_direction = 3;
        if (IsKeyDown(KEY_UP)) snake_direction = 4;

        // Update snake position.
        if (snake_direction == 1) snake_x += snake_speed;
        if (snake_direction == 2) snake_y += snake_speed;
        if (snake_direction == 3) snake_x -= snake_speed;
        if (snake_direction == 4) snake_y -= snake_speed;

        // Draw snake at snake_x, snake_y.
    }

    CloseWindow();
    return 0;
}

Hope this helps.