Hi, im quite new to java games, so i thought i’d post here.
Im working on a pacman game, which has to have some pathfinding to it later on, but for now im just concentrating on movement of pacman itself.
I have a working movable pacman, but in my opinion it doesnt run as smooth as it should be, and i think its the way i come up with to get it working.
've got a class, Game, which is an extention of JPanel. Furthermore i’ve implemented KeyListener to make pacman move by passing the direction it is supposed to be moving to, to the Pacman object. (ie, pressing left will do something like pacman.setDirection(Pacman.LEFT)).
So i’ve got a class Pacman, which implements Runnable, and has a constructorargument Game.
//constructor
public Pacman(Game game) {
this.game=game;
}
I also made a method drawObject, which i call from within the Game object its paintComponent method which i have overridden. This method draws the pacman on the screen at the right position.
// in class Game
public void paintComponent(Graphics g) {
pacman.drawObject(g);
}
// in class Pacman
public void drawObject(Graphics g) {
g.fillRect(x,y,20,20);
}
I initialize the Pacman object in the Game object, and let pacman run by creating a new Thread and starting it. In the run method of the Pacman object i have a while loop, and each time it passes it checks the direction of the pacman and then updates its coordinates. Last in the whileloop i call game.repaint(); to redraw.
public void run() {
while(running) {
// update x, y
game.repaint();
Thread.sleep(50); // if i dont do this, it goes too fast ;) (and yes i know i have to catch an exception here ;))
}
}
It works, but like i said, its not running as smooth as it should be, and i’ve yet to paint the 30x30 tiles maze ;). What could i consider to make it run more smooth than it does now?
