电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

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

Java實現推箱子游戲

瀏覽:2日期: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
相關文章:
主站蜘蛛池模板: 光伏家 - 太阳能光伏发电_分布式光伏发电_太阳能光伏网 | 铝合金电阻-无源谐波滤波器-上海稳达电讯设备厂 | 上海噪音治理公司-专业隔音降噪公司-中广通环保| 电动百叶窗,开窗器,电动遮阳百叶,电动开窗机生产厂家-徐州鑫友工控科技发展有限公司 | 厦门ISO认证|厦门ISO9001认证|厦门ISO14001认证|厦门ISO45001认证-艾索咨询专注ISO认证行业 | 江苏齐宝进出口贸易有限公司 | 广州/东莞小字符喷码机-热转印打码机-喷码机厂家-广州瑞润科技 | 掺铥光纤放大器-C/L波段光纤放大器-小信号光纤放大器-合肥脉锐光电技术有限公司 | 土壤墒情监测站_土壤墒情监测仪_土壤墒情监测系统_管式土壤墒情站-山东风途物联网 | 家庭教育吧-在线家庭教育平台,专注青少年家庭教育 | 运动木地板_体育木地板_篮球馆木地板_舞台木地板-实木运动地板厂家 | 色谱柱-淋洗液罐-巴罗克试剂槽-巴氏吸管-5ml样品瓶-SBS液氮冻存管-上海希言科学仪器有限公司 | 螺纹三通快插接头-弯通快插接头-宁波舜驰气动科技有限公司 | R507制冷剂,R22/R152a制冷剂厂家-浙江瀚凯制冷科技有限公司 | 深圳市源和塑胶电子有限公司-首页 | 多功能三相相位伏安表-变压器短路阻抗测试仪-上海妙定电气 | 便携式高压氧舱-微压氧舱-核生化洗消系统-公众洗消站-洗消帐篷-北京利盟救援 | 浙江建筑资质代办_二级房建_市政_电力_安许_劳务资质办理公司 | 熔体泵_熔体出料泵_高温熔体泵-郑州海科熔体泵有限公司 | 对照品_中药对照品_标准品_对照药材_「格利普」高纯中药标准品厂家-成都格利普生物科技有限公司 澳门精准正版免费大全,2025新澳门全年免费,新澳天天开奖免费资料大全最新,新澳2025今晚开奖资料,新澳马今天最快最新图库 | arch电源_SINPRO_开关电源_模块电源_医疗电源-东佑源 | 油漆辅料厂家_阴阳脚线_艺术漆厂家_内外墙涂料施工_乳胶漆专用防霉腻子粉_轻质粉刷石膏-魔法涂涂 | 「安徽双凯」自动售货机-无人售货机-成人用品-自动饮料食品零食售货机 | 地埋式垃圾站厂家【佳星环保】小区压缩垃圾中转站转运站 | 交通信号灯生产厂家_红绿灯厂家_电子警察监控杆_标志杆厂家-沃霖电子科技 | 菲希尔FISCHER测厚仪-铁素体检测仪-上海吉馨实业发展有限公司 | 无轨电动平车_轨道平车_蓄电池电动平车★尽在新乡百特智能转运设备有限公司 | 电动卫生级调节阀,电动防爆球阀,电动软密封蝶阀,气动高压球阀,气动对夹蝶阀,气动V型调节球阀-上海川沪阀门有限公司 | 超声波焊接机,振动摩擦焊接机,激光塑料焊接机,超声波焊接模具工装-德召尼克(常州)焊接科技有限公司 | 土壤养分检测仪|土壤水分|土壤紧实度测定仪|土壤墒情监测系统-土壤仪器网 | 档案密集架_电动密集架_移动密集架_辽宁档案密集架-盛隆柜业厂家现货批发销售价格公道 | 餐饮加盟网_特色餐饮加盟店_餐饮连锁店加盟 | 通用磨耗试验机-QUV耐候试验机|久宏实业百科 | 重庆监控_电子围栏设备安装公司_门禁停车场管理系统-劲浪科技公司 | 欧必特空气能-商用空气能热水工程,空气能热水器,超低温空气源热泵生产厂家-湖南欧必特空气能公司 | 浙江富广阀门有限公司| PC阳光板-PC耐力板-阳光板雨棚-耐力板雨棚,厂家定制[优尼科板材] | 阴离子聚丙烯酰胺价格_PAM_高分子聚丙烯酰胺厂家-河南泰航净水材料有限公司 | 南京精锋制刀有限公司-纵剪机刀片_滚剪机刀片_合金刀片厂家 | 承插管件_不锈钢承插管件_锻钢高压管件-温州科正阀门管件有限公司 | 镀锌钢格栅_热镀锌格栅板_钢格栅板_热镀锌钢格板-安平县昊泽丝网制品有限公司 |