0

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");

                }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
saadsaf
  • 1,421
  • 1
  • 17
  • 28
  • 1
    `(key == 's' || key == 'S')` - you _do_ realize letters have their own `VK_X` right? – Paul Samsotha Jan 09 '14 at 07:28
  • public void PlayGame(Graphics2D g2d) throws IOException { 1. should be done in paintComponent (two arrays, inside paintComponent loop inside those two array invoked from Swing Timer, don't to use repaint() from Thread/Runnable#Thread for Java7), 2. any of painting methods can't throws IOException, those Object prepare as local variable, 3. do not load any FileIo on runtime for Graphics – mKorbel Jan 09 '14 at 07:46
  • Use key bindings, for [example](http://stackoverflow.com/a/14001011/230513). – trashgod Jan 09 '14 at 10:01

0 Answers0