简体   繁体   English

太空侵略者游戏

[英]Space invaders game

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: 我正在编写一个太空侵略者游戏,我需要编写5个公共实例变量,这些变量保存用于记录有关一次游戏运行的所有信息的集合:

spaceShips will reference a list of SpaceShip, in the order they appeared on the screen spaceShips将按照它们在屏幕上出现的顺序引用一个SpaceShip列表。

public List spaceShips; 公共列表空间

blinks are shots which will reference a list of all instances of Blink in the order which they occured 眨眼即是镜头,这些镜头将按照发生的顺序引用所有Blink实例的列表

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 hitsMap,它将引用地图,其键将是被眨眼命中的太空飞船的实例,其值将是对应于“成功的”眨眼实例的列表

???? ????

unscathed which will reference a list of all instances of SpaceShip that were not 'hit' by any blink 毫发无损,它将引用所有未被眨眼“击中”的SpaceShip实例的列表

??? ???

misses, which will reference a list of all instances of Blink that did not 'hit' any spaceship 未命中,它将引用未“击中”任何太空飞船的所有Blink实例的列表

??? ???

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 然后,我必须向构造函数中添加行,以将HashMap的新实例分配给hits map,并将ArrayList分配给其他变量,到目前为止,我已经

spaceShips = new ArrayList(); spaceShips = new ArrayList(); blinks = new ArrayList(); 闪烁=新的ArrayList();

Any help would be great 任何帮助都会很棒

cheers 干杯

In Java 5 and up, you can use Generics to make your life slightly more simple. 在Java 5及更高版本中,您可以使用泛型使您的生活稍微简单一些。 Use these definitions: 使用以下定义:

public List<SpaceShip> spaceShips = new ArrayList<SpaceShip>();
public List<Blink> blinks ...;
public Map<SpaceShip, List<Blink>> hitsMap = new HashMap<SpaceShip, List<Blink>>();
public List<SpaceShip> unscathed ...;
public List<Blink> misses ...;

To add a hit, use this code: 要添加匹配,请使用以下代码:

public void addHit(SpaceShip ship, Blink blink) {
    List<Blink> hits = hitsMap.get(spaceShip);
    if(null == hits) {
        hits = new ArrayList<Blank>();
        hitsMap.put(spaceShip, hits);
    }
    hits.add(blink);
}

That said, I suggest a slightly different API: Add a list of "hits" to SpaceShip and a boolean field hit (or maybe a reference to the space ship it did hit) to Blink . 就是说,我建议使用一个稍微不同的API:向SpaceShip添加一个“命中”列表,向Blink添加一个布尔字段hit (或对它确实命中的太空飞船的引用)。 That way, relevant information will be in the affected object instance and you can use a simple filter on the list of spaceShips or blinks to get the other three lists/maps. 这样,相关信息将出现在受影响的对象实例中,您可以在spaceShipsblinks列表上使用简单的过滤器来获取其他三个列表/映射。

And mind your naming. 请注意您的命名。 Shouldn't "Blink" be "Missile" or "Shot"? “眨眼”不应该是“导弹”还是“射击”?

Some general points because this homework (at least, I think so - feel free to clarify if this is not the case). 一些一般性的观点是因为这项作业(至少,我认为是这样-如果不是这样,请随时澄清)。

  1. Use generics in your data structures where possible. 尽可能在数据结构中使用泛型。 Eg List<SpaceShip> spaceShips 例如List<SpaceShip> spaceShips
  2. Don't declare variables 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) 默认情况下,不要将变量声明为public变量(尽管话虽如此,但我还没有看到其余的代码,所以也许这样做是有充分理由的)

You've said that your hitsMap variable should be an instance of Map but later on you've said that it needs to be a HashMap . 您已经说过hitsMap变量应该是Map的实例,但是稍后您又说它必须是HashMap Therefore there's not much choice but to declare Map<SpaceShip, List<Blink>> hitsMap = new HashMap<SpaceShip, List<Blink>>(); 因此,除了声明Map<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 ?). 对于misses列表,请先复制所有太空飞船的列表(也许通过Collections.copy ?)。 As each SpaceShip gets hit, remove it from the misses list. 当每个SpaceShip被击中时,将其从未misses列表中删除。

Your misses list should just be a List<Blink> that gets Blink objects added to it whenever you determine whether the Blink in question misses or hits (in which case you have to edit the hitsMap . 您的misses列表应该只是一个List<Blink> ,只要您确定所讨论的Blink是未命中还是命中(在这种情况下,您必须编辑hitsMap ,它就会向其中添加Blink对象。

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). 另外,我以PacMan游戏(其他:p)为例,您必须记住某些游戏实体可以具有状态(例如Pacman godmode或Pacman易受攻击,与Ghosts相反)。 Thus the idea of making Ghosts and Pacman (which are move/state related) children of a common abstract class. 因此,使Ghosts和Pacman(与移动/状态相关)的孩子成为一个通用抽象类的想法。

Hope this helps ! 希望这可以帮助 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM