Java實(shí)現(xiàn)推箱子游戲
本文實(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)專題,分享給大家:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ConcurrentHashMap是如何實(shí)現(xiàn)線程安全的你知道嗎
這篇文章主要介紹了ConcurrentHashMap是如何實(shí)現(xiàn)線程安全的你知道嗎,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10Java中關(guān)于MouseWheelListener的鼠標(biāo)滾輪事件詳解
這篇文章主要介紹了Java中關(guān)于MouseWheelListener的鼠標(biāo)滾輪事件詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12你肯定能看懂的Java IO相關(guān)知識(shí)總結(jié)
群里有大佬說(shuō)想讓我寫(xiě)一篇NIO,一直也沒(méi)寫(xiě),但是和同事聊天也說(shuō)對(duì)Java的IO不是很清晰,因此今天就寫(xiě)下Java的io,先打個(gè)基礎(chǔ),下次寫(xiě)NIO,需要的朋友可以參考下2021-05-05SpringBoot實(shí)現(xiàn)過(guò)濾器Filter的三種方式
過(guò)濾器Filter由Servlet提供,基于函數(shù)回調(diào)實(shí)現(xiàn)鏈?zhǔn)綄?duì)網(wǎng)絡(luò)請(qǐng)求與響應(yīng)的攔截與修改,本文講給大家詳細(xì)介紹SpringBoot實(shí)現(xiàn)過(guò)濾器Filter的三種方式,需要的朋友可以參考下2023-08-08java編寫(xiě)創(chuàng)建數(shù)據(jù)庫(kù)和表的程序
這篇文章主要為大家詳細(xì)介紹了java編寫(xiě)創(chuàng)建數(shù)據(jù)庫(kù)和表的程序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10Java集合類(lèi)的組織結(jié)構(gòu)和繼承、實(shí)現(xiàn)關(guān)系詳解
這篇文章主要介紹了Java集合類(lèi)的組織結(jié)構(gòu)和繼承、實(shí)現(xiàn)關(guān)系,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11SpringBoot使用Sa-Token實(shí)現(xiàn)路徑攔截和特定接口放行
這篇文章主要介紹了SpringBoot使用Sa-Token實(shí)現(xiàn)路徑攔截和特定接口放行,文中通過(guò)代碼示例講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-06-06