I made two games so far, one was a simple 2D MMO, and another one was a 2D portal clone with some more features.
Anyway, I noticed that my class design in those two games varied a bit:
In the MMO, I’d create a class called “Enemy”, and this class would take some arguments such as image and attack_power.
In the Portal-like game, I’d create classes for every kind of object specifically, for example “Box” or “Wall” or “Doors”. Then, these would take only position as an argument, and inside I’d flag them for movable, physics with true/false, and then I would have an update function which would act upon these objects.
What is the best approach to this kind of problem? How specific should I be, and should I use something completely different?
I made those games in Python and Javascript.
That depends on the purposes of each class, on the language which you’re using, on what you’re trying to achieve, the specific problems you’re trying to solve, and a zillion other factors – every problem and every program is different.
I’d recommend doing some research around the SOLID prinicples of OO design, these principles are more like guidelines than rules, but understanding them and being able to apply them to a real problem might help you achieve ‘good’ class design – http://davesquared.net/2009/01/introduction-to-solid-principles-of-oo.html
There is no “best” approach, since there are no universal right or wrong ways to solve a problem, and a perfect solution is not always possible or desirable when you factor in other real world issues which you might come across along the way.
The kinds of questions you might think about asking yourself could be along the lines of
What does my program actually do? Often, the most useful classes emerge when you start to think about the way your program works. For example, your movable Box class may end up doing very little to the point where it’s easier to represent it as a simple struct, but you might find a use for a BoxMover class which is packed full of ‘move’ logic, and knows how to move Boxes around.
How do the different entities in my program behave? You don’t mention very many details about your classes, so only you would know whether they do different things, but if you end up with dozens of almost-identical classes (particularly where the differences are restricted to data) then you may have gone too far.
What do I actually want to do with the classes? Its important to think about interfaces; the focus of OO design is more about the way your classes are used than the data which your classes store. (e.g. contrary to popular belief, a Square may have absolutely nothing in common with a Rectangle). Likewise, a class which has nothing more than single-line get-set functions for its interface is probably not making your life any easier, or bringing you any closer to finding a good solution.