Java推箱子小游戏实现:
推箱子小游戏曾经在我们的童年给我们带来了很多乐趣。推箱子这款游戏现在基本上没人玩了,甚至在新一代人的印象中都已毫无记忆了。。。但是,这款游戏可以在一定程度上锻炼自己的编程能力。
窗口画面设计:
import javax.swing.*;
import java.awt.event.*;
public class Pushbox extends JFrame implements ActionListener {
private static final long serialVersionUID = -392323205976303799L;
Game game;
JLabel jl = new JLabel("推箱子游戏");
JTextArea tips = new JTextArea(" 游戏规则\n玩家通过上下左右按键\n移动小人推箱子到指定\n"
+ "的箱子归位处,当所有\n箱子都被推至归位处后\n则玩家成功通过该关卡");
JButton jb1 = new JButton("选择关卡");
JButton jb2 = new JButton("重新开始");
JButton jb3 = new JButton("退出游戏");
JLabel jlt = new JLabel("在下方输入关卡1-3");
JTextField jt = new JTextField();
Pushbox() {
setTitle("推箱子");//设置窗口显示的名字
setBounds(500, 200, 600, 470);//设置窗口相对于屏幕的显示位置(500,200),窗口大小(600,470)
setVisible(true);// 使窗口显示
setLayout(null);// 清空布局管理器,之后的控件可自定义坐标位置
jl.setBounds(265, 10, 100, 20);
tips.setBounds(430, 40, 120, 120);
jlt.setBounds(430, 180, 120, 20);
jt.setBounds(430, 210, 120, 30);
jb1.setBounds(430, 250, 120, 40);
jb2.setBounds(430, 300, 120, 40);
jb3.setBounds(430, 350, 120, 40);
add(jl);
add(tips);
add(jlt);
add(jt);
add(jb1);
add(jb2);
add(jb3);
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
game = new Game();
add(game);
game.Load(game.flag);
game.requestFocus();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jb1) {
String f = jt.getText();
game.flag = Integer.parseInt(f) - 1;
if (game.flag > 2 || game.flag < 0) {
JOptionPane.showMessageDialog(this, "没有这一关!!!");//弹窗
jt.setText("");
} else {
game.Load(game.flag);
game.requestFocus();
jt.setText("");
}
}
if (e.getSource() == jb2) {
game.Load(game.flag);
game.requestFocus();
}
//如果点击了jb3按钮(退出游戏)
if (e.getSource() == jb3) {
System.exit(0); //退出
}
}
}
地图读取及初始点设置:
public class Read {
public int f, x, y;
public int[][] map = new int[12][12];
Read(int n) {
f = n;
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 12; j++) {
map[i][j] = Pass.pass[f][i][j];
if (map[i][j] == 5) {//找到人的位置
x = j;//把下标存起来
y = i;//把下标存起来
}
}
}
}
int[][] getmap() {
return map;
}
int getmx() {
return x;
}
int getmy() {
return y;
}
}
游戏地图显示和控件操作:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Game extends JPanel implements KeyListener {
int len = 30;//地图格子的长度
Image[] img;//定义图像数组
public int flag = 0;//定义关数,因为数组下表第一个是0,所以0代表第一关
int mx, my;//定义任务初始位置下标
int[][] newmap;//定义目前的地图数组(这个就是实时显示的,会根据移动改变)
int[][] oldmap;//定义加载的地图数组(这个就是一开始的地图数据,不会改变)
Read fnewmap;
Read foldmap;
Game() {
setBounds(40, 40, 360, 360);
setBackground(Color.white);
addKeyListener(this);
img = new Image[7];
for (int i = 0; i < 7; i++) {
//图片资源存放的相对路径
img[i] = Toolkit.getDefaultToolkit().getImage(".\\pushing_boxes\\src\\main\\java\\com\\txz\\img\\" + i + ".png");
}
setVisible(true);
Load(flag);
}
void Load(int i)//加载地图函数
{
fnewmap = new Read(i);//调用Read
foldmap = new Read(i);//调用Read
newmap = fnewmap.getmap();//获取地图数据给newmap数组
mx = fnewmap.getmx();//获取初始人物的X位置
my = fnewmap.getmy();//获取初始人物的Y位置
oldmap = foldmap.getmap();//获取地图数据给oldmap数组
repaint();//刷新界面并绘制地图
}
@Override
public void paint(Graphics g)//绘制地图函数,(自带的库函数),调用repaint()时就会调用这个函数,重写画界面
{
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 12; j++) {
g.drawImage(img[newmap[j][i]], i * len, j * len, this);
}
}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e)//键盘响应函数
{
if (e.getKeyCode() == KeyEvent.VK_UP) {
up();
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
down();
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
left();
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
right();
}
if (win()) {
if (flag == 2) {
JOptionPane.showMessageDialog(this, "恭喜您通过最后一关!!!");
} else {
String a = "恭喜您通过第" + (flag + 1) + "关!!!\n是否要进入下一关?";
String title = "恭喜过关";
int text = JOptionPane.YES_NO_OPTION;
int choice = 0;
choice = JOptionPane.showConfirmDialog(null, a, title, text);
if (choice == 0) {
flag++;
Load(flag);
} else if (choice == 0) {
System.exit(0);
}
}
}
}
void up()//上移函数
{
if (newmap[my - 1][mx] == 2 || newmap[my - 1][mx] == 4) {
if (oldmap[my][mx] == 4) {
newmap[my][mx] = 4;
} else {
newmap[my][mx] = 2;
}
newmap[my - 1][mx] = 5;
repaint();
my--;
} else if (newmap[my - 1][mx] == 3) {
if (newmap[my - 2][mx] == 4) {
if (oldmap[my][mx] == 4) {
newmap[my][mx] = 4;
} else {
newmap[my][mx] = 2;
}
newmap[my - 1][mx] = 5;
newmap[my - 2][mx] = 6;
repaint();
my--;
} else if (newmap[my - 2][mx] == 2) {
if (oldmap[my][mx] == 4) {
newmap[my][mx] = 4;
} else {
newmap[my][mx] = 2;
}
newmap[my - 1][mx] = 5;
newmap[my - 2][mx] = 3;
repaint();
my--;
}
} else if (newmap[my - 1][mx] == 6) {
if (newmap[my - 2][mx] == 4) {
if (oldmap[my][mx] == 4) {
newmap[my][mx] = 4;
} else {
newmap[my][mx] = 2;
}
newmap[my - 1][mx] = 5;
newmap[my - 2][mx] = 6;
repaint();
my--;
} else if (newmap[my - 2][mx] == 2) {
if (oldmap[my][mx] == 4) {
newmap[my][mx] = 4;
} else {
newmap[my][mx] = 2;
}
newmap[my - 1][mx] = 5;
newmap[my - 2][mx] = 3;
repaint();
my--;
}
}
}
void down()//下移函数
{
if (newmap[my + 1][mx] == 2 || newmap[my + 1][mx] == 4) {
if (oldmap[my][mx] == 4) {
newmap[my][mx] = 4;
} else {
newmap[my][mx] = 2;
}
newmap[my + 1][mx] = 5;
repaint();
my++;
} else if (newmap[my + 1][mx] == 3) {
if (newmap[my + 2][mx] == 4) {
if (oldmap[my][mx] == 4) {
newmap[my][mx] = 4;
} else {
newmap[my][mx] = 2;
}
newmap[my + 1][mx] = 5;
newmap[my + 2][mx] = 6;
repaint();
my++;
} else if (newmap[my + 2][mx] == 2) {
if (oldmap[my][mx] == 4) {
newmap[my][mx] = 4;
} else {
newmap[my][mx] = 2;
}
newmap[my + 1][mx] = 5;
newmap[my + 2][mx] = 3;
repaint();
my++;
}
} else if (newmap[my + 1][mx] == 6) {
if (newmap[my + 2][mx] == 4) {
if (oldmap[my][mx] == 4) {
newmap[my][mx] = 4;
} else {
newmap[my][mx] = 2;
}
newmap[my + 1][mx] = 5;
newmap[my + 2][mx] = 6;
repaint();
my++;
} else if (newmap[my + 2][mx] == 2) {
if (oldmap[my][mx] == 4) {
newmap[my][mx] = 4;
} else {
newmap[my][mx] = 2;
}
newmap[my + 1][mx] = 5;
newmap[my + 2][mx] = 3;
repaint();
my++;
}
}
}
void left()//左移函数
{
if (newmap[my][mx - 1] == 2 || newmap[my][mx - 1] == 4) {
if (oldmap[my][mx] == 4) {
newmap[my][mx] = 4;
} else {
newmap[my][mx] = 2;
}
newmap[my][mx - 1] = 5;
repaint();
mx--;
} else if (newmap[my][mx - 1] == 3) {
if (newmap[my][mx - 2] == 4) {
if (oldmap[my][mx] == 4) {
newmap[my][mx] = 4;
} else {
newmap[my][mx] = 2;
}
newmap[my][mx - 1] = 5;
newmap[my][mx - 2] = 6;
repaint();
mx--;
} else if (newmap[my][mx - 2] == 2) {
if (oldmap[my][mx] == 4) {
newmap[my][mx] = 4;
} else {
newmap[my][mx] = 2;
}
newmap[my][mx - 1] = 5;
newmap[my][mx - 2] = 3;
repaint();
mx--;
}
} else if (newmap[my][mx - 1] == 6) {
if (newmap[my][mx - 2] == 4) {
if (oldmap[my][mx] == 4) {
newmap[my][mx] = 4;
} else {
newmap[my][mx] = 2;
}
newmap[my][mx - 1] = 5;
newmap[my][mx - 2] = 6;
repaint();
mx--;
} else if (newmap[my][mx - 2] == 2) {
if (oldmap[my][mx] == 4) {
newmap[my][mx] = 4;
} else {
newmap[my][mx] = 2;
}
newmap[my][mx - 1] = 5;
newmap[my][mx - 2] = 3;
repaint();
mx--;
}
}
}
void right()//右移函数
{
if (newmap[my][mx + 1] == 2 || newmap[my][mx + 1] == 4) {
if (oldmap[my][mx] == 4) {
newmap[my][mx] = 4;
} else {
newmap[my][mx] = 2;
}
newmap[my][mx + 1] = 5;
repaint();
mx++;
} else if (newmap[my][mx + 1] == 3) {
if (newmap[my][mx + 2] == 4) {
if (oldmap[my][mx] == 4) {
newmap[my][mx] = 4;
} else {
newmap[my][mx] = 2;
}
newmap[my][mx + 1] = 5;
newmap[my][mx + 2] = 6;
repaint();
mx++;
} else if (newmap[my][mx + 2] == 2) {
if (oldmap[my][mx] == 4) {
newmap[my][mx] = 4;
} else {
newmap[my][mx] = 2;
}
newmap[my][mx + 1] = 5;
newmap[my][mx + 2] = 3;
repaint();
mx++;
}
} else if (newmap[my][mx + 1] == 6) {
if (newmap[my][mx + 2] == 4) {
if (oldmap[my][mx] == 4) {
newmap[my][mx] = 4;
} else {
newmap[my][mx] = 2;
}
newmap[my][mx + 1] = 5;
newmap[my][mx + 2] = 6;
repaint();
mx++;
} else if (newmap[my][mx + 2] == 2) {
if (oldmap[my][mx] == 4) {
newmap[my][mx] = 4;
} else {
newmap[my][mx] = 2;
}
newmap[my][mx + 1] = 5;
newmap[my][mx + 2] = 3;
repaint();
mx++;
}
}
}
boolean win()//判断输赢函数
{
boolean num = false;
out:
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 12; j++) {
if (oldmap[i][j] == 4) {
if (newmap[i][j] == 6) {
num = true;
} else {
num = false;
break out;
}
}
}
}
return num;
}
}
运行效果如图:
完整素材及全部代码
代码已上传csdn,0积分下载,觉得这片博文有用请留下你的点赞。
基于JAVA的推箱子小游戏,一起来实现自己的趣味小游戏