Managing data in a game
I'm working on a pokemon clone and am the point where I want to include the pokemon and attacks. My initial thought is to go the OO way and make some abstract classes and have pokemon extend those classes(one generic pokemon, and one per type). My predicament is that I do not think it would be a good idea to have 151 classes, it seems like too many to manage. I need each pokemon (think of them as enemies if you are not familiar with pokemon) to have it's own images, stats, name, and other misc. details. Is there a way to represent all 151 of these pokemon in a more concise ma开发者_JAVA百科nner while still sticking to the OOP? I also have the same issue for representing an attack, (there are ~100 of them), each has an image/animation, a different effect, a different name etc.
Sounds like this is more of a resource management problem than an OO problem. I'd create a generic pokemon object and then use an external resource (since Java, probably XML or JSON) to populate the members. You could even get fancy and use some form of object binding (JAXB, GSON, etc) to automate the process. All depends on how complicated/automated you want this to be. I tend to try things manually, learn the automatic ways of doing the task, and improve the original task.
super simple example:
class Pokemon
{
private String name; // assume getters/setters
private List<Attack> attackList;
}
XML file:
<pokemon>
<name>Charizard</name>
<attacks>
<attack dmg="50">Some name for an attack</attack>
...
</attacks>
</pokemon>
Pseudo code:
Load XML file
For each pokemon entry in xml
Create a new Pokemon object, set the fields
Add to list of pokemon
Continue with Game stuff
Why not just make a generic pokemon class with fields for images and stats and such, and then make each pokemon an object of that class? The whole point of OOP is that each object of the class will be able to access the same methods but have individual data. You'd want to extend the pokemon class for different types of pokemon, but not just different ones. So you might have separate classes for flying pokemon (if those exist) and non-flying ones. Similarly, you'd make one attack class and create an object of that class for each individual attack.
What does all Pokémons have in common? They all have a name, a maximum of four attacks, an image to represent them, some lines about them for the Pokédex and so on.
I'd just write a struct called pokemon which has all of these things and then some functions that can handle all of the stuff.
精彩评论