Michael Haywood
Michael Haywood

Reputation: 319

Adding images with same logic into game in different positions?

I am creating a top down shooter where zombies chase you. I have added all the logic so the zombie follows the player in the center and if the zombie intersects the player, the player loses. Currently only one zombie is in the game. But I want to be able to add in more zombies without there being much more code. I want there to be a part where I can just use for example to add a new one in a random position.

public void addZombie(){}

I'm using the paint method.

This is the zombie class. It gets added to the panel. Thanks in advance.

package zombieGame;

import javax.swing.ImageIcon;

public class Zombie extends Sprite implements Commons {

String zombie = "/images/zombieUp.png";
int width1 = (int) screenSize.getWidth();
int height1 = (int) screenSize.getHeight();
//Move Left/Right
int dx;
int dy;
//Directions
boolean upLeft;
boolean upRight;
boolean downLeft;
boolean downRight;
boolean left;
boolean right;
boolean up;
boolean down;

public Zombie() {
    if (zombie != null) {
        ImageIcon ii = new ImageIcon(this.getClass().getResource(zombie));
        image = ii.getImage();
        width = image.getWidth(null);
        height = image.getHeight(null);
        resetState();
    }
}

public void move() {
    if (zombie != null) {
        x += dx;
        y += dy;
        animate();
    }
}

public void animate() {
    if (zombie != null) {
        //If inbetween X
        if (x + Board.amountX > width1 / 2 && x + Board.amountX < width1 / 2 + 3) {
            dx = 0;
        }
        //Move right
        if (x + Board.amountX < width1 / 2) {
            dx = 1;
            right = true;
            left = false;
        } else {

            right = false;
        }
        //move left
        if (x + Board.amountX > width1 / 2 + 3) {
            dx = -1;
            left = true;
            right = false;
        } else {

            left = false;
        }
        //If inbetween Y
        if (y + Board.amountY > height1 / 2 - 2 && y + Board.amountY < height1 / 2 + 3) {
            dy = 0;
        }
        //move down
        if (y + Board.amountY < height1 / 2 - 2) {
            dy = 1;
            down = true;
            up = false;
        } else {
            down = false;
        }
        //Move up
        if (y + Board.amountY > height1 / 2 + 3) {
            dy = -1;
            up = true;
            down = false;
        } else {
            up = false;
        }
        if (left) {
            zombie = "/images/zombieLeft.png";
        }
        if (right) {
            zombie = "/images/zombieRight.png";
        }
        if (up) {
            zombie = "/images/zombieUp.png";
        }
        if (down) {
            zombie = "/images/zombieDown.png";
        }
        if (up && left) {
            zombie = "/images/zombieUpLeft.png";
        }
        if (up && right) {
            zombie = "/images/zombieUpRight.png";
        }
        if (down && left) {
            zombie = "/images/zombieDownLeft.png";
        }
        if (down && right) {
            zombie = "/images/zombieDownRight.png";
        }
        ImageIcon ii = new ImageIcon(this.getClass().getResource(zombie));
        image = ii.getImage();
        width = image.getWidth(null);
        height = image.getHeight(null);
    }
}

public void resetState() {
    x = 200;
    y = 385;
}

Upvotes: 1

Views: 109

Answers (1)

Saj
Saj

Reputation: 18702

Say you want to have atmost 10 zombies in a given time. So, create a pool of 10 zombies at start (an array of 10 zombies). Spawn a zombie from your array/pool in random position. When you want to spawn another one get another zombie from your pool which is not active or on screen and spawn it. Update the zombies that are alive (You should have a boolean flag in your zombie class perhaps for you to know if the zombie is on the screen so you don't update/render an invisible one).

Upvotes: 1

Related Questions