Code:
SDL_Surface *screen = NULL;
void make_move();
void collision();
void setup_board();
void randomize();
//Global
int score,snake_length, speed, level,r,c;
int obstacles=10;
char grid[maxrow][maxcol];
int direction = SDLK_RIGHT;
struct snake_segment {
int row,col;
} snake[100];
// Start up SDL
void init() {
// Start SDL
SDL_Init(SDL_INIT_EVERYTHING);
// Set the caption
SDL_WM_SetCaption("Snake", NULL);
//Set up Screen
screen = SDL_SetVideoMode(640,480,32, SDL_SWSURFACE);
}
void draw() {
// Declare Images
SDL_Surface *food= SDL_LoadBMP("food.bmp");
SDL_Surface *obstacle= SDL_LoadBMP("obstacle.bmp");
SDL_Surface *background= SDL_LoadBMP("background.bmp");
SDL_Surface *segment= SDL_LoadBMP("segment.bmp");
SDL_Surface *cats= SDL_LoadBMP("cats.bmp");
SDL_Surface *blank= SDL_LoadBMP("blank.bmp");
SDL_BlitSurface(background, NULL, screen, NULL);
int row,col,i,j;
// Plot Obstacle and Food
SDL_Rect location;
for(row=1;row <= maxrow;row++){
for(col=1;col <= maxcol;col++){
location.y = row * px;
location.x = col * px;
if(grid[row][col] == 'o') {
SDL_BlitSurface(obstacle, NULL, screen,&location);
}
else if (grid[row][col] == 'f') {
SDL_BlitSurface(food, NULL, screen, &location);
}
}
}
for(i=0;i<snake_length;i++){
location.y=snake[i].row * px;
location.x=snake[i].col * px;
SDL_BlitSurface(segment,NULL,screen,&location);
}
SDL_Flip(screen);
}
void init_board() {
int row, col, i,j;
snake_length=5;
//Set up Grid
for(row=0; row<=maxrow;row++){
for(col=0;col<=maxcol;col++){
grid[row][col]= ' ';
}
}
// Set up obstacles
for(j=0;j<=obstacles*2;j++){
row= rand()%maxrow;
col= rand()%maxcol;
if(j<obstacles){
grid[row][col] = 'o';
} else {
grid[row][col] = 'f';
}
}
for(i=0;i<snake_length;i++){
snake[i].row=snake_start_row;
snake[i].col=(snake_start_col + i);
}
}
int main(int argc, char **argv)
{
int j,row,col;
SDL_Event keyevent;
init(); // Start SDL
// initialize the grid
init_board();
do { // repeatedly
// draw the state of the game
draw();
// await user input
make_move();
collision();
} while(1);
}
void make_move(){
SDL_Surface *blank= SDL_LoadBMP("blank.bmp");
SDL_Surface *segment= SDL_LoadBMP("segment.bmp");
SDL_Rect location;
SDL_Event keyevent;
int j;
if(SDL_PollEvent(&keyevent)){
if(keyevent.type==SDL_KEYDOWN){// (keyevent.type){
switch(keyevent.key.keysym.sym){
case SDLK_LEFT:
snake[snake_length].row=snake[snake_length-1].row; // End segment loses segments go up one row
snake[snake_length].col=snake[snake_length-1].col-1;
break;
case SDLK_RIGHT:
snake[snake_length].row=snake[snake_length-1].row; // End segment loses segments go up one row
snake[snake_length].col=snake[snake_length-1].col+1;
break;
case SDLK_UP:
snake[snake_length].row=snake[snake_length-1].row-1; // End segment loses segments go up one row
snake[snake_length].col=snake[snake_length-1].col;
break;
case SDLK_DOWN:
snake[snake_length].row=snake[snake_length-1].row+1;
snake[snake_length].col=snake[snake_length-1].col;
break;
case SDLK_q:
exit(0);
}
int k;
for(k=1;k<=snake_length;k++){
snake[k-1]=snake[k];
}
} else if (keyevent.type == SDL_QUIT) {
exit(0);
}
}
SDL_Flip(screen);
}
void collision(){
int i;
// Walls
if((snake[snake_length-1].row>maxrow-2) || (snake[snake_length-1].row<1)
|| (snake[snake_length-1].col>maxcol-2) || (snake[snake_length-1].col<1)
// Obstacle
|| (grid[snake[snake_length-1].row][snake[snake_length-1].col] == 'o')){
exit(0);
}
// Snake
for(i=0;i<snake_length-1;i++){
if((snake[snake_length-1].row == snake[i].row) &&
(snake[snake_length-1].col == snake[i].col)){
exit(0);
}
}
// Food
if(grid[snake[snake_length-1].row][snake[snake_length-1].col] == 'f'){
snake_length=snake_length+1;
}
}
I added the collision function and added to the make_move function but am not sure why when i pick up the food the snake length does not increase. I am not going to completely finish this by tommorow but I want to at least implement the food eating.