Ejemplo n.º 1
0
  @Override
  public void paint(Graphics g) {
    // Create an accelerated image to use for double buffering to make
    // screen updates smooth
    GraphicsConfiguration gc =
        GraphicsEnvironment.getLocalGraphicsEnvironment()
            .getDefaultScreenDevice()
            .getDefaultConfiguration();
    Image image = gc.createCompatibleImage(getWidth(), getWidth(), Transparency.BITMASK);
    Graphics2D g2d = (Graphics2D) image.getGraphics();

    // Call clear with g2d local variable to clear the current contents of window
    clear(g2d);

    // Draw bonus saucer
    bonusSaucer.draw(g2d);

    if (bonusSaucer.killed && resetBonusTime == 0) {
      bonusSaucerKillTimer.reset();
      bonusSaucerInitialMoveTimer =
          new Timer((long) (randomBonusSaucerTime.nextInt(10) + 6) * 1000);
      oldBonusSaucerXPosition = bonusSaucer.xPosition;
      oldBonusSaucerYPosition = bonusSaucer.yPosition;
      resetBonusTime = resetBonusTime + 1;
    }

    if (!bonusSaucerKillTimer.expired() && resetBonusTime == 1) {
      bonusScore = bonusSaucer.determineBonusScore();
      bonusSaucer =
          new BonusSaucer(INITIAL_SAUCER_X_BLOCK_POSITION, INITIAL_SAUCER_Y_BLOCK_POSITION, this);
      bonusScoreDisplay.set((long) 1000);
      resetBonusTime = resetBonusTime - 1;
    }

    if (!bonusScoreDisplay.expired()) {
      g2d.setFont(scoreFont);
      g2d.setColor(Color.white);
      g2d.drawString(
          String.valueOf(bonusScore), oldBonusSaucerXPosition, oldBonusSaucerYPosition + 20);
    }

    if (tank.killed) {
      bonusSaucer =
          new BonusSaucer(INITIAL_SAUCER_X_BLOCK_POSITION, INITIAL_SAUCER_Y_BLOCK_POSITION, this);
    }

    // Draw aliens
    // Define a local variable called row of type Integer and initailize it
    // to 0, this is used to access the aliens by row
    Integer row = 0;
    Integer yBlockPosition = INITIAL_ALIEN_Y_BLOCK_POSITION;
    // Loop while row is not eual to the NBR_OF_ALIEN_ROWS
    while (row != aliens.length) { // Number of rows

      // Define a local variable called column of type Integer and
      // initailize it to 0, this is used to access the aliens by column
      Integer column = 0;
      Integer xBlockPosition = 0;
      // Loop while column is not eual to the NBR_OF_ALIEN_COLUMNS
      while (column != aliens[row].length) { // Number of columns
        // Call aliens[row][column].draw with g2d local variable
        // to draw alien on screen
        aliens[row][column].draw(g2d);

        if (aliens[row][column].resetKilled) {
          aliens[row][column].resetKilled = false;
          nbrOfAliensKilled++;
        }
        if (nbrOfAliensKilled == 55) {
          if (row == 0) {
            // Initailize aliens[row][column] to a new Squid alien using
            // xBlockPosition, yBlockPosition as the block position
            // parameters and this are the gamePanel parameter
            aliens[row][column] = new Squid(xBlockPosition, yBlockPosition, this);
          } // End if

          // Else If row is equal to 1 or 2 then
          else if ((row == 1) || (row == 2)) {
            // Initailize aliens[row][column] to a new Grasshopper alien
            // using xBlockPosition, yBlockPosition as the block position
            // parameters and this are the gamePanel parameter
            aliens[row][column] = new Grasshopper(xBlockPosition, yBlockPosition, this);
          } // End else if

          // Else this must be rows 3 or 4
          else {
            // Initailize aliens[row][column] to a new Skull alien using
            // xBlockPosition, yBlockPosition as the block position
            // parameters and this are the gamePanel parameter
            aliens[row][column] = new Skull(xBlockPosition, yBlockPosition, this);
          } // End else
        }
        // Increment the column local variable by 1
        column = column + 1;
        xBlockPosition = xBlockPosition + Alien.BLOCK_WIDTH + COLUMN_SEPERATION_BLOCK;
      } // End while loop

      // Increment the row local variable by 1
      row = row + 1;
      yBlockPosition = yBlockPosition + Alien.BLOCK_HEIGHT + ROW_SEPERATION_BLOCK;
    } // End while loop

    // Draw shields
    // TODO for shield starting at 0, while less than NBR_OF_SHEILDS,
    // increment shild by 1
    for (Integer shield = 0; shield < NBR_OF_SHEILDS; shield++) {
      // TODO for shield starting at 0, while less than NBR_OF_SHEILD_PARTS,
      // increment shild by 1
      for (Integer shieldPart = 0; shieldPart < NBR_OF_SHEILD_PARTS; shieldPart++) {
        // TODO Call shields[shield][shieldPart].draw with g2d as the parameter
        if (shields[shield][shieldPart].explosionState
            != ShieldPart.ExplosionState.EXPLOSION_DONE) {
          shields[shield][shieldPart].draw(g2d);
        }
      } // End for loop
    } // End for loop

    // Draw tank shots
    for (int i = 0; i < tankShots.size(); i++) {
      tankShots.get(i).draw(g2d);
    } // for

    // Draw aliens shots
    for (int i = 0; i < alienShots.size(); i++) {
      alienShots.get(i).draw(g2d);
    } // for

    bonusSaucer.draw(g2d);
    // Draw tank
    // Call tank.draw with g2d local variable to draw tank on screen
    tank.draw(g2d);

    // Driaw line at bottom of screen
    g2d.setColor(Color.white);
    g2d.fillRect(0, 560, GamePanel.FRAME_WIDTH, 3);

    // Draw lives area
    // Call livesArea.draw with g2d local variable to draw tank on screen
    livesArea.draw(g2d);

    // Draw score area
    // Call scoreArea.draw with g2d local variable to draw score area
    // on the screen
    scoreArea.draw(g2d);

    if (displayGameover == true) {
      g2d.setFont(gameOverFont);
      g2d.setColor(Color.white);
      g2d.drawString("Game Over", 260, 290);
    } // End if

    // Draw over entire screen
    g.drawImage(image, 0, 0, null);
  } // paint
