JAVA开发拼图游戏

前言

游戏描述:游戏界面

功能:

技术:JAVA SE,swing库和action event

逻辑:这个拼图游戏是4*4的,定义一个二维数组来存储1~16;每个数字代表一个图片,游戏开始前打乱数组数据顺序,然后往界面插入图片(因为数组是乱序的,图片也是乱序的),图片中有一个是空白图片,图片数字为16,我们只需要移动16这个数字的图片,每次移动,刷新当前界面图片,当我们把拼图完成时,即数组排好序,即获得胜利。

实现游戏视图图框架代码:

private void initJFrame() {
    this.setSize(603,680);
    this.setTitle("拼图游戏v1.0");
    this.setLocationRelativeTo(null);       //窗口居中对齐
    this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    this.setLayout(null);
    this.addKeyListener(this);
}

实现游戏菜单功能代码:

    JMenuBar menuBar = new JMenuBar();
    JMenu functionJMenu = new JMenu("功能");
    JMenu aboutsJMenu = new JMenu("关于我们");
    JMenu  changeItem = new JMenu("更换图片");
​
    JMenuItem replayItem = new JMenuItem("重新游戏");
    JMenuItem reloadItem = new JMenuItem("重新登录");
    JMenuItem closeItem = new JMenuItem("关闭游戏");
​
    JMenuItem animalItem = new JMenuItem("动物");
    JMenuItem girlItem = new JMenuItem("美女");
    JMenuItem sportItem = new JMenuItem("运动");
​
​
    JMenuItem accountItem = new JMenuItem("公众号");
private void initMenu() {        //菜单
​
​
        replayItem.addActionListener(this);
        reloadItem.addActionListener(this);
        closeItem.addActionListener(this);
        accountItem.addActionListener(this);
        animalItem.addActionListener(this);
        girlItem.addActionListener(this);
        sportItem.addActionListener(this);
​
​
        functionJMenu.add(changeItem);
        functionJMenu.add(replayItem);
        functionJMenu.add(reloadItem);
        functionJMenu.add(closeItem);
        changeItem.add(animalItem);
        changeItem.add(girlItem);
        changeItem.add(sportItem);
​
​
        aboutsJMenu.add(accountItem);
​
        menuBar.add(functionJMenu);
        menuBar.add(aboutsJMenu);
​
        this.setJMenuBar(menuBar);
    }

实现插入图片的代码:

 private void initImage() {
        this.getContentPane().removeAll();
        if (victory()) {
             JLabel winJlabel = new JLabel(new ImageIcon("puzzlegame\\image\\win.png"));
            winJlabel.setBounds(203,283,197,73);
            this.getContentPane().add(winJlabel);
        }
        JLabel stepCount = new JLabel("步数"+step);
        stepCount.setBounds(50,30,100,20);
        this.getContentPane().add(stepCount);
        for(int i =0;i<4;i++){//图片导入
            for(int j = 0;j<4;j++){
                JLabel label = new JLabel(new ImageIcon( path.toString()+"\\"+date[i][j]+".jpg"));
                label.setBounds(j*105+83,i*105+134,105,105);
                label.setBorder(new BevelBorder(1));
                this.getContentPane().add(label);
            }
        }
        JLabel background = new JLabel(new ImageIcon("puzzlegame\\image\\background.png"));
        background.setBounds(40,40,508,560);
        this.getContentPane().add(background);
//        this.add(label);
        this.getContentPane().repaint();
    }

实现键盘操作行为和其逻辑代码:

public void keyReleased(KeyEvent e) {
        if(victory()){
            return;
        }
        int code = e.getKeyCode();//左37   上38 右39 下40
        if(code == 37){
            if(y!=0){
                date[x][y]=date[x][y-1];
                date[x][y-1]=0;
                y=y-1;
                step++;
            }
​
        }
        else if (code ==39){
            if(y!=3){
                date[x][y]=date[x][y+1];
                date[x][y+1]=0;
                y=y+1;
                step++;
            }
        } else if (code == 38) {
            if(x!=0){
                date[x][y]=date[x-1][y];
                date[x-1][y]=0;
                x=x-1;
                step++;
            }
​
        } else if (code == 40) {
            if(x!=3){
                date[x][y]=date[x+1][y];
                date[x+1][y]=0;
                x=x+1;
                step++;
            }
​
        } else if (code == 65) {
            initImage();
​
        } else if(code == 87){
            this.getContentPane().removeAll();
            for(int i =0;i<15;i++){
                date[i/4][i%4]=i+1;
            }
            date[3][3]=0;
​
        }else {
            return;
        }
        this.initImage();
​
    }

实现鼠标操作行为以及功能实现代码:

public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if(source==replayItem){
             step=0;
             initDate();
             initImage();
​
        } else if (source==reloadItem) {
            this.setVisible(false);
​
        } else if (source==closeItem) {
            System.exit(0);
​
        } else if (source==animalItem) {
​
            path.delete(40,path.length());// D:\stunew\java\javastu1\puzzlegame\image
            path.append("\\animal\\animal");
            if(changeTime>8){
                changeTime=1;
            }
            if(changeTime==8){
                path.append(changeTime);
                changeTime=1;
            }else{
                path.append(changeTime);
                changeTime++;
            }
            step=0;
            initDate();
            initImage();
​
        } else if (source==girlItem) {
            path.delete(40,path.length());
            path.append("\\girl\\girl");
​
            if(changeTime==13){
                path.append(changeTime);
                changeTime=1;
            }else{
                path.append(changeTime);
                changeTime++;
            }
            step=0;
            initDate();
            initImage();
​
        } else if (source==sportItem) {
            path.delete(40,path.length());// D:\stunew\java\javastu1\puzzlegame\image
            path.append("\\sport\\sport");
            if(changeTime>10){
                changeTime=1;
            }
            if(changeTime==10){
                path.append(changeTime);
                changeTime=1;
            }else{
                path.append(changeTime);
                changeTime++;
            }
            step=0;
            initDate();
            initImage();
​
​
        } else if (source==accountItem) {
            JFrame frame = new JFrame();
            frame.setSize(300,300);
            frame.setLocationRelativeTo(null);       //窗口居中对齐
            JLabel aboutJLabel = new JLabel(new ImageIcon("D:\\stunew\\java\\javastu1\\puzzlegame\\image\\about.png"));
            aboutJLabel.setBounds(20,0,258 ,258);
            frame.getContentPane().add(aboutJLabel);
            frame.setLayout(null);
            frame.setVisible(true);
​
​
        }
​
    }

判断胜利条件代码:

public boolean victory(){
    for(int x=0;x<4;x++){
        for (int y=0;y<4;y++){
            if(date[x][y]!=win[x][y]){
                return false;
            }
        }
    }
    return true;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值