I'm making Pacman game but it is slightly different from the normal one. The game have 3 chances initially. Chance 1 is controlled by user through cursor and all moves are logged in a file until the Pacman get's caught by the ghosts.
So, in short I want to dispatch/dispose the KeyPressed
event so that the action performed event doesn't get called and I will call it manually. My code is given below:
addKeyListener(new TAdapter());
class TAdapter extends KeyAdapter {
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (ingame)
{
if (key == KeyEvent.VK_LEFT)
{
System.out.println("moved L");
reqdx=-1;
reqdy=0;
try {
if(bw!=null)
bw.write("L");
} catch (IOException ex) {
Logger.getLogger(Board.class.getName()).log(Level.SEVERE, null, ex);
}
}
else if (key == KeyEvent.VK_RIGHT)
{System.out.println("moved R");
reqdx=1;
reqdy=0;
try {
if(bw!=null)
bw.write("R");
} catch (IOException ex) {
Logger.getLogger(Board.class.getName()).log(Level.SEVERE, null, ex);
}
}
else if (key == KeyEvent.VK_UP)
{System.out.println("moved U");
reqdx=0;
reqdy=-1;
try {
if(bw!=null)
bw.write("U");
} catch (IOException ex) {
Logger.getLogger(Board.class.getName()).log(Level.SEVERE, null, ex);
}
}
else if (key == KeyEvent.VK_DOWN)
{System.out.println("moved D");
reqdx=0;
reqdy=1;
try {
if(bw!=null)
bw.write("D");
} catch (IOException ex) {
Logger.getLogger(Board.class.getName()).log(Level.SEVERE, null, ex);
}
}
else if (key == KeyEvent.VK_ESCAPE && timer.isRunning())
{
ingame=false;
}
else if (key == KeyEvent.VK_PAUSE) {
if (timer.isRunning())
timer.stop();
else timer.start();
}
}
else
{
if (key == 's' || key == 'S')
{
ingame=true;
GameInit();
}
}
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == Event.LEFT || key == Event.RIGHT ||
key == Event.UP || key == Event.DOWN)
{
reqdx=0;
reqdy=0;
}
}
}
public void actionPerformed(ActionEvent e) {
System.out.println("action performed : " + e);
PlayGame();
}
public void PlayGame(Graphics2D g2d) throws IOException {
if (dying) {
Death();
} else {
if(pacsleft<=2)
{
System.out.println(pacsleft + " Yessss");
}
MovePacMan();
DrawPacMan(g2d);
moveGhosts(g2d);
CheckMaze();
}
}
I want to dispatch all events here, in the below condition.
if(pacsleft<=2)
{
System.out.println(pacsleft + " Yessss");
}