我正在编写一款太空入侵者游戏,我需要编写 5 个公共实例变量,其中包含记录一次游戏运行的所有信息的集合:
spaceShips 将引用 SpaceShip 列表,按照它们出现在屏幕上的顺序
公共列表 spaceShips;
Blinks 是按照 Blink 发生顺序引用所有 Blink 实例的列表的镜头
公共列表闪烁;
hitMap 它将引用地图,其键将是被眨眼击中的宇宙飞船的实例,其值将列出相应的“成功”眨眼实例
???
unscathed 它将引用未被任何眨眼“击中”的所有 SpaceShip 实例的列表
???
未击中,它将引用未“击中”任何宇宙飞船的所有 Blink 实例的列表
???
然后,我必须向构造函数添加行,以将 HashMap 的新实例分配给命中映射,并将 ArrayList 分配给其他变量,到目前为止我已经
spaceShips = new ArrayList();
闪烁 = new ArrayList();
任何帮助都会非常
高兴
I am writing a space invaders game I need to write 5 public instance variables which hold collections recording all of the information about one run of the game:
spaceShips will reference a list of SpaceShip, in the order they appeared on the screen
public List spaceShips;
blinks are shots which will reference a list of all instances of Blink in the order which they occured
public List blinks;
hitsMap which will reference a map, who keys will be instances of Spaceship that where hit by a blink and whose values will be lists corresponding 'successful' instances of blink
????
unscathed which will reference a list of all instances of SpaceShip that were not 'hit' by any blink
???
misses, which will reference a list of all instances of Blink that did not 'hit' any spaceship
???
I then have to add lines to the constructor to assign a new instance of HashMap to hits map and ArrayList to the other variables, so far i have
spaceShips = new ArrayList();
blinks = new ArrayList();
Any help would be great
cheers
发布评论
评论(3)
在 Java 5 及更高版本中,您可以使用泛型让您的生活稍微变得更加简单。使用这些定义:
要添加命中,请使用以下代码:
也就是说,我建议使用稍微不同的 API:将“命中”列表添加到
SpaceShip
和一个布尔字段hit
(或者可能是对其击中的太空飞船的引用)到Blink
。这样,相关信息将位于受影响的对象实例中,您可以在spaceShips
或blinks
列表上使用简单的过滤器来获取其他三个列表/地图。请注意你的命名。 “眨眼”不应该是“导弹”或“射击”吗?
In Java 5 and up, you can use Generics to make your life slightly more simple. Use these definitions:
To add a hit, use this code:
That said, I suggest a slightly different API: Add a list of "hits" to
SpaceShip
and a boolean fieldhit
(or maybe a reference to the space ship it did hit) toBlink
. That way, relevant information will be in the affected object instance and you can use a simple filter on the list ofspaceShips
orblinks
to get the other three lists/maps.And mind your naming. Shouldn't "Blink" be "Missile" or "Shot"?
因为这个作业的一些一般性观点(至少,我是这么认为的 - 如果情况并非如此,请随时澄清)。
List spaceShips
public
(尽管如此,我还没有看到你们其他人的代码,所以也许你们这样做是有充分理由的)已经说过您的
hitsMap
变量应该是Map
但后来你说过它需要是一个HashMap
。因此,除了声明Map> 之外,没有太多选择。 hitMap = new HashMap>();
对于您的
misses
列表,首先复制所有宇宙飞船的列表(可能通过 Collections.copy ?)。当每个SpaceShip
被击中时,将其从misses
列表中删除。您的
misses
列表应该只是一个List
,每当您确定是否Blink
时,都会将Blink
对象添加到其中。 code> 有问题的未命中或命中(在这种情况下,您必须编辑hitsMap
.Some general points because this homework (at least, I think so - feel free to clarify if this is not the case).
List<SpaceShip> spaceShips
public
by default (although having said that, I haven't seen the rest of you code so maybe there's a good reason why you've done that)You've said that your
hitsMap
variable should be an instance ofMap
but later on you've said that it needs to be aHashMap
. Therefore there's not much choice but to declareMap<SpaceShip, List<Blink>> hitsMap = new HashMap<SpaceShip, List<Blink>>();
For your
misses
list, start by copying your list of all spaceships (maybe via Collections.copy ?). As eachSpaceShip
gets hit, remove it from themisses
list.Your
misses
list should just be aList<Blink>
that getsBlink
objects added to it whenever you determine whether theBlink
in question misses or hits (in which case you have to edit thehitsMap
.您还应该记住,您可以使用继承,并以这种方式定义具有相同行为或与其他对象交互的类组。
在设计面向对象的游戏时,您应该始终记住,有些图形表示会移动,有些不会,有些会破坏其他图形,有些会穿过其他图形,有些则不会。 (可能有不同的优先级)。
另外,我以吃豆人游戏为例(还有什么:p),你必须记住一些游戏实体可以有状态(例如吃豆人上帝模式,或吃豆人脆弱,与幽灵相反)。因此,我们提出了将 Ghost 和 Pacman(与移动/状态相关)作为一个公共抽象类的子类的想法。
希望这有帮助!
You should also remember that you can use inheritance, and in this way define groups of classes that will have the same behavior, or interactions with other objects.
When Designing an object oriented game, you should always keep in mind that some graphic representations will move, others won't, some will destroy others, some will go through others, some won't. (probably with different priorities).
Also, I take the example of a PacMan game (what else :p) you have to remember some game entities can have states (like Pacman godmode, or Pacman vulnerable, opposite for Ghosts). Thus the idea of making Ghosts and Pacman (which are move/state related) children of a common abstract class.
Hope this helps !