欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java實(shí)現(xiàn)推箱子游戲

 更新時(shí)間:2020年05月19日 11:52:35   作者:這個(gè)異常不拋出  
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)推箱子游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Java實(shí)現(xiàn)推箱子游戲的具體代碼,供大家參考,具體內(nèi)容如下

package Test1;
 
//用于調(diào)用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{
 
 //定義一個(gè)JLabel數(shù)組,用來(lái)存放羊的位置
 JLabel [][]sheep = new JLabel[12][16];
 
 //0表示的是空地,1表示的是樹(shù)木
 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.當(dāng)羊進(jìn)入籠子的時(shí)候,num+1
 * 2.當(dāng)羊離開(kāi)籠子的時(shí)候,num-1
 * 3.當(dāng)羊從一個(gè)籠子離開(kāi)進(jìn)入另外一個(gè)籠子的時(shí)候,num不變
 */
 //開(kāi)始的時(shí)候羊進(jìn)入箱子的總數(shù)量
 int num = 0;
 //籠子的總數(shù)量
 int total = 3;
 
 //構(gòu)造函數(shù)
 public Members()
 {
 /*
 * 如果先放大的圖片再放下的會(huì)把小的給覆蓋,不能看到
 * 圖片有大小。把小的圖片放在大的圖片上面
 * 所以添加圖片組件的時(shí)候有順序,要注意把小的放在大的上面
 */
 //小圖片
 
 //障礙的設(shè)計(jì)
 treeInit();
 
 //做籠子
 targetInit();
 
 //推箱子人物的初始化
 WolfInit();
 
 //羊的初始化
 sheepInit();
 
 //背景圖片,大的
 //添加背景圖片到窗體中
 backGroundInit();
 
 //設(shè)置整個(gè)窗體
 setForm();
 
 //注冊(cè)監(jiān)聽(tīng)
 this.addKeyListener(this);
 
 }
 
 
 //設(shè)置整個(gè)窗體
 private void setForm() {
 // TODO Auto-generated method stub
 
 this.setTitle("推箱子游戲");
 this.setSize(825,645);
 //禁止用戶改變窗體大小
 this.setResizable(false);
 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 //設(shè)置窗口居中顯示
 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);
 //設(shè)置要添加的組件的位置與大小
 lab_bg.setBounds(0, 0, 800, 600);
 //將這個(gè)東西添加到窗體里面
 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);
 //羊所在位置的值設(shè)置為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 ;
 
 //使用一張圖片來(lái)模擬人物
 //1.創(chuàng)建一張圖片,人物圖片
 Icon i = new ImageIcon("3.png");
 //2.使用JLabel組件模擬人物
 jb = new JLabel(i);
 //3.設(shè)置人物在屏幕上的顯示位置
 //人物的顯示位置放置在何處較為合理?----------------
 jb.setBounds(wx*50, wy*50, 50, 50);
 //4.把這個(gè)人物放到窗體里面
 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;
 }
 
 
 //樹(shù)木的初始化
 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){
 //設(shè)計(jì)一個(gè)彈框,提示游戲完成
 Icon i = new ImageIcon("6.png");
 JOptionPane.showMessageDialog(null, "游戲結(jié)束","推箱子",2,i);
 
 /*
 * 如果要設(shè)置關(guān)卡,則要在這里添加信息
 * 注意修改num的值
 * 根據(jù)自己關(guān)卡的數(shù)量,把datas數(shù)組設(shè)計(jì)成三維的額
 */
 }
 }
 
 
 @Override
 public void keyTyped(KeyEvent e) {
 // TODO Auto-generated method stub
 
 }
 
 
 @Override
 public void keyPressed(KeyEvent e) {
 // TODO Auto-generated method stub
 
 /*
 * datas數(shù)值的情況
 * 0 空地
 * 1 樹(shù)木
 * 4 羊
 * 8 空籠子
 * 12 放羊的籠子
 * 結(jié)合這些數(shù)值去看下面的代碼
 */
 
 /*
 * W 向上
 * D 向右
 * S 向下
 * A 向左
 * 注意一個(gè)盲區(qū),這個(gè)問(wèn)題考慮了好久,在Java坐標(biāo)體系中,坐標(biāo)軸是水平方向?yàn)閤軸,豎直方向?yàn)閥軸
 * 而在數(shù)組中先水平方向,后豎直方向,所以在datas數(shù)組中填寫(xiě)數(shù)值為先y后x
 */
 
 if(e.getKeyCode() == KeyEvent.VK_ENTER){
 
 /*
 * 每一次按鍵都要討論下面這些情況
 * 1.浪 樹(shù)木
 * 2.狼 羊 樹(shù)木
 * 3.狼 羊 羊
 * 4.狼 羊 放羊的籠子
 * 5.狼 放羊的籠子 樹(shù)
 * 6.狼 放羊的籠子 羊
 * 7.狼 放羊的籠子 放羊的籠子
 * 上面的這些情況都不做處理,因?yàn)椴荒芤苿?dòng)
 * 8.狼 空地
 * 9.狼 空籠子
 * 10.狼 羊 空地
 * 11.狼 羊 空籠子 
 * 12.狼 放羊的籠子 空地
 * 13.狼 放羊的籠子 空籠子
 * 這些情況需要有相應(yīng)的變化,見(jiàn)代碼
 */
 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;
 //坐標(biāo)得到的不是int類(lèi)型。注意強(qiáng)制類(lèi)型轉(zhuǎn)化
 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;
 //坐標(biāo)得到的不是int類(lèi)型。注意強(qiáng)制類(lèi)型轉(zhuǎn)化
 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;
 //坐標(biāo)得到的不是int類(lèi)型。注意強(qiáng)制類(lèi)型轉(zhuǎn)化
 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;
 //坐標(biāo)得到的不是int類(lèi)型。注意強(qiáng)制類(lèi)型轉(zhuǎn)化
 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;
 //坐標(biāo)得到的不是int類(lèi)型。注意強(qiáng)制類(lèi)型轉(zhuǎn)化
 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;
 //坐標(biāo)得到的不是int類(lèi)型。注意強(qiáng)制類(lèi)型轉(zhuǎn)化
 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;
 //坐標(biāo)得到的不是int類(lèi)型。注意強(qiáng)制類(lèi)型轉(zhuǎn)化
 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;
 //坐標(biāo)得到的不是int類(lèi)型。注意強(qiáng)制類(lèi)型轉(zhuǎn)化
 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;
 //坐標(biāo)得到的不是int類(lèi)型。注意強(qiáng)制類(lèi)型轉(zhuǎn)化
 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;
 //坐標(biāo)得到的不是int類(lèi)型。注意強(qiáng)制類(lèi)型轉(zhuǎn)化
 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;
 //坐標(biāo)得到的不是int類(lèi)型。注意強(qiáng)制類(lèi)型轉(zhuǎn)化
 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;
 //坐標(biāo)得到的不是int類(lèi)型。注意強(qiáng)制類(lèi)型轉(zhuǎn)化
 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
 
 }
}

更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專題,分享給大家:

C++經(jīng)典小游戲匯總

python經(jīng)典小游戲匯總

python俄羅斯方塊游戲集合

JavaScript經(jīng)典游戲 玩不停

javascript經(jīng)典小游戲匯總

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論