📄 boxworld.java
字号:
// Image img=imgVec[BACK_ST];
// if(img==null){
// img=createImage(ISIZE,ISIZE);
// imgVec[BACK_ST]=img;
// Graphics ig=img.getGraphics();
g.setColor(Color.blue);
g.fillRect(j * CELL_SIZE, i * CELL_SIZE, CELL_SIZE, CELL_SIZE);
// }
// g.drawImage(img,j * CELL_SIZE, i * CELL_SIZE,this);
}
private void paintGoal(Graphics g, int j, int i) {
// Image img=imgVec[GOAL_ST];
// if(img==null){
// img=createImage(ISIZE,ISIZE);
// imgVec[GOAL_ST]=img;
// Graphics ig=img.getGraphics();
g.setColor(Color.green);
g.fillOval(j * CELL_SIZE + CELL_SIZE / 4,
i * CELL_SIZE + CELL_SIZE / 4, CELL_SIZE / 2, CELL_SIZE / 2);
// }
// g.drawImage(img,j * CELL_SIZE, i * CELL_SIZE,this);
}
private void paintBox(Graphics g, int j, int i, Color c) {
g.setColor(c);
g.fillRect(j * CELL_SIZE, i * CELL_SIZE, CELL_SIZE, CELL_SIZE);
g.setColor(Color.lightGray);
g.drawRect(j * CELL_SIZE + 1, i * CELL_SIZE + 1, CELL_SIZE - 2,
CELL_SIZE - 2);
g.drawLine(j * CELL_SIZE + 1, i * CELL_SIZE + 1, (j + 1) * CELL_SIZE
- 1, (i + 1) * CELL_SIZE - 1);
g.drawLine(j * CELL_SIZE + 1, (i + 1) * CELL_SIZE - 1, (j + 1)
* CELL_SIZE - 1, i * CELL_SIZE + 1);
}
private void paintBox(Graphics g, int j, int i) {
// Image img=imgVec[BOX_ST];
// if(img==null){
// img=createImage(ISIZE,ISIZE);
// imgVec[BOX_ST]=img;
// Graphics ig=img.getGraphics();
paintBox(g, j, i, Color.yellow);
// }
// g.drawImage(img,j * CELL_SIZE, i * CELL_SIZE,this);
}
private void paintFit(Graphics g, int j, int i) {
paintBox(g, j, i, Color.red);
}
private void paintMan(Graphics g, int j, int i) {
g.setColor(Color.cyan);
//head
g.fillOval(j * CELL_SIZE + CELL_SIZE / 2 - CELL_SIZE / 8, i * CELL_SIZE
+ CELL_SIZE / 16, CELL_SIZE / 4, CELL_SIZE / 4);
//g.drawLine(j * CELL_SIZE + CELL_SIZE / 2, i * CELL_SIZE + 2, j *
// CELL_SIZE + CELL_SIZE / 2, i * CELL_SIZE + 13);
//body
g.fillRect(j * CELL_SIZE + CELL_SIZE / 2 - CELL_SIZE / 16, i
* CELL_SIZE + CELL_SIZE / 4, CELL_SIZE / 8, CELL_SIZE / 3
+ CELL_SIZE / 12);
//g.drawLine(j * CELL_SIZE + CELL_SIZE / 2 + 1, i * CELL_SIZE + 8, j *
// CELL_SIZE + CELL_SIZE / 2 + 1, i * CELL_SIZE + 13);
//arms
g.fillRect(j * CELL_SIZE + CELL_SIZE / 8, i * CELL_SIZE + CELL_SIZE * 5
/ 16, CELL_SIZE * 3 / 4, CELL_SIZE / 16);
//legs
g.drawLine(j * CELL_SIZE + CELL_SIZE / 2, i * CELL_SIZE + CELL_SIZE * 7
/ 12, j * CELL_SIZE + 4, (i + 1) * CELL_SIZE - 2);
g.drawLine(j * CELL_SIZE + CELL_SIZE / 2, i * CELL_SIZE + CELL_SIZE * 7
/ 12, (j + 1) * CELL_SIZE - 4, (i + 1) * CELL_SIZE - 2);
g.drawLine(j * CELL_SIZE + CELL_SIZE / 2 + 1, i * CELL_SIZE + CELL_SIZE
* 7 / 12, j * CELL_SIZE + 4 + 1, (i + 1) * CELL_SIZE - 2);
g.drawLine(j * CELL_SIZE + CELL_SIZE / 2 - 1, i * CELL_SIZE + CELL_SIZE
* 7 / 12, (j + 1) * CELL_SIZE - 4 - 1, (i + 1) * CELL_SIZE - 2);
g.drawLine(j * CELL_SIZE + CELL_SIZE / 2 + 2, i * CELL_SIZE + CELL_SIZE
* 7 / 12, j * CELL_SIZE + 4 + 2, (i + 1) * CELL_SIZE - 2);
g.drawLine(j * CELL_SIZE + CELL_SIZE / 2 - 2, i * CELL_SIZE + CELL_SIZE
* 7 / 12, (j + 1) * CELL_SIZE - 4 - 2, (i + 1) * CELL_SIZE - 2);
g.drawLine(j * CELL_SIZE + CELL_SIZE / 2 + 3, i * CELL_SIZE + CELL_SIZE
* 7 / 12, j * CELL_SIZE + 4 + 3, (i + 1) * CELL_SIZE - 2);
g.drawLine(j * CELL_SIZE + CELL_SIZE / 2 - 3, i * CELL_SIZE + CELL_SIZE
* 7 / 12, (j + 1) * CELL_SIZE - 4 - 3, (i + 1) * CELL_SIZE - 2);
}
private void paintManGoal(Graphics g, int j, int i) {
paintGoal(g, j, i);
paintMan(g, j, i);
}
private void paintStart(Graphics g) {
g.setColor(Color.black);
int dx = 200;
int dy = 100;
int x = (BW_WIDTH - dx) / 2;
int y = (BW_HEIGHT - dy) / 2;
g.fillRect(x, y, dx, dy);
g.setColor(Color.white);
g.drawRect(x + 2, y + 2, dx - 4, dy - 4);
g.drawRect(x + 3, y + 3, dx - 6, dy - 6);
g.drawRect(x + 4, y + 4, dx - 8, dy - 8);
g.drawString("CLICK HERE!", x + 70, y + 50);
}
private void paintEnd(Graphics g) {
g.setColor(Color.black);
int dx = 200;
int dy = 100;
int x = (BW_WIDTH - dx) / 2;
int y = (BW_HEIGHT - dy) / 2;
g.fillRect(x, y, dx, dy);
g.setColor(Color.white);
g.drawRect(x + 2, y + 2, dx - 4, dy - 4);
g.drawRect(x + 3, y + 3, dx - 6, dy - 6);
g.drawRect(x + 4, y + 4, dx - 8, dy - 8);
g.drawString("GOOD JOB!", x + 70, y + 50);
}
private static String[] help = { "s - start game", "n - next world",
"p - previous world", "u - undo", "r - redo", "b - toggle beep",
"h - help", " ", "Press C to continue", " ", "Create by",
"Levente S醤tha, 2002",
};
private void paintHelp(Graphics g) {
g.setColor(Color.black);
int dx = 200;
int dy = 320;
int x = (BW_WIDTH - dx) / 2;
int y = (BW_HEIGHT - dy) / 2;
g.fillRect(x, y, dx, dy);
g.setColor(Color.white);
g.drawRect(x + 2, y + 2, dx - 4, dy - 4);
g.drawRect(x + 3, y + 3, dx - 6, dy - 6);
g.drawRect(x + 4, y + 4, dx - 8, dy - 8);
g.drawString("H E L P", x + 80, y + 40);
for (int i = 0; i < help.length; i++) {
g.drawString(help[ i], x + 40, y + 70 + 20 * i);
}
}
private void paintShape(Graphics g, int j, int i) {
int c = state[ i][ j];
if (c > NULL_ST) switch (c) {
case NULL_ST:
paintNull(g, j, i);
break;
case WALL_ST:
paintWall(g, j, i);
break;
case BACK_ST:
paintBack(g, j, i);
break;
case GOAL_ST:
paintBack(g, j, i);
paintGoal(g, j, i);
break;
case BOX_ST:
paintBox(g, j, i);
break;
case FIT_ST:
paintFit(g, j, i);
break;
case MAN_ST:
paintBack(g, j, i);
paintMan(g, j, i);
break;
case MAN_GOAL_ST:
paintBack(g, j, i);
paintManGoal(g, j, i);
break;
}
}
//private Image iBuff = null;
public void paint(Graphics g) {
/*
* if (iBuff == null) { iBuff = createImage(WIDTH, HEIGHT);
*/
//Graphics g = iBuff.getGraphics();
g.setColor(Color.black);
g.fillRect(0, 0, BW_WIDTH, BW_HEIGHT);
switch (gameMode) {
case START_MODE:
{
paintStart(g);
break;
}
case PLAY_MODE:
{
for (int j = 0; j < X_SIZE; j++) {
for (int i = 0; i < Y_SIZE; i++) {
paintShape(g, j, i);
}
}
break;
}
case HELP_MODE:
{
paintHelp(g);
break;
}
case END_MODE:
{
paintEnd(g);
break;
}
}
//gr.drawImage(iBuff, 0, 0, this);
}
public void update(Graphics g) {
paint(g);
}
private int invert(int key) {
int inv = -1;
switch (key) {
case LEFT_MOVE:
inv = RIGHT_MOVE;
break;
case RIGHT_MOVE:
inv = LEFT_MOVE;
break;
case UP_MOVE:
inv = DOWN_MOVE;
break;
case DOWN_MOVE:
inv = UP_MOVE;
break;
case LEFT_PUSH:
inv = RIGHT_DRAW;
break;
case RIGHT_PUSH:
inv = LEFT_DRAW;
break;
case UP_PUSH:
inv = DOWN_DRAW;
break;
case DOWN_PUSH:
inv = UP_DRAW;
break;
case LEFT_DRAW:
inv = RIGHT_MOVE;
break;
case RIGHT_DRAW:
inv = LEFT_MOVE;
break;
case UP_DRAW:
inv = DOWN_MOVE;
break;
case DOWN_DRAW:
inv = UP_MOVE;
break;
}
return inv;
}
private void undoController(int key) {
if (isUndo) {
redoStack.push(new Integer(invert(key)));
} else {
if (redoStack.size() > 0 && !isRedo) redoStack.removeAllElements();
undoStack.push(new Integer(invert(key)));
}
}
private void changeState(int key) {
Graphics g = getGraphics();
int xt = x, yt = y, ct;
xo = x;
yo = y;
boolean isDraw = false;
switch (key) {
case LEFT_MOVE:
y--;
yt = y - 1;
break;
case RIGHT_MOVE:
y++;
yt = y + 1;
break;
case UP_MOVE:
x--;
xt = x - 1;
break;
case DOWN_MOVE:
x++;
xt = x + 1;
break;
case LEFT_DRAW:
yt = y + 1;
y--;
isDraw = true;
break;
case RIGHT_DRAW:
yt = y - 1;
y++;
isDraw = true;
break;
case UP_DRAW:
xt = x + 1;
x--;
isDraw = true;
break;
case DOWN_DRAW:
xt = x - 1;
x++;
isDraw = true;
break;
}
int c = state[ x][ y];
if (isDraw) {
undoController(key);
cellExited(xo, yo);
cellExited(xt, yt);
cellEntered(x, y, MAN_ST);
cellEntered(xo, yo, BOX_ST);
paintShape(g, yt, xt);
paintShape(g, y, x);
paintShape(g, yo, xo);
} else if (c == BACK_ST || c == GOAL_ST) {
undoController(key);
cellExited(xo, yo);
cellEntered(x, y, MAN_ST);
paintShape(g, y, x);
paintShape(g, yo, xo);
} else if (c == BOX_ST || c == FIT_ST) {
ct = state[ xt][ yt];
if (ct == BACK_ST || ct == GOAL_ST) {
undoController(PUSH_BASE + key);
cellExited(xo, yo);
cellExited(x, y);
cellEntered(x, y, MAN_ST);
cellEntered(xt, yt, BOX_ST);
paintShape(g, yt, xt);
paintShape(g, y, x);
paintShape(g, yo, xo);
} else {
block();
}
} else {
block();
}
if (checkEnd()) {
gameMode = END_MODE;
paintEnd(g);
}
}
private void cellEntered(int x, int y, int mst) {
int st = state[ x][ y];
if (st == BACK_ST) {
state[ x][ y] = mst;
} else if (st == GOAL_ST) {
state[ x][ y] = mst == MAN_ST ? MAN_GOAL_ST : FIT_ST;
}
}
private void cellExited(int x, int y) {
int st = state[ x][ y];
if (st == MAN_ST || st == BOX_ST) {
state[ x][ y] = BACK_ST;
} else if (st == FIT_ST || st == MAN_GOAL_ST) {
state[ x][ y] = GOAL_ST;
}
}
private void block() {
x = xo;
y = yo;
repeat = false;
beep();
}
private boolean beepOn = true;
private void beep() {
if (beepOn) getToolkit().beep();
}
public Dimension getPreferredSize() {
return new Dimension(BW_WIDTH, BW_HEIGHT);
}
public void windowClosing(WindowEvent e) {
System.exit(0);
}
//*********** MAIN ************************
public static void main(String[] argv) {
BoxWorld bw = new BoxWorld();
bw.init();
Frame frame = new Frame("Boxworld");
frame.setResizable(false);
frame.add(bw);
frame.addWindowListener(bw);
frame.pack();
frame.setSize(400, 400);
frame.setLocation(40, 40);
frame.show();
// frame.setVisible(true);
}
//************ EMPTY METHODS *********************
public void windowActivated(WindowEvent e) {
}
public void windowDeactivated(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowOpened(WindowEvent e) {
}
public void windowClosed(WindowEvent e) {
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -