java swing实战——baba is you(stop逻辑实现)
Posted 江淼98
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 swing实战——baba is you(stop逻辑实现)的主要内容,如果未能解决你的问题,请参考以下文章
java.lang.IllegalArgumentException: You must not call setTag() on a view Glide is targeting