Пример #1
0
int main(int argc, char *argv[]) {
    if (argc > 2) {
        print_usage_and_exit();
    }
    difficulty_t level = MEDIUM;
    if (argc == 2) {
        if (!strcmp(argv[1], "easy")) {
            level = EASY;
        } else if (!strcmp(argv[1], "medium")) {
            level = MEDIUM;
        } else if (!strcmp(argv[1], "hard")) {
            level = HARD;
        } else {
            print_usage_and_exit();
        }
    }

    grid_t* grid = init_game_grid(level);
    ui_init(grid->tiles_per_row * ICON_SIZE);
    load_icons(grid);

    srand(time(0));

    int done = 0;
    waiting = 0;
    previous_tile = NULL;
    ui_event_t event;
    while (!done) {
        while (ui_poll_event(&event)) {
            switch (event.type) {
            case ui_event_quit:
                done = 1;
                break;
            case ui_event_click:
                if (waiting) {
                    break;
                }
                int x = event.click.x;
                int y = event.click.y;
                clicked_tile = get_clicked_tile(grid, x, y);
                on_tile_click();
                break;
            }
        }
        ui_draw_grid(grid);
        ui_render();
    }
    ui_destroy();
    return 0;
}
Пример #2
0
int main() {

  // Initial blocks
  uint8_t blocks[5][2] = { {2,1}, {3,5}, {4,5}, {4,6}, {9,8}};

  // Initial start position
  uint8_t startx = WIDTH / 2;
  uint8_t starty = HEIGHT / 2;
	
  int readc = 0, quit = 0, playerid = PLAYER1;
  int textpos = 0;
	
  // Game area
  gamearea* game = new_gamearea(WIDTH,HEIGHT,5,blocks);
	
  // Player
  player* pl1 = new_player(playerid,startx,starty,HEALTH);

  initscr(); // Start ncurses
  noecho(); // Disable echoing of terminal input
  cbreak(); // Individual keystrokes
  intrflush(stdscr, FALSE); // Prevent interrupt flush
  keypad(stdscr,TRUE); // Allow keypad usage 

  start_color(); // Initialize colors

  // Color pairs init_pair(colorpair id,foreground color, background color)
  init_pair(PLAYER1,PLAYER1,COLOR_BLACK); // Player1 = COLOR_RED (1)
  init_pair(PLAYER2,PLAYER2,COLOR_BLACK); // Player2 = COLOR_GREEN (2)
  init_pair(PLAYER3,PLAYER3,COLOR_BLACK); // Player3 = COLOR_YELLOW (3)
  init_pair(PLAYER4,PLAYER4,COLOR_BLACK); // Player4 = COLOR_BLUE (4)
  init_pair(PLAYER_HIT_COLOR,COLOR_RED,COLOR_YELLOW);
  init_pair(WALL_COLOR,COLOR_WHITE,COLOR_WHITE);

  // Prepare everything
  clear_log();
  prepare_horizontal_line(WIDTH);
  ui_draw_grid(game, pl1);

  fd_set readfs;
  int rval = 0;

  while(1) {
    FD_ZERO(&readfs);
    FD_SET(fileno(stdin),&readfs);
 
    // Block until we have something
    if((rval = select(fileno(stdin)+1,&readfs,NULL,NULL,NULL)) > 0) {

      // From user
      if(FD_ISSET(fileno(stdin),&readfs)) {
        readc = getch(); // Get each keypress
        pl1->hitwall = 0;

        switch(readc) {
          case KEY_LEFT:
            if(is_position_a_wall(game,pl1->posx-1,pl1->posy)) pl1->hitwall = 1;
            else pl1->posx--;
            break;
          case KEY_RIGHT:
            if(is_position_a_wall(game,pl1->posx+1,pl1->posy)) pl1->hitwall = 1;
            else pl1->posx++;
            break;
          case KEY_UP:
            if(is_position_a_wall(game,pl1->posx,pl1->posy-1)) pl1->hitwall = 1;
            else pl1->posy--;
            break;
          case KEY_DOWN:
            if(is_position_a_wall(game,pl1->posx,pl1->posy+1)) pl1->hitwall = 1;
            else pl1->posy++;
            break;
          // Function keys, here F1 is reacted to
          case KEY_F(1):
            status = status ^ 1;
            break;
          case 27: // Escape key
            quit = 1;
            break;
          case '/':
            // User wants to write something
            memset(&textbuffer,0,BUFFER);
            textinput = 1;
            textpos = 0;
            break;
          // Erase text
          case KEY_BACKSPACE:
          case KEY_DC:
            textpos--;
            textbuffer[textpos] = '\0';
            break;
          // Push the line to log with enter
          case KEY_ENTER:
          case '\n':
            textinput = 0;
            if(strlen(textbuffer) > 0) add_log(textbuffer,textpos);
            textpos = 0;
            memset(&textbuffer,0,BUFFER);
            break;
          // Add the character to textbuffer if we were inputting text
          default:
            if(textinput == 1) {
              textbuffer[textpos] = readc;
              textpos++;
              if(textpos == BUFFER-1) {
                textpos = 0;
                textinput = 0;
              }
            }
            break;
          }
        }
      }

    // Hit a wall, change player
    if(pl1->hitwall) {
      pl1->health--;
      if(pl1->id == PLAYER4) pl1->id = PLAYER1;
      else pl1->id++;
    }

    // Update screen
    ui_draw_grid(game, pl1);

    // Suicide
    if(pl1->health == 0) {
      ui_draw_end(1);
      break;
    }
		
    // Surrended
    if(quit) {
      ui_draw_end(0);
      break;
    }
  }
  free_gamearea(game);
  free_player(pl1);
  free_horizontal_line();
  sleep(1);
  endwin(); // End ncurses
  return 0;
}