Ejemplo n.º 1
0
int snake_start()
{
restart:
  score = 0;
  init_snake();
  init_map();
  clear();
  refresh();
  draw_score();
  draw_help();
  draw_box(0, 1, LINES-2, COLS-2);
  draw_snake();
  draw_map();
  gen_food();

  if ((pthread_create(&thread_input, NULL, get_input, NULL)) != 0)
    exit(1);
  while (1) {
    usleep(speed);
    check_input();
    if (exit_flag) {
      pthread_cancel(thread_input);
      exit_snake();
      break;
    }
    if (pause_flag) {
      pthread_cancel(thread_input);
      getch();
      pause_flag = 0;
      if ((pthread_create(&thread_input, NULL, get_input, NULL)) != 0)
        exit(1);
    }
    move_snake();
    if (check_dead()) {
      pthread_cancel(thread_input);
      if (snake_dead_exit()) {
        exit_snake();
        break;
      }
      goto restart;
    }
    if (check_food()) {
      inc_score();
      draw_score();
      snake_grow_one();
      gen_food();
    }
  };

  return 0;
}
Ejemplo n.º 2
0
 void env::reset( size_t length, size_t width, representing_char && rc, size_t food_num, list<coord> && snake )
 {
   this->rc = rc;
   food_in_snake = 0;
   srand( time( 0 ) );
   vec.clear( );
   is_snake_alive = true;
   assert( length > 0 );
   assert( width > 0 );
   cur_map_length = length;
   cur_map_width = width;
   vec.resize( cur_map_length, vector< shared_ptr< point > >( cur_map_width ) );
   for ( size_t i = 0; i < cur_map_length; ++i )
   {
     for ( size_t ii = 0; ii < cur_map_width; ++ii )
     {
       vec[ i ][ ii ] = shared_ptr< point >( fact.space_fact( * this, move( coord( i, ii ) ) ) );
     }
   }
   cur_head = snake.front( );
   size_t life = snake.size( );
   assert( life > 0 );
   for ( auto & i : snake )
   {
     vec[ i.y ][ i.x ]->pass( shared_ptr< point >( fact.snake_fact( life, * this, move( i ) ) ) );
     --life;
   }
   assert( life == 0 );
   gen_food( food_num );
 }