java推箱子2.0代码
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class GameFrame extends JFrame {
private JPanel panel = null;//面板
private JLabel box = null;
private JLabel worker = null;
private JLabel goal = null;
private JLabel[] walls = null;
public GameFrame(String title) {
super(title);
setSize(22 * 48 + 10, 12 * 48 + 38);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
initPanel();
}
private void initPanel() {
panel = new JPanel();
panel.setBackground(Color.CYAN);
panel.setLayout(null);
super.setContentPane(panel);
this.box = initLable("box3.png", 8, 9);
this.worker = initLable("workerRight3.png", 3, 3);
this.goal = initLable("goal3.png", 17, 6);
initWalls();
keyEvent();
}
/**
* @param imgPath
* @param x
* @param y
* @return
*/
private JLabel initLable(String imgPath, int x, int y) {
ImageIcon img = new ImageIcon("imgs/" + imgPath);
JLabel label = new JLabel(img);
panel.add(label);
label.setBounds(x * 48, y * 48, 48, 48);
return label;
}
private void initWalls() {
ImageIcon wallImg = new ImageIcon("imgs/wall3.png");
this.walls = new JLabel[22 * 2 + (12 - 2) * 2 + 6 + 5 + 5];
for (int i = 0; i < walls.length; i++) {
walls[i] = new JLabel(wallImg);
}
int index = 0;
for (int i = 0; i < 22; i++) {
panel.add(walls[index]);
walls[index++].setBounds(i * 48, 0, 48, 48);
panel.add(walls[index]);
walls[index++].setBounds(i * 48, 11 * 48, 48, 48);
}
for (int i = 1; i <= 10; i++) {
panel.add(walls[index]);
walls[index++].setBounds(0, i * 48, 48, 48);
panel.add(walls[index]);
walls[index++].setBounds(21 * 48, i * 48, 48, 48);
}
//横着加
for (int i = 0; i < 6; i++) {
panel.add(walls[index]);
walls[index++].setBounds((6 + i) * 48, 3 * 48, 48, 48);
}
//竖着加
for (int i = 0; i < 5; i++) {
panel.add(walls[index]);
walls[index++].setBounds(14 * 48, (i + 4) * 48, 48, 48);
}
//斜着加
for (int i = 0; i < 5; i++) {
panel.add(walls[index]);
walls[index++].setBounds((3 + i) * 48, (i + 4) * 48, 48, 48);
}
}
private void keyEvent() {
addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
int x = 0, y = 0;
String imgPath = "workerDown3.png";
if (keyCode == 37 || keyCode == 65) {
x = -48;
imgPath = "workerLeft3.png";
} else if (keyCode == 38 || keyCode == 87) {
y = -48;
imgPath = "workerUp3.png";
} else if (keyCode == 39 || keyCode == 68) {
x = 48;
imgPath = "workerRight3.png";
} else if (keyCode == 40 || keyCode == 83) {
y = 48;
}
worker.setBounds(worker.getBounds().x + x, worker.getBounds().y + y, 48, 48);
ImageIcon workerImg = new ImageIcon("imgs/" + imgPath);
worker.setIcon(workerImg);
for (int i = 0; i < walls.length; i++) {
if (worker.getBounds().intersects(walls[i].getBounds())) {
worker.setBounds(worker.getBounds().x - x, worker.getBounds().y - y, 48, 48);
break;
}
}
//工人推箱子
if (worker.getBounds().intersects(box.getBounds())) {
box.setBounds(box.getBounds().x + x, box.getBounds().y + y, 48, 48);
}
//判断穿墙
for (int i = 0; i < walls.length; i++) {
if (box.getBounds().intersects(walls[i].getBounds())) {
box.setBounds(box.getBounds().x - x, box.getBounds().y - y, 48, 48);
worker.setBounds(worker.getBounds().x - x, worker.getBounds().y - y, 48, 48);
break;
}
}
if (box.getBounds().intersects(goal.getBounds())) {
JOptionPane.showMessageDialog(null, "恭喜你,赢了!");
System.exit(0);//结束进程
}
}
@Override
public void keyReleased(KeyEvent e) {
}
});
}
}
主函数
public class Run {
public static void main(String[] args) {
GameFrame gameFrame = new GameFrame("推箱子2.0");
gameFrame.setVisible(true);
}
}