Game Development Reference
In-Depth Information
In the above example, repeated code is found in all the classes. That means three
different areas that need to be modified in case of change. That is more work for
you, the coder; it's also more code so it is less clear. The more code, the greater
the chance for bugs to appear.
Now shields need to be added to the space invader creatures.
class Invader
{
...
int _health ¼ 10;
bool _dead ¼ false;
int _shieldPower ¼ 2;
void OnShot()
{
if (_shieldPower > 0)
{
_shieldPower--;
return;
}
_health--;
if (_health <= 0)
{
_dead ¼ true;
}
}
...
}
class FlyingSaucer
{
...
int _health ¼ 10;
bool _dead ¼ false;
int _shieldPower ¼ 2;
void OnShot()
{
if (_shieldPower > 0)
{
_shieldPower--;
return;
}
_health--;
Search WWH ::




Custom Search