前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >喜洋洋大战灰太狼(大结局)

喜洋洋大战灰太狼(大结局)

作者头像
杨校
发布2021-11-30 19:46:07
5180
发布2021-11-30 19:46:07
举报
文章被收录于专栏:Java技术分享圈Java技术分享圈

效果图:

在这里插入图片描述
在这里插入图片描述
代码语言:javascript
复制
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class MainFrame extends Frame  implements KeyListener {

    public MainFrame() throws HeadlessException {
        targetInit(); // 目标位置  (笼子)
        wolfInit();// 狼
        sheepInit();// 羊   (箱子)
        treeInit();// 树
        backgroundInit();// 调用  初始化背景的方法
        setMainFrameUI();// 界面
        this.addKeyListener(this);// 键盘监听器
    }

    /**
     * 目标位置  (笼子)
     */
    private void targetInit() {
        ImageIcon i = new ImageIcon("8.png"); //  创建一张图片  笼的图片
        JLabel target1_Lab = new JLabel(i); // 使用JLabel 组件 模拟笼
        target1_Lab.setBounds(12+700,36+4*50,50,50);// 设置 笼的位置   四个空分别是  横坐标、纵坐标、笼的宽、笼的高
        this.add(target1_Lab);// 把笼添加到组件内
        datas[4][14] = 8;

        JLabel target2_Lab = new JLabel(i); // 使用JLabel 组件 模拟笼
        target2_Lab.setBounds(12+700,36+5*50,50,50);// 设置 笼的位置   四个空分别是  横坐标、纵坐标、笼的宽、笼的高
        this.add(target2_Lab);// 把笼添加到组件内
        datas[5][14] = 8;

        JLabel target3_Lab = new JLabel(i); // 使用JLabel 组件 模拟笼
        target3_Lab.setBounds(12+700,36+6*50,50,50);// 设置 笼的位置   四个空分别是  横坐标、纵坐标、笼的宽、笼的高
        this.add(target3_Lab);// 把笼添加到组件内
        datas[6][14] = 8;

        JLabel target4_Lab = new JLabel(i); // 使用JLabel 组件 模拟笼
        target4_Lab.setBounds(12+700,36+7*50,50,50);// 设置 笼的位置   四个空分别是  横坐标、纵坐标、笼的宽、笼的高
        this.add(target4_Lab);// 把笼添加到组件内
        datas[7][14] = 8;
    }

    JLabel [] []  sheeps = new JLabel[12][16];
    /**
     * 羊   (箱子)
     */
    private void sheepInit() {
        ImageIcon i = new ImageIcon("4.png"); //  创建一张图片  羊的图片
        JLabel sheep1_Lab = new JLabel(i); // 使用JLabel 组件 模拟羊
        sheep1_Lab.setBounds(12+6*50,36+2*50,50,50);// 设置 羊的位置   四个空分别是  横坐标、纵坐标、羊的宽、羊的高
        this.add(sheep1_Lab);// 把羊添加到组件内

        // 修改羊的位置  遍布在地图内
        datas[2][6] = 4;
        sheeps[2][6] = sheep1_Lab;

        JLabel sheep2_Lab = new JLabel(i); // 使用JLabel 组件 模拟羊
        sheep2_Lab.setBounds(12+6*50,36+4*50,50,50);// 设置 羊的位置   四个空分别是  横坐标、纵坐标、羊的宽、羊的高
        this.add(sheep2_Lab);// 把羊添加到组件内

        // 修改羊的位置  遍布在地图内
        datas[4][6] = 4;
        sheeps[4][6] = sheep2_Lab;
        JLabel sheep3_Lab = new JLabel(i); // 使用JLabel 组件 模拟羊
        sheep3_Lab.setBounds(12+6*50,36+6*50,50,50);// 设置 羊的位置   四个空分别是  横坐标、纵坐标、羊的宽、羊的高
        this.add(sheep3_Lab);// 把羊添加到组件内

        // 修改羊的位置  遍布在地图内
        datas[6][6] = 4;
        sheeps[6][6] = sheep3_Lab;

        JLabel sheep4_Lab = new JLabel(i); // 使用JLabel 组件 模拟羊
        sheep4_Lab.setBounds(12+6*50,36+8*50,50,50);// 设置 羊的位置   四个空分别是  横坐标、纵坐标、羊的宽、羊的高
        this.add(sheep4_Lab);// 把羊添加到组件内

        // 修改羊的位置  遍布在地图内
        datas[8][6] = 4;
        sheeps[8][6] = sheep4_Lab;
    }


    // 场景模拟  二维数组进行模拟
    //①  数字1 代表的是障碍
    //②  数字0 代表的是空地
    //③  数字4 代表的是羊(箱子)
    //④  数字8 代表的是目标(笼子)
    //④  数字12 代表的是羊进入了目标(笼子)
    // 12 row *  16col
    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,0,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;
    //代表狼的纵向位置
    int wy;

    int num  = 0 ;//当前的羊进入笼内的个数

    int total= 4; //一共有多少个笼

    private void treeInit() {
        ImageIcon i = new ImageIcon("1.png");
        for (int j = 0; j < datas.length; j++) {
            for (int k = 0; k < datas[j].length; k++) {
                if (datas[j][k] == 1){
                    JLabel lab = new JLabel(i);

                    lab.setBounds(12+50*k,36+50*j,50,50);

                    this.add(lab);
                }
            }
        }
    }
    JLabel wolf_lab;
    //显示狼的方法
    private void wolfInit() {

       wx = 4;
       wy = 5;

        ImageIcon i = new ImageIcon("-10.png");

        wolf_lab  = new JLabel(i);

        wolf_lab.setBounds(12+ wx *50 , 36+ wy *50,50,50);

        this.add(wolf_lab);
    }

    // 下面方法是处理  大草坪 背景的
    private void backgroundInit() {

        ImageIcon i = new ImageIcon("floor.png");

        JLabel lab = new JLabel(i);

        lab.setBounds(12,36,800,600);

        this.add(lab);
    }


    // 显示窗体背景
    public void  setMainFrameUI(){

        this.setTitle(BoxConfig.title);

        this.setLocation(BoxConfig.Uix,BoxConfig.Uiy);

        this.setSize(BoxConfig.width,BoxConfig.height);

        this.setVisible(true); //  设置窗口显示出来
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {

    }
    // ←   会显示 37数字
    // ↑   会显示 38数字
    // →   会显示 39数字
    // ↓   会显示 40数字
    @Override
    public void keyReleased(KeyEvent e) {
        int keyNumber = e.getKeyCode();
        System.out.println(keyNumber);
        if (keyNumber == 39){
             if (datas[wy][wx + 1] == 0){ // 判断是 空地
                 wx = wx  + 1;
                 int x = (int) wolf_lab.getLocation().getX();//获取狼的横坐标
                 int y = (int) wolf_lab.getLocation().getY();//获取狼的纵坐标
                 wolf_lab.setLocation(x + 50 , y);//因为是39 所以是往右走,为横坐标移动 ,  往右是前进 所以是  +
                 ImageIcon icon = new ImageIcon("0-1.png");
                 wolf_lab.setIcon(icon);
             }
             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] == 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;//目标地
            }
            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] == 4 && datas[wy][wx + 2] == 0){// 判断的是 狼碰到羊 且 羊往右还是空地时
                datas[wy][wx + 1] = 0;
                datas[wy][wx + 2] = 4;
            }
            sheeps[wy][wx+1].setLocation(12+wx*50+100 , 36+wy*50);
            sheeps[wy][wx+2] = sheeps[wy][wx+1];
            sheeps[wy][wx+1] = null;
            wx = wx + 1;
            int x = (int) wolf_lab.getLocation().getX();//获取狼的横坐标
            int y = (int) wolf_lab.getLocation().getY();//获取狼的纵坐标
            wolf_lab.setLocation(x + 50 , y);//因为是39 所以是往右走,为横坐标移动 ,  往右是前进 所以是  +
            ImageIcon icon = new ImageIcon("0-1.png");
            wolf_lab.setIcon(icon);
            vectory();

         }
        if (keyNumber == 37){
            if (datas[wy][wx - 1] == 0){
                wx = wx  - 1;
                int x = (int) wolf_lab.getLocation().getX();//获取狼的横坐标
                int y = (int) wolf_lab.getLocation().getY();//获取狼的纵坐标
                wolf_lab.setLocation(x - 50 , y);//因为是37 所以是往左走,为横坐标移动 ,  往右是前进 所以是  -
                ImageIcon icon = new ImageIcon("01.png");
                wolf_lab.setIcon(icon);
            }
            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] == 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;
            }

            sheeps[wy][wx-1].setLocation(12+wx*50-100 , 36+wy*50);
            sheeps[wy][wx-2] = sheeps[wy][wx-1];
            sheeps[wy][wx-1] = null;
            wx = wx - 1;
            int x = (int) wolf_lab.getLocation().getX();//获取狼的横坐标
            int y = (int) wolf_lab.getLocation().getY();//获取狼的纵坐标
            wolf_lab.setLocation(x - 50 , y);//因为是37 所以是往左走,为横坐标移动 ,  往右是前进 所以是  -
            ImageIcon icon = new ImageIcon("01.png");
            wolf_lab.setIcon(icon);
            vectory();
        }
        if (keyNumber == 38){
            if (datas[wy - 1][wx] == 0){
                wy = wy  - 1;
                int x = (int) wolf_lab.getLocation().getX();//获取狼的横坐标
                int y = (int) wolf_lab.getLocation().getY();//获取狼的纵坐标
                wolf_lab.setLocation(x  , y- 50);//因为是37 所以是往左走,为横坐标移动 ,  往右是前进 所以是  +
                ImageIcon icon = new ImageIcon("10.png");
                wolf_lab.setIcon(icon);
            }
            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]== 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;
            }

            sheeps[wy-1][wx].setLocation(12+wx*50,36+wy*50-100);
            sheeps[wy-2][wx]= sheeps[wy-1][wx];
            sheeps[wy-1][wx] = null;
            wy = wy  - 1;
            int x = (int) wolf_lab.getLocation().getX();//获取狼的横坐标
            int y = (int) wolf_lab.getLocation().getY();//获取狼的纵坐标
            wolf_lab.setLocation(x  , y- 50);//因为是37 所以是往左走,为横坐标移动 ,  往右是前进 所以是  +
            ImageIcon icon = new ImageIcon("10.png");
            wolf_lab.setIcon(icon);
            vectory();
        }
        if (keyNumber == 40){
            if (datas[wy + 1][wx] == 0){
                wy = wy  + 1;
                int x = (int) wolf_lab.getLocation().getX();//获取狼的横坐标
                int y = (int) wolf_lab.getLocation().getY();//获取狼的纵坐标
                wolf_lab.setLocation(x  , y+ 50);//因为是37 所以是往左走,为横坐标移动 ,  往右是前进 所以是  +
                ImageIcon icon = new ImageIcon("-10.png");
                wolf_lab.setIcon(icon);
            }
            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]== 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;
            }
            sheeps[wy+1][wx].setLocation(12+wx*50,36+wy*50+100);
            sheeps[wy+2][wx]= sheeps[wy+1][wx];
            sheeps[wy+1][wx] = null;
            wy = wy  + 1;
            int x = (int) wolf_lab.getLocation().getX();//获取狼的横坐标
            int y = (int) wolf_lab.getLocation().getY();//获取狼的纵坐标
            wolf_lab.setLocation(x  , y+ 50);//因为是37 所以是往左走,为横坐标移动 ,  往右是前进 所以是  +
            ImageIcon icon = new ImageIcon("-10.png");
            wolf_lab.setIcon(icon);
            vectory();
        }

    }

    private void vectory() {

        if (num == total){
            System.out.println("您已获得胜利!");
        }

    }


}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-11-30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档