Package oop13.space.views

Examples of oop13.space.views.GamePanel


    Assert.assertTrue(model.getStatistics().getScore() == 990)
  }
 
  @Test
  public void testChangesInModel() {
    IModel model = new Model();
    model.initIndividuals();
    model.initAchievements();
    Ship spaceShip = model.getShip();
    Statistics statistics = model.getStatistics();
   
    //Testing adding and resetting statistics
    statistics.setLevelsCompleted(3);
    statistics.setMotherShipsKilled(5);
    statistics.setScore(300);
    statistics.setAliensKilled(100);
    Assert.assertTrue(statistics.getLevelsCompleted() == 3);
    Assert.assertTrue(statistics.getMotherShipsKilled() == 5);
    Assert.assertTrue(statistics.getScore() == 300);
    Assert.assertTrue(statistics.getAliensKilled() == 100);
    statistics.resetStatistics();
    for (Field f : statistics.getClass().getDeclaredFields()) {
      f.setAccessible(true);
      try {
        Assert.assertTrue(f.getInt(statistics) == 0);
      } catch (IllegalArgumentException e) {
        System.err.println(GameStrings.ILLEGAL_ARG_ERROR);
      } catch (IllegalAccessException e) {
        System.err.println(GameStrings.ILLEGAL_ACC_ERROR);
      }
    }
   
    //Testing movement and some methods of ship
    spaceShip.moveIndividual(spaceShip.getPositionX() + 10, spaceShip.getPositionY());
    Assert.assertTrue(spaceShip.getPositionX() == 280);
    spaceShip.moveIndividual(spaceShip.getPositionX() - 260, spaceShip.getPositionY());
    Assert.assertTrue(model.getShip().getPositionX() == 20);
    spaceShip.moveIndividual(spaceShip.getPositionX() - 10, spaceShip.getPositionY());
    Assert.assertTrue(spaceShip.getPositionX() == 20); //reached minimum left movement, the ship position doesn't change
    spaceShip.moveIndividual(spaceShip.getPositionX() + 520, spaceShip.getPositionY());
    Assert.assertTrue(spaceShip.getPositionX() == 540);
    spaceShip.moveIndividual(spaceShip.getPositionX() + 10, spaceShip.getPositionY());
    Assert.assertTrue(spaceShip.getPositionX() == 540); // reached maximum right movement, the ship position doesn't change
    spaceShip.decreaseLives();
    spaceShip.decreaseLives();
    Assert.assertTrue(spaceShip.getLives() == 1);
    spaceShip.increaseLives();
    Assert.assertTrue(spaceShip.getLives() == 2);
    spaceShip.decreaseLives();
   
    //Testing collisions AlienShot -> Ship and Shot -> Alien
    model.getList().add(new AlienShot(spaceShip.getPositionX(), spaceShip.getPositionY()));
    ShipShot shot = new ShipShot(10, 50);
    model.getList().add(shot);
    shot.collideWith(model.getList());
    spaceShip.collideWith(model.getList());
    Assert.assertTrue(spaceShip.isDead());
    Assert.assertTrue(shot.isDead());
    int individualsKilled = 0;
    for (Individual ind : model.getList()) {
      if (ind.isDead()) {
        individualsKilled++;
      }
    }
    Assert.assertTrue(individualsKilled == 4); //Individuals dead: alienShot, shot, ship and an alien
    for (Individual ind : model.getList()) {
      if (ind.getPositionX() == 10 && ind.getPositionY() == 50) {
        Assert.assertTrue(ind.isDead());
      }
    }
   
    //Testing collisions Shot -> Wall and Alien shot -> Wall
    ShipShot shipShot = new ShipShot(40, 450);
    Wall wallKicked = null;
    model.getList().add(shipShot);
    for (Wall wall : model.getWalls().getList()) {
      wall.collideWith(model.getList());
    }
    for (Wall wall : model.getWalls().getList()) {
      if (wall.getPositionX() == 40 && wall.getPositionY() == 450) {
        wallKicked = wall;
      }
    }
    Assert.assertTrue(wallKicked.getLives() == 1);
    model.getList().remove(shipShot);
    AlienShot alienShot = new AlienShot(35, 450);
    model.getList().add(alienShot);
    wallKicked.collideWith(model.getList());
    Assert.assertTrue(wallKicked.getLives() == 0);
    Assert.assertTrue(wallKicked.isDead());
   
    //Testing collisions Shot -> Alien Shot
    model.getList().add(shipShot);
    alienShot.collideWith(model.getList());
    Assert.assertTrue(shipShot.isDead());
    Assert.assertTrue(alienShot.isDead());
   
    //Testing score when aliens die
    model.resetStatistics();
    Alien alien1 = new Alien(new AlienType(1), 50, 30);
    Alien alien2 = new Alien(new AlienType(2), 80, 30);
    Alien alien3 = new Alien(new AlienType(3), 100, 30);
    model.getList().add(alien1);
    model.getList().add(alien2);
    model.getList().add(alien3);
    model.removeIndividual(alien1);
    Assert.assertTrue(model.getStatistics().getScore() == 10);
    model.removeIndividual(alien2);
    Assert.assertTrue(model.getStatistics().getScore() == 30);
    model.removeIndividual(alien3);
    Assert.assertTrue(model.getStatistics().getScore() == 60)
   
  }
