精华内容
下载资源
问答
  • Java推箱子游戏代码

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

    2013-01-10 16:05:40
    推箱子小游戏是用java语言编写的 实现的推箱子小游戏的基本功能
  • java推箱子游戏代码

    2011-04-02 12:04:03
    利用java.jsp和sql实现的一个小游戏
  • 推箱子小游戏是由java编写 基本实习了推箱子小游戏的简单功能 朋友们可以试着扩展一下功能。
  • java实现推箱子小游戏

    2020-08-19 04:37:20
    主要为大家详细介绍了java实现推箱子小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
  • java开发的推箱子小游戏,100关,内含java代码,适合新手学习。本人新手,代码写的一般般,希望大家多多指教。
  • JAVA 推箱子小游戏

    千次阅读 2018-05-05 18:20:45
    示例代码:1.Frame.java2.Run.java3.GPoint.java4.PointManger.java5.GameTools.java6.GamePanel.java7.Box.java8.BoxManger.java9.Player.java10.Map.java

    示例代码:

    1.Frame.java



    2.Run.java



    3.GPoint.java



    4.PointManger.java



    5.GameTools.java



    6.GamePanel.java




    7.Box.java



    8.BoxManger.java



    9.Player.java



    10.Map.java









    展开全文
  • java简单的推箱子小游戏,可以下载学习一下 java简单的推箱子小游戏,可以下载学习一下 java简单的推箱子小游戏,可以下载学习一下 java简单的推箱子小游戏,可以下载学习一下
  • java写的推箱子程序,源代码在jar文件中。
  • java实现推箱子小游戏(附源码)

    万次阅读 多人点赞 2020-10-09 16:01:13
    可以通过AWSD进行移动和推箱子 自己弄出来的代码玩起来还是很有意思的。 代码一共是三个.java文件,代码内容如下所示 package ss; import java.awt.Graphics; import java.awt.Image; import java.awt.Point;...

    先上效果图

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

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

    代码一共是三个.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
    		
    	}
    	
    }
    

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

    本文地址:https://ai.52learn.online/1785

    展开全文
  • Java推箱子小游戏

    2014-01-01 21:31:41
    一个由Java编写的简单推箱子小游戏。 游戏由障碍、空地、箱子、终点与玩家组成。 通过上下左右控制玩家推动箱子。当箱子的推动方向没有障碍时,向前移动到新的位置,玩家也向前移动一步。 当所有箱子都处于终点时,...
  • 本游戏是简易版的基于java推箱子小游戏,只是简易的一关配有图形化界面,希望对大家有所帮助
  • Java语言实现的推箱子小游戏,附实验源码+背景图片+背景音乐+地图二进制代码,带有计步器,还有退一步以及选关等功能,功能完善,直接可用。实现方式简单易懂,带有详细注释,下载绝不会后悔系列
  • java简单推箱子小游戏

    2018-11-11 17:38:29
    纯属自己写的代码,不是网上找的,全网仅此一份,推荐给java的新手
  • JAVA实现推箱子小游戏

    万次阅读 多人点赞 2019-08-21 16:57:15
    我们做的是 “灰太狼推箱子” 的小游戏 首先准备需要的图片: 然后放入二进制文件(所谓的地图) 当然music是必不可少的 目录: 话不多说直接上代码: package com.xhh.xyx; import javax.swing.*; import ...

    我们做的是 “灰太狼推箱子” 的小游戏

    首先准备需要的图片:
    在这里插入图片描述
    然后放入二进制文件(所谓的地图)
    在这里插入图片描述
    在这里插入图片描述
    当然music是必不可少的
    在这里插入图片描述
    目录:
    在这里插入图片描述
    话不多说直接上代码:

    package com.xhh.xyx;
    
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.io.*;
    import javax.sound.midi.*;
    import java.util.Stack;
    public class Tuixiangzi
    {
    public static void main(String[] args)
    	{
    		new mainFrame();
    	}
    }
    class mainFrame extends JFrame implements ActionListener,ItemListener
    {
    	JLabel lb;
    	JLabel lb2;
    	JButton btnrenew,btnlast,btnnext,btnchoose,btnfirst,btnover,btnmuc,btnback;
    	mainpanel panel;
    	Sound sound;
    	JComboBox jc=new JComboBox();
    	MenuItem renew=new MenuItem("重新开始");
    	MenuItem back=new MenuItem("悔一步");
    	MenuItem last=new MenuItem("上一关");
    	MenuItem next=new MenuItem("下一关");
    	MenuItem choose=new MenuItem("选关");
    	MenuItem exit=new MenuItem("退出");
    	MenuItem qin=new MenuItem("琴萧合奏");
    	MenuItem po=new MenuItem("泡泡堂");
    	MenuItem guang=new MenuItem("灌篮高手");
    	MenuItem nor=new MenuItem("默认");
    	MenuItem eye=new MenuItem("eyes on me");
    	MenuItem about=new MenuItem("关于推箱子...");
    	mainFrame()
    	{
    		super("推箱子2.0");
    		setSize(720,720);
    		setVisible(true);
    		setResizable(false);
    		setLocation(300,20);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		Container cont=getContentPane();
    		cont.setLayout(null);
    		cont.setBackground(Color.black);
    		Menu choice=new Menu("选项");
    		choice.add(renew);
    		choice.add(last);
    		choice.add(next);
    		choice.add(choose);
    		choice.add(back);
    		choice.addSeparator();
    		choice.add(exit);
    		renew.addActionListener(this);
    		last.addActionListener(this);
    		next.addActionListener(this);
    		choose.addActionListener(this);
    		exit.addActionListener(this);
    		back.addActionListener(this);
    		Menu setmuc=new Menu("设置音乐");
    		setmuc.add(nor);
    		setmuc.add(qin);
    		setmuc.add(po);
    		setmuc.add(guang);
    		setmuc.add(eye);
    		nor.addActionListener(this);
    		qin.addActionListener(this);
    		po.addActionListener(this);
    		guang.addActionListener(this);
    		eye.addActionListener(this);
    		Menu help=new Menu("帮助");
    		help.add(about);
    		about.addActionListener(this);
    		MenuBar bar=new MenuBar();
    		bar.add(choice);bar.add(setmuc);bar.add(help);
    		setMenuBar(bar);                                        
    
    		nor.setEnabled(false);
    		lb=new JLabel("灰太狼古堡推箱子!",SwingConstants.CENTER);
    		lb2=new JLabel("更换音乐",SwingConstants.CENTER);
    		add(lb);add(lb2);
    		lb.setBounds(100,20,400,20);
    		lb.setForeground(Color.white);
    		lb2.setBounds(625,500,55,20);
    		lb2.setForeground(Color.white);
    		btnrenew=new JButton("重玩");
    		btnback=new JButton("悔一步");
    		btnlast=new JButton("上一关");
    		btnnext=new JButton("下一关");
    		btnchoose=new JButton("选关");
    		btnfirst=new JButton("第1关");
    		btnover=new JButton("最终关");
    		btnmuc=new JButton("音乐关");
    		add(btnrenew);
    		add(btnlast);
    		add(btnnext);
    		add(btnchoose);
    		add(btnfirst);
    		add(btnover);
    		add(btnmuc);
    		add(btnback);
    		btnrenew.setBounds(625,100,80,30);
    		btnrenew.addActionListener(this);
    		btnback.setBounds(625,150,80,30);
    		btnback.addActionListener(this);
    		btnfirst.setBounds(625,200,80,30);
    		btnfirst.addActionListener(this);
    		btnlast.setBounds(625,250,80,30);
    		btnlast.addActionListener(this);
    		btnnext.setBounds(625,300,80,30);
    		btnnext.addActionListener(this);
    		btnover.setBounds(625,350,80,30);
    		btnover.addActionListener(this);
    		btnchoose.setBounds(625,400,80,30);
    		btnchoose.addActionListener(this);
    		btnmuc.setBounds(625,450,80,30);
    		btnmuc.addActionListener(this);
    		jc.setBounds(625,530,80,20);
    		jc.addItem("默认");
    		jc.addItem("琴萧合奏");
    		jc.addItem("泡泡堂");
    		jc.addItem("灌篮高手");
    		jc.addItem("eyes on me");
    		jc.addItemListener(this);
    		cont.add(jc);
    		sound=new Sound();
    		sound.loadSound();
    		panel=new mainpanel();
    		add(panel);
    		panel.Tuixiangzi(panel.level);
    		panel.requestFocus();
    		validate();
    	}
    	
    	public void actionPerformed(ActionEvent e)
    	{
    		if(e.getSource()==btnrenew||e.getSource()==renew)
    		{
    			panel.Tuixiangzi(panel.level);
    			panel.requestFocus();
    			panel.remove();
    		}
    		else if(e.getSource()==btnlast||e.getSource()==last)
    		{
    			panel.level--;
    			if(panel.level<1)
    			{panel.level++;
    			JOptionPane.showMessageDialog(this,"本关是第一关");
    			panel.requestFocus();}
    			else 
    			{
    				panel.Tuixiangzi(panel.level);
    				panel.requestFocus();
    			}
    			panel.remove();
    		}
    		else if(e.getSource()==btnnext||e.getSource()==next)
    		{
    			panel.level++;
    			if(panel.level>panel.maxlevel())
    			{panel.level--;
    			JOptionPane.showMessageDialog(this,"本关已是最后一关");
    			panel.requestFocus();}
    			else 
    			{
    				panel.Tuixiangzi(panel.level);
    				panel.requestFocus();
    			}
    			panel.remove();
    		}
    		else if(e.getSource()==exit)System.exit(0);
    		else if(e.getSource()==about)
    		{
    			JOptionPane.showMessageDialog(this, "JAVA推箱子游戏!(version 3.0)");
    		}
    		else if(e.getSource()==btnchoose||e.getSource()==choose)
    		{
    			String lel=JOptionPane.showInputDialog(this,"请输入您要转到的关卡号:(1~50)");
    			panel.level=Integer.parseInt(lel);
    			panel.remove();
    			if(panel.level>panel.maxlevel()||panel.level<1)
    			{JOptionPane.showMessageDialog(this, "没有这一关!!!");panel.requestFocus();}
    			else
    				{
    				panel.Tuixiangzi(panel.level);
    				panel.requestFocus();
    				}
    		}
    		
    		else if(e.getSource()==btnfirst)
    		{
    			panel.level=1;
    			panel.Tuixiangzi(panel.level);
    			panel.requestFocus();
    			panel.remove();
    		}
    		else if(e.getSource()==btnover)
    		{
    			panel.level=panel.maxlevel();
    			panel.Tuixiangzi(panel.level);
    			panel.requestFocus();
    			panel.remove();
    		}
    		else if(e.getSource()==btnmuc)
    		{
    			if(sound.isplay())
    			{
    				sound.mystop();
    				btnmuc.setLabel("音乐开");
    			}
    			else 
    			{
    				sound.loadSound();
    				btnmuc.setLabel("音乐关");
    			}
    			panel.requestFocus();
    		}
    		else if(e.getSource()==btnback||e.getSource()==back)
    		{
    			if(panel.isMystackEmpty())JOptionPane.showMessageDialog(this, "您还未移动!!!");
    			else
    			{
    				switch(panel.back())
    				{
    					case 10:panel.backup(10);break;
    					case 11:panel.backup(11);break;
    					case 20:panel.backdown(20);break;
    					case 21:panel.backdown(21);break;
    					case 30:panel.backleft(30);break;
    					case 31:panel.backleft(31);break;
    					case 40:panel.backright(40);break;
    					case 41:panel.backright(41);break;
    				}
    			}
    			panel.requestFocus();
    		}
    		else if(e.getSource()==nor)
    		{
    			jc.setSelectedIndex(0);
    		}
    		
    		else if(e.getSource()==qin)
    		{
    			jc.setSelectedIndex(1);
    		}
    		else if(e.getSource()==guang)
    		{
    			
    			jc.setSelectedIndex(3);
    			
    		}
    		else if(e.getSource()==eye)
    		{
    			jc.setSelectedIndex(4);
    		}
    		else if(e.getSource()==po)
    		{
    			jc.setSelectedIndex(2);
    		}
    	}
    	public void itemStateChanged(ItemEvent ie)
    	{
    		int no=jc.getSelectedIndex();
    		switch(no)
    		{
    			case 0:sound.setMusic("nor.mid");
    					 if(sound.isplay())
    					 sound.mystop();
    					 sound.loadSound();
    					 btnmuc.setLabel("音乐关");
    					 nor.setEnabled(false);
    					 qin.setEnabled(true);
    					 guang.setEnabled(true);
    					 eye.setEnabled(true);
    					 po.setEnabled(true);panel.requestFocus();break;
    			case 1:sound.setMusic("qin.mid");
    					 if(sound.isplay())
    					 sound.mystop();
    					 sound.loadSound();
    					 btnmuc.setLabel("音乐关");
    					 nor.setEnabled(true);
    					 qin.setEnabled(false);
    					 guang.setEnabled(true);
    					 eye.setEnabled(true);
    					 po.setEnabled(true);panel.requestFocus();break;
    			case 2:sound.setMusic("popo.mid");
    					if(sound.isplay())
    					sound.mystop();
    					sound.loadSound();
    					btnmuc.setLabel("音乐关");
    					nor.setEnabled(true);
    					qin.setEnabled(true);
    					guang.setEnabled(true);
    					eye.setEnabled(true);
    					po.setEnabled(false);panel.requestFocus();break;
    			case 3:sound.setMusic("guang.mid");
    					if(sound.isplay())
    					sound.mystop();
    					sound.loadSound();
    					btnmuc.setLabel("音乐关");
    					nor.setEnabled(true);
    					qin.setEnabled(true);
    					guang.setEnabled(false);
    					eye.setEnabled(true);
    					po.setEnabled(true);panel.requestFocus();break;
    			case 4:sound.setMusic("eyes on me.mid");
    					if(sound.isplay())
    					sound.mystop();
    					sound.loadSound();
    					btnmuc.setLabel("音乐关");
    					nor.setEnabled(true);
    					qin.setEnabled(true);
    					guang.setEnabled(true);
    					eye.setEnabled(false);
    					po.setEnabled(true);panel.requestFocus();break;
    		}
    	}
    }
    
    class mainpanel extends JPanel implements KeyListener
    {
    	int max=50;
    	int[][] map,maptmp;
    	int manX,manY,boxnum;
    	Image[] myImage;
    	Readmap Levelmap;
    	Readmap Levelmaptmp;
    	int len=30;
    	public int level=1;
    	Stack mystack=new Stack();
    	mainpanel()
    	{	
    		setBounds(15,50,600,600);
    		setBackground(Color.white);
    		addKeyListener(this);
    		myImage=new Image[10];
    		for(int i=0; i<10; i++)
    		{
    		    myImage[i] = Toolkit.getDefaultToolkit().getImage("pic\\"+i+".gif");
    		}
    		
    		setVisible(true);
    	}
    
    	void Tuixiangzi(int i)
    	{
    		Levelmap=new Readmap(i);
    		Levelmaptmp=new Readmap(i);
    		map=Levelmap.getmap();
    		manX=Levelmap.getmanX();
    		manY=Levelmap.getmanY();
    		maptmp=Levelmaptmp.getmap();
    		repaint();
    	}
    	int maxlevel(){return max;}
    
    	public void paint(Graphics g)
    	{
    		for(int i=0; i<20; i++)
    			for(int j=0; j<20; j++)
    		    {
    			    g.drawImage(myImage[map[j][i]],i*len,j*len,this);
    			}		
    		g.setColor(new Color(0,0,0));
    		g.setFont(new Font("楷体_2312",Font.BOLD,30));
    		g.drawString("第",240,40);
    		g.drawString(String.valueOf(level),310,40);
    		g.drawString("层",360,40);
    	}
    
    	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(iswin())
    		{
    			if(level==max){JOptionPane.showMessageDialog(this, "恭喜您通过最后一关!!!");}
    			else
    			{
    				String msg="恭喜您通过第"+level+"关!!!\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)
    				{
    					level++;
    					Tuixiangzi(level);
    				}
    			}
    			mystack.removeAllElements();
    		}
    	}
    	public void keyTyped(KeyEvent e){}
    	public void keyReleased(KeyEvent e){}
    
    	boolean isMystackEmpty(){return mystack.isEmpty();}
    	
    	int  back(){return (Integer)mystack.pop();}
    
    	void remove(){mystack.removeAllElements();}
    	
    	void moveup()
    	{
    		if(map[manY-1][manX]==2||map[manY-1][manX]==4)
    		{
    			if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)
    				map[manY][manX]=4;
    			else map[manY][manX]=2;
    			map[manY-1][manX]=8;
    			repaint();manY--;mystack.push(10);
    		}
    		else if(map[manY-1][manX]==3)
    		{
    			if(map[manY-2][manX]==4)
    			{
    				if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)
    					map[manY][manX]=4;
    				else map[manY][manX]=2;
    				map[manY-1][manX]=8;
    				map[manY-2][manX]=9;
    				repaint();manY--;mystack.push(11);
    			}
    			else if(map[manY-2][manX]==2)
    			{
    				if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)
    					map[manY][manX]=4;
    				else map[manY][manX]=2;
    				map[manY-1][manX]=8;
    				map[manY-2][manX]=3;
    				repaint();manY--;mystack.push(11);
    			}
    			else {map[manY][manX]=8;repaint();}
    		}
    		else if(map[manY-1][manX]==9)
    		{
    			if(map[manY-2][manX]==4)
    			{
    				if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)
    					map[manY][manX]=4;
    				else map[manY][manX]=2;
    				map[manY-1][manX]=8;
    				map[manY-2][manX]=9;
    				repaint();manY--;mystack.push(11);
    			}
    			else if(map[manY-2][manX]==2)
    			{
    				if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)
    					map[manY][manX]=4;
    				else map[manY][manX]=2;
    				map[manY-1][manX]=8;
    				map[manY-2][manX]=3;
    				repaint();manY--;mystack.push(11);
    			}
    			else {map[manY][manX]=8;repaint();}
    		}
    		if(map[manY-1][manX]==1)
    		{
    			map[manY][manX]=8;repaint();
    		}
    	}
    	
    	void backup(int t)
    	{
    		int n=t;
    		if(n==10)
    		{
    			if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)
    			{
    				map[manY][manX]=4;
    			}
    			else map[manY][manX]=2;
    		}
    		else if(n==11)
    		{
    			if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)
    			{
    				map[manY][manX]=9;
    			}
    			else map[manY][manX]=3;
    			if(maptmp[manY-1][manX]==4||maptmp[manY-1][manX]==9)
    			{
    				map[manY-1][manX]=4;
    			}
    			else map[manY-1][manX]=2;
    		}
    		map[manY+1][manX]=8;
    		repaint();manY++;
    	}
    
    	void movedown()
    	{
    		if(map[manY+1][manX]==2||map[manY+1][manX]==4)
    		{
    			if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)
    				map[manY][manX]=4;
    			else map[manY][manX]=2;
    			map[manY+1][manX]=5;
    			repaint();manY++;mystack.push(20);
    		}
    		else if(map[manY+1][manX]==3)
    		{
    			if(map[manY+2][manX]==4)
    			{
    				if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)
    					map[manY][manX]=4;
    				else map[manY][manX]=2;
    				map[manY+1][manX]=5;
    				map[manY+2][manX]=9;
    				repaint();manY++;mystack.push(21);
    			}
    			else if(map[manY+2][manX]==2)
    			{
    				if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)
    					map[manY][manX]=4;
    				else map[manY][manX]=2;
    				map[manY+1][manX]=5;
    				map[manY+2][manX]=3;
    				repaint();manY++;mystack.push(21);
    			}
    			else {map[manY][manX]=5;repaint();}
    		}
    		else if(map[manY+1][manX]==9)
    		{
    			if(map[manY+2][manX]==4)
    			{
    				if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)
    					map[manY][manX]=4;
    				else map[manY][manX]=2;
    				map[manY+1][manX]=5;
    				map[manY+2][manX]=9;
    				repaint();manY++;mystack.push(21);
    			}
    			else if(map[manY+2][manX]==2)
    			{
    				if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)
    					map[manY][manX]=4;
    				else map[manY][manX]=2;
    				map[manY+1][manX]=5;
    				map[manY+2][manX]=3;
    				repaint();manY++;mystack.push(21);
    			}
    			else {map[manY][manX]=5;repaint();}
    		}
    		else if(map[manY+1][manX]==1)
    		{
    			map[manY][manX]=5;repaint();
    		}
    	}
    
    	void backdown(int t)
    	{
    		int n=t;
    		if(n==20)
    		{
    			if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)
    			{
    				map[manY][manX]=4;
    			}
    			else map[manY][manX]=2;
    		}
    		else if(n==21)
    		{
    			if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)
    			{
    				map[manY][manX]=9;
    			}
    			else map[manY][manX]=3;
    			if(maptmp[manY+1][manX]==4||maptmp[manY+1][manX]==9)
    			{
    				map[manY+1][manX]=4;
    			}
    			else map[manY+1][manX]=2;
    		}
    		map[manY-1][manX]=5;
    		repaint();manY--;
    	}
    
    	void moveleft()
    	{
    		if(map[manY][manX-1]==2||map[manY][manX-1]==4)
    		{
    			if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)
    				map[manY][manX]=4;
    			else map[manY][manX]=2;
    			map[manY][manX-1]=6;			
    			repaint();manX--;mystack.push(30);
    		}
    		else if(map[manY][manX-1]==3)
    		{
    			if(map[manY][manX-2]==4)
    			{
    				if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)
    					map[manY][manX]=4;
    				else map[manY][manX]=2;
    				map[manY][manX-1]=6;
    				map[manY][manX-2]=9;
    				repaint();manX--;mystack.push(31);
    			}
    			else if(map[manY][manX-2]==2)
    			{
    				if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)
    					map[manY][manX]=4;
    				else map[manY][manX]=2;
    				map[manY][manX-1]=6;
    				map[manY][manX-2]=3;
    				repaint();manX--;mystack.push(31);
    			}
    			else {map[manY][manX]=6;repaint();}
    		}
    		else if(map[manY][manX-1]==9)
    		{
    			if(map[manY][manX-2]==4)
    			{
    				if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)
    					map[manY][manX]=4;
    				else map[manY][manX]=2;
    				map[manY][manX-1]=6;
    				map[manY][manX-2]=9;
    				repaint();manX--;mystack.push(31);
    			}
    			else if(map[manY][manX-2]==2)
    			{
    				if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)
    					map[manY][manX]=4;
    				else map[manY][manX]=2;
    				map[manY][manX-1]=6;
    				map[manY][manX-2]=3;
    				repaint();manX--;mystack.push(31);
    			}
    			else {map[manY][manX]=6;repaint();}
    		}
    		else if(map[manY][manX-1]==1)
    		{
    			map[manY][manX]=6;repaint();
    		}
    	}
    
    	void backleft(int t)
    	{
    		int n=t;
    		if(n==30)
    		{
    			if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)
    			{
    				map[manY][manX]=4;
    			}
    			else map[manY][manX]=2;
    		}
    		else if(n==31)
    		{
    			if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)
    			{
    				map[manY][manX]=9;
    			}
    			else map[manY][manX]=3;
    			if(maptmp[manY][manX-1]==4||maptmp[manY][manX-1]==9)
    			{
    				map[manY][manX-1]=4;
    			}
    			else map[manY][manX-1]=2;
    		}
    		map[manY][manX+1]=6;
    		repaint();manX++;
    	}
    
    	void moveright()
    	{
    		if(map[manY][manX+1]==2||map[manY][manX+1]==4)
    		{			
    			if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)
    				map[manY][manX]=4;
    			else map[manY][manX]=2;
    			map[manY][manX+1]=7;			
    			repaint();manX++;mystack.push(40);
    		}
    		else if(map[manY][manX+1]==3)
    		{
    			if(map[manY][manX+2]==4)
    			{
    				if(maptmp[manY][manX]==4)
    					map[manY][manX]=4;
    				else map[manY][manX]=2;
    				map[manY][manX+1]=7;
    				map[manY][manX+2]=9;
    				repaint();manX++;mystack.push(41);
    			}
    			else if(map[manY][manX+2]==2)
    			{
    				if(maptmp[manY][manX]==4)
    					map[manY][manX]=4;
    				else map[manY][manX]=2;
    				map[manY][manX+1]=7;
    				map[manY][manX+2]=3;
    				repaint();manX++;mystack.push(41);
    			}
    			else {map[manY][manX]=7;repaint();}
    		}
    		else if(map[manY][manX+1]==9)
    		{
    			if(map[manY][manX+2]==4)
    			{
    				if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)
    					map[manY][manX]=4;
    				else map[manY][manX]=2;
    				map[manY][manX+1]=7;
    				map[manY][manX+2]=9;
    				repaint();manX++;mystack.push(41);
    			}
    			else if(map[manY][manX+2]==2)
    			{
    				if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)
    					map[manY][manX]=4;
    				else map[manY][manX]=2;
    				map[manY][manX+1]=7;
    				map[manY][manX+2]=3;
    				repaint();manX++;mystack.push(41);
    			}
    			else {map[manY][manX]=7;repaint();}
    		}
    		else if(map[manY][manX+1]==1)
    		{
    			map[manY][manX]=7;repaint();
    		}
    	}
    
    	void backright(int t)
    	{
    		int n=t;
    		if(n==40)
    		{
    			if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)
    			{
    				map[manY][manX]=4;
    			}
    			else map[manY][manX]=2;
    		}
    		else if(n==41)
    		{
    			if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)
    			{
    				map[manY][manX]=9;
    			}
    			else map[manY][manX]=3;
    			if(maptmp[manY][manX+1]==4||maptmp[manY][manX+1]==9)
    			{
    				map[manY][manX+1]=4;
    			}
    			else map[manY][manX+1]=2;
    		}
    		map[manY][manX-1]=7;
    		repaint();manX--;
    	}
    
    	boolean iswin()
    	{
    		boolean num=false;
    		out:for(int i=0; i<20; i++)
    			for(int j=0; j<20; j++)
    		{
    			if(maptmp[i][j]==4||maptmp[i][j]==9)
    				if(map[i][j]==9)num=true;
    			    else {num=false;break out;}
    		}
    		return num;
    	}
    }
    
    class Sound
    {
    	String path=new String("musics\\");
    	String  file=new String("nor.mid");
    	Sequence seq;
        Sequencer midi;
    	boolean sign;
    	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 (Exception ex) {ex.printStackTrace();}
    		sign=true;
    	}
    	void mystop(){midi.stop();midi.close();sign=false;}
    	boolean isplay(){return sign;}
    	void setMusic(String e){file=e;}
    }
    
    class Readmap
    {
    	private int level,mx,my;
    	private int[][] mymap=new int[20][20];
    	FileReader r;
    	BufferedReader br;
    	String bb="";
    	int[] x;int c=0;
    	Readmap(int k)
    	{
    		level=k;
    		String s;
    		try
    		{
    			File f=new File("maps\\"+level+".map");
    			r=new FileReader(f);
    			br=new BufferedReader(r);
    		}
    		catch (IOException e)
    		{
    			System.out.println(e);
    		}
    		try
    		{
    			while ((s=br.readLine())!=null)
    			{
    				bb=bb+s;
    				
    			}
    		}
    		catch (IOException g)
    		{
    			System.out.println(g);
    		}
    		byte[] d=bb.getBytes();
    		int len=bb.length();
    		int[] x=new int[len];
    		for(int i=0;i<bb.length();i++)x[i]=d[i]-48;
    		for(int i=0;i<20;i++)
    		{
    			for(int j=0;j<20;j++)
    		    {
    				mymap[i][j]=x[c];
    		        if(mymap[i][j]==5)
    		        {
    					mx=j;my=i;
    		        }
    		        c++;
    		    }
    	    }
    	}
    	int[][] getmap(){return mymap;}
    	int getmanX(){return mx;}
    	int getmanY(){return my;}
    }
    
    

    运行:
    在这里插入图片描述
    以上就是我们的灰太狼古堡推箱子的小游戏啦!!

    展开全文
  • 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目录下)

    二、推箱子四大类

    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();
    	}
    	
    }
    
    
    

    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;
    	}
    	
    }
    
    
    

    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;
    	}
    
    }
    
    

    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;
    	}
    	
    	//public Sound(){
    		
    	//}
    	
    
    	
    	void mystop(){
    		midi.stop();
    		midi.close();
    		sign=false;
    	}
    	boolean isplay(){
    		return sign;
    	}
    	void setMusic(String e){
    		file=e;
    	}
    	
    }
    
    

    四、游戏分析

    游戏操作

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

    数组解读

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

    五、wav格式音乐代码

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

    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{
    
    	/**
    	 * 序列化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);
    
    	}
    	
    	//初始化地图
    	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();                //继续开始播放音频
    	}
    	
    }
    
    
    

    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实现简单的推箱子小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
  • 想成为一名优秀的 Java 开发,学好并发,绝对是你走入高薪行列的必备能力之一。 并发涉及的知识点,其实十分琐碎。学完记不住,记住了用不对。在并发底层原理中,不仅涉及 Java 语言,更涉及 JVM、操作系统、内存、...
  • Java推箱子游戏源码

    2018-05-07 13:53:34
    使用JAVA做的一个推箱子小游戏,比较有趣,可以作为入门学习。
  • 本文实例为大家分享了android推箱子游戏的具体实现代码,供大家参考,具体内容如下自定义view:package com.jisai.materialdesigndemo.tuixiangzhi;import android.content.Context;import android.graphics.Bitmap;...
  • import java.util.Scanner; public class SokobanPojo { static Scanner sc = new Scanner(System.in); static int[][] map = new int[10][10]; static int playerX = 1, playerY = 1; // 玩家坐标 static ...
  • java小游戏推箱子

    2014-03-13 14:40:23
    java编写的小游戏推箱子,导入到eclipse就能使用.
  • 基于JAVA推箱子小游戏(JAVA GUI) 话不多说,直接贴代码吧 是刚开始学JAVA写的,可能很简陋,可供初学者学习 功能就是简单的推箱子,多线程播放背景音乐,可以自行编辑地图,地图不是写死的,每关地图都是通过读取...
  • Java实现推箱子游戏

    2020-08-19 04:37:42
    主要为大家详细介绍了Java实现推箱子游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的伙伴们可以参考一下
  • Java小游戏灰太狼推箱子
  • java课程设计大作业写的 推箱子小游戏(含说明文档) java课程设计大作业写的 推箱子小游戏(含说明文档)
  • JAVA课程设计 推箱子小游戏

    万次阅读 2017-12-17 14:18:02
    先随机产生两个不重合的点,一个作为人的起点,另一个作为箱子的起点。从起点开始,随机朝四个方向走一定的步数,若走到的点不是空地,即对应数组元素不为0,则将该点设为空地,即数组元素设为0。将地图分为左上、...
  • java swing篇之推箱子小游戏实现及见解 swing介绍 swing是java里的一个包,是程序员在图像方面编程的接口,包含了GUI(graphical user interface)图形用户界面组件(文本框,按钮,表)等等 说到swing不得不提一下...
  • JAVA简易推箱子小游戏制作

    千次阅读 多人点赞 2018-04-27 22:36:06
    以下是代码: Run.java public class Run { public static void main(String[] args) { // TODO Auto-generated method stub Frame frame = new Frame(); } } Frame.java import java.awt.event....

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 810
精华内容 324
关键字:

java推箱子小游戏代码

java 订阅