1
\$\begingroup\$

I am making a Pacman Player vs Player game in Java, and I am wondering if I am implementing movement in the game in a good manner. Anything that will reduce the code size or make the program faster is wanted.

Note that the game is not complete, and I have only worked on player one (Pacman). I have not started on player two (ghost) or the maze or pellets.

Movable Interface

public interface Movable {
void move();
}

Pacman / Player One Class

This class represents player one, or the Pacman character. It implements Movable.

@Override
public void move() {
    if (this.direction == Direction.UP) {
        y -= 5;
    } else if (this.direction == Direction.DOWN) {
        y += 5;
    } else if (this.direction == Direction.RIGHT) {
        x += 5;
    } else if (this.direction == Direction.LEFT) {
        x -= 5;
    }
    create();
}

Game Class

In this class, I make a global type of my Pacman class, and I instantiate it in the Game's initialize method.

Pacman pacman;

I also make a global array of my Movable interface with a size of one (for now) which will hold my pacman instance.

private Movable mover[] = new Movable[1];

Then:

@Override
public void actionPerformed(ActionEvent e) {

    if (running) {
        moveEntities();
    }

    this.repaint();
}

private void moveEntities() {

    initializeMovement();

    for (Movable m : mover) {
        m.move();
    }
}

private void initializeMovement() {
    mover[0] = pacman;
}
\$\endgroup\$
2
  • 1
    \$\begingroup\$ I think it would be easier to review entire classes than to review these excerpts and imagine how to fill in the gaps. \$\endgroup\$ Jun 18, 2016 at 19:34
  • \$\begingroup\$ @200_success I didn't think it was needed considering I just wanted help on the movement. But if it's really required, I'd be happy to edit and post the classes. \$\endgroup\$
    – tanu1215
    Jun 18, 2016 at 19:53

1 Answer 1

5
\$\begingroup\$

Initial thoughts are:

  • Why is move calling create? I don't know what create does, but this seems like a strange way for move to work.
  • You're using 5 for all four directions, this seems like it should probably be a constant.
  • It's unclear how you're going to implement collision detection at the moment, but it's something to consider when defining the interface for moving items.

From your comments, it seems that create draws Pacman. If this is the case, then I'd still say that you don't want to be calling it from within your move method. You want to try to decouple your game mechanics + movement/collision logic from your drawing/rendering logic, they are different concerns and may well move at different speeds. One of the things you've said that you are looking for is to improve the speed that your code runs at. If you think about running games on different hardware, one of the things to consider is that you need to start thinking about using that hardware effectively. To give a concrete example, if your current computer processes your move method 10 times a second, that's going to move your Pacman 50 units. Another computer may only run it 5 (moving 25 units) or may run it 50 (moving 250 units) that's a lot of variability that's usually ironed by linking the distance moved to the amount of elapsed time, which may be detached from the render time.

I haven't read the whole thing (and it seems to be aimed at the mobile market), but this seems like a fairly good introduction to game loop mechanics.

\$\endgroup\$
5
  • \$\begingroup\$ Create just re draws the pacman rectangle after movement. And yes I have not implemented any collision detection yet, I just wanted to know if was implementing my Movable interface in a correct fashion. I can post the entire class if needed as @200_success suggested. \$\endgroup\$
    – tanu1215
    Jun 18, 2016 at 19:55
  • 1
    \$\begingroup\$ @tanu1215 if asked about what create does you say that it redraws, then perhaps it should be called redraw instead of "create" : ) that's the simplest litmus test for naming - what would we say if someone actually came over and asked us what some bit of code does, in as few words as possible \$\endgroup\$ Jun 18, 2016 at 20:12
  • \$\begingroup\$ @KonradMorawski Well in the case of the move method, create() redraws my pacman. However, create() is called in the Pacman class' constructor to initially create the rectangle which represents Pacman. The create method's body is: Rectangle body = new Rectangle(x,y,width,height); So naming it redraw wouldn't really fit in my opinion. \$\endgroup\$
    – tanu1215
    Jun 18, 2016 at 20:23
  • \$\begingroup\$ @tanu1215 ok, if i understand you correctly, this would turn it into "draw" and not necessarily "redraw", and it's still closer to the truth than "create" \$\endgroup\$ Jun 18, 2016 at 20:29
  • \$\begingroup\$ @KonradMorawski Yup! That sounds better, thanks :) \$\endgroup\$
    – tanu1215
    Jun 19, 2016 at 3:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.