View Full Code Here


    }
  }
 
  @Test
  public void testAddIndividuals() {
    IModel model = new Model();
    model.initIndividuals();
    for (int i = 0; i < 10; i++) {
      ShipShot shot = model.addShot();
      Assert.assertTrue(model.getList().contains(shot));
    }
    Assert.assertEquals(model.getList().size(), 75);
    AlienMotherShip motherShip = model.addMotherShip();
    Assert.assertEquals(model.getList().size(), 76);
    for (int i = 0; i < 5; i++) {
      AlienShot alienShot = model.addAlienShot(new AlienShot(10, i));
      Assert.assertTrue(model.getList().contains(alienShot));
    }
    Assert.assertEquals(model.getList().size(), 81);
    Assert.assertTrue(model.getList().contains(motherShip));
  }
View Full Code Here

    Assert.assertTrue(model.getList().contains(motherShip));
  }
 
  @Test
  public void testRemoveIndividuals() {
    IModel model = new Model();
    model.initIndividuals();
    Ship spaceShip = model.getShip();
    ShipShot shot = model.addShot();
    model.removeIndividual(shot);
    model.removeIndividual(spaceShip);
    Assert.assertTrue(model.getList().size() == 64);
    for (Individual ind : model.getList()) {
      model.removeIndividual(ind);
    }
    Assert.assertTrue(model.getList().isEmpty());
    Assert.assertTrue(model.getStatistics().getScore() == 990)
  }
View Full Code Here

    Assert.assertTrue(model.getStatistics().getScore() == 990)
  }
 
  @Test
  public void testChangesInModel() {
    IModel model = new Model();
    model.initIndividuals();
    model.initAchievements();
    Ship spaceShip = model.getShip();
    Statistics statistics = model.getStatistics();
   
    //Testing adding and resetting statistics
    statistics.setLevelsCompleted(3);
    statistics.setMotherShipsKilled(5);
    statistics.setScore(300);
    statistics.setAliensKilled(100);
    Assert.assertTrue(statistics.getLevelsCompleted() == 3);
    Assert.assertTrue(statistics.getMotherShipsKilled() == 5);
    Assert.assertTrue(statistics.getScore() == 300);
    Assert.assertTrue(statistics.getAliensKilled() == 100);
    statistics.resetStatistics();
    for (Field f : statistics.getClass().getDeclaredFields()) {
      f.setAccessible(true);
      try {
        Assert.assertTrue(f.getInt(statistics) == 0);
      } catch (IllegalArgumentException e) {
        System.err.println(GameStrings.ILLEGAL_ARG_ERROR);
      } catch (IllegalAccessException e) {
        System.err.println(GameStrings.ILLEGAL_ACC_ERROR);
      }
    }
   
    //Testing movement and some methods of ship
    spaceShip.moveIndividual(spaceShip.getPositionX() + 10, spaceShip.getPositionY());
    Assert.assertTrue(spaceShip.getPositionX() == 280);
    spaceShip.moveIndividual(spaceShip.getPositionX() - 260, spaceShip.getPositionY());
    Assert.assertTrue(model.getShip().getPositionX() == 20);
    spaceShip.moveIndividual(spaceShip.getPositionX() - 10, spaceShip.getPositionY());
    Assert.assertTrue(spaceShip.getPositionX() == 20); //reached minimum left movement, the ship position doesn't change
    spaceShip.moveIndividual(spaceShip.getPositionX() + 520, spaceShip.getPositionY());
    Assert.assertTrue(spaceShip.getPositionX() == 540);
    spaceShip.moveIndividual(spaceShip.getPositionX() + 10, spaceShip.getPositionY());
    Assert.assertTrue(spaceShip.getPositionX() == 540); // reached maximum right movement, the ship position doesn't change
    spaceShip.decreaseLives();
    spaceShip.decreaseLives();
    Assert.assertTrue(spaceShip.getLives() == 1);
    spaceShip.increaseLives();
    Assert.assertTrue(spaceShip.getLives() == 2);
    spaceShip.decreaseLives();
   
    //Testing collisions AlienShot -> Ship and Shot -> Alien
    model.getList().add(new AlienShot(spaceShip.getPositionX(), spaceShip.getPositionY()));
    ShipShot shot = new ShipShot(10, 50);
    model.getList().add(shot);
    shot.collideWith(model.getList());
    spaceShip.collideWith(model.getList());
    Assert.assertTrue(spaceShip.isDead());
    Assert.assertTrue(shot.isDead());
    int individualsKilled = 0;
    for (Individual ind : model.getList()) {
      if (ind.isDead()) {
        individualsKilled++;
      }
    }
    Assert.assertTrue(individualsKilled == 4); //Individuals dead: alienShot, shot, ship and an alien
    for (Individual ind : model.getList()) {
      if (ind.getPositionX() == 10 && ind.getPositionY() == 50) {
        Assert.assertTrue(ind.isDead());
      }
    }
   
    //Testing collisions Shot -> Wall and Alien shot -> Wall
    ShipShot shipShot = new ShipShot(40, 450);
    Wall wallKicked = null;
    model.getList().add(shipShot);
    for (Wall wall : model.getWalls().getList()) {
      wall.collideWith(model.getList());
    }
    for (Wall wall : model.getWalls().getList()) {
      if (wall.getPositionX() == 40 && wall.getPositionY() == 450) {
        wallKicked = wall;
      }
    }
    Assert.assertTrue(wallKicked.getLives() == 1);
    model.getList().remove(shipShot);
    AlienShot alienShot = new AlienShot(35, 450);
    model.getList().add(alienShot);
    wallKicked.collideWith(model.getList());
    Assert.assertTrue(wallKicked.getLives() == 0);
    Assert.assertTrue(wallKicked.isDead());
   
    //Testing collisions Shot -> Alien Shot
    model.getList().add(shipShot);
    alienShot.collideWith(model.getList());
    Assert.assertTrue(shipShot.isDead());
    Assert.assertTrue(alienShot.isDead());
   
    //Testing score when aliens die
    model.resetStatistics();
    Alien alien1 = new Alien(new AlienType(1), 50, 30);
    Alien alien2 = new Alien(new AlienType(2), 80, 30);
    Alien alien3 = new Alien(new AlienType(3), 100, 30);
    model.getList().add(alien1);
    model.getList().add(alien2);
    model.getList().add(alien3);
    model.removeIndividual(alien1);
    Assert.assertTrue(model.getStatistics().getScore() == 10);
    model.removeIndividual(alien2);
    Assert.assertTrue(model.getStatistics().getScore() == 30);
    model.removeIndividual(alien3);
    Assert.assertTrue(model.getStatistics().getScore() == 60)
   
  }
View Full Code Here

* @author Manuel Bottazzi
*/
public class Main {
 
