Пример #1
0
int main() {
  initscr();
  cbreak();
  noecho();
  keypad(stdscr, TRUE); // make keys work
  curs_set(0); // hide cursor
  timeout(100);

  int xmax;
  int ymax;
  getmaxyx(stdscr, ymax, xmax);
  enum Direction dir = RIGHT;

  Board* board = create_board(create_snake(), NULL, xmax, ymax);
  int i;
  for (i = 0; i < 6; i++) {
    add_new_food(board);
  }

  while(true) {
    erase();
    display_points(board->snake, ACS_BLOCK);
    display_points(board->foods, ACS_DIAMOND);
    dir = get_next_move(dir);
    enum Status status = move_snake(board, dir);
    if (status == FAILURE) break;
  }
  endwin();

  return 0;
}
Пример #2
0
int tick() {
    // Handle user input
    Direction in = get_direction();
    if (in != NO_DIRECTION) {
        push_move(move_list, in);
    }
    
    // Handle snake movement
    update_direction(snake, move_list);
    move_snake(snake);

    // Draw everything
    draw_frame(snake, food_list);

    Collision collision = find_collision(snake, food_list, screen);
    switch(collision){
        case SNAKE:
        case SCREEN:
            return 0;
        
        case FOOD:
            grow_snake();
            new_food();
            break;

        case NONE:
            break;
    }
    
    snake_sleep();
    return 1;
}
void start(int diff)
{
	STRU_B board[M][N];
	char drection = RIGHT;
	int isn_lose = 1, i, dc;

	initialise_board(board);
	creat_sanke(board);

	while (isn_lose)
	{
		dc = 0;
		system("CLS");
		show_board(board);
		for (i = 0; i < diff; i++)
		{
			if (GetKeyState(VK_UP) < 0 && drection != DOWN && dc == 0) drection = UP, dc++;
			if (GetKeyState(VK_DOWN) < 0 && drection != UP && dc == 0) drection = DOWN, dc++;
			if (GetKeyState(VK_LEFT) < 0 && drection != RIGHT && dc == 0) drection = LEFT, dc++;
			if (GetKeyState(VK_RIGHT) < 0 && drection != LEFT && dc == 0) drection = RIGHT, dc++;
			Sleep(10);
		}
		isn_lose = move_snake(board, drection);
	}
	system("CLS");
	show_board(board);
}
Пример #4
0
void do_startup_blinky (void)
{
	uint32_t ms;

	move_snake();
			
	ms = 1000;
	for (int i = 0; i < 12; i++) {
		LC0_HIGH;
		delay_ms(ms);
		LC0_LOW;
		move_snake();
		LC1_HIGH;
		delay_ms(ms);
		LC1_LOW;
		move_snake();
		LC2_HIGH;
		delay_ms(ms);
		LC2_LOW;
		move_snake();
		LC3_HIGH;
		delay_ms(ms);
		LC3_LOW;
		move_snake();
		ms = ms * 0.66;
	}	
	
	for (int j = 0; j < 3; j++) {
		LC0_HIGH;
		LC1_HIGH;
		LC2_HIGH;
		LC3_HIGH;
		set_num(9);
		_delay_ms(800);
		LC0_LOW;
		LC1_LOW;
		LC2_LOW;
		LC3_LOW;
		shift_reg_set(0xFF);
		_delay_ms(800);
	}	
	
	set_num(9);
}
Пример #5
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;
}
Пример #6
0
int start_game(snake psnake,food Tfood)
{
   move_snake(psnake);
   draw_snake(psnake);
   //检测按键:当按键为Q/q时,返回-1
   if(wait_for_press(psnake)==-1){
	   print_infowin("Quit Game!");
       gameover(gamewin, "Quit Game!");
	   return -1;
   }
   //检测贪吃蛇状态:1.是否吃到食物;2.是否触碰自己或者边界.
   if(check_snake(psnake,Tfood)<0){
	 gameover(gamewin,"GAME OVER!!!");
	 print_infowin("QUIT GMAE!");
	 return -1;
   }
   else
	 return 1;
}
Пример #7
0
int main(int argc, char *argv[])
{

  WINDOW *world;
  int offsetx, offsety, i, ch;

  initscr();
  noecho();
  cbreak();
  timeout(TICKRATE);
  keypad(stdscr, TRUE);

  printw("Cursed Snake v. 0.1 - Press q to quit...");

  refresh();

  offsetx = (COLS - WORLD_WIDTH) / 2;
  offsety = (LINES - WORLD_HEIGHT) / 2;

  world = newwin(WORLD_HEIGHT, 
                 WORLD_WIDTH, 
                 offsety, 
                 offsetx); 

  snake_part snake[SNAKE_LENGTH];

  int sbegx = (WORLD_WIDTH - SNAKE_LENGTH) / 2;
  int sbegy = (WORLD_HEIGHT - 1) / 2;

  for (i = 0; i < SNAKE_LENGTH; i++)
  {
    snake[i].x = sbegx + i;
    snake[i].y = sbegy;
  }

  int current_direction = RIGHT;


  while((ch = getch()) != 'q')
  {
    move_snake(world, current_direction, snake);

    if (ch != ERR)
    {
      switch(ch)
      {
        case KEY_UP:
          current_direction = UP;
          break;
        case KEY_DOWN:
          current_direction = DOWN;
          break;
        case KEY_RIGHT:
          current_direction = RIGHT;
          break;
        case KEY_LEFT:
          current_direction = LEFT;
          break;
        default:
          break;
      }
    }
  }

  delwin(world);
  
  endwin();

  return 0;
}
Пример #8
0
void run() {
  int ch = 0, ich, i, current_columns, current_rows, success = 1;

  // some variables for the timer (inclusive the interval)
  struct timespec last_time              = {};
  struct timespec current_time           = {};
  long long default_interval             = 40000000;
  long long interval                     = default_interval;
  long long res;
  char playername[HIGHSCORE_NAME_LENGTH] = {};

  int range_counter = 0;

  // create the game struct
  GAME game = {};

  // set the eat range to 1
  game.snake.eat_range = 1;

  // helper variable to keep track of how long we've paused
  time_t pause_start;

  // get the dimensions of the terminal and store them in the GAME struct
  getmaxyx(stdscr, game.rows, game.columns);

  // clear the whole screen
  clear();

  // draw the walls
  draw_border(&game);

  // show the newly created walls
  refresh();

  // place the snake in the middle of the game field
  grow_snake(&game.snake, game.rows / 2, game.columns / 2);
  game.snake.dir = DIR_LEFT;

  // create some fruits on the screen
  // NOM, NOM, NOM
  for(i = 0; i < 50; i++) {
    grow_fruit(&game);
  }
  
  // get the time when the game started
  time(&game.started);
  // get the current time
  current_utc_time(&last_time);

  // start the event loop
  while((ich = getch()) && success) {
    // key typed?
    if(ich == ERR) {
    } else if(ich == '0') {
      // reset the speed
      interval = default_interval;
    } else if(ich == '8') {
      // speed up
      interval *= 1.1;
    } else if(ich == '9') {
      // slow down
      interval *= 0.9;
    } else {
      // use this key as a direction
      ch = ich;
    }
    // check if we have an overrun
    current_utc_time(&current_time);

    // calculate the dirrence between the last snake move and the current time
    res = timeval_diff(&last_time, &current_time);

    // is the interval over?
    if(res > interval) {
      // has an effect on the eat_range ?
      if(game.snake.eat_range > 1) {
        // every 200th field, decrease the range
        range_counter = (range_counter + 1) % 150;
        // it turns to 0 after the 200th field
        if(range_counter == 0) {
          game.snake.eat_range--; // so, decrease it!
        }
      }
      // new direction? 
      if((ch == KEY_UP || ch == 'w') && game.snake.dir != DIR_DOWN) {
        game.snake.dir = DIR_UP;
      } else if((ch == KEY_LEFT || ch == 'a') && game.snake.dir != DIR_RIGHT) {
        game.snake.dir = DIR_LEFT;
      } else if((ch == KEY_RIGHT || ch == 'd') && game.snake.dir != DIR_LEFT) {
        game.snake.dir = DIR_RIGHT;
      } else if((ch == KEY_DOWN || ch == 's') && game.snake.dir != DIR_UP) {
        game.snake.dir = DIR_DOWN;
      }
      // move the snake
      success = move_snake(&game);

      // refresh the screen
      refresh();

      // display the status bar (top-right)
      status_display(&game);

      // update the time when we last moved the snake
      last_time = current_time;
    }
    
    getmaxyx(stdscr, current_rows, current_columns);
    // 'p' pressed || size of the terminal changed
    if(ich == 'p' || (current_rows != game.rows || current_columns != game.columns)) {
      // use the terminal new size
      game.rows = current_rows;
      game.columns = current_columns;

      // get the time
      time(&pause_start);

      // show the pause dialog
      switch(pause_dialog()) {
        case 2:
          // leave the game if '2' is pressed
          success = 0;
        default:
          // redraw the screen on resume
          game.paused += time(NULL) - pause_start;
          redraw_game(&game);
          break;
      }
    }
  }

  // get the time when the game has ended
  time(&game.ended);

  // display the highscore dialog & let the player enter his name
  display_highscore(&game, playername, HIGHSCORE_NAME_LENGTH);

  // has a name been entered? if not don't create a highscore entry
  if(playername[0]) {
    add_highscore(playername, game.highscore, game.ended - game.started - game.paused);
  }

  // free all the resources reserved in the game struct
  kill_game(&game);
}
Пример #9
0
/*
 * main -- Main program.
 */
