Ejemplo n.º 1
0
 public WorldView(
     Asteroids<World, Ship, Asteroid, Bullet> game, World world, Ship player1, Ship player2) {
   this.game = game;
   this.facade = game.getFacade();
   this.world = world;
   this.player1 = player1;
   this.player2 = player2;
   this.timer = new Timer(TIMER_DELAY, this);
   setBackground(Color.BLACK);
   ClassLoader loader = WorldView.class.getClassLoader();
   background =
       Toolkit.getDefaultToolkit()
           .getImage(loader.getResource("asteroids/resources/game-background.jpg"));
   background =
       background.getScaledInstance(game.getWidth(), game.getHeight(), Image.SCALE_DEFAULT);
   if (player1 != null) {
     int size = (int) (2 * facade.getShipRadius(player1));
     Image image =
         Toolkit.getDefaultToolkit()
             .getImage(loader.getResource("asteroids/resources/deathstar.png"))
             .getScaledInstance(size, size, Image.SCALE_DEFAULT);
     visualizations.put(player1, new ShipVisualization(Color.RED, player1, image));
   }
   if (player2 != null) {
     int size = (int) (2 * facade.getShipRadius(player2));
     Image image =
         Toolkit.getDefaultToolkit()
             .getImage(loader.getResource("asteroids/resources/sphere.png"))
             .getScaledInstance(size, size, Image.SCALE_DEFAULT);
     visualizations.put(player2, new ShipVisualization(Color.GREEN, player2, image));
   }
   addKeyListener(this);
 }
Ejemplo n.º 2
0
 @Override
 public void keyPressed(KeyEvent e) {
   switch (e.getKeyCode()) {
     case KeyEvent.VK_ESCAPE:
       timer.stop();
       game.showMenu();
     case THRUSTER_P1:
       facade.setThrusterActive(player1, true);
       break;
     case LEFT_P1:
       player1_angle = Math.PI / 20;
       break;
     case RIGHT_P1:
       player1_angle = -Math.PI / 20;
       break;
     case FIRE_P1:
       player1_fire = true;
       break;
     case THRUSTER_P2:
       if (player2 != null) facade.setThrusterActive(player2, true);
       break;
     case LEFT_P2:
       if (player2 != null) player2_angle = Math.PI / 20;
       break;
     case RIGHT_P2:
       if (player2 != null) player2_angle = -Math.PI / 20;
       break;
     case FIRE_P2:
       if (player2 != null) player2_fire = true;
       break;
   }
 }
Ejemplo n.º 3
0
 @Override
 public void objectCollision(Object entity1, Object entity2, double x, double y) {
   if ((facade.isBullets(entity1) && !facade.isBullets(entity2))
       || (facade.isBullets(entity2) && !facade.isBullets(entity1))) {
     game.getSound().play("explosion");
     explosions.add(new Explosion(x, facade.getWorldHeight(world) - y));
   }
 }
Ejemplo n.º 4
0
 @Override
 public void actionPerformed(ActionEvent e) {
   long now = System.currentTimeMillis();
   long millisSinceLastEvolve = now - timeAfterLastEvolve;
   timeAfterLastEvolve = now;
   if (player1_angle != 0) {
     facade.turn(player1, player1_angle);
   }
   if (player2 != null && player2_angle != 0) {
     facade.turn(player2, player2_angle);
   }
   if (player1_fire && facade.getShips(world).contains(player1)) {
     player1_fire = false;
     facade.fireBullet(player1);
     game.getSound().play("torpedo");
   }
   if (player2 != null && player2_fire && facade.getShips(world).contains(player2)) {
     player2_fire = false;
     facade.fireBullet(player2);
     game.getSound().play("torpedo");
   }
   facade.evolve(world, millisSinceLastEvolve / 1000., this);
   Iterator<Explosion> iter = explosions.iterator();
   while (iter.hasNext()) {
     boolean done = iter.next().evolve(millisSinceLastEvolve / 1000.);
     if (done) iter.remove();
   }
   boolean player1Alive = facade.getShips(world).contains(player1);
   if (player1Alive) {
     if (player2 != null && !facade.getShips(world).contains(player2)) {
       timer.stop();
       msg = "Player 1 wins!";
     } else if (facade.getAsteroids(world).isEmpty() && player2 == null) {
       timer.stop();
       msg = "You win!";
     }
   } else {
     timer.stop();
     if (player2 == null || !facade.getShips(world).contains(player2)) {
       msg = "Asteroids win!";
     } else {
       msg = "Player 2 wins!";
     }
   }
   repaint();
 }
Ejemplo n.º 5
0
 public static void main(String[] args) {
   if (GraphicsEnvironment.isHeadless()) {
     System.out.println("no screen detected");
     return;
   } else {
     // <begin>
     IFacade facade = new asteroids.Facade();
     // <end>
     GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
     GraphicsDevice device = env.getDefaultScreenDevice();
     Asteroids asteroids;
     if (device.isFullScreenSupported()) {
       asteroids = new Asteroids(facade, true);
       device.setFullScreenWindow(asteroids);
     } else {
       asteroids = new Asteroids(facade, false);
     }
     asteroids.start();
   }
 }
Ejemplo n.º 6
0
 public void startGame() {
   game.getSound().loop("game-theme");
   timeAfterLastEvolve = System.currentTimeMillis();
   timer.start();
 }