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;
}