问题描述
我正在尝试用Java制作小型游戏,挥杆动作,但我找不到解决问题的好方法。我有两个数组,第一个是Crate对象
I'm trying to make a small game in Java, swing, and I can't figure out a good way to solve my problem. I've got two arrays, first of Crate objects
public class Crate {
private static ImageIcon crate = new ImageIcon(Player.class.getResource("/Images/crate.jpg"));
private int x=0;
private int y=0;
private static int Xdisplacement;
private static int Ydisplacement;
private int id;
//getters and setters n stuff
第二个是Tile对象。
second one is of a Tile objects.
public class Tile {
private static ImageIcon tile = new ImageIcon(Player.class.getResource("/Images/tile.jpg"));
private int x=0;
private int y=0;
private boolean hasBox=false;
//getters and setters n stuff
我想检查是否放置了所有箱子在瓷砖上。我的意思是,哪个盒子在哪个图块上,哪个盒子在几个图块上,几个瓷砖在哪,每个盒子都应该放在一个图块上都没有关系,无论哪个盒子在哪个图块上。在游戏中,玩家走来走去的板条箱,因此他们的坐标发生了变化。瓦片坐标不会改变(如果有帮助的话)。这将是我的停止条件。将板条箱放置在瓷砖上时,游戏结束。
我想在另一个循环中创建一个循环,然后检查每个对象的每个字段不是一个好的解决方案?那么还有其他方法吗?
I would like to check if all Crates are placed on Tiles. I mean it doesn't matter which box is on which tile, there are few boxes, few tiles, and each box should be placed on a tile, doesn't matter which box on which tile. In the game, player walks and move crates around, so their coords change. Tiles coords do not change (if it may help). This is gonna be my stop condition. The game is over when crates are placed on tiles.I suppose making a loop inside another loop and then checking every single field of every single object isn't a good solution? So are there any other ways to do so?
推荐答案
完整的设计超出了StackOverflow的范围,但是您可能想要使用模式,如。答案包括一个非常简单的示例游戏,并引用了一个更复杂的游戏,其中涉及在平铺网格上的玩家。
A complete design is beyond the scope of StackOverflow, but you will probably want to use the model-view-controller pattern, illustrated here. The answer includes a very simple example game and cites a more complex game involving players on a tiled grid.
附录:在特定情况下,在移动后检查网格占用者,引用的示例使用嵌套循环在方法 RCModel#moveBots()
中对照副本检查当前状态。
Addendum: In the particular case of checking grid occupants after a move, the example cited uses nested loops to check the current state against a copy in the method RCModel#moveBots()
.
这篇关于检查一个数组的所有元素是否与另一个数组的所有元素具有相同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!