java swing实战——baba is you(墙体碰撞)

4.2 墙体碰撞

我们的角色遇到墙壁,不可以前进,具体的逻辑如下:

​ 角色撞击墙壁,墙壁不动,角色被弹回

处理逻辑交由GameClosure,每次绘制前判断,是否有墙块Closure与其他块碰撞,如果碰撞,该角色块反弹

  • 首先GameClosure的draw(Graphics graphics, GamePeople gamePeople, GameText gameText),新增了两个参数,即可能发生碰撞的gamePeople和gameText
  • 对每个墙块Closure检测是否与gamePeople和gameText发生碰撞,如果碰撞,撞击块反弹
  • 反弹逻辑,如果向上移动1格,反弹后就向下1格,因此我们的Block需要一个方向属性

4.2.1 添加方向属性

    public boolean up, down, right, left;

​ 设置为公共属性,便于后续操作;此外,我们对这些变量初始化时在gamePeople.move()中:

    public void move(int direction, People you) {
        /**
         * 先移动
         */
        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;
        }
    }

4.2.2 墙体检测与反弹

本游戏中,我们规定好的单元格大小,碰撞时,只需判断坐标重合即可

/**
 * 绘制墙体
 */
public void draw(Graphics graphics, GamePeople gamePeople, GameText gameText) {

    List<People> peopleList = gamePeople.getPeopleList();
    List<Text> textList = gameText.getTextList();
    /**
     * people碰撞判断
     */
    for (int i = 0; i < peopleList.size(); i++) {
        People people = peopleList.get(i);
        for (int j = 0; j < closureList.size(); j++) {
            Closure closure = closureList.get(j);
            // 碰撞判断
            if(people.getX() == closure.getX() && people.getY() == closure.getY()) {
                /**
                 * 反弹逻辑
                 */
                if (people.up) {
                    people.setY(people.getY() + 24);//y += 24;
                } else if (people.down) {
                    people.setY(people.getY() - 24);//y -= 24;
                } else if (people.right) {
                    people.setX(people.getX() - 24);//x -= 24;
                } else if (people.left) {
                    people.setX(people.getX() + 24);//x += 24;
                }
            }
        }
    }

    /**
     * text碰撞
     */
    for (int i = 0; i < textList.size(); i++) {
        Text text = textList.get(i);
        for (int j = 0; j < closureList.size(); j++) {
            Closure closure = closureList.get(j);
            if (text.getX() == closure.getX() && text.getY() == closure.getY()) {
                if (text.up) {
                    text.setY(text.getY() + 24);//y += 24;
                } else if (text.down) {
                    text.setY(text.getY() - 24);//y -= 24;
                } else if (text.right) {
                    text.setX(text.getX() - 24);//x -= 24;
                } else if (text.left) {
                    text.setX(text.getX() + 24);//x += 24;
                }
            }
        }
    }

    
    URL resource = GameFrame.class.getResource("asset/icon/LAVA.gif");
    Image image = new ImageIcon(resource).getImage();
    for (int i = 0; i < closureList.size(); i++) {
        Closure closure = closureList.get(i);
        graphics.drawImage(image,closure.getX(),closure.getY(),null);
    }
}

至此,我们的角色便无法走出围墙

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>