Exemplo n.º 1
0
static void collision() {
	int isSafe = 0;
	isSafe = gotHome(isSafe);

	int forgive = 5;
	if (frog.x <= 10 || frog.x >= 230 || frog.y <= 10 || frog.y >= 150) {
		numOfLives--;
		loseLife();
	}
	
	//crocodiles
	for (int i = 0; i < numOfCrocodiles; i++) {
		if (frog.x + 14 >= crocodiles[i].x &&
			frog.x <= crocodiles[i].x + 20 &&
			frog.y + 14 >= crocodiles[i].y &&
			frog.y <= crocodiles[i].y + 16 - forgive) {
			frog.x += crocodiles[i].dx;
			isSafe = 1;
		}
	}
	//woods
	for (int i = 0; i < numOfWoods; i++) {
		if (frog.x + 14 >= woods[i].x &&
			frog.x <= woods[i].x + 42 - forgive &&
			frog.y + 14 >= woods[i].y &&
			frog.y <= woods[i].y + 12 - forgive) {
			frog.x -= woods[i].dx;
			isSafe = 1;
		}
	}
	//cars
	for (int i = 0; i < numOfCars; i++) {
		if (frog.x + 14 >= cars[i].x &&
			frog.x <= cars[i].x + 16 - forgive &&
			frog.y + 14 >= cars[i].y &&
			frog.y <= cars[i].y + 10 - forgive) {
			loseLife();
		}
	}
	//trucks
		for (int i = 0; i < numOfTrucks; i++) {
			if (frog.x + 14 >= trucks[i].x &&
			frog.x <= trucks[i].x + 20 &&
			frog.y + 14 >= trucks[i].y &&
			frog.y <= trucks[i].y + 16 - forgive) {
			frog.x += trucks[i].dx;
			isSafe = 1;
		}
	}
	//river
	if (isSafe == 0 && frog.y <= 60 && frog.y >= 10) {
		loseLife();
	}
}
Exemplo n.º 2
0
void Battleship::gameLoop()
{

	bool end = false;
	while (!end) {

		// Ask user for co-ordinates
		int x, y;
		cout << endl << "Choose your co-ordinates in the " << GRID_SIZE << "x" << GRID_SIZE << " grid:" << endl;
		cout << "Your x co-ord: ";
		cin >> x;
		cout << "Your y co-ord: ";
		cin >> y;

		// Validate data inputted by user
		if (x < 1 || x > GRID_SIZE || y < 1 || y > GRID_SIZE) {
			cout << "You chose co-ordinates outside of the battleground!" << endl;
			cout << "Try again..." << endl;
		}
		else {

			if (battleground[y - 1][x - 1]) { // HIT
				if (player_battleground[y - 1][x - 1] == TileType::NONE) {
					incrementScore();
					player_battleground[y - 1][x - 1] = TileType::HIT;
					cout << endl << "HIT!" << endl;
				}
				else {
					cout << "You have already shot at this co-ordinate!" << endl;
				}
			}
			else { // MISS
				if (player_battleground[y - 1][x - 1] == TileType::NONE){
					loseLife();
					player_battleground[y - 1][x - 1] = TileType::MISS;
					cout << endl << "MISS!" << endl;
				}
				else {
					cout << "You have already shot at this co-ordinate!" << endl;
				}
			}

			printPlayerBattleground();

			end = checkEnd();

		}
	}
}