Ejemplo n.º 1
0
// create a new fruit in the game
void grow_fruit(GAME* game) {
  int randy,randx;
  // generate a new random position until a empty spot is found
  do {
    randy = rand() % game->rows;
    randx = rand() % game->columns;
    // is nothing else there in the generated position?
  } while (snake_part_is_on(&game->snake, randy, randx) != NULL || fruit_is_on(&game->fruits, randy, randx) != NULL || check_extended_border_collision(game, randy, randx));

  // increase the length
  game->fruits.length++;
  // allocate memory for the new fruit
  if(game->fruits.length == 0) {
    // initialize the array with malloc if it is the first fruit in the array
    game->fruits.fruits = malloc(sizeof(FRUIT) * game->fruits.length);
  } else {
    // allocate more memory
    game->fruits.fruits = realloc(game->fruits.fruits, sizeof(FRUIT) * game->fruits.length);
  }
  
  // get a filled struct (containing the displayed char, the effect & co.) of the new fruit
  get_fruit(&game->fruits.fruits[game->fruits.length - 1], randy, randx);
}
Ejemplo n.º 2
0
// check if the snake is colliding on it's self
// we only need to check if the snake head is colliding
int check_self_collision(GAME* game, int cury, int curx) {
  WINDOW* on;
  // check if the position of the snake head is matching with the position of a snake part
  // exept for the last part (because it will move
  return !((on = snake_part_is_on(&game->snake, cury, curx)) == NULL || on == game->snake.parts[game->snake.length - 1]);
}