Relapse505
Relapse505

Reputation: 1

initializing multiple images simultaneoulsy

I am pretty new to java, I am working on implementing a pacman game, I initialized a ghost my using one image, and currently using one object in my game, but I want to have multiple images so I can have different colored ghosts. But when I initialized multiple images the just overlaps on another. Here's how I intialized the Ghost

private Image RedGhost;
private Image pacman1, pacman2up, pacman2left, pacman2right, pacman2down;

here the code that I use to moveGhosts

private void moveGhosts(Graphics2D g2d) {

        short i;
        int pos;
        int count;

        for (i = 0; i < nrofghosts; i++) {
            if (ghostx[i] % blocksize == 0 && ghosty[i] % blocksize == 0) {
                pos = ghostx[i] / blocksize + nrofblocks * (int) (ghosty[i] / blocksize);

                count = 0;

                if ((screendata[pos] & 1) == 0 && ghostdx[i] != 1) {
                    dx[count] = -1;
                    dy[count] = 0;
                    count++;
                }

                if ((screendata[pos] & 2) == 0 && ghostdy[i] != 1) {
                    dx[count] = 0;
                    dy[count] = -1;
                    count++;
                }

                if ((screendata[pos] & 4) == 0 && ghostdx[i] != -1) {
                    dx[count] = 1;
                    dy[count] = 0;
                    count++;
                }

                if ((screendata[pos] & 8) == 0 && ghostdy[i] != -1) {
                    dx[count] = 0;
                    dy[count] = 1;
                    count++;
                }

                if (count == 0) {

                    if ((screendata[pos] & 15) == 15) {
                        ghostdx[i] = 0;
                        ghostdy[i] = 0;
                    } else {
                        ghostdx[i] = -ghostdx[i];
                        ghostdy[i] = -ghostdy[i];
                    }

                } else {

                    count = (int) (Math.random() * count);
                    if (count > 3) {
                        count = 3;
                    }

                    ghostdx[i] = dx[count];
                    ghostdy[i] = dy[count];
                }

            }

            ghostx[i] = ghostx[i] + (ghostdx[i] * ghostspeed[i]);
            ghosty[i] = ghosty[i] + (ghostdy[i] * ghostspeed[i]);
            drawGhost(g2d, ghostx[i] + 1, ghosty[i] + 1);
            //drawblueGhost(g2d, ghostx[i] + 1, ghosty[i] + 1);

            if (pacmanx > (ghostx[i] - 12) && pacmanx < (ghostx[i] + 12)
                && pacmany > (ghosty[i] - 12) && pacmany < (ghosty[i] + 12)
                && ingame) {

                dying = true;
            }
        }
    }

In summery all the Ghosts are currently Red, I would like it to be different colors

Upvotes: 0

Views: 59

Answers (1)

Mike M.
Mike M.

Reputation: 56

With your current code, you could modify the drawGhost method to take an identifier for the color of the ghost being drawn. You could then have another array that holds the identifier for each ghost's color.

However, since you are using Java I would recommend taking advantage of OOP and create a Ghost class that holds all of a ghost's properties (x position, y position, color, etc). You can then have a collection of Ghost objects that represent each individual ghost.

Here is an example:

Make a Ghost class that holds all the properties a ghost can have:

public class Ghost {
    private String color;
    private int x;
    private int y;
    private boolean alive;
    // getters and setters. You could even move the logic for moving a ghost to this class
    ...
}

Then you would initialize the ghosts like this:

Ghost redGhost = new Ghost("Red");
Ghost greenGhost = new Ghost("Green");
Ghost pinkGhost = new Ghost("Pink");

You could store them in a set:

Set<Ghost> ghostSet = new HashSet<Ghost>();
ghostSet.add(redGhost);
...

Then your method can change to something like this:

private void moveGhosts(Graphics2D g2d) {
    // Utilize an enhanced for loop
    for(Ghost ghost : ghosts){
        ...
        ...
        ghostx[i] = ghostx[i] + (ghostdx[i] * ghostspeed[i]);
        ghosty[i] = ghosty[i] + (ghostdy[i] * ghostspeed[i]);
        drawGhost(g2d, ghost);

        if (pacmanx > (ghostx[i] - 12) && pacmanx < (ghostx[i] + 12)
        ...
        ...
    }
}

Now the drawGhost method would look at the fields within the ghost class (by calling getter and setter methods) to determine the x and y to start drawing. The color field is used to determine which image to draw.

Upvotes: 2

Related Questions