⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 boxesgame3.java

📁 游戏基础
💻 JAVA
字号:
package net.java.gamebase.sample.boxes.v3;

import java.awt.event.KeyEvent;
import java.util.Iterator;
import java.util.Random;

import javax.swing.JOptionPane;

import net.java.gamebase.core.GameBaseUI;
import net.java.gamebase.core.GameControlKeySetDummy;
import net.java.gamebase.core.GameControlKeySetOneAction;
import net.java.gamebase.sample.boxes.v2.Box;

/**
 * Create a timer that moves the GameCharMovable with the new KeySet that only
 * have the enter handling
 *
 * @author hlima
 *
 */
public class BoxesGame3
    extends GameBaseUI {

  public Queue[] lanes;

  private GameActionEnter enterAction;

  private GameControlKeySetOneAction keySet;

  public static final int VERTICAL_SPACE = 8;

  public static final int HORIZONTAL_SPACE = 16;

  public static final int QUEUE_LIMIT = 14;
  public static final int BASE_SIZE = 32;

  public static void main(String[] args) {
    BoxesGame3 g = new BoxesGame3(640, 480);
    g.show();
  }

  public BoxesGame3(int width, int height) {
    super(width, height);

    lanes = new Queue[10];
    for (int i = 0; i < lanes.length; i++) {
      lanes[i] = new Queue();
    }

    enterAction = new GameActionEnter(lanes, this);
    keySet = new GameControlKeySetOneAction(KeyEvent.VK_ENTER, enterAction);

    drawLanes();
    createNewBox();

  }

  /**
   * space of 16 before each line of boxes
   *
   */
  public void drawLanes() {
    for (int i = 0; i < lanes.length; i++) {
      Queue q = lanes[i];
      int line = (i * (BASE_SIZE + HORIZONTAL_SPACE)) + HORIZONTAL_SPACE;
      /**
       * note that the fist one should be at the end of the line
       */
      for (int j = 0; j < q.size(); j++) {
        FallingBox box = q.getBox(j);
        int column = ( (QUEUE_LIMIT - j) * (box.getWidth() + VERTICAL_SPACE));
        box.setX(column);
        box.setY(line);
      }

    }

  }

  public void createNewBox() {
    Random r = new Random();
    int color = r.nextInt(Box.colors.length - 1);
    FallingBox box = new FallingBox(color, VERTICAL_SPACE,
                                    HORIZONTAL_SPACE, this);
    box.setHorizontalSpace(HORIZONTAL_SPACE);

    box.setKeySet(keySet);
    add(box);
  }

  public void addToLane(int position, FallingBox box) {
    box.stopFalling();
    box.setKeySet(GameControlKeySetDummy.getInstance());

    if (lanes[position].size() >= QUEUE_LIMIT) {
      JOptionPane.showMessageDialog(this, "Game Over");
    }
    else {
      lanes[position].addBox(box);
      drawLanes();
      checkDuplicates();
      createNewBox();
    }
  }

  public boolean isLastLane(FallingBox box) {
    if (box.getPosition() >= lanes.length - 1) {
      addToLane(lanes.length - 1, box);
      return true;
    }
    return false;
  }

  public void checkDuplicates() {
    // implement this !!
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -