Attachment 15602Attachment 15603
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
Printable View
Attachment 15602Attachment 15603
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
> 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
You can then do this to put the snake on the board.Code:void moveSnake(Snake snake, int direction) {
// code
}
Code:void drawSnake(Snake snake, Board board) {
// code
}
ok so to print the board i used 2 functions
void help_print_board(int size)
and this array (char board[N][N]) is representing the coordinates on the boardCode:{
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");
}
so what i did to print the board initial board is:
void start_board(char board[N][N], int size)
and this is the problem how can i represent the snakes?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);
}
Don't do this.
> food_loc_in(board);Code:board[0][0]='%';
board[0][1]='*';
board[0][2]='*';
board[size-1][0]='@';
board[size-1][1]='+';
board[size-1][2]='+';
This is much better.
Now use the same idea for snakes.
Then create a function (or two) to initialise both players,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
Use this structure to also implement moveSnake() and drawSnake().Code:player[0].headSymbol = '%';
// etc etc