You're going to want to create an Enemy class that has all the properties that an enemy can have. For cars you'd probably want properties like maxSpeed, acceleration, direction, etc.
Then you need to decide how the game engine will add them to the game world. I would create a function called createEnemy (or whatever you like) which simply spawns enemies. Then you call the function createEnemy whenever you want to add a new enemy to the game. Maybe this is on a timer or maybe it's dependent on the conditions of the game. That part is up to you.
You'll probably want to save all the enemies in an array so that you can do things to them all at the same time. So every time an enemy is created you at it to the enemies array, and then you loop through that array every time the game engine updates. Each update the enemy cars will move, turn, be destroyed, etc.
I have created an Enemy superclass in which I have created Enemy1 subclass.
Enemy superclass stores all the parameters(health-max & current, coordinates).
Enemy1 subclass only stores variables about coordinates
package palt;
public class Enemy1 extends Enemy {
public Enemy1(int centerX, int centerY) {
setCenterX(centerX);
setCenterY(centerY);
}
}
After this I place Enemy1 on screen via mainclass
em = new Enemy1(300, 300); // for coordinate
g.drawImage(Enemy1, em.getCenterX() - 50, em.getCenterY() - 50, this); // to draw enemy1
I can print more then one enemy using above instruction again & again with different variables.
But, what i want is always have 3 enemies on screen. I could use an if loop stating
If (em < 3){em++;}
but with which varaiable do i do this and I beleive I should be doing it in main class probably to update method.