太空侵略者游戏

发布于 2024-09-23 23:48:08 字数 731 浏览 2 评论 0 原文

我正在编写一款太空入侵者游戏,我需要编写 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

执手闯天涯 2024-09-30 23:48:08

在 Java 5 及更高版本中,您可以使用泛型让您的生活稍微变得更加简单。使用这些定义:

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 ...;

要添加命中,请使用以下代码:

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);
}

也就是说,我建议使用稍微不同的 API:将“命中”列表添加到 SpaceShip 和一个布尔字段 hit(或者可能是对其击中的太空飞船的引用)到Blink。这样,相关信息将位于受影响的对象实例中,您可以在 spaceShipsblinks 列表上使用简单的过滤器来获取其他三个列表/地图。

请注意你的命名。 “眨眼”不应该是“导弹”或“射击”吗?

In Java 5 and up, you can use Generics to make your life slightly more simple. 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. 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.

And mind your naming. Shouldn't "Blink" be "Missile" or "Shot"?

清风无影 2024-09-30 23:48:08

因为这个作业的一些一般性观点(至少,我是这么认为的 - 如果情况并非如此,请随时澄清)。

  1. 尽可能在数据结构中使用泛型。例如 List spaceShips
  2. 默认情况下不要声明变量 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).

  1. Use generics in your data structures where possible. E.g. 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)

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. Therefore there's not much choice but to declare 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 ?). As each SpaceShip gets hit, remove it from the misses list.

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.

送你一个梦 2024-09-30 23:48:08

您还应该记住,您可以使用继承,并以这种方式定义具有相同行为或与其他对象交互的类组。

在设计面向对象的游戏时,您应该始终记住,有些图形表示会移动,有些不会,有些会破坏其他图形,有些会穿过其他图形,有些则不会。 (可能有不同的优先级)。

另外,我以吃豆人游戏为例(还有什么: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 !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文