📄 boxesgame4.java
字号:
package net.java.gamebase.sample.boxes.v4;
import java.awt.Component;
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 BoxesGame4
extends GameBaseUI {
public Queue2[] lanes;
private GameActionEnter2 enterAction;
private GameControlKeySetOneAction keySet;
public static final int HORIZONTAL_SPACE = 8;
public static final int VERTICAL_SPACE = 16;
public static final int QUEUE_LIMIT = 14;
public static final int BASE_SIZE = 32;
public static void main(String[] args) {
BoxesGame4 g = new BoxesGame4(640, 480);
g.show();
}
public BoxesGame4(int width, int height) {
super(width, height);
lanes = new Queue2[9];
for (int i = 0; i < lanes.length; i++) {
lanes[i] = new Queue2();
}
setStep(700);
enterAction = new GameActionEnter2(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++) {
Queue2 q = lanes[i];
int line = (i * (BASE_SIZE + VERTICAL_SPACE)) + VERTICAL_SPACE;
/**
* note that the first one should be at the end of the line
*/
for (int j = 0; j < q.size(); j++) {
FallingBox2 box = q.getBox(j);
int step = box.getWidth() + HORIZONTAL_SPACE;
int column = (QUEUE_LIMIT - j + 1) * step;
box.setX(column);
box.setY(line);
}
}
}
public void createNewBox() {
FallingBox2 box = new FallingBox2(HORIZONTAL_SPACE, VERTICAL_SPACE,
this);
box.setHorizontalSpace(VERTICAL_SPACE);
box.setKeySet(keySet);
add(box);
}
public synchronized void addToLane(FallingBox2 box) {
box.stopFalling();
box.setKeySet(GameControlKeySetDummy.getInstance());
int position = box.getPosition();
if (lanes[position].size() >= QUEUE_LIMIT) {
JOptionPane.showMessageDialog(this, "Game Over");
}
else {
lanes[position].addBox(box);
drawLanes();
checkDuplicates();
createNewBox();
}
}
public boolean isLastLane(FallingBox2 box) {
if (box.getPosition() >= lanes.length - 1) {
addToLane(box);
return true;
}
return false;
}
public void checkDuplicates() {
for (int i = 0; i < lanes.length; i++) {
Queue2 q = lanes[i];
int j = 0;
while (j < q.size() - 2) {
if (q.getBox(j).equals(q.getBox(j + 1))
&& q.getBox(j).equals(q.getBox(j + 2))) {
Component box = (Component) q.remove(j);
remove(box);
box = (Component) q.remove(j);
remove(box);
box = (Component) q.remove(j);
remove(box);
repaint();
}
j++;
}
}
}
private void showQueue(Queue2 q) {
for (int i = 0; i < q.size(); i++) {
System.out.print(q.getBox(i).getColor() + " ");
}
System.out.print("\n");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -