Exemplo n.º 1
0
Arquivo: game.c Projeto: posva/msnake
// redraw the whole screen
void redraw_game(GAME *game) {
  // redraw the main window (containg the border and stuff)
  clear();
  draw_border(game);
  redrawwin(stdscr);
  refresh();
  
  // redraw the fruits
  redraw_fruits(&game->fruits);

  // redraw the snake
  redraw_snake(&game->snake);
}
Exemplo n.º 2
0
static void game_pause (void)
{
    int button;

    rb->lcd_clear_display();
    rb->lcd_getstringsize("Paused",&strwdt,&strhgt);
    rb->lcd_putsxy((LCD_WIDTH - strwdt)/2,LCD_HEIGHT/2,"Paused");

    rb->lcd_update();
    while (1)
    {
        button = rb->button_get(true);
        switch (button)
        {
            case SNAKE2_PLAYPAUSE:
                redraw();
                redraw_snake();
                draw_head_bit(headx, heady);
                rb->lcd_update();
                rb->sleep(HZ/2);
                return;

#ifdef SNAKE2_RC_QUIT
            case SNAKE2_RC_QUIT:
#endif
            case SNAKE2_QUIT:
                dead = 1;
                quit = 1;
                return;

            default:
                if (rb->default_event_handler(button)==SYS_USB_CONNECTED) {
                    dead = 1;
                    quit = 2;
                    return;
                }
                break;
        }
    }
}
Exemplo n.º 3
0
// move the snake
int move_snake(GAME *game) {
  int success = 1, i;

  // some variables containing positions of certain things
  int curx, cury, tmpy, tmpx;

  // callbacks to check collisions
  int (*collision_checks[EVENTS])(GAME*, int, int) = {
    check_self_collision,
    check_extended_border_collision,
    check_fruit_collision
  };

  // callbacks which will be called automaticly if a collision is detected
  // the callback at position 1 in the array belongs to the check-function
  // at position 1 from the array above
  int (*collision_handlers[EVENTS])(GAME*, int, int) = {
    check_self_collision_handler,
    check_border_collision_handler,
    check_fruit_collision_handler
  };


  // difference on x-axis according to the direction
  int xdiff = game->snake.dir == DIR_LEFT ? -1 : (game->snake.dir == DIR_RIGHT ? 1 : 0);
  // difference on y-axis according to the direction
  int ydiff = game->snake.dir == DIR_UP ? -1 : (game->snake.dir == DIR_DOWN ? 1 : 0);

  // the position of the snake head
  getbegyx(game->snake.parts[0], cury, curx);

  // make a copy
  tmpy = cury;
  tmpx = curx;

  // calculate the new position and prevent from exceeding the size of the screen
  cury = (cury + ydiff) % game->rows;
  curx = (curx + xdiff) % game->columns;
  // the values have to be positive
  cury = cury < 0 ? game->rows + cury : cury;
  curx = curx < 0 ? game->columns + curx : curx;

  // check for collisons and execute the handlers if a collision occured
  for(i = 0; i < EVENTS && success; i++) {
    // collision?
    if((collision_checks[i](game, cury, curx))) {
      // should we end the game because of the collision?
      if(!collision_handlers[i](game, cury, curx)) {
        success = 0;
      }
    }
  }

  // no collisions ?
  if(success) {
    // set the direction of the head
    mvwprintw(game->snake.parts[0], 0, 0, "%c", game->snake.dir);
    // move the window
    mvwin(game->snake.parts[0], cury, curx);

    // copy values back
    cury = tmpy;
    curx = tmpx;

    // iterate through each part of the snake
    for(i = 1; i < game->snake.length; i++) {
      // get the position of the current part
      getbegyx(game->snake.parts[i], tmpy, tmpx);

      // move the part to the position of the previous one
      mvwin(game->snake.parts[i], cury, curx);

      // make a copy
      cury = tmpy;
      curx = tmpx;
    }

    // grow?
    if(game->snake.grow > 0) {
      // grow the snake
      grow_snake(&game->snake, cury, curx);

      // decrease the grow counter (number of times the snake will grow in the future)
      game->snake.grow--;
    } else {
      // is the snake head on the same position as the last part of the snake was before?
      getbegyx(game->snake.parts[0], tmpy, tmpx);
      if(!(tmpy == cury && tmpx == curx)) {
        // if no print a space at this position
        WINDOW *cur = newwin(1, 1, cury, curx);
        wprintw(cur, "%c", ' ');
        wrefresh(cur);
        delwin(cur);
      }
    }

    // redraw the snake on the screen
    redraw_snake(&game->snake);
  } else {
    SDL_LockAudio();
    oamlPlaySfx("lose");
    SDL_UnlockAudio();
  }
  return success;
}