public void checkForHit() { for (int y = 0; y < 4; y++) { for (int x = 0; x < 10; x++) { if (enemies[y][x].getBounds().contains(playerProjX, playerProj.getPositionY()) && playerProj.isVisible() && enemies[y][x].isVisible()) { // loop through enemies, if they have the projectile in // them, destroy them and the shot playerProj.setVisible(false); score += enemies[y][x].damage(); updateLabels(); } } } checkForWin(); for (int i = 0; i < projectileAmount; i++) { if (player.getBounds().contains(enemyProjX[i], enemyProj[i].getPositionY()) && player.isVisible() && enemyProj[i].isVisible()) { enemyProj[i].setVisible(false); player.setVisible(false); if (lives != -1) { livesImages[lives].setVisible(false); } else { // Player loses, lives = -1 loss(); } } } }
public void shootProjectile() { // called when the player shoots (presses up) if (!playerProj.isVisible() && player.isVisible() && thereAreInvaders()) { // Cannot shoot during win time playerProjX = player.getPositionX() + 17; // Center playerProj.setPosition(playerProjX, PLAYER_START_Y); playerProj.setLocation(playerProjX, PLAYER_START_Y); playerProj.setVisible(true); // Start it from they player's x, and starting y (constant) } // The player cannot shoot while the projectile is visible (exists) }
public void moveProjectile() { // if the player's shot exists if (playerProj.isVisible()) { playerProj.setPositionY(playerProj.getPositionY() - 5); // Move it down (higher edge of the screen) playerProj.setLocation(playerProjX, playerProj.getPositionY()); // Check for hit checkForHit(); } if (playerProj.getPositionY() <= ceiling.getLocation().getY() + 5) { playerProj.setVisible(false); } checkForHit(); }