av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

Java實現推箱子游戲

瀏覽:5日期:2022-09-01 13:14:21

本文實例為大家分享了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 }}

Java實現推箱子游戲

Java實現推箱子游戲

更多有趣的經典小游戲實現專題,分享給大家:

C++經典小游戲匯總

python經典小游戲匯總

python俄羅斯方塊游戲集合

JavaScript經典游戲 玩不停

javascript經典小游戲匯總

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Java
相關文章:
主站蜘蛛池模板: 欧美日韩综合精品 | 四虎影音| 日韩av在线中文字幕 | 国产欧美日韩精品一区二区三区 | 国产成人免费视频 | 成人在线小视频 | 青青草视频免费观看 | 色综合久久久久 | 永久av| 日韩精品人成在线播放 | 亚av在线 | 91美女在线| 超碰激情 | 国产精品一区二区久久久久 | 成人激情视频网 | 91精品国产色综合久久 | 亚洲九九| 91国在线高清视频 | 亚洲欧美一区二区三区在线 | 亚洲第一av | 欧美精品网站 | 午夜影院在线观看视频 | 免费精品久久久久久中文字幕 | 羞羞的视频免费在线观看 | 精品国产乱码久久久久久久久 | 日日操操 | 成人午夜精品 | 日韩在线视频一区 | 日日日日日日bbbbb视频 | 久久精品国产久精国产 | 精品久久网 | 精品国产一区二区三区在线观看 | 久久久久久成人 | 国产精品久久久久久亚洲调教 | 国产亚洲一区精品 | 精品毛片 | 久久久国产一区二区三区 | 久久久久久国产精品免费免费狐狸 | 亚洲精品电影 | 丁香六月激情 | 国产视频欧美 |