java界面推箱子游戏
时间: 2023-09-05 08:14:29 浏览: 46
推箱子游戏是经典的益智游戏,以下是一个基于Java Swing的推箱子游戏示例:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PushBox extends JFrame implements ActionListener {
private final int COLS = 20;
private final int ROWS = 15;
private final int IMAGE_SIZE = 20;
private Image wall;
private Image box;
private Image goal;
private Image man;
private int[][] map = new int[ROWS][COLS];
private JPanel panel;
private JButton btnPrev;
private JButton btnNext;
public PushBox() {
initImages();
initMap();
initUI();
}
private void initImages() {
ImageIcon iconWall = new ImageIcon("wall.png");
wall = iconWall.getImage();
ImageIcon iconBox = new ImageIcon("box.png");
box = iconBox.getImage();
ImageIcon iconGoal = new ImageIcon("goal.png");
goal = iconGoal.getImage();
ImageIcon iconMan = new ImageIcon("man.png");
man = iconMan.getImage();
}
private void initMap() {
int[][] mapData = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
};
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
map[i][j] = mapData[i][j];
}
}
}
private void initUI() {
panel = new JPanel() {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
drawMap(g);
}
};
panel.setPreferredSize(new Dimension(COLS * IMAGE_SIZE, ROWS * IMAGE_SIZE));
setContentPane(panel);
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btnPrev = new JButton("Prev");
btnPrev.addActionListener(this);
btnNext = new JButton("Next");
btnNext.addActionListener(this);
JPanel ctrlPanel = new JPanel();
ctrlPanel.add(btnPrev);
ctrlPanel.add(btnNext);
add(ctrlPanel, BorderLayout.SOUTH);
}
private void drawMap(Graphics g) {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
switch (map[i][j]) {
case 1:
g.drawImage(wall, j * IMAGE_SIZE, i * IMAGE_SIZE, this);
break;
case 2:
g.drawImage(box, j * IMAGE_SIZE, i * IMAGE_SIZE, this);
break;
case 3:
g.drawImage(goal, j * IMAGE_SIZE, i * IMAGE_SIZE, this);
break;
case 4:
g.drawImage(man, j * IMAGE_SIZE, i * IMAGE_SIZE, this);
break;
}
}
}
}
private void move(int dx, int dy) {
int manX = -1;
int manY = -1;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (map[i][j] == 4) {
manX = j;
manY = i;
break;
}
}
}
int nextX = manX + dx;
int nextY = manY + dy;
if (map[nextY][nextX] == 0 || map[nextY][nextX] == 3) {
map[manY][manX] = 0;
map[nextY][nextX] = 4;
} else if (map[nextY][nextX] == 2) {
int boxNextX = nextX + dx;
int boxNextY = nextY + dy;
if (map[boxNextY][boxNextX] == 0 || map[boxNextY][boxNextX] == 3) {
map[manY][manX] = 0;
map[nextY][nextX] = 4;
map[boxNextY][boxNextX] = 2;
}
}
panel.repaint();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnPrev) {
// TODO: Implement undo
} else if (e.getSource() == btnNext) {
// TODO: Implement redo
}
}
public static void main(String[] args) {
PushBox game = new PushBox();
game.setVisible(true);
}
}
```
在这个示例中,我们使用了一个二维数组 `map` 来存储地图数据,其中:
- 0 表示空地
- 1 表示墙
- 2 表示箱子
- 3 表示目标点
- 4 表示人物
`initMap()` 方法初始化了地图数据。
`initUI()` 方法初始化了界面,其中使用了一个 `JPanel` 作为画布,使用 `Graphics` 对象在画布上绘制地图。
`move()` 方法处理了人物移动的逻辑,如果人物前方是空地或目标点,则将人物移动到前方;如果人物前方是箱子,则判断箱子前方是否是空地或目标点,如果是,则将人物和箱子同时移动。
目前示例中还没有实现撤销和重做功能,你可以自行扩展。此外,还可以添加计时器、计步器等功能,增加游戏的趣味性。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)