Example #1
0
int main (int argc, char* argv[]) {
  if (argc > 1) args_handle (argc, argv);

  if(tron.user_name[0] == 0) {
    char *tmp = getlogin();
    if(tmp == NULL) {
      ifitron_abort("No username provided and attempts to fetch it automatically, failed.\n");
    }
    strcpy(tron.user_name, tmp);
  }

  if(get_server_mode() == 2) {
    while(TRUE) {
      int result = play_multiplayer();
      if(result == -1)
        break;
    }
    engine_exit();
    ifitron_exit();
  }

  if(client_host_addr != NULL) { //Connect to the server
    int sd = connect_to_server();
    if(sd == -1) {
      ifitron_abort("Couldn't connect to the server.\n");
    }else{
      set_client_socket_descriptor(sd);
    }
  }

  engine_init ();
  engine_show_main_menu ();
  ifitron_init ();
	
  while (TRUE) {
    if (tron.is_alive == FALSE) 
      ifitron_game_over ();
    
    engine_get_game_input();
    player_update(get_tron());
    
    player_increase_size (get_tron(), 1);
    player_increase_score (get_tron(), game.level);
	  
    if (tron.score % 50 == 0 && game.level < 9) game.level++;

    if (player_hit_self(get_tron()) == TRUE  || player_hit_borders(get_tron()) == TRUE)
      tron.is_alive = FALSE;

    engine_show_screen ();
  }
  return 0;
}
Example #2
0
File: engine.c Project: gsrr/Python
/**	Get the user input during game and make the right decisions
 */
void engine_get_game_input ()
{
	// The input variable MUST be int to accept non-ascii characters
	int input = getch ();

	switch (input)
	{

	case ERR:
		// If we get no input
		break;

	case KEY_UP:    case 'w': case 'W':
		player_change_direction (UP);
		break;

	case KEY_LEFT:  case 'a': case 'A':
		player_change_direction (LEFT);
		break;

	case KEY_DOWN:  case 's': case 'S':
		player_change_direction (DOWN);
		break;

	case KEY_RIGHT: case 'd': case 'D':
		player_change_direction (RIGHT);
		break;

	case 'q':	case 'Q':
		engine_exit ();
		nsnake_exit ();
		break;

#ifdef DEBUG //debug key to increase score and size (gcc -DDEBUG)
	case 'e':	case 'E':
		player_increase_score (100);
		player_increase_size (2);
		break;
#endif
	case 'p':	case 'P':
		engine_show_pause ();
		break;

	default:
		break;
	}
}
Example #3
0
File: main.c Project: gsrr/Python
/**	The main function - contains the main loop of the game.
 *
 *
 *	@note I tried to make this function as clear as possible, so anyone
 *        could understaing the whole game logic starting by here. @n
 *        Have fun with the source code!
 */
 int main (int argc, char* argv[])
{
	if (argc > 1)
		args_handle (argc, argv);

	engine_init ();
	engine_show_main_menu ();
	nsnake_init ();

	while (TRUE == TRUE)
	{
		if (snake.is_alive == FALSE)
			nsnake_game_over ();

		engine_get_game_input ();

		player_update ();
		fruit_update_bonus ();

		if (player_hit_fruit () == TRUE)
		{
			// Is this score arbitrary?
			player_increase_score (game.level*3 + fruit.bonus);
			player_increase_size (2);
			fruit_init ();
		}

		if (player_hit_self () == TRUE)
			snake.is_alive = FALSE;

		if (player_collided_with_borders () == TRUE)
			snake.is_alive = FALSE;

		engine_show_screen ();
	}


	// Even though we have this here, the game always quits during
	// the main loop
	engine_exit ();
	nsnake_exit ();
	return 0;
}
Example #4
0
int listen_for_connections(void) {
    int request_sd;
    int sds[2];
    player_t *player[2];
    player[0] = get_player(0);
    player[1] = get_player(1);

    byte game_type;
    int game_level;
    int clientaddrlen = sizeof(struct sockaddr);
    struct sockaddr_in serveraddr, clientaddr;

    //request-socket, lytter på innkommende forbindelser
    request_sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

    //adresse struct
    memset((void*)  &serveraddr, 0, sizeof(serveraddr));
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_addr.s_addr = INADDR_ANY;

    if(listening_port != 0) {
        serveraddr.sin_port = htons(listening_port);
        printf("Listening for connections on port '%d'\n", listening_port);
    } else {
        serveraddr.sin_port = htons(9090); //default if no port is provided
        printf("Listening for connections on default port '9090'\n");
    }
    //bind adressen til socketen
    bind(request_sd, (struct sockaddr *) &serveraddr, sizeof(serveraddr));

    //start lytting
    listen(request_sd, SOMAXCONN);

    //Receive connection from players
    int i;
    for(i = 0; i < 2; i++) {
        sds[i] = accept(request_sd, (struct sockaddr *) &clientaddr,
                        (socklen_t *) &clientaddrlen);
    }
    //Receive init from players
    for(i = 0; i < 2; i++) {
        int x, y, ox, oy;
        byte tmp;

        if(receive_game_init(sds[i], &tmp, &player[i]->opponents, &x, &y, &ox, &oy, player[i]->user_name) == -1) {
            perror("receive_game_init in listen_for_connection() encountered a error.\n");
            return -1;
        }
        if(i == 0) {
            game_type = tmp;
            game_level = game_type - 0x10;
        }
    }
    //Send init to both players
    for(i = 0; i < 2; i++) {
        int opponent = (i == 0) ? 1 : 0;
        if(send_game_init(sds[i], game_type, player[i]->opponents,
                          player[i]->body[0].x, player[i]->body[0].y,
                          player[opponent]->body[0].x, player[opponent]->body[0].y,
                          player[i]->user_name) == -1) {
            perror("send_game_init() encountered a problem.\n");
            return -1;
        }
    }
    //Send game start to both players
    for(i = 0; i < 2; i++) {
        if(send_game_start(sds[i]) == -1) {
            perror("send_game_start() encountered a problem.\n");
            return -1;
        }
    }
    //receive updates from players, check for collisons and respond with a update or
    //game_end if it was a collision.
    while(1) {
        for(i = 0; i < 2; i++) {
            int x, y;
            if(receive_game_update(sds[i], &x, &y) == -1) {
                perror("receive_game_update() encountered a problem.\n");
                return -1;
            }
            player_increase_size(player[i], 1);
            update_player_coordinates(player[i], x, y);
            player_increase_score(player[i], game_level);

            if(player[i]->score % 50 == 0 && game_level < 9)
                game_level++;
        }
        //check for collisions
        for(i = 0; i < 2; i++) {
            int opponent = (i == 0) ? 1 : 0;
            int result = 0;

            if(player_hit_borders(player[i])) {
                result = 0x1; //Player collided with wall
                player[i]->result = result;
                player[opponent]->result = 0x7;//No remaining opponents
            } else if(player_hit_self(player[i])) {
                result = 0x2; // Player collided with himself
                player[i]->result = result;
                player[opponent]->result = 0x7;
            } else if(player_hit_opponent(player[i], player[opponent])) {
                result = 0x4; //Player hit opponent
                player[i]->result = result;
                player[opponent]->result = 0x7;
                player[opponent]->takeouts++;
            }
            if(result != 0) {
                //send stop
                for(i = 0; i < 2; i++) {
                    if(send_game_end(sds[i]) == -1) {
                        perror("send_game_end encountered a error.\n");
                        return -1;
                    }
                }
                //send highscores
                if(client_host_addr != NULL) {
                    int highscore_sd = get_client_socket_descriptor();

                    score *sl[2];
                    for(i = 0; i < 2; i++) {
                        sl[i] = init_score();
                        sl[i]->game_type = game_type;
                        get_score(player[i], sl[i]);
                    }
                    if(send_score_list(highscore_sd, sl, 2) == -1) {
                        perror("Something went wrong sending score list.\n");
                        return -1;
                    }
                }
                close(sds[0]);
                close(sds[0]);
                close(request_sd);
                return 0;
            }
        }
        //send updates
        for(i = 0; i < 2; i++) {
            int which = (i == 0) ? 1 : 0;
            if(send_game_update(sds[i], player[which]->body[0].x, player[which]->body[0].y) == -1) {
                perror("send_game_update encountered a error.\n");
                return -1;
            }
        }
    }
}
Example #5
0
int play_multiplayer(void) {
  int sd = connect_to_server();
  if(sd == -1) {
    ifitron_abort("Couldn't connect to the server.\n");
  }else
    set_client_socket_descriptor(sd);

  engine_init();
  engine_show_main_menu();
  
  //initiate
  multiplayer_init(get_tron());
  player_t *opponent = malloc(sizeof(player_t));
  
  if(opponent == NULL) {
    ifitron_abort("Error allocating memory for the opponent in play_multiplayer().\n");
  }
  
  multiplayer_init(opponent);
  
  if(send_game_init(sd, MULTIPLE_PLAYER_TYPE + (game.level - 1), 0, 
                    0, 0, 0, 0, tron.user_name) == -1) 
    ifitron_abort("send_game_init encountered an error.\n");
  
  //Receive init. In case someone initiated sooner and level is something other
  //than what this player requested.
  byte game_type;
  int x, y;
  int ox, oy;
  char buf[25];
  
  if(receive_game_init(sd, &game_type, &get_tron()->opponents,
                       &x, &y, &ox, &oy, buf) == -1) 
    ifitron_abort("Something went wrong receiving_game_init()\n");
    
  game.level = game_type - MULTIPLE_PLAYER_TYPE;
  player_increase_size(&tron, 1);
  update_player_coordinates(&tron, x, y); 
  
  player_increase_size(opponent, 1);
  update_player_coordinates(opponent, ox, oy);
  
  //wait for start.
  if(receive_game_start(sd) == -1) 
    ifitron_abort("Something went wrong receiving game start.\n");
  
  while(TRUE) {
    engine_get_game_input();
    
    player_increase_size(get_tron(), 1);
    player_update(get_tron());
    
    player_increase_score(get_tron(), game.level);
    
    if(tron.score % 50 == 0 && game.level < 9) game.level++;
    
    if(send_game_update(sd, get_tron()->body[0].x, get_tron()->body[0].y) == -1) 
      ifitron_abort("send_game_update encountered an error.\n");
    
    //Check if end or update is coming
    int size = 5;
    char buf[5];
    if(read(sd, buf, size) == -1) 
      ifitron_abort("Error reading end or update.\n");
    
    if(buf[2] == GAME_UPDATE) {
      player_increase_size(opponent, 1);
      update_player_coordinates(opponent, buf[3], buf[4]);
      player_increase_score(opponent, game.level);
      
    }else if(buf[2] == GAME_END) {
      //free opponent's memory
      if(opponent->body != NULL) 
        free(opponent->body);
      free(opponent);
      
      //close the connection
      close_server_connection(sd);
      ifitron_game_over();
      return 0;
    }
    //Draw the map with the players
    multiplayer_show_screen (opponent);
  }
}