Java实现推箱子小游戏

来源:互联网 发布:淘宝卖大米 编辑:程序博客网 时间:2024/05/02 00:40
package Test1;//用于调用Test2包import Test2.*;import java.awt.*;import javax.swing.*;public class APP extends JFrame{public static void main(String[] args) {// TODO Auto-generated method stubAPP a = new APP();}public APP(){new Members();}}

package Test2;import java.awt.Event;import java.awt.Font;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import javax.swing.Icon;import javax.swing.ImageIcon;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;public class Members extends JFrame implements KeyListener{//定义一个JLabel数组,用来存放羊的位置JLabel [][]sheep = new JLabel[12][16];//0表示的是空地,1表示的是树木int[][] datas = {{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,1},{1,1,0,0,0,0,0,0,0,1,1,1,0,0,0,1},{1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1},{1,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};//狼的位置int wx,wy;/* * num值变化的情况 * 1.当羊进入笼子的时候,num+1 * 2.当羊离开笼子的时候,num-1 * 3.当羊从一个笼子离开进入另外一个笼子的时候,num不变 *///开始的时候羊进入箱子的总数量int num = 0;//笼子的总数量int total = 3;//构造函数public Members(){/* * 如果先放大的图片再放下的会把小的给覆盖,不能看到 * 图片有大小。把小的图片放在大的图片上面 * 所以添加图片组件的时候有顺序,要注意把小的放在大的上面 *///小图片//障碍的设计treeInit();//做笼子targetInit();//推箱子人物的初始化WolfInit();//羊的初始化sheepInit();//背景图片,大的//添加背景图片到窗体中backGroundInit();//设置整个窗体setForm();//注册监听this.addKeyListener(this);}//设置整个窗体private void setForm() {// TODO Auto-generated method stubthis.setTitle("推箱子游戏");this.setSize(825,645);//禁止用户改变窗体大小this.setResizable(false);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗口居中显示this.setLocationRelativeTo(null);this.setVisible(true);}//背景图片初始化private void backGroundInit() {// TODO Auto-generated method stubIcon i = new ImageIcon("floor.png");//使用JLabel制作背景JLabel lab_bg = new JLabel(i);//设置要添加的组件的位置与大小lab_bg.setBounds(0, 0, 800, 600);//将这个东西添加到窗体里面this.add(lab_bg);}//羊所在的位置初始化private void sheepInit() {// TODO Auto-generated method stub//三只羊Icon i = new ImageIcon("7.png");JLabel jb1 = new JLabel(i);jb1.setBounds(6 * 50, 4 * 50, 50, 50);this.add(jb1);//羊所在位置的值设置为4datas[4][6] = 4;sheep[4][6] = jb1;JLabel jb2 = new JLabel(i);jb2.setBounds(6 * 50, 6 * 50, 50, 50);this.add(jb2);datas[6][6] = 4;sheep[6][6] = jb2;JLabel jb3 = new JLabel(i);jb3.setBounds(6 * 50, 10 * 50, 50, 50);this.add(jb3);datas[10][6] = 4;sheep[10][6] = jb3;}JLabel jb = null;private void WolfInit() {// TODO Auto-generated method stub//人物最初位置在哪里?wx = 4 ;wy = 5 ;//使用一张图片来模拟人物//1.创建一张图片,人物图片Icon i = new ImageIcon("3.png");//2.使用JLabel组件模拟人物jb = new JLabel(i);//3.设置人物在屏幕上的显示位置//人物的显示位置放置在何处较为合理?----------------jb.setBounds(wx*50, wy*50, 50, 50);//4.把这个人物放到窗体里面this.add(jb);}//笼子的位置初始化private void targetInit() {// TODO Auto-generated method stubIcon i = new ImageIcon("target.png");JLabel jb1 = new JLabel(i);jb1.setBounds(14 * 50, 10 * 50,50,50);this.add(jb1);datas[10][14] = 8;JLabel jb2 = new JLabel(i);jb2.setBounds(13 * 50, 10 * 50, 50, 50);this.add(jb2);datas[10][13] = 8;JLabel jb3 = new JLabel(i);jb3.setBounds(14 * 50, 9 * 50, 50, 50);this.add(jb3);datas[9][14] = 8;}//树木的初始化private void treeInit() {// TODO Auto-generated method stubIcon k = new ImageIcon("tree.png");JLabel t = null;for(int i = 0;i < datas.length;i ++){for(int j = 0;j < datas[i].length;j ++){if(datas[i][j] == 1){t = new JLabel(k);t.setBounds(j*50, i*50, 50, 50);this.add(t);}}}}//判断是否胜利private void victory(){if(num == total){//设计一个弹框,提示游戏完成Icon i = new ImageIcon("6.png");JOptionPane.showMessageDialog(null, "游戏结束","推箱子",2,i);/* * 如果要设置关卡,则要在这里添加信息 * 注意修改num的值 * 根据自己关卡的数量,把datas数组设计成三维的额 */}}@Overridepublic void keyTyped(KeyEvent e) {// TODO Auto-generated method stub}@Overridepublic void keyPressed(KeyEvent e) {// TODO Auto-generated method stub/* * datas数值的情况 * 0空地 * 1树木 * 4羊 * 8空笼子 * 12放羊的笼子 * 结合这些数值去看下面的代码 *//* * W 向上 * D 向右 * S 向下 * A 向左 * 注意一个盲区,这个问题考虑了好久,在Java坐标体系中,坐标轴是水平方向为x轴,竖直方向为y轴 * 而在数组中先水平方向,后竖直方向,所以在datas数组中填写数值为先y后x */if(e.getKeyCode() == KeyEvent.VK_ENTER){/* * 每一次按键都要讨论下面这些情况 * 1.浪树木 * 2.狼羊  树木 * 3.狼羊 羊 * 4.狼羊 放羊的笼子 * 5.狼放羊的笼子树 * 6.狼放羊的笼子羊 * 7.狼放羊的笼子 放羊的笼子 * 上面的这些情况都不做处理,因为不能移动 * 8.狼空地 * 9.狼空笼子 * 10.狼羊空地 * 11.狼羊空笼子 * 12.狼放羊的笼子空地 * 13.狼放羊的笼子空笼子 * 这些情况需要有相应的变化,见代码 */if(datas[wy-1][wx] == 1){return;}if(datas[wy-1][wx] == 4 && datas[wy-2][wx] == 1){return;}if(datas[wy-1][wx] == 4 && datas[wy-2][wx] == 4){return;}if(datas[wy-1][wx] == 4 && datas[wy-1][wx] == 12){return;}if(datas[wy-1][wx] == 12 && datas[wy-2][wx] == 1){return;}if(datas[wy-1][wx] == 12 && datas[wy-2][wx] == 4){return;}if(datas[wy-1][wx] == 12 && datas[wy-2][wx] == 12){return;}if(datas[wy-1][wx] == 0){wy -= 1;//坐标得到的不是int类型。注意强制类型转化int x = (int)jb.getLocation().getX();int y = (int)jb.getLocation().getY();jb.setLocation(x, y - 50);Icon i = new ImageIcon("1.png");jb.setIcon(i);return;}if(datas[wy-1][wx] == 8){wy -= 1;//坐标得到的不是int类型。注意强制类型转化int x = (int)jb.getLocation().getX();int y = (int)jb.getLocation().getY();jb.setLocation(x, y - 50);Icon i = new ImageIcon("1.png");jb.setIcon(i);return;}if(datas[wy-1][wx] == 4 && datas[wy-2][wx] == 0){datas[wy-1][wx] = 0;datas[wy-2][wx] = 4;}if(datas[wy-1][wx] == 4 && datas[wy-2][wx] == 8){datas[wy-1][wx] = 0;datas[wy-2][wx] = 12;num ++;}if(datas[wy-1][wx] == 12 && datas[wy-2][wx] == 0){datas[wy-1][wx] = 8;datas[wy-2][wx] = 4;num --;}if(datas[wy-1][wx] == 12 && datas[wy-2][wx] == 8){datas[wy-1][wx] = 8;datas[wy-2][wx] = 12;}sheep[wy-1][wx].setLocation(wx*50, wy*50-100);sheep[wy-2][wx] = sheep[wy-1][wx];sheep[wy-1][wx] = null;wy -= 1;//坐标得到的不是int类型。注意强制类型转化int x = (int)jb.getLocation().getX();int y = (int)jb.getLocation().getY();jb.setLocation(x, y - 50);Icon i = new ImageIcon("1.png");jb.setIcon(i);victory();return;}else if(e.getKeyCode() == KeyEvent.VK_D){if(datas[wy][wx+1] == 1){return;}if(datas[wy][wx+1] == 4 && datas[wy][wx+2] == 1){return;}if(datas[wy][wx+1] == 4 && datas[wy][wx+2] == 4){return;}if(datas[wy][wx+1] == 4 && datas[wy][wx+2] == 12){return;}if(datas[wy][wx+1] == 12 && datas[wy][wx+2] == 1){return;}if(datas[wy][wx+1] == 12 && datas[wy][wx+2] == 4){return;}if(datas[wy][wx+1] == 12 && datas[wy][wx+2] == 12){return;}if(datas[wy][wx+1] == 0){wx += 1;//坐标得到的不是int类型。注意强制类型转化int x = (int)jb.getLocation().getX();int y = (int)jb.getLocation().getY();jb.setLocation(x + 50, y);Icon i = new ImageIcon("2.png");jb.setIcon(i);return;}if(datas[wy][wx+1] == 8){wx += 1;//坐标得到的不是int类型。注意强制类型转化int x = (int)jb.getLocation().getX();int y = (int)jb.getLocation().getY();jb.setLocation(x + 50, y);Icon i = new ImageIcon("2.png");jb.setIcon(i);return;}if(datas[wy][wx+1] == 4 && datas[wy][wx+2] == 0){datas[wy][wx+1] = 0;datas[wy][wx+2] = 4;}if(datas[wy][wx+1] == 4 && datas[wy][wx+2] == 8){datas[wy][wx+1] = 0;datas[wy][wx+2] = 12;num ++;}if(datas[wy][wx+1] == 12 && datas[wy][wx+2] == 0){datas[wy][wx+1] = 8;datas[wy][wx+2] = 4;num --;}if(datas[wy][wx+1] == 12 && datas[wy][wx+2] == 8){datas[wy][wx+1] = 8;datas[wy][wx+2] = 12;}sheep[wy][wx+1].setLocation(wx*50+100, wy*50);sheep[wy][wx+2] = sheep[wy][wx+1];sheep[wy][wx+1] = null;wx += 1;//坐标得到的不是int类型。注意强制类型转化int x = (int)jb.getLocation().getX();int y = (int)jb.getLocation().getY();jb.setLocation(x + 50, y);Icon i = new ImageIcon("2.png");jb.setIcon(i);victory();return;}else if(e.getKeyCode() == KeyEvent.VK_S){if(datas[wy+1][wx] == 1){return;}if(datas[wy+1][wx] == 4 && datas[wy+2][wx] == 1){return;}if(datas[wy+1][wx] == 4 && datas[wy+2][wx] == 4){return;}if(datas[wy+1][wx] == 4 && datas[wy+2][wx] == 12){return;}if(datas[wy+1][wx] == 12 && datas[wy+2][wx] == 1){return;}if(datas[wy+1][wx] == 12 && datas[wy+2][wx] == 4){return;}if(datas[wy+1][wx] == 12 && datas[wy+2][wx] == 12){return;}if(datas[wy+1][wx] == 0){wy += 1;//坐标得到的不是int类型。注意强制类型转化int x = (int)jb.getLocation().getX();int y = (int)jb.getLocation().getY();jb.setLocation(x, y + 50);Icon i = new ImageIcon("3.png");jb.setIcon(i);return;}if(datas[wy+1][wx] == 8){wy += 1;//坐标得到的不是int类型。注意强制类型转化int x = (int)jb.getLocation().getX();int y = (int)jb.getLocation().getY();jb.setLocation(x, y + 50);Icon i = new ImageIcon("3.png");jb.setIcon(i);return;}if(datas[wy+1][wx] == 4 && datas[wy+2][wx] == 0){datas[wy+1][wx] = 0;datas[wy+2][wx] = 4;}if(datas[wy+1][wx] == 4 && datas[wy+2][wx] == 8){datas[wy+1][wx] = 0;datas[wy+2][wx] = 12;num ++;}if(datas[wy+1][wx] == 12 && datas[wy+2][wx] == 0){datas[wy+1][wx] = 8;datas[wy+2][wx] = 4;num --;}if(datas[wy+1][wx] == 12 && datas[wy+2][wx] == 8){datas[wy+1][wx] = 8;datas[wy+2][wx] = 12;}sheep[wy+1][wx].setLocation(wx*50, wy*50+100);sheep[wy+2][wx] = sheep[wy+1][wx];sheep[wy+1][wx] = null;wy += 1;//坐标得到的不是int类型。注意强制类型转化int x = (int)jb.getLocation().getX();int y = (int)jb.getLocation().getY();jb.setLocation(x, y + 50);Icon i = new ImageIcon("3.png");jb.setIcon(i);victory();return;}else if(e.getKeyCode() == KeyEvent.VK_A){if(datas[wy][wx-1] == 1){return;}if(datas[wy][wx-1] == 4 && datas[wy][wx-2] == 1){return;}if(datas[wy][wx-1] == 4 && datas[wy][wx-2] == 4){return;}if(datas[wy][wx-1] == 4 && datas[wy][wx-2] == 12){return;}if(datas[wy][wx-1] == 12 && datas[wy][wx-2] == 1){return;}if(datas[wy][wx-1] == 12 && datas[wy][wx-2] == 4){return;}if(datas[wy][wx-1] == 12 && datas[wy][wx-2] == 12){return;}if(datas[wy][wx-1] == 0){wx -= 1;//坐标得到的不是int类型。注意强制类型转化int x = (int)jb.getLocation().getX();int y = (int)jb.getLocation().getY();jb.setLocation(x - 50, y);Icon i = new ImageIcon("4.png");jb.setIcon(i);return;}if(datas[wy][wx-1] == 8){wx -= 1;//坐标得到的不是int类型。注意强制类型转化int x = (int)jb.getLocation().getX();int y = (int)jb.getLocation().getY();jb.setLocation(x - 50, y);Icon i = new ImageIcon("4.png");jb.setIcon(i);return;}if(datas[wy][wx-1] == 4 && datas[wy][wx-2] == 0){datas[wy][wx-1] = 0;datas[wy][wx-2] = 4;}if(datas[wy][wx-1] == 4 && datas[wy][wx-2] == 8){datas[wy][wx-1] = 0;datas[wy][wx-2] = 12;num ++;}if(datas[wy][wx-1] == 12 && datas[wy][wx-2] == 0){datas[wy][wx-1] = 8;datas[wy][wx-2] = 4;num --;}if(datas[wy][wx-1] == 12 && datas[wy][wx-2] == 8){datas[wy][wx-1] = 8;datas[wy][wx-2] = 12;}sheep[wy][wx-1].setLocation(wx*50-100, wy*50);sheep[wy][wx-2] = sheep[wy][wx-1];sheep[wy][wx-1] = null;wx -= 1;//坐标得到的不是int类型。注意强制类型转化int x = (int)jb.getLocation().getX();int y = (int)jb.getLocation().getY();jb.setLocation(x - 50, y);Icon i = new ImageIcon("4.png");jb.setIcon(i);victory();return;}}@Overridepublic void keyReleased(KeyEvent e) {// TODO Auto-generated method stub}}



图片另附,要的话找我要链接

2 0
原创粉丝点击