zoukankan      html  css  js  c++  java
  • 杨校老师课堂之喜洋洋大战灰太狼

    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;
        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("鼎利学院出品  推箱子 v1.0   作者QQ:35725672");
    
            this.setLocation(110,30);
    
            this.setSize(826,650);
    
            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] == 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);
    
             }
            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] == 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);//因为是37 所以是往左走,为横坐标移动 ,  往右是前进 所以是  -
                ImageIcon icon = new ImageIcon("01.png");
                wolf_lab.setIcon(icon);
            }
            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] == 4){
                   return;
                }
                if (datas[wy - 1][wx]== 4 && datas[wy - 2][wx] == 0){
                    datas[wy-1][wx] = 0;
                    datas[wy-2][wx] = 4;
                }
                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);
            }
            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] == 4){
                    return;
                }
                if (datas[wy + 1][wx]== 4 && datas[wy + 2][wx] == 0){
                    datas[wy+1][wx] = 0;
                    datas[wy+2][wx] = 4;
                }
                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);
            }
    
        }
    
    
    }
    

      



    public class App {
        public static void main(String[] args) {
            new MainFrame();
        }
    }
    

      

    素材

     

  • 相关阅读:
    就业DAY7_web服务器_tcp三次握手四次挥手,返回浏览器需要的页面http服务器
    就业DAY7_web服务器_http协议
    就业DAY6_web服务器_正则表达式
    就业DAY5_多任务_协程
    就业DAY5_多任务_进程,进程池,队列
    win10安装ubuntu系统,报错WslRegisterDistribution failed with error: 0x8007019e
    解决ubuntu与win10双系统时间不同步
    Linux常用压缩解压命令
    ubuntu添加国内源
    解决Ubuntu“下载额外数据文件失败 ttf-mscorefonts-installer”的问题 (转载)
  • 原文地址:https://www.cnblogs.com/xiaoxiao5016/p/15611381.html
Copyright © 2011-2022 走看看