准备工作:1.首先导入需要的图片(大小有严格要求)
2.再创建一个TXZ类
3.开始写代码(我用的是IDEA开发软件)
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class TXZ6 {
public static void main(String[] args) {
Jframe gameframe = new Jframe("推箱子1.0 ");
gameframe.setDefaultCloseOperation(3);
gameframe.setSize(22 * 48 + 11, 12 * 48 + 37);
gameframe.setLocationRelativeTo(null);
gameframe.setResizable(false);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setBackground(Color.GRAY);
gameframe.setContentPane(panel);
ImageIcon boxImg = new ImageIcon("imgs/box3.png");
JLabel box = new JLabel(boxImg);
panel.add(box);
box.setBounds(4 * 48, 3 * 48, 48, 48);
ImageIcon goalImg = new ImageIcon("imgs/goal3.png");
JLabel goal = new JLabel(goalImg);
panel.add(goal);
goal.setBounds(14 * 48, 6 * 48, 48, 48 );
ImageIcon workerImg = new ImageIcon("imgs/workerLeft2.png");
JLabel worker = new JLabel(workerImg);
panel.add(worker);
worker.setBounds(6 * 48, 9 * 48, 48, 48);
ImageIcon wallImg = new ImageIcon("imgs/wall2.png");
JLabel[] walls = new JLabel[22 * 2 + (12 - 2) * 2 + 11 + 5 + 4 + 4];
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);
index++;
panel.add(walls[index]);
walls[index].setBounds(i * 48, 11 * 48, 48, 48);
index++;
}
// 左右围墙
for (int i = 1; i <=10 ; i++) {
panel.add(walls[index]);
walls[index].setBounds(0, i * 48, 48, 48);
index++;
panel.add(walls[index]);
walls[index].setBounds(21 * 48, i * 48, 48, 48);
index++;
}
//障碍物
for (int i = 0; i < 11; i++) {
panel.add(walls[index]);
walls[index].setBounds((5 + i) * 48, 3 * 48, 48, 48);
index++;
}
for (int i = 0; i < 5; i++) {
panel.add(walls[index]);
walls[index].setBounds(7 * 48, (i + 5) * 48, 48, 48);
index++;
}
for (int i = 0; i < 4; i++) {
panel.add(walls[index]);
walls[index].setBounds((9 + i) * 48, (i + 5) * 48, 48, 48);
index++;
}
for (int i = 0; i < 4; i++) {
panel.add(walls[index]);
walls[index].setBounds((15 + i) * 48, (7 - i) * 48, 48, 48);
index++;
}
// 匿名内部类(接口,事件处理)
gameframe.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 workerImgPath = "workerDown2.png";
if (keyCode == 37 || keyCode == 65){
x = - 48;
workerImgPath = "workerLeft2.png";
} else if (keyCode == 38 || keyCode == 87){
y = - 48;
workerImgPath = "workerUp2.png";
} else if (keyCode == 39 || keyCode == 68){
x = 48;
workerImgPath = "workerRight2.png";
} else if (keyCode == 40 || keyCode == 83){
y = 48;
}
//0 更改工人移动图片
ImageIcon img = new ImageIcon("imgs/" + workerImgPath);
worker.setIcon(img);
//1 工人移动
worker.setBounds(worker.getBounds().x + x, worker.getBounds().y + y, 48, 48);
//2 判断工人是否穿墙
for (int i = 0; i < walls.length; i++) {
if (walls[i].getBounds().contains(worker.getBounds())){
worker.setBounds(worker.getBounds().x - x, worker.getBounds().y - y, 48, 48);
break;
}
}
//3 让工人推动箱子
for (int i = 0; i < walls.length; i++) {
if (box.getBounds().contains(worker.getBounds())){
box.setBounds(box.getBounds().x + x, box.getBounds().y + y, 48, 48);
break;
}
}
//4 判断箱子是否穿墙
for (int i = 0; i < walls.length; i++) {
if (box.getBounds().contains(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;
}
}
//5 判断输赢
for (int i = 0; i < walls.length; i++) {
if (box.getBounds().contains(goal.getBounds())) {
JOptionPane.showMessageDialog(null, "恭喜通关");
System.exit(0);
}
}
}
@Override
public void keyReleased(KeyEvent e) {
}
});
gameframe.setVisible(true);
}
}
运行后的结果:



