Exemplo n.º 1
0
  private void playerSetup() {

    players = new ArrayList(twoPlayerRB.isSelected() ? 2 : 1);
    if (mouseRB.isSelected()) {
      MouseControlledPlayer p1 =
          new MouseControlledPlayer(width / 2, height / 2, 3, true, this, 3, 1, width);
      addMouseMotionListener(p1);
      addMouseListener(p1);
      players.add(p1);
    } else {
      KeyboardControlledPlayer p1 =
          new KeyboardControlledPlayer(
              width / 2,
              height / 2,
              3,
              true,
              this,
              3,
              KeyEvent.VK_W,
              KeyEvent.VK_A,
              KeyEvent.VK_S,
              KeyEvent.VK_D,
              keyboardSpeedS1.getValue(),
              KeyEvent.VK_SPACE,
              KeyEvent.VK_F,
              1,
              width);
      KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(p1);
      players.add(p1);
    }

    if (twoPlayerRB.isSelected()) {
      if (mouseRB.isSelected()) {
        KeyboardControlledPlayer p2 =
            new KeyboardControlledPlayer(
                width / 2,
                height / 2,
                3,
                true,
                this,
                3,
                KeyEvent.VK_W,
                KeyEvent.VK_A,
                KeyEvent.VK_S,
                KeyEvent.VK_D,
                keyboardSpeedS2.getValue(),
                KeyEvent.VK_SPACE,
                KeyEvent.VK_F,
                2,
                width);
        KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(p2);
        players.add(p2);
      } else {
        KeyboardControlledPlayer p2 =
            new KeyboardControlledPlayer(
                width / 2,
                height / 2,
                3,
                true,
                this,
                3,
                KeyEvent.VK_NUMPAD8,
                KeyEvent.VK_NUMPAD4,
                KeyEvent.VK_NUMPAD5,
                KeyEvent.VK_NUMPAD6,
                keyboardSpeedS2.getValue(),
                KeyEvent.VK_NUMPAD0,
                KeyEvent.VK_NUMPAD1,
                2,
                width);
        KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(p2);
        players.add(p2);
      }
    }
  }
Exemplo n.º 2
0
  private void drawLayout(Graphics g) {
    g.setColor(Color.blue);
    g.drawRect(10, 10, width - 20, height - 20);

    g.setColor(Color.red.brighter());
    Iterator f = players.iterator();
    int lifeDisplayPosition = 0;
    while (f.hasNext()) {
      Player h = (Player) f.next();
      if (h.isActive()) {
        lifeDisplayPosition++;
        String lifeInformation = "Player " + lifeDisplayPosition + ": ";
        for (int i = 0; i < h.getLives(); i++) {
          lifeInformation += "\u2606";
        }
        g.drawString(lifeInformation, 20, lifeDisplayPosition * 40);
      }
    }

    borders[2] = width - 20;
    borders[3] = height - 20;

    Iterator i = players.iterator();
    Player p;
    while (i.hasNext()) {
      p = (Player) i.next();
      if (p.isActive()
          && !countdownF
          && (p.getX() < borders[0]
              || p.getY() < borders[1]
              || p.getX() > borders[2]
              || p.getY() > borders[3])
          && (!p.getImmunity())) {
        p.decLives(width / 2, height / 2);
        p.setImmunity(true);
      }
      if (p.getLives() <= 0) {
        if (p instanceof MouseControlledPlayer) {
          MouseControlledPlayer m = (MouseControlledPlayer) p;
          removeMouseListener(m);
          removeMouseMotionListener(m);
        } else if (p instanceof KeyboardControlledPlayer) {
          KeyboardFocusManager.getCurrentKeyboardFocusManager()
              .removeKeyEventDispatcher((KeyboardControlledPlayer) p);
        }
        i.remove();
        for (int h = 0; h < 4; h++) {
          if (deathLocation[h] == -1) {
            deathLocation[h] = p.getX();
            deathLocation[h + 1] = p.getY();
            break;
          }
        }
      }
    }

    if ((deathLocation[0] != -1) && (deathLocation[1] != -1)) {
      g.drawLine(
          deathLocation[0] - 15,
          deathLocation[1] - 15,
          deathLocation[0] + 15,
          deathLocation[1] + 15);
      g.drawLine(
          deathLocation[0] + 15,
          deathLocation[1] - 15,
          deathLocation[0] - 15,
          deathLocation[1] + 15);
    }
    if ((deathLocation[2] != -1) && (deathLocation[3] != -1)) {
      g.drawLine(
          deathLocation[2] - 15,
          deathLocation[3] - 15,
          deathLocation[2] + 15,
          deathLocation[3] + 15);
      g.drawLine(
          deathLocation[2] + 15,
          deathLocation[3] - 15,
          deathLocation[2] - 15,
          deathLocation[3] + 15);
    }

    if (!onePlayerAlive) {
      Font old = g.getFont();
      g.setFont(new Font("monospaced", Font.BOLD, 20));
      g.setColor(Color.red.darker());
      g.drawString("GAME OVER", width / 2 - 40, height / 2);
      g.setFont(old);
      g.setColor(Color.WHITE);
      g.drawString("Score", width - 60, 25);
      g.drawString(String.valueOf(score), width - 60, 40);
    }
  }