Question

How does a program work when there is say 2 or more enemies, obviously they don't have say:

int Enemy1Health = 100;
int Enemy2Health = 100;

and so on, especially in zombies where there can be up to 100 at a time, so how is it done? I assume the answer is pretty complex so if someone could at least point me in a direction it would be greatly appreciated.

Was it helpful?

Solution

Actually the answer is not too complex, but we'll all learning here.

What you should learn about is classes and objects. It's pretty much the base of object oriented programming.

As a guide... (come back to this when you learn more about objects)

You should have a Enemy class (I added some other typical variables).

public class Enemy {
    int name = "charles";
    int health = 100;
    int xPosition = 0;
    int yPosition = 0;
}

and this will hold the enemy attributes.

When creating multiple enemies...

Enemy enemy1 = new Enemy();
Enemy enemy2 = new Enemy();

To access those variables, you would do enemy1.health or enemy1.name

Normally if you're programming a game you would hold these enemies in some sort of list...

ArrayList<Enemy> enemies = new ArrayList<Enemy>();

And then when you are adding or removing enemies...

emenies.add(/* enemy object */);
emenies.remove(/* index of enemy */);

OTHER TIPS

In all computer language, there are Arrays and Loops,for c ,you can do like this

int EnemyHealth[100];

for (int i=0;i < 100;i++)
{
    EnemyHealth[i] = 100;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top