IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> java swing实战——baba is you(stop逻辑实现) -> 正文阅读

[Java知识库]java swing实战——baba is you(stop逻辑实现)

4.3 WALL is STOP

? stop不同于gameClosure, 因为stop的对象有可能变化,因此碰撞具有stop属性的物体,应该交由gamePeople,为了简化,我们先假定WALL IS STOP.

private void peopleMove(KeyEvent e) {
    String stop = PeopleLocation.WALL;
    /**
     * 遍历循环(添加GamePeople的getPeopleList()方法)
     * 寻找可以移动的角色
     */
    List<People> peopleList = gamePeople.getPeopleList();
    for (int i = 0; i < peopleList.size(); i++) {
        People people = peopleList.get(i);
        if(PeopleLocation.BABA.equalsIgnoreCase(people.getName())) {
            switch (e.getKeyCode()) {
                case KeyEvent.VK_UP:
                    gamePeople.move(1,people,stop);
                    break;
                case KeyEvent.VK_DOWN:
                    gamePeople.move(-1,people,stop);
                    break;
                case KeyEvent.VK_RIGHT:
                    gamePeople.move(2,people,stop);
                    break;
                case KeyEvent.VK_LEFT:
                    gamePeople.move(-2,people,stop);
                    break;
            }
        }
    }
}

? 我们在GamePeople中的move方法,遍历找出who is stop,当然,此时,使用List,遍历操作有些麻烦,于是,我们使用了数据结构Map.(当然,初始化的工作也增加了)

private List<People> peopleList;
// 便于后续数据操作
private Map<String,List<People>> peopleMap;
public GamePeople() {
    this.peopleList = new ArrayList<>();
    this.peopleMap = new HashMap<>(); // 初始化

    /**
     * 4种可操作对象,之后可增加water等
     */
    this.peopleMap.put(PeopleLocation.BABA, new ArrayList<People>());
    this.peopleMap.put(PeopleLocation.ROCK, new ArrayList<People>());
    this.peopleMap.put(PeopleLocation.WALL, new ArrayList<People>());
    this.peopleMap.put(PeopleLocation.FLAG, new ArrayList<People>());


    People baba = new People(5 * 24, 9 * 24, PeopleLocation.BABA);
    this.peopleList.add(baba);
    this.peopleMap.get(PeopleLocation.BABA).add(baba);

    People flag = new People(14 * 24, 9 * 24, PeopleLocation.FLAG);
    this.peopleList.add(flag);
    this.peopleMap.get(PeopleLocation.FLAG).add(flag);


    People rock0 = new People(9 * 24, 8 * 24, PeopleLocation.ROCK);
    People rock1 = new People(9 * 24, 9 * 24, PeopleLocation.ROCK);
    People rock2 = new People(9 * 24, 10 * 24, PeopleLocation.ROCK);
    this.peopleList.add(rock0);this.peopleMap.get(PeopleLocation.ROCK).add(rock0);
    this.peopleList.add(rock1);this.peopleMap.get(PeopleLocation.ROCK).add(rock1);
    this.peopleList.add(rock2);this.peopleMap.get(PeopleLocation.ROCK).add(rock2);


    for (int i = 4; i < 15; i++) {
        People wall0 = new People(i * 24, 7 * 24, PeopleLocation.WALL);
        People wall1 = new People(i * 24, 11 * 24, PeopleLocation.WALL);
        this.peopleList.add(wall0);this.peopleMap.get(PeopleLocation.WALL).add(wall0);
        this.peopleList.add(wall1);;this.peopleMap.get(PeopleLocation.WALL).add(wall1);
    }
}

修改GamePeople种的move方法:(这里物体先移动,后碰撞,反弹)

public void move(int direction, People you, String stop) {
    /**
     * 先移动
     */
    switch (direction) {
        case 1:
            you.up = true;
            you.down = you.right = you.left = false;
            you.setY(you.getY() - 24);//y -= 24;
            break;
        case -1:
            you.down = true;
            you.up = you.right = you.left = false;
            you.setY(you.getY() + 24);//y += 24;
            break;
        case 2:
            you.right = true;
            you.up = you.down = you.left = false;
            you.setX(you.getX() + 24);//x += 24;
            break;
        case -2:
            you.left = true;
            you.up = you.right = you.down = false;
            you.setX(you.getX() - 24);//x -= 24;
            break;
    }

    List<People> stopList = this.peopleMap.get(stop);
    for (int i = 0; i < stopList.size(); i++) { // 遍历所有不可动的对象判断
        People stopPeople = stopList.get(i);
        if(stopPeople.getX() == you.getX() && stopPeople.getY() == you.getY()) { // 发生碰撞
            // 反弹
            if (you.up) {
                you.setY(you.getY() + 24);//y += 24;
            } else if (you.down) {
                you.setY(you.getY() - 24);//y -= 24;
            } else if (you.right) {
                you.setX(you.getX() - 24);//x -= 24;
            } else if (you.left) {
                you.setX(you.getX() + 24);//x += 24;
            }
        }
    }


}

? 此时,我们控制的角色便无法穿墙和WALL.之后,我们会进行游戏的核心逻辑ROCK IS PUSH.目前,大家操作的角色在穿越rock时,显示在rock后,这是由于我们绘制时先绘制baba,后绘制rock,因此导致rock覆盖了baba.因此,大家可以根据自己需求,调整draw()的先后顺序。

  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2022-01-08 13:51:00  更:2022-01-08 13:51:44 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 7:46:16-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码