java swing篇之推箱子小游戏实现及见解

java swing篇之推箱子小游戏实现及见解

swing介绍

swing是java里的一个包,是程序员在图像方面编程的接口,包含了GUI(graphical user interface)图形用户界面组件(文本框,按钮,表)等等
说到swing不得不提一下AWT(abstruct window toolkit)抽象窗口工具包,他是早期的GUI编程包,
AWT的图像函数于操作系统提供的图形函数有着一一对应的关系,所以AWT控件被称为 重量级控件,(现在程序代码中的事件,及事件监听依然会用到AWT),但更多的还是swing,因为swing是为了解决AWT存在的问题而新开发的图形界面包,是对AWT的改良和扩展

swing与awt两者区别

1:awt是基于北地方法的c/c++程序 速度相对快
2:swing是基于awt的java程序 速度慢
(由以上可推断swing更能符合java一次编译,到处运行的特点)
3:导swing包,javax.swing.* . 导awt包,java.awt.*

swing组件

  • 顶层容器:JFrame(窗口),Jdialog(对话框)
    • 菜单栏:JMenuBar-------Jmenu-------JMenuItem(逐层包含,通过add添加)
    • 中间容器:JPanel(面板) JScrollPane(滚动面板) JSpiltPane(分隔面板) JTabbedPane(选项卡) JLayeredPane(层级面板)
      • 基本组件:JLabel (重要,划重点) JButton ,JTextField
        容器里可以包含组件,容器里可以包含容器,顶层容器最大,往往就是程序运行时的那个窗口,添加容器的时候也注意,先添加小容器,(我不知道是否有这个说法,但是我在代码中这样写就不会报错,反之就不行,有大佬知道的望不吝赐教),就是比如有JFrame,JPanel,JLabel,要先添加JLabel到JPanel,然后再添加JPanel到JFrame

万物皆可JLabel

这么说只是想说明我理解的JLabel是非常重要的,因为对于GUI程序来讲,最重要的就是图像再用户界面的呈现了,JLabel小到可以作为一颗棋子呈现,大到也可以作为程序背景图来呈现,就初学者来讲称他为最基本元素也不为过(我也是初学者,望大佬们勿喷,多鼓励,哈哈哈哈)。以下几个以JLabel呈现图像的步骤。
1:Icon i = new ImageIcon(“*.jpg”);
2:JLabel t = new JLabel(i);
3:t.setBounds(x,y,width,height);
这里讲一下setBounds(),JFrame默认是边界布局(borderLayout),JPanel的默认布局是流式布局,流失布局的特点就是:一行占满才到下一行,所以要用setBounds(),先设置布局管理器,容器.setLayout(null),然后用setBounds()自定义控件的位置,四个参数,分别是x,y,width,height,宽度高度好理解,x,y,就理解为控件与容器左上角的距离,有的小程序(包括我下面的推箱子)是通过数组来定义布局的,数组的两个参数分别是行,列,对应的是y,x,(望自行理解一番),所以代码中需要把x,y,带进数组的y在x前面,datas[y][x]

swing布局加载问题

再加载布局的时候总有个先后顺序吧,那你觉想背景这样的布局应该多久加载呢?
正确的做法应该是这样的,先加载小布局,小JLabel这样的,再加载进大布局,像背景JLabel这样的,(原因是避免大布局把小布局覆盖点),最后在设置顶层容器,jFrame.setVisible(true);设置顶层容器可见(默认是不可见的),就像是加载最大的布局一样,这样方便记。

进入推箱子正题

public class App extends JFrame  {
    public static void main(String[] args) {
        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, 4, 0, 0, 1, 0, 0, 0, 0, 0, 1},
                {1, 0, 0, 4, 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, 8, 0, 1},
                {1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1},
                {1, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 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, 8, 0, 0, 0, 1},
                {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
        };
        new Member(datas);

    }

构造函数传参方便后边多个关卡的编写

package cc.cque.cjf;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Member extends JFrame implements KeyListener, ActionListener {
    /**
     * 0:空地
     * 1:树,障碍
     * 3:笼子
     * 4:羊
     *
     */
    int[][] datas;
    JLabel [][] sheep = new JLabel[12][16];
    int wx,wy;
    int num=0;  //判断羊的数量
    final static int TOTAL = 3;
    JLabel wolfLable;
    JMenuBar jMenuBar;
    JMenu choose,about;
    JMenuItem restar,author;



    public Member(int[][] datas){
        this.datas = datas;

        BarInit();
        treeInit();
        wolfInit();
        sheepInit();
        cageInit();
        backgroundInit();


        setFrom();
        this.addKeyListener(this);
    }
    public void setFrom(){


        this.setTitle("推箱子");
        this.setSize(850,660);
        this.setResizable(false);  //禁止用户改变窗口大小
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);   //设置在屏幕中间显示
        this.setVisible(true);
    }
    public void BarInit(){

        jMenuBar = new JMenuBar();
        choose = new JMenu("选择");
        about = new JMenu("关于作者");
        restar = new JMenuItem("重来");
        author = new JMenuItem("关于作者");
        restar.addActionListener(this);
        author.addActionListener(this);
        choose.add(restar);
        about.add(author);
        jMenuBar.add(choose);
        jMenuBar.add(about);
        this.add(jMenuBar);
        this.setJMenuBar(jMenuBar);


    }

    public void treeInit(){
        JLabel t;
        Icon icon = new ImageIcon("pic/tree.jpg");
        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(icon);
                    t.setBounds(j*50,i*50,50,50);
                    this.add(t);
                }

            }
        }

    }
    public void sheepInit(){
        JLabel t;
        Icon icon = new ImageIcon("pic/sheep.jpg");
        for(int i =0 ;i<datas.length;i++){
            for (int j = 0;j<datas[i].length;j++){
                if (datas[i][j]==4){
                    t=new JLabel(icon);
                    sheep[i][j] = t;
                    t.setBounds(j*50,i*50,50,50);
                    this.add(t);
                }

            }
        }

    }

    public void cageInit(){
        JLabel t;
        Icon icon = new ImageIcon("pic/cage.jpg");
        for(int i =0 ;i<datas.length;i++){
            for (int j = 0;j<datas[i].length;j++){
                if (datas[i][j]==8){
                    t=new JLabel(icon);
                    t.setBounds(j*50,i*50,50,50);
                    this.add(t);
                }

            }
        }

    }

    public void backgroundInit(){
        Icon background = new ImageIcon("pic/grass.jpg");
        JLabel jLabel = new JLabel(background);
        jLabel.setBounds(0,0,825,645);
        this.add(jLabel);
    }

    public void wolfInit(){
        wx = 1;
        wy = 1;//不是二维数组里的坐标

        Icon wolf = new ImageIcon("pic\\wolf.jpg"); //在文件路径中用“/”或者“\\”,有一个转义字符
        wolfLable = new JLabel(wolf);
        wolfLable.setBounds(wx*50,wy*50,50,50);
        this.add(wolfLable);

    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {

        if(e.getKeyCode()==KeyEvent.VK_UP){

            /**
             * 下有return 都是不能动的情况
             */

            //上面是障碍
            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;
            }

            /**
             * 接下两个return 是能动但是上面没有羊,不能执行后面羊位置的计算
             */
            //上面是空地
            if(datas[wy-1][wx] == 0){
                wy = wy-1;
                int x = (int)wolfLable.getLocation().getX();
                int y = (int)wolfLable.getLocation().getY();
                wolfLable.setLocation(x,y-50);

                return;
            }
            //上面是空笼子
            if(datas[wy-1][wx] == 8){
                wy = wy-1;
                /*int x = (int)wolfLable.getLocation().getX();
                int y = (int)wolfLable.getLocation().getY();*/
                wolfLable.setLocation(wx*50,wy*50);
                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 = wy-1;

            wolfLable.setLocation(wx*50,wy*50);
            victory();
            return;
        }
        else if(e.getKeyCode()==KeyEvent.VK_DOWN){


            /**
             * 下有return 都是不能动的情况
             */

            //下面是障碍
            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;
            }

            /**
             * 接下两个return 是能动但是下面没有羊,不能执行后面羊位置的计算
             */
            //下面是空地
            if(datas[wy+1][wx] == 0){
                wy = wy+1;
                int x = (int)wolfLable.getLocation().getX();
                int y = (int)wolfLable.getLocation().getY();
                wolfLable.setLocation(x,y+50);

                return;
            }
            //下面是空笼子
            if(datas[wy+1][wx] == 8){
                wy = wy+1;
                /*int x = (int)wolfLable.getLocation().getX();
                int y = (int)wolfLable.getLocation().getY();*/
                wolfLable.setLocation(wx*50,wy*50);
                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 = wy+1;

            wolfLable.setLocation(wx*50,wy*50);
            victory();
            return;

        }



        else if(e.getKeyCode()==KeyEvent.VK_LEFT){

            /**
             * 下有return 都是不能动的情况
             */

            //左面是障碍
            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;
            }

            /**
             * 接下两个return 是能动但是上面没有羊,不能执行后面羊位置的计算
             */
            //左面是空地
            if(datas[wy][wx-1] == 0){
                wx = wx-1;
                int x = (int)wolfLable.getLocation().getX();
                int y = (int)wolfLable.getLocation().getY();
                wolfLable.setLocation(x-50,y);

                return;
            }
            //左面是空笼子
            if(datas[wy][wx-1] == 8){
                wx=wx-1;
               /* int x = (int)wolfLable.getLocation().getX();
                int y = (int)wolfLable.getLocation().getY();*/
                wolfLable.setLocation(wx*50,wy*50);
                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=wx-1;
            wolfLable.setLocation(wx*50,wy*50);
            victory();
            return;

        }

        else if (e.getKeyCode()==KeyEvent.VK_RIGHT){

            //右面是障碍
            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;
            }

            /**
             * 接下两个return 是能动但是上面没有羊,不能执行后面羊位置的计算
             */
            //右面是空地
            if(datas[wy][wx+1] == 0){
                wx = wx+1;
                int x = (int)wolfLable.getLocation().getX();
                int y = (int)wolfLable.getLocation().getY();
                wolfLable.setLocation(x+50,y);

                return;
            }
            //右面是空笼子
            if(datas[wy][wx+1] == 8){
                wx=wx+1;
               /* int x = (int)wolfLable.getLocation().getX();
                int y = (int)wolfLable.getLocation().getY();*/
                wolfLable.setLocation(wx*50,wy*50);
                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=wx+1;
            wolfLable.setLocation(wx*50,wy*50);

            victory();
            return;

        }

    }

    public void victory(){
        if (TOTAL==num){
            Icon i = new ImageIcon("pic/wolf1.jpg");
            Object[] options = {"重来","第二关","第三关","第四关"};
            int result = JOptionPane.showOptionDialog(null,"恭喜过关","选择关卡",JOptionPane.YES_OPTION,
                    JOptionPane.PLAIN_MESSAGE,null,options,options[0]);
            if(result==0){
                int [][] data0 = new DataOptins().data0();
                new Member(data0);
                this.dispose();//撤销窗口,释放窗口使用过的资源
            }else if(result==1){
                int [][] data1 = new DataOptins().data1();
                new Member(data1);
                this.dispose();
            }else if (result==2){
                int [][] data2 = new DataOptins().data2();
                new Member(data2);
                this.dispose();
            }else if (result==3){
                int [][] data3 = new DataOptins().data3();
                new Member(data3);
                this.dispose();
            }

        }
    }



    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==restar){
            new Member(new DataOptins().data0());
            this.dispose();
        }
        if (e.getSource()==author){
            JFrame j = new JFrame("关于作者");
            j.setLayout(null);
            JTextArea area = new JTextArea();
            area.setBounds(0,0,500,400);
            area.setText("author:ChengC\nQQ:782053943");
            j.setSize(500,400);
            j.setLocationRelativeTo(null);
            j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            j.add(area);
            j.setVisible(true);
        }
    }
}

下面是各个关卡地图数据的类,地图由二维数组构造

package cc.cque.cjf;

public class DataOptins {
    public DataOptins() {
    }
    public int[][] data0(){
        int[][] no0 = {
                {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, 4, 0, 0, 1, 0, 0, 0, 0, 0, 1},
                {1, 0, 0, 4, 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, 8, 0, 1},
                {1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1},
                {1, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 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, 8, 0, 0, 0, 1},
                {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
        };
        return no0;
    }
    public int[][] data1(){
        int[][] no1 = {
                {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, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 8, 0, 1},
                {1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 8, 0, 1},
                {1, 0, 0, 0, 0, 4, 0, 1, 0, 0, 0, 0, 0, 8, 0, 1},
                {1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1},
                {1, 0, 0, 0, 1, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                {1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                {1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1},
                {1, 0, 4, 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}
        };
        return no1;
    }


    public int[][] data2(){
        int[][] no2 = {
                {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, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1},
                {1, 8, 0, 0, 0, 1, 0, 0, 0, 4, 0, 0, 0, 0, 0, 1},
                {1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1},
                {1, 0, 0, 0, 0, 1, 0, 4, 0, 0, 0, 0, 0, 0, 0, 1},
                {1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1},
                {1, 0, 0, 0, 0, 1, 0, 0, 0, 4, 0, 0, 0, 0, 0, 1},
                {1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 8, 1},
                {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                {1, 0, 0, 0, 8, 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}
        };
        return no2;
    }

    public int[][] data3(){
        int[][] no3 = {
                {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, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 1},
                {1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                {1, 0, 0, 1, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                {1, 0, 8, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                {1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1},
                {1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 4, 0, 0, 0, 1},
                {1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1},
                {1, 0, 0, 0, 4, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1},
                {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 1},
                {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
        };
        return no3;

    }
}

游戏演示

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 5
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
基于Java Swing推箱子游戏设计与实现主要包括以下几个步骤: 1. 游戏界面设计:利用Swing提供的组件进行界面设计,包括游戏地图、箱子、目标点、人物等元素的图形化展示,并添加按钮以及文本框等交互组件。 2. 游戏逻辑设计:设计游戏规则和逻辑,包括人物移动、箱子推动、胜利判断等核心功能。通过监听按钮事件以及键盘事件来实现玩家的操作,并根据操作进行游戏状态的更新。 3. 地图设计与关卡:设计游戏地图以及关卡,可以通过二维数组或者读取外部文件的方式来表示地图。每个关卡的地图布局和目标点位置可以通过代码或者配置文件进行定义。 4. 碰撞检测与移动处理:在游戏中需要处理地图、箱子、目标点以及人物之间的碰撞关系,通过判断下一步的位置是否可行来确定是否进行移动操作,避免发生错误的移动。 5. 游戏状态管理:设计游戏的状态管理机制,包括游戏开始、暂停、重新开始、胜利、失败等状态的切换和更新。同时需要记录游戏的当前状态,并在界面上进行相应的显示和提示。 6. 游戏界面布局和美化:通过合理的布局和设计来提高游戏的美观度和可操作性,可以考虑添加音效、动画效果等来增加游戏的趣味性。 7. 优化和测试:在开发完成后进行测试,包括对游戏逻辑和界面的测试、异常处理的测试以及性能优化的测试,保证游戏的稳定性和流畅性。 通过以上步骤的设计和实现,基于Java Swing推箱子游戏可以有效地展示出游戏的功能和特色,提供给用户一个良好的游戏体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值