我们依旧会遇到无限的递归循环之中,即如果我们将Push块重叠,控制台报错:
此时,较好的解决办法就是,碰到stop块反弹后,我们立刻返回上级递归
/** * stop逻辑 */ 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; } return;// 优化 } }
仅仅多了一行return,游戏的运行效率大大提高,无限递归也解决了,同理,由于我们游戏中每个Block只能碰撞stop、push、win、text一次,因此每种碰撞结果后,都添加return,这样可以避免一些莫名其妙的报错
我们操作Text的逻辑与Push块相同,因此,我们需要让GameFrame将gameText也传递给GamePeople,此外,我们需要注意的是,我们要将you的类型改为People和Text的父类Block.这里就运用了面向对象的继承、多态思想。
此外,这里的逻辑与push基本相同,如下为代码展示:
/** * text推动逻辑 */ List<Text> textList = gameText.getTextList(); for (int i = 0; i < textList.size(); i++) { Text text = textList.get(i); if(text == you) continue; if(text.getX() == you.getX() && text.getY() == you.getY()) { move(direction, text, stop, push, gameText); } return; }
现在,我们处理游戏胜利的判断,同上面一样,我们先默认FLAG is WIN,先在GamePeople完成WIN的逻辑:
String win = PeopleLocation.FLAG; // 默认flag is win
public void move(int direction, Block you, String stop, String push, String win , GameText gameText) { /** * 先移动 */ 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; } /** * stop逻辑 */ 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; } return;// 优化 } } /** * push逻辑 */ List<People> pushList = this.peopleMap.get(push); for (int i = 0; i < pushList.size(); i++) { // 遍历所有Push的对象判断 People pushPeople = pushList.get(i); if (pushPeople == you) continue; if (pushPeople.getX() == you.getX() && pushPeople.getY() == you.getY()) { // 发生碰撞 move(direction, pushPeople, stop, push,win, gameText); } } /** * text推动逻辑 */ List<Text> textList = gameText.getTextList(); for (int i = 0; i < textList.size(); i++) { Text text = textList.get(i); if(text == you) continue; if(text.getX() == you.getX() && text.getY() == you.getY()) { move(direction, text, stop, push,win, gameText); return; } } /** * win逻辑 */ List<People> winList = this.peopleMap.get(win); for (int i = 0; i < winList.size(); i++) { People winPeople = winList.get(i); if(winPeople.getX() == you.getX() && winPeople.getY() == you.getY()) { System.out.println("胜利了"); return; } } }
目前,我们的获胜条件从控制台输出,之后,我们回对获胜界面进行优化
那么,现在我们完成了游戏基本的逻辑,下几章,我们进行改变游戏规则的逻辑实现。