Thread: help with c snake game

  1. #1
    Registered User
    Join Date
    Dec 2018
    Posts
    2

    help with c snake game

    help with c snake game-capture-pnghelp with c snake game-capture1-png
    can someone help me to create a snake game that will look like this and will preform like in the pictures
    my main probles is how to make the snake move and eat

    and i cannot use pointers

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,485
    > my main probles is how to make the snake move and eat
    So you've done all of these things?
    - initialise the board
    - place the food
    - create the initial snakes for % and @ players.

    How are you representing the snakes?

    You need some kind of data structure to represent 'N' segment coordinates of the snake.
    As the snake moves, the 'head' occupies the new position and the 'tail' is removed.

    Something like say
    Code:
    void moveSnake(Snake snake, int direction) {
        // code
    }
    You can then do this to put the snake on the board.
    Code:
    void drawSnake(Snake snake, Board board) {
        // code
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2018
    Posts
    2
    Quote Originally Posted by Salem View Post
    > my main probles is how to make the snake move and eat
    So you've done all of these things?
    - initialise the board
    - place the food
    - create the initial snakes for % and @ players.

    How are you representing the snakes?

    You need some kind of data structure to represent 'N' segment coordinates of the snake.
    As the snake moves, the 'head' occupies the new position and the 'tail' is removed.

    Something like say
    Code:
    void moveSnake(Snake snake, int direction) {
        // code
    }
    You can then do this to put the snake on the board.
    Code:
    void drawSnake(Snake snake, Board board) {
        // code
    }

    ok so to print the board i used 2 functions
    void help_print_board(int size)
    Code:
    {
        int i=0;
        printf("%c",BOARD_ELEMENT);
        for(i=0;i<size;i++)
        {
            printf("%c",BOARD_ELEMENT);
        }
        printf("%c\n",BOARD_ELEMENT);
    }
    void print_board(char board[N][N], int size)
    {
        int i=0,j=0;
        help_print_board(size);
        for(i=0;i<size;i++)
        {
            printf("%c",BOARD_ELEMENT);
            for(j=0;j<size;j++)
            {
                printf("%c",board[i][j]);
            }
            printf("%c\n",BOARD_ELEMENT);
        }
        help_print_board(size);
        printf("\n\n");
    }
    and this array (char board[N][N]) is representing the coordinates on the board
    so what i did to print the board initial board is:
    void start_board(char board[N][N], int size)
    Code:
    {
        int i=0,j=0;
        for(i=0;i<size;i++)
        {
            for(j=0;j<size;j++)
            {
                board[i][j]=' ';
                board[0][0]='%';
                board[0][1]='*';
                board[0][2]='*';
                board[size-1][0]='@';
                board[size-1][1]='+';
                board[size-1][2]='+';
            }
        }
        food_loc_in(board);
        print_board(board,size);
    }
    and this is the problem how can i represent the snakes?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,485
    Don't do this.
    Code:
                board[0][0]='%';
                board[0][1]='*';
                board[0][2]='*';
                board[size-1][0]='@';
                board[size-1][1]='+';
                board[size-1][2]='+';
    > food_loc_in(board);
    This is much better.

    Now use the same idea for snakes.

    Code:
    struct pos {
      int x,y;
    };
    
    // a player
    struct player {
        char headSymbol;
        char tailSymbol;
        struct pos positions[10];  // how long can your snake be?
    };
    
    struct player players[2];  // two players
    Then create a function (or two) to initialise both players,
    Code:
    player[0].headSymbol = '%';
    // etc etc
    Use this structure to also implement moveSnake() and drawSnake().
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Snake game
    By coder222 in forum C Programming
    Replies: 2
    Last Post: 12-08-2015, 03:03 PM
  2. Snake Game help on it please...
    By LekhAsh in forum C++ Programming
    Replies: 2
    Last Post: 01-30-2015, 01:45 AM
  3. Help with snake game in sdl/c
    By joellllmal in forum Game Programming
    Replies: 4
    Last Post: 08-22-2010, 12:14 AM
  4. About SnaKE game..
    By ozumsafa in forum C Programming
    Replies: 3
    Last Post: 10-19-2007, 05:46 PM
  5. game (snake)
    By nps in forum C Programming
    Replies: 1
    Last Post: 10-09-2001, 10:37 PM

Tags for this Thread