  public static void main(String[] args) {
    Model model = new Model();
    MainMenuController c = new MainMenuController(model);
    MainFrame mainFrame = new MainFrame();
    c.setView(mainFrame);
    mainFrame.setVisible(true);
  }
View Full Code Here

public class TestModel {

  @Test
  public void testInit() {
    IModel model = new Model();
    String[] achievements = new String[]{GameStrings.ACH_100_ALIENS, GameStrings.ACH_SURVIVOR,
        GameStrings.ACH_3_LEVELS, GameStrings.ACH_5_MOTHERSHIPS};
    model.initIndividuals();
    model.initAchievements();
    Assert.assertEquals(model.getList().size(), 65);
    Assert.assertEquals(model.getAliens().getAlienList().size(), 55);
    Assert.assertEquals(model.getWalls().getList().size(), 9);
    Assert.assertEquals(model.getAchievementsName().length, 4);
    Assert.assertArrayEquals(model.getAchievementsName(), achievements);
    Assert.assertTrue(model.getList().contains(model.getShip()))
    for (Field f : model.getStatistics().getClass().getDeclaredFields()) {
      f.setAccessible(true);
      try {
        Assert.assertTrue(f.getInt(model.getStatistics()) == 0);
      } catch (IllegalArgumentException e) {
        System.err.println(GameStrings.ILLEGAL_ARG_ERROR);
      } catch (IllegalAccessException e) {
        System.err.println(GameStrings.ILLEGAL_ACC_ERROR);
      }
View Full Code Here

   * @param name - The player name.
   * @param score - The score value.
   */
  public void addScore(String name, Integer score) {
   
    Score s = new Score(name, score);
   
    this.scoreList = scoreManager.readList();
    this.scoreList.add(s)
    this.saveHighScore();
   
View Full Code Here

 
  @Test
  public void testRemoveIndividuals() {
    IModel model = new Model();
    model.initIndividuals();
    Ship spaceShip = model.getShip();
    ShipShot shot = model.addShot();
    model.removeIndividual(shot);
    model.removeIndividual(spaceShip);
    Assert.assertTrue(model.getList().size() == 64);
    for (Individual ind : model.getList()) {
View Full Code Here

  @Test
  public void testChangesInModel() {
    IModel model = new Model();
    model.initIndividuals();
    model.initAchievements();
    Ship spaceShip = model.getShip();
    Statistics statistics = model.getStatistics();
   
    //Testing adding and resetting statistics
    statistics.setLevelsCompleted(3);
    statistics.setMotherShipsKilled(5);
    statistics.setScore(300);
    statistics.setAliensKilled(100);
    Assert.assertTrue(statistics.getLevelsCompleted() == 3);
    Assert.assertTrue(statistics.getMotherShipsKilled() == 5);
    Assert.assertTrue(statistics.getScore() == 300);
    Assert.assertTrue(statistics.getAliensKilled() == 100);
    statistics.resetStatistics();
    for (Field f : statistics.getClass().getDeclaredFields()) {
      f.setAccessible(true);
      try {
        Assert.assertTrue(f.getInt(statistics) == 0);
      } catch (IllegalArgumentException e) {
        System.err.println(GameStrings.ILLEGAL_ARG_ERROR);
      } catch (IllegalAccessException e) {
        System.err.println(GameStrings.ILLEGAL_ACC_ERROR);
      }
    }
   
    //Testing movement and some methods of ship
    spaceShip.moveIndividual(spaceShip.getPositionX() + 10, spaceShip.getPositionY());
    Assert.assertTrue(spaceShip.getPositionX() == 280);
    spaceShip.moveIndividual(spaceShip.getPositionX() - 260, spaceShip.getPositionY());
    Assert.assertTrue(model.getShip().getPositionX() == 20);
    spaceShip.moveIndividual(spaceShip.getPositionX() - 10, spaceShip.getPositionY());
    Assert.assertTrue(spaceShip.getPositionX() == 20); //reached minimum left movement, the ship position doesn't change
    spaceShip.moveIndividual(spaceShip.getPositionX() + 520, spaceShip.getPositionY());
    Assert.assertTrue(spaceShip.getPositionX() == 540);
    spaceShip.moveIndividual(spaceShip.getPositionX() + 10, spaceShip.getPositionY());
    Assert.assertTrue(spaceShip.getPositionX() == 540); // reached maximum right movement, the ship position doesn't change
    spaceShip.decreaseLives();
    spaceShip.decreaseLives();
    Assert.assertTrue(spaceShip.getLives() == 1);
    spaceShip.increaseLives();
    Assert.assertTrue(spaceShip.getLives() == 2);
    spaceShip.decreaseLives();
   
    //Testing collisions AlienShot -> Ship and Shot -> Alien
    model.getList().add(new AlienShot(spaceShip.getPositionX(), spaceShip.getPositionY()));
    ShipShot shot = new ShipShot(10, 50);
    model.getList().add(shot);
    shot.collideWith(model.getList());
    spaceShip.collideWith(model.getList());
    Assert.assertTrue(spaceShip.isDead());
    Assert.assertTrue(shot.isDead());
    int individualsKilled = 0;
    for (Individual ind : model.getList()) {
      if (ind.isDead()) {
        individualsKilled++;
View Full Code Here

    int counterTimer = 0;
    int lives = model.getShip().getLives();
      double motherSpawnMoment = Math.random() * MOTHERSHIP_SPAWN_FREQ;
    AlienBlock alienBlock = model.getAliens();
    Ship spaceShip = model.getShip();
    List<Individual> individuals = model.getList();

    alienBlock.startMove();
    AudioPlayer.getAudioPlayer().playLoop(AudioPlayer.BACKGROUND_SOUND);

    while (!this.isOver) {
      counterTimer++;
      if (counterTimer % SHOT_RATIO == 0) {
        alienShot();
      }

      //Adds a mother ship to the game
      if (counterTimer >= motherSpawnMoment) {
          AlienMotherShip motherShip = model.addMotherShip();
        motherShip.startMove();
        motherSpawnMoment = MOTHERSHIP_BASE_FREQ + Math.random() * MOTHERSHIP_SPAWN_FREQ;
        counterTimer = 0;
      }

      /* Moves all the individuals
       * (except the aliens 'cause they already move).
       * Checks all the possible collisions of all individuals
       * Checks if some aliens are dead
       * Sets lives and score in the game panel */
      for (Individual ind : individuals) {
        if (!(ind instanceof Alien)) {
                    ind.moveIndividual(ind.getPositionX(), ind.getPositionY());
        }
        ind.collideWith(individuals);
        if (spaceShip.getLives() != lives) {
          this.obs.setShipLivesInPanel();
          lives--;
        }
        if (ind.isDead()) {
          this.model.removeIndividual(ind);
                    if (ind instanceof Alien && !(ind instanceof AlienMotherShip)) {
                      AudioPlayer.getAudioPlayer().playOnce(AudioPlayer.ALIEN_KILLED_SOUND);
            this.aliensKilled++;
          }
          if (ind instanceof AlienMotherShip) {
                        if (!((AlienMotherShip) ind).isOutOfLimit()) {
                this.motherShipsKilled++;
            }
          }
                    this.obs.setScoreInPanel(this.model.getStatistics().getScore());
        }
      }

      //Speed up the last alien survivor
      if (this.aliensKilled >= KILLED_ALIENS_BEFORE_SPEEDUP) {
        alienBlock.setSleepTime(ALIENS_SLEEP_TIME);
      }

      //Check game won
      if (alienBlock.checkGameWon()) {
        this.levelsCompleted++;
        this.obs.gameWon();
        this.isOver = true;
      }

      //Check game over
      if (alienBlock.checkGameOver() || spaceShip.isDead()) {
        //Stops the alien block's thread
        if (!alienBlock.checkGameOver()) {
          alienBlock.setGameOver();
        }
        spaceShip.setLives(0);
        this.obs.gameOver();
        this.isOver = true;
      }
      this.obs.updatePanel();
      try {
View Full Code Here

TOP

Related Classes of oop13.space.views.GamePanel

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.