喜洋洋大战灰太狼

简介: 喜洋洋大战灰太狼
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("CSDN博客作者  杨校出品  推箱子 v1.0   ");

        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();
    }
}

目录
相关文章
|
存储 关系型数据库 MySQL
mysql数据库备份与恢复
MySQL数据库的备份与恢复是确保数据安全性和业务连续性的关键操作。
876 4
|
2月前
|
人工智能 自然语言处理 文字识别
阿里云3组AI免费权益:新用户0成本领7000万tokens、500元算力券,试用30+款产品与100+Agent‌
本文梳理阿里云面向新用户的 AI 免费权益,分两类。一是大模型 tokens 免费额度,含"7000 万 tokens"组合权益,经免费试用页或体验馆领取,有效期 90 天,仅限华北 2(北京)地域,只抵扣实时推理费用,可开启"用完即停"防扣费。二是 AI 产品免费试用,覆盖 PAI、NLP、视觉智能、GPU 等超 30 款产品,仅限产品新用户,额度时长各异。两类可并行使用,领取前需确认资格并及时评估续费或关停。
|
7月前
|
人工智能 自然语言处理 Java
Java做AI真不行?2026年最被低估的机会来了
Spring官宣集成DeepSeek,Java正式迈入AI驱动时代!2026年AI岗位缺口巨大,大厂招聘普遍要求大模型能力。Java团队借力Spring生态与JBoltAI等国产框架,可低门槛接入代码生成、RAG、Agent等全链路AI能力,实现差异化突围。(239字)
577 3
|
9月前
|
存储 机器学习/深度学习 人工智能
智慧商场的新基石:如何利用玩美移动 AI API 打造下一代“智能试妆镜”
在实体零售转型浪潮中,玩美移动(Perfect Corp.)推出AI虚拟试妆(VTO)解决方案,赋能商场试妆镜:通过云端S2S工作流、亚像素级五官追踪与物理级妆效还原,实现非接触式、高拟真、全渠道一致的沉浸试妆体验,助力品牌提升转化与数据运营。(239字)
533 1
智慧商场的新基石:如何利用玩美移动 AI API 打造下一代“智能试妆镜”
|
9月前
|
传感器 机器学习/深度学习 资源调度
Figure 03自主清洁147秒引关注,马斯克亲自下场追问
Figure 03人形机器人搭载Helix 02模型,147秒全自主完成客厅清洁,无干预、无加速。其端到端神经网络实现感知-决策-控制一体化,凭真实家庭数据驱动技能迭代,展现卓越环境适应力与全身协同能力,加速人形机器人家用落地进程。
|
12月前
|
NoSQL Shell Linux
Windows 系统下的 MongoDB 单机部署
本文详细介绍 MongoDB 在 Windows 和 Linux 系统中的单机部署方法,涵盖下载安装、目录配置、服务启停、Shell 与 Compass 连接等步骤,助你快速搭建开发与生产环境。
|
存储 消息中间件 人工智能
【04】AI辅助编程完整的安卓二次商业实战-寻找修改替换新UI首页图标-菜单图标-消息列表图标-优雅草伊凡
【04】AI辅助编程完整的安卓二次商业实战-寻找修改替换新UI首页图标-菜单图标-消息列表图标-优雅草伊凡
796 4
|
算法 应用服务中间件 Shell
搭建高可用负载均衡器: haproxy+keepalived
企业业务量比较小的时候,单台服务器就可以满足业务需要了。但是随着业务发展,单服务器的问题就凸显出来了,如何透明的扩展服务器和带宽,增加服务器吞吐量呢?负载均衡器可以解决以上问题。
10735 47
|
开发工具 git 索引
如何使用Git的暂存区来管理代码更改?
如何使用Git的暂存区来管理代码更改?
1736 0