int main(void) {
	uint8_t chars_into_escape_sequence = 0;
	int8_t moveStatus = 0;

	char c;

	/* Initialise our main clock */
	init_timer();

	/* Initialise serial I/O */
	init_serial_stdio(19200, 0);

	/* Make the display_row() function be called every 2ms.
	** (This function returns a timer number, but we ignore 
	** this since we'll never do anything with it.)
	*/
	execute_function_periodically(2, display_row);

	/* Register the time_increment() function to be called every 500ms.
	** This function just sets a variable (timePassedFlag).
	*/
	mainTimerNum = execute_function_periodically(500, time_increment);

	//4209435
	/* setup AVR to handle sounds*/
	init_sound();

	/*
	** Turn on interrupts (needed for timer and serial input/output to work)
	*/
	sei();
	
	/*
	** Display splash screen 
	*/
	splash_screen();
	show_instruction(NEWGAME);
	
	/*
	** Perform necessary initialisations for a new game.
	*/
	new_game();
		
	/*
	** Event loop - wait for a certain amount of time to pass or wait
	** for a character to arrive from standard input. The time_passed_flag
	** is set within the function time_increment() below - which is setup
	** to be called periodically.
	*/
	for(;;) {
		if(timePassedFlag) {
			moveStatus = move_snake();
			timePassedFlag = 0;
		} else if(input_available()) {
			/* Read the input from our terminal and handle it */
			c = fgetc(stdin);			
			if(chars_into_escape_sequence == 0 && c == '\x1b') {
				/*
				** Received ESCAPE character - we're one character into
				** an escape sequence
				*/
				chars_into_escape_sequence = 1;
			} else if(chars_into_escape_sequence == 1 && c == '[') {
				/* 
				** We're now two characters into an escape sequence
				*/
				chars_into_escape_sequence = 2;
			} else if (chars_into_escape_sequence == 2) {
				/* We're two characters into an escape sequence and
				** have received another - see if it is as expected.
				*/
				if (c == 'C') {
					/* Cursor right key pressed - Set next direction to
					** be moved to RIGHT */
					set_snake_dirn(RIGHT);
				}  
				if (c == 'D') {
					/* Cursor left key pressed - Set next direction to
					** be moved to LEFT */
					set_snake_dirn(LEFT);
				}  
				if (c == 'A') {
					/* Cursor up key pressed - Set next direction to
					** be moved to UP */
					set_snake_dirn(UP);
				}  
				if (c == 'B') {
					/* Cursor down key pressed - Set next direction to
					** be moved to DOWN */
					set_snake_dirn(DOWN);
				}

				/* else, unknown escape sequence */

				/* We're no longer part way through an escape sequence */
				chars_into_escape_sequence = 0; 
			} else if(chars_into_escape_sequence != 0) {
				/*
				** We started an escape sequence but didn't get a character
				** we recognised - discard it and assume that we're not
				** in an escape sequence.
				*/
				chars_into_escape_sequence = 0;
			} else if (c == ' ') {
				/* Space character received - move snake immediately */
				moveStatus = move_snake();
			} else {					
				if(c == 'N' || c == 'n'){	
					show_instruction(NEWGAME);				
					new_game();
				} else if(c == 'P' || c == 'p'){
					moveStatus = 0;
					pause_game();
				} else if(c == 'M' || c == 'm'){
					toggle_sound();
					display_sound_status();
				}
			}
		}

		switch(moveStatus){
			case ATE_FOOD:
				if(sound_status())
					play_sound();
				moveStatus = MOVE_OK;
				break;
		}

		if(moveStatus < 0) {
			/* Move failed - game over */
			handle_game_over();
			moveStatus = 0;
			update_score();
		}
	}
}
Пример #10
0
int main()
{
    init();
    set_window_title("Team27, inc - Innocent Snake");
    srand((unsigned)time(NULL));

    //kondisi map awal
    int map[SIZE_Y][SIZE_X] = {-1,-1,-1,-1,-1,-1,-1, 0, 0, 0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 0, 0, 0,-1,-1,-1,-1, 0, 0, 0,-1,-1,-1,-1,-1,-1,-1,-1,-1,
                               -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1,
                               -1, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1,
                               -1, 0, 0, 0,-1,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1,
                               -1, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1,
                               -1, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1,
                               -1, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1,
                               -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1,-1,-1,-1,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1,
                               -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1,
                               -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1,
                               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1,-1,-1, 0, 0, 0, 0,
                               -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0,-1,
                               -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0,-1,
                               -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0,-1,
                               -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1,-1,-1,-1,-1, 0, 0, 0,-1,-1,-1,-1,-1,-1, 0, 0,-1,
                               -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0,-1,
                               -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0,-1,
                               -1, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0,-1,
                               -1, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0,-1,
                               -1, 0, 0, 0, 0, 0, 0,-1,-1,-1,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1,
                               -1, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1,
                               -1, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1,
                               -1, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1,
                               -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1,
                               -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1,
                               -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1,
                               -1,-1,-1,-1,-1,-1,-1, 0, 0, 0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 0, 0, 0,-1,-1,-1,-1, 0, 0, 0,-1,-1,-1,-1,-1,-1,-1,-1,-1
                              };

    int counter = 0;
    int posX, posY;
    int loaded = 0;
    int x_his, y_his;
    int i, j, moveX = 1, moveY = 0;

    BITMAP *buffer      = create_bitmap(SCREEN_W, SCREEN_H);
    BITMAP *path        = create_bitmap(OBJECT_SIZE, OBJECT_SIZE);
    BITMAP *wall        = load_bitmap("wall.bmp", NULL);
    BITMAP *head        = load_bitmap("head.bmp", NULL);
    BITMAP *snake_body  = load_bitmap("body.bmp", NULL);
    BITMAP *eat         = create_bitmap(OBJECT_SIZE, OBJECT_SIZE);
    BITMAP *bg_map      = create_bitmap(SCREEN_W, SCREEN_H);

    SNAKE  *snake       = NULL;
    SNAKE  *pointer     = NULL;

    clear_to_color(path, makecol(255, 255, 255)); //jalan berwarna putih
    clear_to_color(eat,  makecol(255, 255, 255)); //makanan backgroundnya putih
    circlefill(eat, OBJECT_SIZE/2, OBJECT_SIZE/2, OBJECT_SIZE/2 - 4, makecol(255, 0, 0)); //makanan berwarna merah

    //membuat map snake di bitmap
    for(i=0; i<SIZE_Y; i++)
    {
        for(j=0; j<SIZE_X; j++)
        {
            switch(map[i][j])
            {
            case PATH :
                draw_sprite(bg_map, path, j*OBJECT_SIZE, i*OBJECT_SIZE);
                break;
            case WALL :
                draw_sprite(bg_map, wall, j*OBJECT_SIZE, i*OBJECT_SIZE);
                break;
            }
        }
    }

    //inisialisasi awal posisi snake
    add_body(&snake, 3, 1, map);
    map[snake->y][snake->x] = HEAD;
    add_body(&snake, 2, 1, map);
    add_body(&snake, 1, 1, map);

    set_keyboard_rate(10, 10);

    while (!key[KEY_ESC])
    {
        //deteksi apakah sudah ada makanan di map
        if(loaded==0) {
            do {
                posX = rand()%(SIZE_X-1) + 1;
                posY = rand()%(SIZE_Y-1) + 1;
            } while(map[posY][posX] != PATH);
            map[posY][posX] = EAT;
            loaded = 1;
        }

        //deteksi keyboard yang ditekan dan mengubah arah gerak snake
        if(counter==5) {
            if(key[KEY_UP] && moveY==0)
            {
                moveX=0;
                moveY=-1;
                clear_keybuf();
            }
            else if(key[KEY_DOWN] && moveY==0)
            {
                moveX=0;
                moveY=1;
                clear_keybuf();
            }
            else if(key[KEY_LEFT] && moveX==0)
            {
                moveX=-1;
                moveY=0;
                clear_keybuf();
            }
            else if(key[KEY_RIGHT] && moveX==0)
            {
                moveX=1;
                moveY=0;
                clear_keybuf();
            }
            x_his = snake->x;
            y_his = snake->y;

            //kalau menabrak sesuatu maka keluar
            if(!move_snake(snake, moveX, moveY, map)) break;

            //jika bertemu makanan maka tambah badan
            if(map[posY][posX] != EAT)
            {
                add_body(&snake, x_his, y_his, map);
                loaded = 0;
            }
            counter=0;
        }
        counter++;

        //tempelkan semua obyek ke buffer
        draw_sprite(buffer, bg_map, 0, 0);
        draw_sprite(buffer, eat, posX*OBJECT_SIZE, posY*OBJECT_SIZE);
        for(pointer = snake; pointer != NULL; pointer = pointer->next)
        {
            switch(map[pointer->y][pointer->x])
            {
            case BODY :
                draw_sprite(buffer, snake_body, pointer->x*OBJECT_SIZE, pointer->y*OBJECT_SIZE);
                break;
            case HEAD :
                draw_sprite(buffer, head, pointer->x*OBJECT_SIZE, pointer->y*OBJECT_SIZE);
                break;
            }
        }

        //tampilkan buffer ke screen
        draw_sprite(screen, buffer, 0, 0);
        clear_bitmap(buffer);
        rest(10);
    }

    //destroy semua bitmap
    destroy_bitmap(head);
    destroy_bitmap(buffer);
    destroy_bitmap(path);
    destroy_bitmap(wall);
    destroy_bitmap(snake_body);
    destroy_bitmap(eat);

    deinit();
    return 0;
}
Пример #11
0
int main(void)
{
    SCREEN screen;
    SDL_Rect src, food_place;
    SDL_Rect all_dst[32];
    int quit_screen = TRUE;
    int key_quit = KEY_NO;

    /*创建一个窗口*/
    creat_screen(&screen);
    /*初始化蛇的位置*/
    init_snake(&screen, &src, all_dst, snake_lenth);

    begin_time = SDL_GetTicks();

    food_place = rand_xy(&food_place);

    while (quit_screen)
    {
        /*移动*/
        quit_screen = move_snake(&screen, &src, all_dst, snake_lenth);
        /*判断是否吃到食物*/
        if (getfood(&all_dst[0], &food_place))
        {
            /*刷新一个食物*/
            food_place = rand_xy(&food_place);
            /*长度加1*/
            snake_lenth++;
            /*如果长度大于NEXT, 速度加快,进入下一关*/
            if (snake_lenth > NEXT)
            {
                /*清除蛇结点*/
                clean_snake(&screen, all_dst, snake_lenth);
                /*速度增加*/
                speed -= STEP;
                /*长度变为初始化长度*/
                snake_lenth = INIT_LENTH;
            }

            /*如果速度速度达到WIN, 胜利*/
            if (speed < WIN)
            {
                /*显示胜利*/
                game_win(screen.p_screen);
                while (quit_screen)
                {
                    quit_screen = key_snake(&key_quit);
                    sleep(10000);
                }
            }
        }

        /*意外判断, 撞到边界、撞到自己*/
        if (check(all_dst))
        {
            /*显示gameover*/
            game_over(screen.p_screen);
            while (quit_screen)
            {
                quit_screen = key_snake(&key_quit);
                usleep(10000);
            }
        }
        appa(&screen, &food_place);
        SDL_Flip(screen.p_screen);

        usleep(10000);
    }
    SDL_FreeSurface(screen.show_picture);
    SDL_FreeSurface(screen.food);

    return 0;
}
Пример #12
0
static void game_tick(void *data)
{
    if (game->alive) {
        if (!game->is_resetting) {
             game_timer = app_timer_register(GAME_TICK_INTERVAL, game_tick, NULL);
        }
    } else {
        game_end(1);
    }

    // If game is paused, has the user presed the resume button?
    if (game->is_paused && game->queued_input != 3) {
        return;
    }

    // Check User Input
    if (game->queued_input) {
        switch (game->queued_input) {
            case 1:
                // Up pressed, TURN COUNTERCLOCKWISE
                if (snake->direction > 0) {
                    snake->direction -= 1;
                } else {
                    snake->direction = 3;
                }
                break;
            case 2:
                // Down pressed, TURN CLOCKWISE
                if (snake->direction < 3) {
                    snake->direction  += 1;
                } else {
                    snake->direction = 0;
                }
                break;
            case 3:
                game->is_paused = !game->is_paused;
                break;
            default:
                break;
        }
        game->queued_input = 0; 
    }

    if (game->alive) {
        move_snake(snake);
    }

    // Check if snake ate apple
    if(snake_has_eaten_apple(snake, apple)) {
        add_to_head(snake);
        // Move apple
        move_apple();
        game->score += (1 + game->bonus_points);
        snake->length += 1;
        vibes_short_pulse();

        if (game->bonus_points == 0) {
            bonus_points_timer = app_timer_register(BONUS_TIMER_INVTERVAL, bonus_timer_callback, NULL);
        }

        game->bonus_points = snake->length / 2;
        
    }

    // Update the graphics
    // Note: this could be moved to a separate graphics update interval
    // But since the game tick here is the same as the graphics tick
    // I saw no need to do two timers

    layer_mark_dirty(game_layer);
    
    static char score_text[12];

    // Update score layer
    if (game->is_paused) {
        snprintf(score_text, sizeof(score_text), "Paused");  
    } else {
        snprintf(score_text, sizeof(score_text), "Score: %d ", game->score);   
    }

    text_layer_set_text(game_score_label, score_text);

    static char bonus_text[12];
    snprintf(bonus_text, sizeof(bonus_text), "Bonus: %d", game->bonus_points);
    text_layer_set_text(game_bonus_label, bonus_text);
}
Пример #13
0
int game ( int *c ) {
	char title[100];

	int **body;
	int i = 0;
	int dir = *c;
	int state = 0;

	snakesize = 7;
	food_x = 0;
	food_y = 0;
	points = 0;
	*c = KEY_RIGHT;

	clear();
	body = (int **) malloc(sizeof(int *)*snakesize);
	for ( i = 0; i != snakesize; i++ ) {
		body[i] = (int *) malloc(sizeof(int)*2);
	}

	attron(COLOR_PAIR(PANEL));
	for ( i = 0; i != COLS; i++ ) {
		mvaddch(0, i, ' ');
	}
	sprintf(title," ..:: Snakie v 1.0 ::.. ");
	mvaddstr(0, COLS/2-strlen(title)/2, title);
	attroff(COLOR_PAIR(PANEL));
	refresh();

	for ( i = 0; i!= snakesize; i++ ) {
		body[i][x] = (COLS/2)-i;
		body[i][y] = (LINES/2);
	}

	
	pos_food(body);
	draw_food();
	draw_snake(body);

	while ( 1 ) { 
		clear_snake(body);
		switch(*c) {
			case KEY_UP:
				dir = 'U';
				break;

			case KEY_DOWN:
				dir = 'D';
				break;

			case KEY_RIGHT:
				dir = 'R';
				break;

			case KEY_LEFT:
				dir = 'L';
				break;
		}

		if ( *c == 'q' ) { 
			if ( confirm() == 1 ) {
				state = 0; 
				break; 
			}
		}

		move_snake(dir, &body);
		if ( (state = check_position(body)) != 0 ) {
			dead = 1;
			break;
		}
		eat_food(&body);
		draw_food();
		draw_info();
		draw_snake(body);
		usleep( dir == 'L' || dir == 'R' ? speed*0.75 : speed );
	}
	int ret = quit(&body, state);
	return ret;
}
Пример #14
0
int main( int argc, char *argv[ ] )
{
    srand(time(NULL));//Init randome seed.
    memset(world, 0, sizeof(world));
    SDL_Window *window;
    SDL_Surface *screen;
    if( SDL_Init( SDL_INIT_VIDEO ) == -1 )
    {
        printf( "Can't init SDL:  %s\n", SDL_GetError( ) );
        return EXIT_FAILURE;
    }

    atexit( SDL_Quit ); 
    window = SDL_CreateWindow("Ma fenêtre de jeu", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, 0 );
    screen = SDL_GetWindowSurface(window);

    if( screen == NULL )
    {
        printf( "Can't set video mode: %s\n", SDL_GetError( ) );
        return EXIT_FAILURE;
    }   

    int xspeed=0, yspeed=0;
    // Main loop
    SDL_Event event;
    draw_world(screen, world);
    while(1)
    {
	if(xspeed != 0 || yspeed != 0)
	{
            if(move_snake(screen, xspeed, yspeed) == -1)
	    {
		printf("Game Over !!!\n");
		return EXIT_SUCCESS;// Quit the program
                break;
	    }
	}

       // Check for messages
        if (SDL_PollEvent(&event))
        {
            // Check for the quit message
            switch (event.type)
            {
                case SDL_QUIT:
                    SDL_Quit();
		    return EXIT_SUCCESS;// Quit the program
                    break;
		case SDL_KEYDOWN:
			printf("keydown %d\n", event.key.keysym.sym);
			switch(event.key.keysym.sym)
			{
			case SDLK_DOWN:
			    if(yspeed != -SPEED_RATE)
			        yspeed = SPEED_RATE;
			    xspeed = 0;
			    break;
			case SDLK_UP:
			    if(yspeed != SPEED_RATE)
			        yspeed = -SPEED_RATE;
			    xspeed = 0;
			    break;
			case SDLK_RIGHT:
			    if(xspeed != -SPEED_RATE)
			        xspeed = SPEED_RATE;
			    yspeed = 0;
			    break;
			case SDLK_LEFT:
			    if(xspeed != SPEED_RATE)
			        xspeed = -SPEED_RATE;
			    yspeed = 0;
			    break;
			case SDLK_ESCAPE:
			    SDL_Quit();
			    return EXIT_SUCCESS;// Quit the program
			    break;
			}
		        printf("xspeed %d yspeed %d \n", xspeed, yspeed);
                    break;

            }
        }
       
        //Update the display
        SDL_UpdateWindowSurface(window);
        
    }

    // Tell the SDL to clean up and shut down
    SDL_Quit();

    return EXIT_SUCCESS;
}