歡迎您光臨本站 註冊首頁

Java實現推箱子游戲

←手機掃碼閱讀     bom485332 @ 2020-06-10 , reply:0

本文實例為大家分享了Java實現推箱子游戲的具體代碼,供大家參考,具體內容如下

  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 stub      APP 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 stub      this.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 stub   Icon 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);   //羊所在位置的值設置為4   datas[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 stub   Icon 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 stub      Icon 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數組設計成三維的額   */   }   }         @Override   public void keyTyped(KeyEvent e) {   // TODO Auto-generated method stub      }         @Override   public 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;   }   }         @Override   public void keyReleased(KeyEvent e) {   // TODO Auto-generated method stub      }  }

 

  

   


[bom485332 ] Java實現推箱子游戲已經有228次圍觀

http://coctec.com/docs/java/show-post-237790.html