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:
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 hav开发者_StackOverflowe 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
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"?
Some general points because this homework (at least, I think so - feel free to clarify if this is not the case).
- Use generics in your data structures where possible. E.g.
List<SpaceShip> spaceShips
- 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
.
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 !
精彩评论