Reshad
Reshad

Reputation: 2652

for-each loop in Java for different objects

i created a pacman game with everything in it but the problem is that the ghosts and their animation require a lot of code.

example:

every ghost needs 3 if statements at the moment that is 20 lines of code per ghost and if i have 3 ghosts in the game that is 3 x 20 = 60 lines of useless coding..

with my php experience i would say.. use a foreach loop or something similar.. but how should i do this in Java? can someone give me an example? the way i do it now is published below:

creating ghost objects;

DrawPacMan ghost1 = new DrawPacMan();
DrawPacMan ghost2 = new DrawPacMan();

and the painting goes like:

int g1x = 0;
boolean g1r = true;
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    // pacman movement
    diameter = 75;   
    pacman.drawPacMan(g, getHorPlaats(), getVerPlaats(), diameter, getView(), Color.yellow);
    // ghosts movement
    if(g1r == true) {
        g1x += ghostSpeed;          
    }       
    if(g1r == false) {          
        g1x -= ghostSpeed;
    }
    if(g1x == 500 || g1x == 0) {
        g1r = !g1r;
    }
    System.out.println(g1r);
    ghost1.drawGhost(g, g1x, 40, diameter, Color.red);
    ghost2.drawGhost(g, 170, 70, diameter, Color.blue);
}

Upvotes: 0

Views: 508

Answers (3)

Brian Agnew
Brian Agnew

Reputation: 272217

It looks to me like you're not approaching this in a object-oriented fashion. Why not use a collection of ghosts eg. List<Ghost> and define a Ghost object with it's location, colour etc?

This line:

  ghost1.drawGhost(g, g1x, 40, diameter, Color.red);

would then be replace with

  ghost.draw(g);

and you'd iterate through the list, calling draw() for each.

  for(Ghost ghost : ghosts) {
     ghost.draw(g); // pass in the graphics context
  }

Each ghost knows it's location, colour, state etc. and you can create as many as you like:

  List<Ghost> ghosts = new ArrayList<Ghost>();
  for (int i = 0; i < 10; i++) {  
      ghosts.add(new Ghost());
  }

Upvotes: 7

Marko Topolnik
Marko Topolnik

Reputation: 200138

Since you seem to be new to Java and still getting to know the best idioms, I'll advise on something that is not directly an answer to your question, but is so in a more general sense. Your code

if(g1r == true) {
    g1x += ghostSpeed;          
}       
if(g1r == false) {          
    g1x -= ghostSpeed;
}

can be rewritten as just

g1x += ghostSpeed * (g1r? 1 : -1);

A general note: never compare booleans to literal values. b == true is the same as just b and b == false is the same as !b.

This code

if (g1x == 500 || g1x == 0) {
    g1r = !g1r;
}

will probably result in a bug at runtime as you don't precede it with fencing-in code: g1x can easily step over your limits. You should write instead

if (g1x >= 500) { g1x = 500; g1r = false; }
else if (g1x <= 0) { g1x = 0; g1r = true; }

Upvotes: 2

Arham
Arham

Reputation: 2102

  1. Pass the ghost object as another parameter in the same function paintComponent(Graphics g, Ghost gr)
  2. You can make the conditional statements inline, e.g. g1r == true ? g1x += ghostSpeed : g1x -= ghostSpeed

Upvotes: 0

Related Questions