Ejemplo n.º 2
0
 /**
  * 建構子。
  *
  * @param rank 難易度
  * @param j JApplet
  */
 public WarField(Rank rank, JApplet j) {
   super(0, 0, 800, 600);
   this.playerTank1 = new PlayerTank(40, 40, j); // 坦克實體
   allGameObjects.add(this.playerTank1); // 放入儲存戰場物件的Array,用於碰撞判斷
   int[][] data = {
     {5, 500},
     {100, 0},
     {160, 240},
     {320, 400},
     {400, 160},
     {400, 520},
     {640, 240},
     {560, 240},
     {280, 160},
     {720, 160},
     {160, 360}
   };
   // [0][0]=敵人數量,[0][1]=敵方坦克AI執行動作所間隔的時間(毫秒)
   // [1][0]=敵人子彈攻擊力,其餘為敵方坦克的座標
   switch (rank) { // 依據難易度決定WarField的資料
     case EASY:
       break;
     case MEDIUM:
       data[0][0] = 7;
       data[0][1] = 400;
       data[1][0] = 150;
       break;
     case HARD:
       data[0][0] = 9;
       data[0][1] = 340;
       data[1][0] = 200;
   }
   for (int i = 0; i < data[0][0]; i++) { // 依據資料產生敵人,並放入Array中
     EnemyTank e =
         new EnemyTank(data[i + 2][0], data[i + 2][1], data[0][1], data[1][0], j, playerTank1);
     enemyTank.add(e);
     allGameObjects.add(this.enemyTank.get(i));
   }
   for (int i = 0; i < 15; i++) { // 戰場石頭(不可破壞)
     if (i == 0 || i == 14) {
       for (int ii = 0; ii < 20; ii++) {
         Stone s = new Stone(ii * 40, i * 40, j);
         nonmoveArrayList.add(s);
         allGameObjects.add(s);
       }
     } else {
       Stone s = new Stone(0, i * 40, j);
       nonmoveArrayList.add(s);
       allGameObjects.add(s);
       s = new Stone(760, i * 40, j);
       nonmoveArrayList.add(s);
       allGameObjects.add(s);
     }
   }
   Stone[] s =
       new Stone[] {
         new Stone(80, 80, j),
         new Stone(120, 80, j),
         new Stone(120, 80, j),
         new Stone(160, 80, j),
         new Stone(160, 120, j),
         new Stone(320, 120, j),
         new Stone(360, 120, j),
         new Stone(400, 120, j),
         new Stone(440, 120, j),
         new Stone(480, 120, j),
         new Stone(600, 120, j),
         new Stone(600, 160, j),
         new Stone(120, 200, j),
         new Stone(400, 200, j),
         new Stone(600, 200, j),
         new Stone(120, 240, j),
         new Stone(400, 240, j),
         new Stone(600, 240, j),
         new Stone(120, 280, j),
         new Stone(160, 280, j),
         new Stone(200, 280, j),
         new Stone(240, 280, j),
         new Stone(400, 280, j),
         new Stone(600, 280, j),
         new Stone(640, 280, j),
         new Stone(120, 320, j),
         new Stone(400, 320, j),
         new Stone(120, 440, j),
         new Stone(200, 440, j),
         new Stone(240, 440, j),
         new Stone(280, 440, j),
         new Stone(560, 440, j),
         new Stone(600, 440, j),
         new Stone(640, 440, j),
         new Stone(120, 280, j),
         new Stone(160, 280, j),
         new Stone(200, 280, j),
         new Stone(120, 240, j),
         new Stone(400, 240, j),
         new Stone(600, 440, j),
         new Stone(120, 480, j),
         new Stone(440, 520, j)
       };
   for (int i = 0; i < s.length; i++) { // 將allGameObjects的內容設定完
     nonmoveArrayList.add(s[i]);
     allGameObjects.add(s[i]);
   }
   playerTank1.setAllOtherGameObjects(allGameObjects); // 將allGameObjects的內容給予各物件
   for (int i = 0; i < this.enemyTank.size(); i++) {
     enemyTank.get(i).setAllOtherGameObjects(allGameObjects);
   }
 }