精华内容
下载资源
问答
  • java推箱子小游戏代码
    2021-03-08 21:57:06

    《java推箱子游戏源代码(含推箱子的判断)》由会员分享,可在线阅读,更多相关《java推箱子游戏源代码(含推箱子的判断)(6页珍藏版)》请在人人文库网上搜索。

    1、第一个Java文件:package xiaoA;import j ava awt Color;import j ava awt ReadiessException;import j ava awt event KeyEvent;import j ava awt event KeyListener;import j avax swing Imageicon;import j avax swing JFrame;import j avax swing JLabml;import j avax swing JOptionPanE;import j avax swing JPanel;public c。

    2、lass GammFramm extends JFrame JPanel zhuobu = new JPanel();/工人JLabel worker = null;/箱子JLabel box = null;/目的地JLabel goal = null;JLabel walls = null;public static final int SPEED = 12;/设置图片大小int imgSize = 48;public void setlmgSize(int imgSize)this imgSize: = imgSize;public GameFrame(String title) thro。

    3、ws HeadlessException super(title);/构造方法中调用本类的其它方法this initContEntPanE();this addKeyListener(new KeyListener()/键盘按下事件public void keyPressed(KeyEvent e) /2.5使工人可以移动int xSpeed = 0, ySpeed = 0;switch (mgetKeyCode()case KeyEvent VK LEFT :xSpeed = -SPEED;worker seticon(new Imageicon(image/workerUp gif); b。

    4、reak;case KeyEvent VKRIGHT :xSpeed = SPEED;worker seticon(new Imageicon(Himage/workerUp gif);break;case KeyEvent VKUP :ySpeed = -SPEED;worker Icon (new Imageicon (image/workerUp gif ); break;case KeyEvent VK_DOWN :ySpeed = SPEED;worker seticon(new Imageicon(image/workerUp gif);break;default:return;w。

    5、orker smtBounds (worker gmtX() + :Speedr worker gmtY() + ySpeed, worker gmtWidth(), worker getHeight();/2.7判断工人是否撞到墙壁for (int i = 0; i walls1ength; i+)if (worker gmtBounds() intersects(wallsi) gotBounds() vzorker sw七Bounds (work?二getX () - :-:Speedf xorker getY () - ySpeed, worker getWidth(), worker。

    6、 gEtHmight();/3.2使工人可以推动箱子if (worker get Bounds () intmirsects (bo:t got Bounds () ) )box smtBounds (box gmt;X() + xSpeed, box gEtY() + ySpeed, box gmtWidth(), box gEtHmight();/3.3判断箱子是否撞到墙壁for (int i = 0; i walls length; i+)if (box getBounds() intersects(wallsi) getBounds()worker smtBounds (woMker 。

    7、gmtX() 一 xSpeed, worker gmt;Y() 一 ySpeed, worker gmtWidth(), worker getHeight();box smtBounds (boK gmtX() 一 xSpeed, box gmt;Y() 一 ySpeed, box gmtWidth(), box gEtHmight();/3.4判断是否胜利if (box getX () =goa1 gm*tX() & box getY () =goal getY () )JOptionPane sho站Messa丁(null,您贏啦! H);public void keyReleased(K。

    8、eyEvent e) public void keyTyped(KeyEvent e) );/ *设置内容面板*/public void InitContentPane()zhuobu setBackground(Color red); zhuobu setLayout(null);/调用父类的属性和方法super setContentPane(zhuobu);*把某个图片以组件的方式加入窗体* Qparam imgPath 图片路径Qparam x param y宽度高度添加完的组件Qparam widthQparam heightreturnpublic void addComponent。

    9、(int tagf String imgPathf int int y) Imageicon img = new Imageicon(imgPath);/ /创建JLabel:lE!lmageIcon通过构造方法传参传入/把食物放到盘子里JLabel componet = new JLabel(img);/设置盘子在桌布上的位置和大小componet smtBounds(x, y, imgSize, imgSize);/把盘了放到桌布上zhuobu add (componet;);switch (tag) case 1:box = componet;break;case 2:goal = co。

    10、mponet;break;case 3:worker = componet;break;public void add?7all (String imgPath, int loactions)Imageicon walllmg = new Imageicon(imgPath);walls = new JLabel66 + loactions丄mngth;for (int i = 0; i walls丄mngth; i+)/创建没每个围墙,他们使用的是同-个图片walls i = new JLabel(walllmg);for (int i = 0; i V walls丄mngth; i+)/创。

    11、建没每-个围墙,他们使用的是同个图片wallsi = new JLabel(walllmg);int index = 0;分别设置各个图片位置*/for (int i = 0; i 14; i+)/左边墙x-jalls index smtBounds (0z i * imgSizer imgSizer imgSize);zhuobu add(wallsindex);index+;/右边墙wallsindexsetBounds(20 * imgSize, i imgSizef imgSizezimgSize);zhuobu add(wallsindex);index+;for (int i = 。

    12、0; i 19; i+)/上边墙wallsindexsmtBounds(i + 1)* imgSize 0 r imgSizeimgSize);zhuobu add (xalls index);index+;/下边墙x-zalls index setBounds ( (i + 1) * imgSize, 13 * imgSizez imgSize, imgSize);zhuobu add(wallsindex);index+;/添加中间障碍耦合解耦for (int i = 0; i loactions丄mngth; i+)wallsindex smtBounds(loactionsi 0* i。

    13、mgSizef loactionsi1* imgSize, imgSize, imgSize);zhuobu add(wallsindex);inde:+;第二个Java文件:public class Run public static void main(String args) GameFrame gameFrame = new GameFrame(H推箱子游戏 ”); 设宜大小gameFrame.setBounds( 100,50. 21 *48 + 5, 14 *48 + 25);窗体大小不可变gameFranie.setRcsizable(false);gameFranie.setImgSize(48);gameFrame.addComponent(3, HworkerUp.png* 400. 100);gameFrame.addComponent( 1, ”box.png, 160. 60);gamcFramcaddComponciH(2, “goal.pngt 80, 520);int wallLocations =4,5,5,5,6,5,7,5, 8,5,9,5,10,5,6,8,7.8,&8,9.8,10,8,11,5;gameFranie.addWall(Hwall.png, wallLocations);gameFranie.setVisible(true。

    更多相关内容
  • java简易推箱子小游戏

    2019-01-26 20:43:16
    一个简单的java实现推箱子小游戏,涉及简单swing编程,适合初学者练手
  • Java实现推箱子小游戏.zip Java实现推箱子小游戏.zip Java实现推箱子小游戏.zip
  • java推箱子小游戏代码

    2021-11-01 09:47:38
    使用108的二维字符数据表示游戏画面,H表示墙面;&表示玩家角色,o表示箱子,表示目的地。玩家可以通过输入a,d,w,s字符控制角色移动,当箱子推到目的地时显示游戏结束。
  • 主要为大家详细介绍了java实现简单的推箱子小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
  • Java推箱子游戏源码(Java推箱子小游戏源码)java游戏代码

    Java推箱子游戏源码(Java推箱子小游戏源码)java游戏代码

    public Swingtest002() {
    		// 设置标题
    		setTitle("请登陆");
    		// 绝对布局
    		setLayout(null);
    		// 定义一个容器
    		Container c = getContentPane();
    		// 创建"用户名:"标签
    		JLabel jl1 = new JLabel("用户名:");
    		// 创建文本框
    		final JTextField jtf1 = new JTextField();
    		// 创建"密码:"标签
    		JLabel jl2 = new JLabel("密码:");
    		// 创建密码框
    		final JPasswordField jpf1 = new JPasswordField();
    		// 设置密码字符为*
    		jpf1.setEchoChar('*');
    		// 创建"提交"按钮
    		JButton jb1 = new JButton("提交");
    		// 创建"重置"按钮
    		JButton jb2 = new JButton("重置");
    		
    		// 当用户名为"mr",密码为"mrsoft"时点击"提交"按钮弹出"登录成功"提示对话框
    		jb1.addActionListener(new ActionListener() {
     
    			@Override
    			public void actionPerformed(ActionEvent e) {
     
    				String  name=(String)jtf1.getText().trim();
    				String  password=new String(jpf1.getText().trim());
    				String sql="select * from user where name='"+name+"' and pass='"+password+"'";
    			System.out.println("name"+name+"password"+password);
    				// TODO Auto-generated method stub
    				try {
    					ResultSet rs=st.executeQuery(sql);
    					if (rs.next()) {
    						JOptionPane.showMessageDialog(null, "登陆成功!");
    						this.setVisible(false);//关闭当前的窗体
    					
    						MainFrame  mainframe=new MainFrame();
    					
    						
    						
    					} else if (jtf1.getText().trim().length() == 0 || new String(jpf1.getPassword()).trim().length() == 0) {
    						JOptionPane.showMessageDialog(null, "不能为空!");
    					} else {
    						JOptionPane.showMessageDialog(null, "错误");
    						// 清零
    						jtf1.setText("");
    						jpf1.setText("");
    					}
    				} catch (HeadlessException e1) {
    					// TODO Auto-generated catch block
    					e1.printStackTrace();
    				} catch (SQLException e1) {
    					// TODO Auto-generated catch block
    					e1.printStackTrace();
    				}
     
    			}
     
    			private void setVisible(boolean b) {
    				// TODO Auto-generated method stub
    				
    			}
    		});

     

     

     

    展开全文
  • 推箱子小游戏是由java编写 基本实习了推箱子小游戏的简单功能 朋友们可以试着扩展一下功能。
  • Java毕业设计】基于JAVA推箱子游戏(论文+答辩PPT+源代码+讲解视频).zip 【Java毕业设计】基于JAVA推箱子游戏(论文+答辩PPT+源代码+讲解视频).zip 【Java毕业设计】基于JAVA推箱子游戏(论文+答辩PPT+源...
  • java实现推箱子小游戏(附源码)

    千次阅读 2021-02-28 16:01:42
    先上效果图 可以通过AWSD进行移动和推箱子 自己弄出来的代码玩起来还是很有意思的。代码一共是三个.java文件,代码内容如下所示package ss;import java.awt.Graphics;import java.awt.Image;import java.awt.Point;...

    先上效果图

    0e4db05d10dd9ca693f7968a95bcd1db.png

    可以通过AWSD进行移动和推箱子

    ed9bcf1c12bf38c8c910aef8f0f0930e.png

    自己弄出来的代码玩起来还是很有意思的。

    代码一共是三个.java文件,代码内容如下所示

    package ss;

    import java.awt.Graphics;

    import java.awt.Image;

    import java.awt.Point;

    import java.awt.event.KeyEvent;

    import java.awt.event.KeyListener;

    import javax.swing.ImageIcon;

    import javax.swing.JFrame;

    import javax.swing.JOptionPane;

    /**

    * 1.继承窗体类为当前类的父类

    * @author lizhicheng

    *

    */

    public class PushBox extends JFrame implements KeyListener{

    /**

    * 3.声明一个二维数组的地图

    */

    int[][] maps=

    {

    {3,3,3,3,3,3,3,3},

    {3,0,0,0,0,0,0,3},

    {3,0,0,0,0,2,0,3},//3表示墙

    {3,0,0,0,3,0,3,3},//2表示箱子

    {3,0,0,1,0,0,0,3},//1表示人

    {3,0,3,0,2,0,0,3},//0表示空地

    {3,0,0,0,0,0,0,3},

    {3,3,3,3,3,3,3,3},

    };

    /**

    * 6.声明两个变量来保存图标的起始位置

    *

    */

    int top=10,left=25;

    int posX=3,posY=4;//保存大力水手的位置

    /**

    * 2.构造方法添加固定的窗体设置

    */

    public PushBox()

    {

    //设置标题

    this.setTitle("http://ai.52learn.online");

    //设置窗体大小

    this.setSize(500, 350);

    //设置窗体默认关闭操作

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //设置窗体可见

    this.setVisible(true);

    //设置窗体居中显示

    this.setLocationRelativeTo(null);

    //进行监听

    this.addKeyListener(this);

    }

    /**

    * 4.窗体的绘制

    * @param args

    */

    @Override

    public void paint(Graphics g) {

    super.paint(g);

    refresh();//7.调用刷新

    }

    /**

    * 5.重绘所有当前窗体的组件

    * @param args

    */

    public void refresh()

    {

    //获取当前窗体的画笔

    Graphics gs=this.getGraphics();

    //将图片资源引入

    ImageIcon wall = new ImageIcon("bian.png");

    ImageIcon area = new ImageIcon("kongdi.png");

    ImageIcon human = new ImageIcon("ren.png");

    ImageIcon box = new ImageIcon("xiang.png");

    //将资源绘制在窗体上

    for(int i = 0 ; i < maps.length ; i++)

    {

    for(int j = 0 ; j < maps[i].length ; j++)

    {

    if(maps[i][j]==3)

    {

    //绘制墙

    gs.drawImage(wall.getImage(),top+j*40,left+i*40,40,40, null);

    }

    else if(maps[i][j]==0)

    {

    //绘制空地

    gs.drawImage(area.getImage(),top+j*40,left+i*40,40,40, null);

    }

    else if(maps[i][j]==1)

    {

    //绘制水手

    gs.drawImage(human.getImage(),top+j*40,left+i*40,40,40, null);

    }

    else if(maps[i][j]==2)

    {

    //绘制箱子

    gs.drawImage(box.getImage(),top+j*40,left+i*40,40,40, null);

    }

    }

    }

    }

    public static void main(String[] args) {

    new PushBox();

    }

    @Override

    public void keyPressed(KeyEvent arg0) {

    // TODO Auto-generated method stub

    char ch=arg0.getKeyChar();//获取按键

    //JOptionPane.showMessageDialog(null, "你按了"+ch);

    if(ch=='w'||ch=='W')

    {

    //向上走动

    if(maps[posY-1][posX]==0)

    {

    maps[posY-1][posX]=1;

    maps[posY][posX]=0;

    posY--;

    }

    else if(maps[posY-1][posX]==2&&maps[posY-2][posX]==0)

    {

    maps[posY-2][posX]=2;

    maps[posY-1][posX]=1;

    maps[posY][posX]=0;

    posY--;

    }

    }

    else if(ch=='s'||ch=='S')

    {

    //向下走动

    if(maps[posY+1][posX]==0)

    {

    maps[posY+1][posX]=1;

    maps[posY][posX]=0;

    posY++;

    }

    else if(maps[posY+1][posX]==2&&maps[posY+2][posX]==0)

    {

    maps[posY+2][posX]=2;

    maps[posY+1][posX]=1;

    maps[posY][posX]=0;

    posY++;

    }

    }

    else if(ch=='a'||ch=='A')

    {

    //向左走

    if(maps[posY][posX-1]==0)

    {

    maps[posY][posX-1]=1;

    maps[posY][posX]=0;

    posX--;

    }

    else if(maps[posY][posX-1]==2&&maps[posY][posX-2]==0)

    {

    maps[posY][posX-2]=2;

    maps[posY][posX-1]=1;

    maps[posY][posX]=0;

    posX--;

    }

    }

    else if(ch=='d'||ch=='D')

    {

    //向右走

    if(maps[posY][posX+1]==0)

    {

    maps[posY][posX+1]=1;

    maps[posY][posX]=0;

    posX++;

    }

    else if(maps[posY][posX+1]==2&&maps[posY][posX+2]==0)

    {

    maps[posY][posX+2]=2;

    maps[posY][posX+1]=1;

    maps[posY][posX]=0;

    posX++;

    }

    }

    refresh();

    }

    @Override

    public void keyReleased(KeyEvent arg0) {

    // TODO Auto-generated method stub

    }

    @Override

    public void keyTyped(KeyEvent arg0) {

    // TODO Auto-generated method stub

    }

    }

    package mg;

    import java.util.Scanner;

    public class migong {

    public static void main(String[] args) {

    int[][] maps=

    {

    {3,3,3,3,3,3,3,3},

    {3,0,0,0,0,0,0,3},

    {3,0,0,0,0,2,0,3},//3表示墙

    {3,0,0,0,3,0,3,3},//2表示箱子

    {3,0,0,1,0,0,0,3},//1表示人

    {3,0,3,0,2,0,0,3},//0表示空地

    {3,0,0,0,0,0,0,3},

    {3,3,3,3,3,3,3,3},

    };

    //打印输出地图在控制台上

    for(int i = 0 ; i < maps.length ; i++)

    {

    for(int j = 0 ; j < maps[i].length ; j++)

    {

    System.out.print(maps[i][j]);

    }

    System.out.println();

    }

    //声明两个变量记录人的位置

    int posX=3,posY=4;

    //接收用户键盘的输入

    Scanner sc = new Scanner(System.in);

    System.out.print("请输入方向:上:w,下:s,左:a,右:d : ");

    //运行程序

    while(true)

    {

    String str=sc.next();

    if(str.equalsIgnoreCase("w"))

    {

    //向上

    if(maps[posY-1][posX]==0)

    {

    maps[posY-1][posX]=1;

    maps[posY][posX]=0;

    posY--;

    }

    else if(maps[posY-1][posX]==2&&maps[posY-2][posX]==0)

    {

    maps[posY-2][posX]=2;

    maps[posY-1][posX]=1;

    maps[posY][posX]=0;

    posY--;

    }

    }

    else if(str.equalsIgnoreCase("s"))

    {

    //向下

    if(maps[posY+1][posX]==0)

    {

    maps[posY+1][posX]=1;

    maps[posY][posX]=0;

    posY++;

    }

    else if(maps[posY+1][posX]==2&&maps[posY+2][posX]==0)

    {

    maps[posY+2][posX]=2;

    maps[posY+1][posX]=1;

    maps[posY][posX]=0;

    posY++;

    }

    }

    else if(str.equalsIgnoreCase("a"))

    {

    //向左

    if(maps[posY][posX-1]==0)

    {

    maps[posY][posX-1]=1;

    maps[posY][posX]=0;

    posX--;

    }

    else if(maps[posY][posX-1]==2&&maps[posY][posX-2]==0)

    {

    maps[posY][posX-2]=2;

    maps[posY][posX-1]=1;

    maps[posY][posX]=0;

    posX--;

    }

    }

    else if(str.equalsIgnoreCase("d"))

    {

    //向右

    if(maps[posY][posX+1]==0)

    {

    maps[posY][posX+1]=1;

    maps[posY][posX]=0;

    posX++;

    }

    else if(maps[posY][posX+1]==2&&maps[posY][posX+2]==0)

    {

    maps[posY][posX+2]=2;

    maps[posY][posX+1]=1;

    maps[posY][posX]=0;

    posX++;

    }

    }

    else if(str.equalsIgnoreCase("quit"))

    {

    System.out.print("游戏即将结束\n");

    System.exit(0);

    }

    //打印输出地图在控制台上

    for(int i = 0 ; i < maps.length ; i++)

    {

    for(int j = 0 ; j < maps[i].length ; j++)

    {

    System.out.print(maps[i][j]);

    }

    System.out.println();

    }

    System.out.print("请输入方向:上:w,下:s,左:a,右:d : ");

    }

    }

    }

    package lzc;

    import java.awt.*;

    import java.awt.event.KeyEvent;

    import java.awt.event.KeyListener;

    import javax.swing.ImageIcon;

    import javax.swing.JFrame;

    import javax.swing.JLabel;

    import javax.swing.JOptionPane;

    public class Pig extends JFrame implements KeyListener{

    //进行引入图片

    ImageIcon pig = new ImageIcon("右猪.gif");

    JLabel pigLab = new JLabel(pig);

    //背景

    ImageIcon grass = new ImageIcon("草地.gif");

    JLabel grassLab = new JLabel(grass);

    //声明一个构造方法

    public Pig() {

    // TODO Auto-generated constructor stub

    this.setTitle("小猪佩奇");//设置窗体的标题

    this.setSize(841,310);//设置窗体的大小

    this.setVisible(true);//设置窗口的可见

    this.setLocationRelativeTo(null);//设置窗体居中显示

    //设置窗体的关闭默认操作:当你点击关闭窗口就自动退出程序

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //设置窗体大小不可改变

    this.setResizable(false);

    pigLab.setBounds(300, 150, 71, 88);

    //添加背景

    this.add(grassLab);

    grassLab.setBounds(10,10,841,310);

    //添加佩奇

    grassLab.add(pigLab);

    //进行监听

    this.addKeyListener(this);

    //move();

    }

    void move()

    {

    int x=600,y=40;

    while(true)

    {

    x-=6;

    pigLab.setLocation(x, y);

    try {

    Thread.currentThread().sleep(10);

    } catch (Exception a) {

    }

    if(x<0) x=840;

    }

    }

    public static void main(String[] args) {

    new Pig();

    }

    /**

    *用户按下并松开键盘上的按键

    */

    @Override

    public void keyPressed(KeyEvent arg0) {

    // TODO Auto-generated method stub

    char ch=arg0.getKeyChar();//获取按键

    //获取佩琪的位置

    Point p=pigLab.getLocation();

    int x=p.x,y=p.y;

    //JOptionPane.showMessageDialog(null, "你按了"+ch);

    switch(ch)

    {

    case 'w':

    //向上走动

    y=y-20;

    if(y<0)

    {

    y=310;

    }

    break;

    case 's':

    //向下走动

    y=y+20;

    if(y>310)

    {

    y=0;

    }

    break;

    case 'a':

    //向左走

    x=x-20;

    if(x<0)

    {

    x=841;

    }

    break;

    case 'd':

    //向右走

    x=x+20;

    if(x>841)

    {

    x=0;

    }

    break;

    default :;

    }

    pigLab.setLocation(x, y);

    }

    @Override

    public void keyReleased(KeyEvent arg0) {

    // TODO Auto-generated method stub

    }

    @Override

    public void keyTyped(KeyEvent arg0) {

    // TODO Auto-generated method stub

    }

    }

    点击获取代码和相关的图像资源

    展开全文
  • Java推箱子小游戏(带背景音乐)完整代码

    千次阅读 多人点赞 2020-08-17 11:47:22
    Java实现推箱子小游戏一、整体框架二、游戏效果图二、推箱子四大类GameFrame类Map类MapFactory类Sound类四、游戏分析游戏操作数组解读 文章比较长,请耐心看完。 一、整体框架 该程序由四个类组成,GameFrame类...


    文章比较长,请耐心看完。

    一、整体框架

    该程序由四个类组成,GameFrame类(程序主体)、Map类(地图基础设置)、MapFactory类(地图构图)、Sound类(添加音乐),下面是程序结构图。
    推箱子游戏框架图
    注意音乐格式要转MIDI序列(直接改文件后缀名不可行,如果想更换背景音乐,建议下载widi进行格式转换)

    二、游戏效果图

    游戏画面没有设置的很炫酷,因为绿色比较护眼。
    正在看这篇博客的你,让眼睛休息一下吧!
    在这里插入图片描述在这里插入图片描述

    图片音乐素材及游戏源码链接:
    https://pan.baidu.com/wap/init?surl=PvUnMYBukB7A_2K-uNhzxw

    提取码:5k71

    言归正传,我们来看游戏代码

    代码直接复制粘贴就能运行(素材包放在项目src目录下)

    三、推箱子四大类

    1. GameFrame类

    package 推箱子;
    
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.util.ArrayList;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    
    public class GameFrame extends JFrame implements ActionListener ,MouseListener,KeyListener{
    
    	/**
    	 * 序列化ID,相当于身份认证,
    	 * 主要用于程序的版本控制,保持不同版本的兼容性,
    	 * 在程序版本升级时避免程序报出版本不一致的错误。
    	 */
    	private static final long serialVersionUID = 1L;	
    	private int grade = 0;//定义关卡数
    	private int row = 7,column = 7,leftX = 0,leftY = 0;
    	//row,column表示人物坐标;leftX,leftY记载左上角图片位置
    	private int mapRow = 0,mapColumn = 0;//地图的行列数
    	private int width = 0,height = 0;//屏幕大小
    	private boolean acceptKey = true;
    	private Image pic[] = null;//图片数组
    	private byte[][] map = null;//地图数组
    	private ArrayList list = new ArrayList();//用于撤回操作
    	Sound sound;
    	
    	final byte WALL = 1,BOX = 2,BOXONEND = 3,END = 4,MANDOWN = 5,
    			MANLEFT = 6,MANRIGHT = 7,MANUP = 8,GRASS = 9,MANDOWNONEND = 10,
    			MANLEFTONEND = 11,MANRIGHTONEND = 12,MANUPONEND = 13;
    	/**
    	 * 构造方法
    	 */
    	public GameFrame(){
    		super("推箱子游戏带音乐版");
    		//1.窗体设置
    		setSize(600,600);//窗体大小
    		setVisible(true);//窗体可见
    		setResizable(false);//窗体大小不可被用户改变
    		setLocation(680,200);//窗体位置
    		setBackground(Color.green);//窗体背景颜色
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//窗体结束程序
    		
    		//2.cont容器设置
    		Container cont = getContentPane();
    		/**
    		 * 因为JFrame是一个框架,所以JFrame不能通过add方法直接添加组件,
    		 * 创建一个content pane容器,方便向框架内添加组件
    		 */
    		cont.setLayout(null);//清空默认的流式布局管理器
    		cont.setBackground(Color.green);//容器背景颜色
    		getPic();//获取图片
    		width = this.getWidth();
    		height = this.getHeight();
    		this.setFocusable(true);//设置组件可以被选中
    		initMap();
    		this.addKeyListener(this);
    		this.addMouseListener(this);
    		//播放音乐
    		Sound sound = new Sound();
    		sound.loadSound();//加载音乐
    	}
    	
    	//初始化地图
    	public void initMap(){
    		map = getMap(grade);//获取每个地图的关卡数
    		list.clear();//清除列表
    		byte[][] temp = map;
    		
    		//创建地图
    		for(int i=0;i<temp.length;i++)
    		{
    			for(int j=0;j<temp[0].length;j++){
    				System.out.print(temp[i][j]+" ");		
    				}
    			System.out.println();
    		}	
    		
    		getMapSizeAndPosition();
    		getManPosition();
    	}
    	
    	//获取人物当前位置
    	public void getManPosition(){
    		
    		for(int i=0;i<map.length;i++){
    			for(int j=0;j<map[0].length;j++){
    				if(map[i][j]==MANDOWN||map[i][j]==MANUP||map[i][j]==MANLEFT||map[i][j]==MANRIGHT){
    					row = i;
    					column = j;
    					break;
    				}			
    			}
    		}
    	}
    	
    	
    	//获取游戏区域大小及显示游戏的左上角位置
    	public void getMapSizeAndPosition(){
    		mapRow = map.length;
    		mapColumn = map[0].length;
    		leftX = (width - map[0].length * 30)/2;
    		leftY = (height - map.length * 30)/2;
    		System.out.println(leftX);
    		System.out.println(leftY);
    		System.out.println(mapRow);
    		System.out.println(mapColumn);
    	}
    	
    	//获取图片
    	public void getPic(){
    		pic = new Image[14];
    		for(int i=0;i<=13;i++){
    			pic[i] = Toolkit.getDefaultToolkit().getImage
    					("E:\\kkkkk\\\\推箱子\\\\src\\\\img/pic"+i+".png");
    		}
    	}
    	
    	//人走过变草地
    	public byte grassOrEnd(byte man){
    		byte result = GRASS;
    		if(man == MANLEFTONEND || man == MANRIGHTONEND || man == MANUPONEND || man == MANDOWNONEND){
    			result = END;
    		}
    		
    		return result;
    	}
    
    	private void moveUp(){
    		if(map[row-1][column]==WALL)
    			return;
    		byte tempBox;
    		byte tempMan;
    		
    		if(map[row-1][column]==BOX||map[row-1][column]==BOXONEND){		//如果向上一格是箱子
    			if(map[row-2][column]==GRASS||map[row-2][column]==END){     //如果向上第二格是过道或者终点
    				Map currentMap = new Map(row,column,map);
    				list.add(currentMap);//用于撤回操作
    				tempBox = map[row-2][column]==END?BOXONEND:BOX;
    				tempMan = map[row-1][column]==BOXONEND?MANUPONEND:MANUP;
    				map[row][column] = grassOrEnd(map[row][column]);
    				map[row-2][column] = tempBox;
    				map[row-1][column] = tempMan;
    				row--;
    			}
    		}else{//如果向上一格是过道或者终点
    			Map currentMap = new Map(row,column,map);
    			list.add(currentMap);//用于撤回操作
    			tempMan = map[row-1][column]==GRASS?MANUP:MANUPONEND;
    			map[row][column] = grassOrEnd(map[row][column]);
    			map[row-1][column] = tempMan;
    			row--;
    		}		
    	}
    	
    	private void moveDown(){
    	
    		if(map[row+1][column]==WALL)
    			return ;
    		
    		byte tempBox;
    		byte tempMan;
    		
    		if(map[row+1][column]==BOX||map[row+1][column]==BOXONEND){
    			if(map[row+2][column]==END||map[row+2][column]==GRASS){
    				Map currentMap = new Map(row,column,map);
    				list.add(currentMap);//用于撤回操作
    				tempBox = map[row+2][column] == END?BOXONEND:BOX;
    				tempMan = map[row+1][column] == BOXONEND?MANDOWNONEND:MANDOWN;
    				map[row][column] = grassOrEnd(map[row][column]);
    				map[row+2][column] = tempBox;
    				map[row+1][column] = tempMan;
    				row++;
    			}
    		}else{
    			Map currentMap = new Map(row,column,map);
    			list.add(currentMap);//用于撤回操作
    			tempMan = map[row+1][column]==GRASS?MANDOWN:MANDOWNONEND;
    			map[row][column] = grassOrEnd(map[row][column]);
    			map[row+1][column] = tempMan;
    			row++;
    		}		
    	}
    	
    	private void moveLeft(){
    		
    		if(map[row][column-1]==WALL)
    			return ;
    		
    		byte tempBox;
    		byte tempMan;
    		
    		if(map[row][column-1]==BOX||map[row][column-1]==BOXONEND){
    			if(map[row][column-2]==END||map[row][column-2]==GRASS){
    				Map currentMap = new Map(row,column,map);
    				list.add(currentMap);//用于撤回操作
    				tempBox = map[row][column-2] == END?BOXONEND:BOX;
    				tempMan = map[row][column-1] == BOXONEND?MANLEFTONEND:MANLEFT;
    				map[row][column] = grassOrEnd(map[row][column]);
    				map[row][column-2] = tempBox;
    				map[row][column-1] = tempMan;
    				column--;
    			}
    		}else{
    			Map currentMap = new Map(row,column,map);
    			list.add(currentMap);//用于撤回操作
    			tempMan = map[row][column-1]==GRASS?MANLEFT:MANLEFTONEND;
    			map[row][column] = grassOrEnd(map[row][column]);
    			map[row][column-1] = tempMan;
    			column--;
    		}
    		
    	}
    	
    	private void moveRight(){
    		
    		if(map[row][column+1]==WALL)
    			return ;
    		
    		byte tempBox;
    		byte tempMan;
    		
    		if(map[row][column+1]==BOX||map[row][column+1]==BOXONEND){
    			
    			if(map[row][column+2]==END||map[row][column+2]==GRASS){
    				
    				Map currentMap = new Map(row,column,map);
    				list.add(currentMap);//用于撤回操作
    				tempBox = map[row][column+2] == END?BOXONEND:BOX;
    				tempMan = map[row][column+1] == BOXONEND?MANRIGHTONEND:MANRIGHT;
    				map[row][column] = grassOrEnd(map[row][column]);
    				map[row][column+2] = tempBox;
    				map[row][column+1] = tempMan;
    				column++;
    			}
    		}else{
    			Map currentMap = new Map(row,column,map);
    			list.add(currentMap);//用于撤回操作
    			tempMan = map[row][column+1]==GRASS?MANRIGHT:MANRIGHTONEND;
    			map[row][column] = grassOrEnd(map[row][column]);
    			map[row][column+1] = tempMan;
    			column++;
    		}		
    	}
    	
    	//判断游戏是否结束
    	public boolean isFinished(){
    		
    		for(int i=0;i<mapRow;i++) {
    			for(int j=0;j<mapColumn;j++){
    	//		System.out.println("值是"+map[i][j]+",END 的值是"+END+",他们相等吗?:"+(map[i][j]==END));
    				if(map[i][j]==END||map[i][j]==MANDOWNONEND||map[i][j]==MANUPONEND||map[i][j]==MANLEFTONEND||map[i][j]==MANRIGHTONEND){
    					return false;
    				}
    			}
    		}
    		return true;
    	}
    	
    	
    	@Override
    	public void keyTyped(KeyEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void keyPressed(KeyEvent e) {
    		if(e.getKeyCode() == KeyEvent.VK_UP){
    			moveUp();
    		}
    		if(e.getKeyCode() == KeyEvent.VK_DOWN){
    			moveDown();
    		}
    		if(e.getKeyCode() == KeyEvent.VK_LEFT){
    			moveLeft();
    		}
    		if(e.getKeyCode() == KeyEvent.VK_RIGHT){
    			moveRight();
    		}
    		if(e.getKeyCode() == KeyEvent.VK_A){//上一关
    			acceptKey = true;
    			priorGrade();
    			return ;
    		}
    		if(e.getKeyCode() == KeyEvent.VK_D){//下一关
    			acceptKey = true;
    			nextGrade();
    			return ;
    		}
    		if(e.getKeyCode() == KeyEvent.VK_B){//撤回
    			undo();
    		}
    		
    		repaint();
    		
    		if(isFinished()){
    			//禁用按键
    			acceptKey = false;
    			if(grade==10){JOptionPane.showMessageDialog(this, "恭喜通过最后一关");}
    			else{
    				String msg = "恭喜你通过第"+(grade+1)+"关!!!\n是否要进入下一关?";
    				int type = JOptionPane.YES_NO_OPTION;
    				String title = "过关";
    				int choice = 0;
    				choice = JOptionPane.showConfirmDialog(null, msg,title,type);
    				if(choice==1){
    					System.exit(0);
    				}else if(choice == 0){
    					acceptKey = true;
    					nextGrade();
    				}
    			}
    		}
    		
    	}
    	
    	//画图
    	public void paint(Graphics g){
    		//System.out.println("我被调用了");
    		for(int i=0;i<mapRow;i++) {
    			for(int j=0;j<mapColumn;j++){
    				if(map[i][j]!=0){
    //					System.out.println("这个位置 不是0,它的值是"+map[i][j]);
    //					g.drawRect(10, 30, getWidth()/2-50, getHeight()/2-50);
    					g.drawImage(pic[map[i][j]],leftX+j*30,leftY+i*30,30,30,this);
    			}
    		}
    	}
    		g.setColor(Color.black);
    		g.setFont(new Font("楷体_2312",Font.BOLD,30));
    		g.drawString("现在是第", 150, 140);
    		g.drawString(String.valueOf(grade+1),310,140);
    		g.drawString("关", 360, 140);
    	}
    	
    	public int getManX(){
    		return row;
    	}
    	
    	public int getManY(){
    		return column;
    	}
    	
    	public int getGrade(){
    		return grade;
    	}
    	
    	public byte[][] getMap(int grade){		
    		return MapFactory.getMap(grade);
    	}
    	
    	public void DisplayToast(String str){
    		JOptionPane.showMessageDialog(null, str,"提示",JOptionPane.ERROR_MESSAGE);
    	}
    	
    	//过关后不可撤销
    	public void undo(){
    		if(acceptKey){
    			if(list.size()>0){
    				Map priorMap = (Map)list.get(list.size()-1);
    				map = priorMap.getMap();
    				row = priorMap.getManX();
    				column = priorMap.getManY();
    				repaint();
    				list.remove(list.size()-1);
    			}else{
    				DisplayToast("不能再撤销");
    			}
    		}else{
    			DisplayToast("此关已完成,不能撤销");
    		}
    	}
    	
    	public void priorGrade(){
    		grade--;
    		acceptKey = true;
    		if(grade<0)
    			grade = 0;
    		initMap();
    		clearPaint(this.getGraphics());
    		repaint();
    	}
    	
    	public void nextGrade(){
    		if(grade>=MapFactory.getCount()-1){
    			DisplayToast("恭喜你完成所有关卡");
    			acceptKey = false;
    		}else{
    			grade++;
    			initMap();
    			clearPaint(this.getGraphics());
    			repaint();
    			acceptKey = true;
    		}
    	}
    	
    	private void clearPaint(Graphics g) {
    	 g.clearRect(0, 0, width+leftX, height+leftY);
    	}
    
    	@Override
    	public void keyReleased(KeyEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void mouseClicked(MouseEvent e) {
    		// TODO Auto-generated method stub
    		if(e.getButton() == MouseEvent.BUTTON3)
    		{
    			undo();
    		}
    	}
    
    	@Override
    	public void mousePressed(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void mouseReleased(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void mouseEntered(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void mouseExited(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    		
    	public static void main(String[] args){
    		GameFrame a = new GameFrame();
    	}	
    }
    

    2. Map类

    package 推箱子;
    
    public class Map {
    	int manX = 0;
    	int manY = 0;
    	byte map[][];
    	int grade;
    	
    	public Map(int manX,int manY,byte[][] map){
    		this.manX = manX;
    		this.manY = manY;
    		int row = map.length;
    		int column = map[0].length;
    		byte temp[][] = new byte[row][column];
    		
    		for(int i=0;i<row;i++){
    			for(int j=0;j<column;j++){
    				temp[i][j] = map[i][j];
    			}
    		}
    		this.map = temp;//避免直接引用
    		
    	}
    	
    	public Map(int manX,int manY,byte[][] map,int grade){
    		this(manX,manY,map);
    		this.grade = grade;
    	}
    	
    	//人物X坐标
    	public int getManX(){
    		return manX;
    	}
    	
    	//人物Y坐标
    	public int getManY(){
    		return manY;
    	}
    	
    	//地图数组
    	public byte[][] getMap(){
    		return map;
    	}
    	
    	//关卡
    	public int getGrade(){
    		return grade;
    	}	
    }
    
    
    

    3. MapFactory类

    package 推箱子;
    
    public class MapFactory {//地图数据类
    	
    	static byte map[][][] = {
    		{
    			{0,0,1,1,1,0,0,0},
    			{0,0,1,4,1,0,0,0},
    			{0,0,1,9,1,1,1,1},
    			{1,1,1,2,9,2,4,1},
    			{1,4,9,2,5,1,1,1},
    			{1,1,1,1,2,1,0,0},
    			{0,0,0,1,4,1,0,0},
    			{0,0,0,1,1,1,0,0}
    		},
    		{
    			{1,1,1,1,1,0,0,0,0},
    			{1,9,9,5,1,0,0,0,0},
    			{1,9,2,2,1,0,1,1,1},
    			{1,9,2,9,1,0,1,4,1},
    			{1,1,1,9,1,1,1,4,1},
    			{0,1,1,9,9,9,9,4,1},
    			{0,1,9,9,9,1,9,9,1},
    			{0,1,9,9,9,1,1,1,1},
    			{0,1,1,1,1,1,0,0,0}
    		},
    		{
    			{1,1,1,1,1,0,0,0,0},
    			{1,9,9,9,1,1,0,0,0},
    			{1,9,2,9,9,1,0,0,0},
    			{1,1,9,2,9,1,1,1,1},
    			{0,1,1,1,5,4,9,9,1},
    			{0,0,1,9,9,4,1,9,1},
    			{0,0,1,9,9,9,9,9,1},
    			{0,0,1,1,1,1,1,1,1}			
    		},
    		{
    			{0,0,1,1,1,1},
    			{0,1,1,9,9,1},
    			{1,1,9,2,9,1},
    			{1,4,2,9,5,1},
    			{1,2,4,9,1,1},
    			{1,4,9,1,1,0},
    			{1,1,1,1,0,0}
    		},
    		
    		{
    			{1,1,1,1,1,1},
    			{1,9,5,9,9,1},
    			{1,9,2,9,9,1},
    			{1,2,9,1,1,1},
    			{1,4,9,4,1,0},
    			{1,1,1,1,1,0}
    		},
    		{
    			{0,0,0,1,1,1,1,0,0},
    			{0,0,1,1,9,9,1,1,0},
    			{1,1,1,9,5,2,4,1,1},
    			{1,9,9,9,9,2,4,9,1},
    			{1,9,9,9,1,2,4,9,1},
    			{1,1,1,1,1,9,1,9,1},
    			{0,0,0,0,1,9,9,9,1},
    			{0,0,0,0,1,1,1,1,1}
    		},
    		
    		
    		{
    			{0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0},//19行
    			{0,0,0,0,1,9,9,9,1,0,0,0,0,0,0,0,0,0,0},
    			{0,0,0,0,1,2,9,9,1,0,0,0,0,0,0,0,0,0,0},
    			{0,0,1,1,1,9,9,2,1,1,0,0,0,0,0,0,0,0,0},
    			{0,0,1,9,9,2,9,2,9,1,0,0,0,0,0,0,0,0,0},
    			{1,1,1,9,1,9,1,1,9,1,0,0,0,1,1,1,1,1,1},
    			{1,9,9,9,1,9,1,1,9,1,1,1,1,1,9,9,4,4,1},
    			{1,9,2,9,9,2,9,9,9,9,9,9,9,9,9,9,4,4,1},
    			{1,1,1,1,1,9,1,1,1,9,1,5,1,1,9,9,4,4,1},
    			{0,0,0,0,1,9,9,9,9,9,1,1,1,1,1,1,1,1,1},
    			{0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0}
    		},
    		{
    			{1,1,1,1,1,1,1,1,1,1,1,1,0,0},//14行
    			{1,4,4,9,9,1,9,9,9,9,9,1,1,1},
    			{1,4,4,9,9,1,9,2,9,9,2,9,9,1},
    			{1,4,4,9,9,1,2,1,1,1,1,9,9,1},
    			{1,4,4,9,9,9,9,5,9,1,1,9,9,1},
    			{1,4,4,9,9,1,9,1,9,9,2,9,1,1},
    			{1,1,1,1,1,1,9,1,1,2,9,2,9,1},
    			{0,0,1,9,2,9,9,2,9,2,9,2,9,1},
    			{0,0,1,9,9,9,9,1,9,9,9,9,9,1},
    			{0,0,1,1,1,1,1,1,1,1,1,1,1,1}
    		},
    		{
    			{0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0},//17行
    			{0,0,0,0,0,0,0,0,1,9,9,9,9,9,5,1,0},
    			{0,0,0,0,0,0,0,0,1,9,2,1,2,9,1,1,0},
    			{0,0,0,0,0,0,0,0,1,9,2,9,9,2,1,0,0},
    			{0,0,0,0,0,0,0,0,1,1,2,9,2,9,1,0,0},
    			{1,1,1,1,1,1,1,1,1,9,2,9,1,9,1,1,1},
    			{1,4,4,4,4,9,9,1,1,9,2,9,9,2,9,9,1},
    			{1,1,4,4,4,9,9,9,9,2,9,9,2,9,9,9,1},
    			{1,4,4,4,4,9,9,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,0,1,1,1,1,0,0,0,0,0},//19行
    			{0,0,0,0,0,1,1,1,1,0,1,9,9,1,0,0,0,0,0},
    			{0,0,0,1,1,1,9,5,1,1,1,2,9,1,0,0,0,0,0},
    			{0,0,1,1,9,9,9,9,9,9,2,9,9,1,0,0,0,0,0},
    			{0,1,1,9,9,2,9,2,2,1,1,9,1,1,0,0,0,0,0},
    			{0,1,9,9,1,2,1,1,9,9,9,9,9,1,0,0,0,0,0},
    			{0,1,9,1,9,2,9,2,2,9,1,9,1,1,1,0,0,0,0},
    			{0,1,9,9,9,2,9,1,9,9,1,9,2,9,1,1,1,1,1},
    			{1,1,1,1,9,9,9,9,1,9,9,2,2,9,1,9,9,9,1},
    			{1,1,1,1,9,1,1,9,2,9,9,9,9,9,9,9,9,9,1},
    			{1,4,9,9,9,9,1,1,1,9,9,1,1,1,1,1,1,1,1},
    			{1,4,4,9,4,4,1,0,1,1,1,1,0,0,0,0,0,0,0},
    			{1,4,4,4,1,4,1,0,0,0,0,0,0,0,0,0,0,0,0},
    			{1,4,4,4,4,4,1,0,0,0,0,0,0,0,0,0,0,0,0},
    			{1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0}
    			
    		}
    		
    	};
    	static int count = map.length;
    	
    	
    	public static byte[][] getMap(int grade){
    		byte[][] temp;
    		if(grade>=0&&grade<count) {
    			temp = map[grade];
    		}else {
    			temp = map[0];
    		}
    		int row = temp.length;
    		int column = temp[0].length;
    		byte[][] result = new byte[row][column];//备份关卡版本
    		for(int i=0;i<row;i++){
    			for(int j=0;j<column;j++){
    				result[i][j] = temp[i][j];
    			}
    		}		
    		return result;
    	}
    	
    	public static int getCount(){
    		return count;
    	}
    }
    
    

    4. Sound类

    package 推箱子;
    
    import javax.sound.midi.*;
    import java.io.File;
    import java.io.IOException;
    
    public class Sound {
    
    	String path = new String("E:/kkkkk/推箱子/src/img/");
    	String file = new String("nor.mid");
    	Sequence seq;
    	Sequencer midi;
    	boolean sign;
    	
    	//加载音乐
    	public void loadSound(){
    		try {
    			seq = MidiSystem.getSequence(new File(path+file));
    			midi = MidiSystem.getSequencer();
    			midi.open();
    			midi.setSequence(seq);
    			midi.start();
    			midi.setLoopCount(Sequencer.LOOP_CONTINUOUSLY);
    			
    		} catch (InvalidMidiDataException | IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (MidiUnavailableException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}		
    		sign = true;
    	}	
    	
    	void mystop(){
    		midi.stop();
    		midi.close();
    		sign=false;
    	}
    	boolean isplay(){
    		return sign;
    	}
    	void setMusic(String e){
    		file=e;
    	}
    	
    }
    
    

    四、游戏分析

    1.游戏操作

    键盘上下左右键控制人物的移动,按A回到上一关,按D直接进入下一关。

    2.数组解读

    很多人对这个庞大的数组很好奇,其实呢,它们非常简单,下面我来解读一下。
    0(墙外的空余区域);1(墙);2(箱子);3(箱子位于目标点上);
    4(目标点);5(人物向下);9(空地);
    在这里插入图片描述

    五、单独窗体播放音乐(wav格式)

    由于mid格式比较少见,一般的软件无法实现该格式的转换,所以我们使用javax.sound实现简单的音频播放(只支持AU,RA,WAV格式),相比而言比较大众化。可以自己挑选喜欢的音乐作为背景音乐。
    更新后代码如下(只修改GameFrame类和Sound类)

    1.GameFrame类

    package 推箱子;
    
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.util.ArrayList;
    import java.util.concurrent.TimeUnit;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    
    public class GameFrame extends JFrame implements ActionListener ,MouseListener,KeyListener{
    	private static final long serialVersionUID = 1L;	
    	private int grade = 0;//定义关卡数
    	private int row = 7,column = 7,leftX = 0,leftY = 0;
    	//row,column表示人物坐标;leftX,leftY记载左上角图片位置
    	private int mapRow = 0,mapColumn = 0;//地图的行列数
    	private int width = 0,height = 0;//屏幕大小
    	private boolean acceptKey = true;
    	private Image pic[] = null;//图片数组
    	private byte[][] map = null;//地图数组
    	private ArrayList list = new ArrayList();//用于撤回操作
    	Sound sound;
    	
    	final byte WALL = 1,BOX = 2,BOXONEND = 3,END = 4,MANDOWN = 5,
    			MANLEFT = 6,MANRIGHT = 7,MANUP = 8,GRASS = 9,MANDOWNONEND = 10,
    			MANLEFTONEND = 11,MANRIGHTONEND = 12,MANUPONEND = 13;
    	/**
    	 * 构造方法
    	 */
    	public GameFrame(){
    		super("推箱子游戏带音乐版");
    		//1.窗体设置
    		setSize(600,600);//窗体大小
    		setVisible(true);//窗体可见
    		setResizable(false);//窗体大小不可被用户改变
    		setLocation(680,200);//窗体位置
    		setBackground(Color.green);//窗体背景颜色
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//窗体结束程序
    		
    		//2.cont容器设置
    		Container cont = getContentPane();
    		/**
    		 * 因为JFrame是一个框架,所以JFrame不能通过add方法直接添加组件,
    		 * 创建一个content pane容器,方便向框架内添加组件
    		 */
    		cont.setLayout(null);//清空默认的流式布局管理器
    		cont.setBackground(Color.green);//容器背景颜色
    		getPic();//获取图片
    		width = this.getWidth();
    		height = this.getHeight();
    		this.setFocusable(true);//设置组件可以被选中
    		initMap();
    		this.addKeyListener(this);
    		this.addMouseListener(this);
    
    	}
    	
    	//初始化地图
    	public void initMap(){
    		map = getMap(grade);//获取每个地图的关卡数
    		list.clear();//清除列表
    		byte[][] temp = map;
    		
    		//创建地图
    		for(int i=0;i<temp.length;i++)
    		{
    			for(int j=0;j<temp[0].length;j++){
    				System.out.print(temp[i][j]+" ");		
    				}
    			System.out.println();
    		}			
    		getMapSizeAndPosition();
    		getManPosition();
    	}
    	
    	//获取人物当前位置
    	public void getManPosition(){
    		
    		for(int i=0;i<map.length;i++){
    			for(int j=0;j<map[0].length;j++){
    				if(map[i][j]==MANDOWN||map[i][j]==MANUP||map[i][j]==MANLEFT||map[i][j]==MANRIGHT){
    					row = i;
    					column = j;
    					break;
    				}			
    			}
    		}
    	}
    	
    	
    	//获取游戏区域大小及显示游戏的左上角位置
    	public void getMapSizeAndPosition(){
    		mapRow = map.length;
    		mapColumn = map[0].length;
    		leftX = (width - map[0].length * 30)/2;
    		leftY = (height - map.length * 30)/2;
    		System.out.println(leftX);
    		System.out.println(leftY);
    		System.out.println(mapRow);
    		System.out.println(mapColumn);
    	}
    	
    	//获取图片
    	public void getPic(){
    		pic = new Image[14];
    		for(int i=0;i<=13;i++){
    			pic[i] = Toolkit.getDefaultToolkit().getImage
    					("E:\\kkkkk\\\\推箱子\\\\src\\\\img/pic"+i+".png");
    		}
    	}
    	
    	//人走过变草地
    	public byte grassOrEnd(byte man){
    		byte result = GRASS;
    		if(man == MANLEFTONEND || man == MANRIGHTONEND || man == MANUPONEND || man == MANDOWNONEND){
    			result = END;
    		}
    		
    		return result;
    	}
    
    	private void moveUp(){
    		if(map[row-1][column]==WALL)
    			return;
    		byte tempBox;
    		byte tempMan;
    		
    		if(map[row-1][column]==BOX||map[row-1][column]==BOXONEND){		//如果向上一格是箱子
    			if(map[row-2][column]==GRASS||map[row-2][column]==END){     //如果向上第二格是过道或者终点
    				Map currentMap = new Map(row,column,map);
    				list.add(currentMap);//用于撤回操作
    				tempBox = map[row-2][column]==END?BOXONEND:BOX;
    				tempMan = map[row-1][column]==BOXONEND?MANUPONEND:MANUP;
    				map[row][column] = grassOrEnd(map[row][column]);
    				map[row-2][column] = tempBox;
    				map[row-1][column] = tempMan;
    				row--;
    			}
    		}else{//如果向上一格是过道或者终点
    			Map currentMap = new Map(row,column,map);
    			list.add(currentMap);//用于撤回操作
    			tempMan = map[row-1][column]==GRASS?MANUP:MANUPONEND;
    			map[row][column] = grassOrEnd(map[row][column]);
    			map[row-1][column] = tempMan;
    			row--;
    		}	
    	}
    	
    	private void moveDown(){
    	
    		if(map[row+1][column]==WALL)
    			return ;
    		
    		byte tempBox;
    		byte tempMan;
    		
    		if(map[row+1][column]==BOX||map[row+1][column]==BOXONEND){
    			if(map[row+2][column]==END||map[row+2][column]==GRASS){
    				Map currentMap = new Map(row,column,map);
    				list.add(currentMap);//用于撤回操作
    				tempBox = map[row+2][column] == END?BOXONEND:BOX;
    				tempMan = map[row+1][column] == BOXONEND?MANDOWNONEND:MANDOWN;
    				map[row][column] = grassOrEnd(map[row][column]);
    				map[row+2][column] = tempBox;
    				map[row+1][column] = tempMan;
    				row++;
    			}
    		}else{
    			Map currentMap = new Map(row,column,map);
    			list.add(currentMap);//用于撤回操作
    			tempMan = map[row+1][column]==GRASS?MANDOWN:MANDOWNONEND;
    			map[row][column] = grassOrEnd(map[row][column]);
    			map[row+1][column] = tempMan;
    			row++;
    		}		
    	}
    	
    	private void moveLeft(){
    		
    		if(map[row][column-1]==WALL)
    			return ;
    		
    		byte tempBox;
    		byte tempMan;
    		
    		if(map[row][column-1]==BOX||map[row][column-1]==BOXONEND){
    			if(map[row][column-2]==END||map[row][column-2]==GRASS){
    				Map currentMap = new Map(row,column,map);
    				list.add(currentMap);//用于撤回操作
    				tempBox = map[row][column-2] == END?BOXONEND:BOX;
    				tempMan = map[row][column-1] == BOXONEND?MANLEFTONEND:MANLEFT;
    				map[row][column] = grassOrEnd(map[row][column]);
    				map[row][column-2] = tempBox;
    				map[row][column-1] = tempMan;
    				column--;
    			}
    		}else{
    			Map currentMap = new Map(row,column,map);
    			list.add(currentMap);//用于撤回操作
    			tempMan = map[row][column-1]==GRASS?MANLEFT:MANLEFTONEND;
    			map[row][column] = grassOrEnd(map[row][column]);
    			map[row][column-1] = tempMan;
    			column--;
    		}		
    	}
    	
    	private void moveRight(){
    		
    		if(map[row][column+1]==WALL)
    			return ;
    		
    		byte tempBox;
    		byte tempMan;
    		
    		if(map[row][column+1]==BOX||map[row][column+1]==BOXONEND){
    			
    			if(map[row][column+2]==END||map[row][column+2]==GRASS){
    				
    				Map currentMap = new Map(row,column,map);
    				list.add(currentMap);//用于撤回操作
    				tempBox = map[row][column+2] == END?BOXONEND:BOX;
    				tempMan = map[row][column+1] == BOXONEND?MANRIGHTONEND:MANRIGHT;
    				map[row][column] = grassOrEnd(map[row][column]);
    				map[row][column+2] = tempBox;
    				map[row][column+1] = tempMan;
    				column++;
    			}
    		}else{
    			Map currentMap = new Map(row,column,map);
    			list.add(currentMap);//用于撤回操作
    			tempMan = map[row][column+1]==GRASS?MANRIGHT:MANRIGHTONEND;
    			map[row][column] = grassOrEnd(map[row][column]);
    			map[row][column+1] = tempMan;
    			column++;
    		}		
    	}
    	
    	//判断游戏是否结束
    	public boolean isFinished(){
    		
    		for(int i=0;i<mapRow;i++) {
    			for(int j=0;j<mapColumn;j++){
    	//		System.out.println("值是"+map[i][j]+",END 的值是"+END+",他们相等吗?:"+(map[i][j]==END));
    				if(map[i][j]==END||map[i][j]==MANDOWNONEND||map[i][j]==MANUPONEND||map[i][j]==MANLEFTONEND||map[i][j]==MANRIGHTONEND){
    					return false;
    				}
    			}
    		}
    		return true;
    	}	
    	
    	@Override
    	public void keyTyped(KeyEvent e) {
    		// TODO Auto-generated method stub		
    	}
    
    	@Override
    	public void keyPressed(KeyEvent e) {
    		if(e.getKeyCode() == KeyEvent.VK_UP){
    			moveUp();
    		}
    		if(e.getKeyCode() == KeyEvent.VK_DOWN){
    			moveDown();
    		}
    		if(e.getKeyCode() == KeyEvent.VK_LEFT){
    			moveLeft();
    		}
    		if(e.getKeyCode() == KeyEvent.VK_RIGHT){
    			moveRight();
    		}
    		if(e.getKeyCode() == KeyEvent.VK_A){//上一关
    			acceptKey = true;
    			priorGrade();
    			return ;
    		}
    		if(e.getKeyCode() == KeyEvent.VK_D){//下一关
    			acceptKey = true;
    			nextGrade();
    			return ;
    		}
    		if(e.getKeyCode() == KeyEvent.VK_B){//撤回
    			undo();
    		}
    		
    		repaint();
    		
    		if(isFinished()){
    			//禁用按键
    			acceptKey = false;
    			if(grade==10){JOptionPane.showMessageDialog(this, "恭喜通过最后一关");}
    			else{
    				String msg = "恭喜你通过第"+(grade+1)+"关!!!\n是否要进入下一关?";
    				int type = JOptionPane.YES_NO_OPTION;
    				String title = "过关";
    				int choice = 0;
    				choice = JOptionPane.showConfirmDialog(null, msg,title,type);
    				if(choice==1){
    					System.exit(0);
    				}else if(choice == 0){
    					acceptKey = true;
    					nextGrade();
    				}
    			}
    		}
    		
    	}
    	
    	//画图
    	public void paint(Graphics g){
    		//System.out.println("我被调用了");
    		for(int i=0;i<mapRow;i++) {
    			for(int j=0;j<mapColumn;j++){
    				if(map[i][j]!=0){
    //					System.out.println("这个位置 不是0,它的值是"+map[i][j]);
    //					g.drawRect(10, 30, getWidth()/2-50, getHeight()/2-50);
    					g.drawImage(pic[map[i][j]],leftX+j*30,leftY+i*30,30,30,this);
    			}
    		}
    	}
    		g.setColor(Color.black);
    		g.setFont(new Font("楷体_2312",Font.BOLD,30));
    		g.drawString("现在是第", 150, 140);
    		g.drawString(String.valueOf(grade+1),310,140);
    		g.drawString("关", 360, 140);
    	}
    	
    	public int getManX(){
    		return row;
    	}
    	
    	public int getManY(){
    		return column;
    	}
    	
    	public int getGrade(){
    		return grade;
    	}
    	
    	public byte[][] getMap(int grade){		
    		return MapFactory.getMap(grade);
    	}
    	
    	public void DisplayToast(String str){
    		JOptionPane.showMessageDialog(null, str,"提示",JOptionPane.ERROR_MESSAGE);
    	}
    	
    	//过关后不可撤销
    	public void undo(){
    		if(acceptKey){
    			if(list.size()>0){
    				Map priorMap = (Map)list.get(list.size()-1);
    				map = priorMap.getMap();
    				row = priorMap.getManX();
    				column = priorMap.getManY();
    				repaint();
    				list.remove(list.size()-1);
    			}else{
    				DisplayToast("不能再撤销");
    			}
    		}else{
    			DisplayToast("此关已完成,不能撤销");
    		}
    	}
    	
    	public void priorGrade(){
    		grade--;
    		acceptKey = true;
    		if(grade<0)
    			grade = 0;
    		initMap();
    		clearPaint(this.getGraphics());
    		repaint();
    	}
    	
    	public void nextGrade(){
    		if(grade>=MapFactory.getCount()-1){
    			DisplayToast("恭喜你完成所有关卡");
    			acceptKey = false;
    		}else{
    			grade++;
    			initMap();
    			clearPaint(this.getGraphics());
    			repaint();
    			acceptKey = true;
    		}
    	}
    	
    	private void clearPaint(Graphics g) {
    	 g.clearRect(0, 0, width+leftX, height+leftY);
    	}
    
    	@Override
    	public void keyReleased(KeyEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void mouseClicked(MouseEvent e) {
    		// TODO Auto-generated method stub
    		if(e.getButton() == MouseEvent.BUTTON3)
    		{
    			undo();
    		}
    	}
    
    	@Override
    	public void mousePressed(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void mouseReleased(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void mouseEntered(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void mouseExited(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    	
    	//主方法
    	public static void main(String[] args) throws InterruptedException{
    		GameFrame a = new GameFrame();
    		Sound player = new Sound("E:/kkkkk/推箱子/src/img/XiongDi.wav");   //创建音乐播放器
            
            player.start(true);//以开始以循环的形式播放,player(false)为不循环播放
            
            TimeUnit.SECONDS.sleep(5);
            
            player.stop();                        //暂停播放音频
            
            TimeUnit.SECONDS.sleep(4);
            
            player.continues();                //继续开始播放音频
    	}	
    }
    
    

    2. Sound类

    package 推箱子;
    /**
    *
    * Description: 简易音频播放器(只支持AU,RA,WAV)
    * 			   在不使用JMF的情况下快速实现音频播放
    * 
    */
    import javax.sound.sampled.*;
    import java.io.*;
    import java.util.concurrent.TimeUnit;
     
    public class Sound {
    	private String musicPath; //音频文件
    	private volatile boolean run = true;  //记录音频是否播放
    	private Thread mainThread;   //播放音频的任务线程	
    	private AudioInputStream audioStream;
    	private AudioFormat audioFormat;
    	private SourceDataLine sourceDataLine;
    	
    	public Sound(String musicPath) {
    		this.musicPath = musicPath;
    		prefetch();
    	}
    	
    	//数据准备
    	private void prefetch(){
    		try{
    		//获取音频输入流
    	    audioStream = AudioSystem.getAudioInputStream(new File(musicPath));
    		//获取音频的编码对象
    		audioFormat = audioStream.getFormat();
    		//包装音频信息
    		DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class,
    				audioFormat,AudioSystem.NOT_SPECIFIED);
    		//使用包装音频信息后的Info类创建源数据行,充当混频器的源
    		sourceDataLine = (SourceDataLine)AudioSystem.getLine(dataLineInfo);		
    		sourceDataLine.open(audioFormat);
    		sourceDataLine.start();		
    		}catch(UnsupportedAudioFileException ex){
    			ex.printStackTrace();
    		}catch(LineUnavailableException ex){
    			ex.printStackTrace();
    		}catch(IOException ex){
    			ex.printStackTrace();
    		}		
    	}
    	//析构函数:关闭音频读取流和数据行
    	protected void finalize() throws Throwable{
    		super.finalize();
    		sourceDataLine.drain();
    		sourceDataLine.close();
    		audioStream.close();
    	}
    	
    	//播放音频:通过loop参数设置是否循环播放
    	private void playMusic(boolean loop)throws InterruptedException {
    		try{
    				if(loop){
    					while(true){
    						playMusic();
    					}
    				}else{
    					playMusic();
    					//清空数据行并关闭
    					sourceDataLine.drain();
    					sourceDataLine.close();
    					audioStream.close();
    				}			
    		}catch(IOException ex){
    			ex.printStackTrace();
    		}				
    	}
    	private void playMusic(){
    		try{
    			synchronized(this){
    				run = true;
    			}
    			//通过数据行读取音频数据流,发送到混音器;
    			//数据流传输过程:AudioInputStream -> SourceDataLine;
    			audioStream = AudioSystem.getAudioInputStream(new File(musicPath));
    			int count;
    			byte tempBuff[] = new byte[1024];			
    				while((count = audioStream.read(tempBuff,0,tempBuff.length)) != -1){
    					synchronized(this){
    					while(!run)
    						wait();
    					}
    					sourceDataLine.write(tempBuff,0,count);							
    			} 
    		}catch(UnsupportedAudioFileException ex){
    			ex.printStackTrace();
    		}catch(IOException ex){
    			ex.printStackTrace();
    		}catch(InterruptedException ex){
    			ex.printStackTrace();
    		}		
    	}	
    	
    	//暂停播放音频
    	private void stopMusic(){
    		synchronized(this){
    			run = false;
    			notifyAll();
    		}
    	}
    	//继续播放音乐
    	private void continueMusic(){
    		synchronized(this){
    			 run = true;
    			 notifyAll();
    		}
    	}	
    	
    	//外部调用控制方法:生成音频主线程;
    	public void start(boolean loop){
    		mainThread = new Thread(new Runnable(){
    			public void run(){
    				try {
    					playMusic(loop);
    				} catch (InterruptedException e) {
    					e.printStackTrace();
    				}
    			}
    		});
    		mainThread.start();
    	}
    	
    	//外部调用控制方法:暂停音频线程
    	public void stop(){
    		new Thread(new Runnable(){
    			public void run(){
    				stopMusic();			
    			}
    		}).start();
    	}
    	//外部调用控制方法:继续音频线程
    	public void continues(){
    		new Thread(new Runnable(){
    			public void run(){
    				continueMusic();
    			}
    		}).start();
    	} 
     }
    

    修改后就可以自己修改喜欢的背景音乐了

    展开全文
  • 本游戏是简易版的基于java推箱子小游戏,只是简易的一关配有图形化界面,希望对大家有所帮助
  • Java推箱子游戏代码

    2018-06-03 14:40:55
    Java推箱子游戏源代码,完成课程设时计写的一个小游戏
  • Java实现推箱子小游戏

    千次阅读 2021-06-12 14:05:48
    Java实现推箱子小游戏 一、游戏设定 这是一个出售消息的店铺,每天都会有形形色色的客人前来购买消息,当传送消息的精灵把所有的消息放在指定的位置,客人就会得到她想要的信息。 二、需求分析 1.用户登录界面 玩家...
  • Java推箱子小游戏

    2014-01-01 21:31:41
    一个由Java编写的简单推箱子小游戏。 游戏由障碍、空地、箱子、终点与玩家组成。 通过上下左右控制玩家推动箱子。当箱子的推动方向没有障碍时,向前移动到新的位置,玩家也向前移动一步。 当所有箱子都处于终点时,...
  • Java推箱子游戏源码

    2018-05-07 13:53:34
    使用JAVA做的一个推箱子小游戏,比较有趣,可以作为入门学习。
  • Java推箱子

    2019-02-22 15:26:01
    Java实现的推箱子小游戏,运用了JavaSwing和XML技术
  • java开发的推箱子小游戏,100关,内含java代码,适合新手学习。本人新手,代码写的一般般,希望大家多多指教。
  • 基于JAVA推箱子小游戏(JAVA GUI)

    千次阅读 2021-07-28 23:08:14
    基于JAVA推箱子小游戏(JAVA GUI) 话不多说,直接贴代码吧 是刚开始学JAVA写的,可能很简陋,可供初学者学习 功能就是简单的推箱子,多线程播放背景音乐,可以自行编辑地图,地图不是写死的,每关地图都是通过读取...
  • Android下的推箱子游戏源码分享 代码介绍: 一个推子游戏,SurfaceView下作的,没有用到系统控件,所以显示效果不是很理想。音效用MediaPlayer作的,音效播放不流畅,我也因此被迫将按键音功能给屏避掉了。 ...
  • Java毕业设计——基于JAVA推箱子游戏(论文+答辩PPT+源代码+数据库).zip
  • java实现推箱子小游戏

    2020-08-19 04:37:20
    主要为大家详细介绍了java实现推箱子小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
  • 利用Android studio进行制作的推箱子小游戏,个人创做的神奇宝贝风格(指图标界面等),可控制关卡
  • importjava.awt.Button;importjava.awt.Color;importjava.awt.Label;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.KeyEvent;importjava.awt.event.Key...
  • 本文实例为大家分享了Java实现简单推箱子游戏的具体代码,供大家参考,具体内容如下*编写一个简易的推箱子游戏,使用10*8的二维字符数据表示游戏画面,H表示墙壁;&表示玩家角色: o表示箱子,表示目的地。玩家...
  • 基于JAVA推箱子游戏设计软件程序源码+word毕业论文文档. 基于JAVA推箱子游戏系统主要用于实现游戏推箱子功能,提供多种不同难度的关卡。本系统结构如下: (1)初始化模块: 该模块包括屏幕初始化和游戏第一关的...
  • 推箱子游戏java代码

    2018-07-12 11:05:22
    推箱子游戏java代码,适合新手查看,有问题请留言,关卡设置有趣
  • android studio开发 推箱子小游戏
  • java简单推箱子小游戏

    2018-11-11 17:38:29
    纯属自己写的代码,不是网上找的,全网仅此一份,推荐给java的新手
  • 基于JAVA推箱子游戏毕业论文+java源码文件+视频说明,基于JAVA推箱子游戏系统主要用于实现游戏推箱子功能,提供多种不同难度的关卡。本系统结构如下: (1)初始化模块: 该模块包括屏幕初始化和游戏第一关的初始...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 1,044
精华内容 417
关键字:

java推箱子小游戏代码