密钥适配器不工作但重点突出

时间:2016-08-15 02:44:10

标签: java keyboard

我需要帮助来改进我的Tron简单游戏。我首先为一个项目创建了Snake,然后决定创建它,因为我认为它会有点相似。然而,即使我在两者中使用了几乎相同的密钥适配器代码,但Snron的作品却没有。但是Tron并没有这样做。如果它有所不同,Snake在Eclipse中编写了JGrasp和Tron。根据我从调试中可以看出的情况,密钥适配器根本没有响应,这使得我认为这是一个焦点问题,但是Tron的代码请求集中在Snake所做的所有相同的时间,并且正如我所说Snake正常工作。如果有人可以帮助我,那就太好了。

另外,因为我对编码比较陌生,所以后来我会喜欢关于如何提高程序速度或减少内存的建议。此外,你对我应该添加到游戏中的功能的任何建议或我可以使游戏看起来更好的方式,我将非常感谢听到他们。我可能需要帮助制作这些功能,特别是如果他们引入了新概念,那么我可能会在事后寻求帮助以便如何做些事情。

无论如何这里是代码:

import java.awt.*;
import javax.swing.*;  
import java.awt.event.*;
public class Tron_Board extends JPanel{
   public int directionleft = 4;
   public int directionright = 3;
   static JLabel instr, Lwins, Rwins;
   public int numL, numR = 0;
   static int[][] array;
   static JLabel[][] board;
   Timer timer;
   public final int rows = 100;
   public final int columns = 200;
   JButton resetButton, quitButton, startButton;
   public Tron_Board()
   {
      setLayout(new BorderLayout());
      array = new int[rows][columns];
      board = new JLabel[rows][columns];

      JPanel north = new JPanel();
      north.setLayout(new BorderLayout());
      Lwins = new JLabel("" + numL);
      north.add(Lwins, BorderLayout.WEST);
      Rwins = new JLabel("" + numR);
      north.add(Rwins, BorderLayout.EAST);
      instr = new JLabel("Best of 5! Good Luck!");
      instr.setHorizontalAlignment(SwingConstants.CENTER);
      north.add(instr, BorderLayout.CENTER);
      Lwins.setFont(new Font("Serif", Font.BOLD, 30));
      Rwins.setFont(new Font("Serif", Font.BOLD, 30));
      instr.setFont(new Font("Serif", Font.BOLD, 25));
      add(north, BorderLayout.NORTH);

      JPanel center = new JPanel();
      center.setLayout(new GridLayout(rows, columns));
      for(int r = 0; r < array.length; r++)
         for(int c = 0; c < array[0].length; c++)
         {
            array[r][c] = 0;
            board[r][c] = new JLabel();
            board[r][c].setOpaque(true);
            center.add(board[r][c]);
         }
      add(center, BorderLayout.CENTER);

      JPanel south = new JPanel();
      south.setLayout(new FlowLayout());
      add(south, BorderLayout.SOUTH);
      resetButton = new JButton("Reset");
      resetButton.addActionListener(new ResetListener());
      resetButton.setEnabled(false);
      startButton = new JButton("Start Round");
      startButton.addActionListener(new StartListener());
      startButton.setEnabled(false);
      south.add(resetButton);
      south.add(startButton);
      quitButton = new JButton("Quit");
      quitButton.addActionListener(new QuitListener());
      south.add(quitButton);

      addKeyListener(new Key());
      setFocusable(true);
      requestFocus();
      timer = new Timer(75, new Listener());
      reset();
   }
   private void start()
   {
      timer.start();
      startButton.setEnabled(false);
   }  
   private static void update()   
   {
      for(int r = 0; r < array.length; r++)
         for(int c = 0; c < array[r].length; c++)
         {
             switch(array[r][c]){
             case 0: board[r][c].setBackground(Color.BLACK);
             break;
             case 1: board[r][c].setBackground(Color.RED);
             break;
             case 2: board[r][c].setBackground(Color.ORANGE);
             break;
             case 3: board[r][c].setBackground(new Color(13, 182, 233));
             break;
             case 4: board[r][c].setBackground(Color.YELLOW);
             break;
             case 5: board[r][c].setBackground(new Color(169, 212, 144));
             break;
             }
         }
   }
   private void move()
   {
      int oldR = 0;
      int oldC = 0; 
      int newR = 0; 
      int newC = 0;
      int oldR2 = 0;
      int oldC2 = 0; 
      int newR2 = 0; 
      int newC2 = 0;
      for(int r = 0; r < array.length; r++)
      {
         for(int c = 0; c < array[r].length; c++)
         {
            if(array[r][c] == 1)
            {
               oldR = r;
               oldC = c;
               switch(directionleft){
                  case 1: newR = oldR - 1; //up
                     newC = oldC;
                     break;
                  case 2: newR = oldR + 1; //down
                     newC = oldC;
                     break;
                  case 3: newR = oldR; //left
                     newC = oldC - 1;
                     break;
                  case 4: newR = oldR; //right
                     newC = oldC + 1;
                     break;
               }
            }
         }
      }
       for(int r = 0; r < array.length; r++)
      {
         for(int c = 0; c < array[r].length; c++)
         {
            if(array[r][c] == 3)
            {
               oldR2 = r;
               oldC2 = c;
               switch(directionright)
               {
                  case 1: newR2 = oldR2 - 1; //up
                     newC2 = oldC2;
                     break;
                  case 2: newR2 = oldR2 + 1; //down
                     newC2 = oldC2;
                     break;
                  case 3: newR2 = oldR2; //left
                     newC2 = oldC2 - 1;
                     break;
                  case 4: newR2 = oldR2; //right
                     newC2 = oldC2 + 1;
                     break;
               }
            }
         }
      }
               if((array[newR2][newC2] == 0) && (array[newR][newC] == 0)){
                   array[oldR][oldC] = 2;
                   array[newR][newC] = 1;
                   array[oldR2][oldC2] = 4;
                   array[newR2][newC2] = 3;
               }
               else if((array[newR2][newC2] > 0) && (array[newR][newC] > 0)){
                   roundLose("draw");
               }
               else if(array[newR2][newC2] > 0){
                   roundLose("right");
               }
               else if(array[newR][newC] > 0){
                   roundLose("left");
               }

   }
   private void gameWin(String player)
   {
      timer.stop();
      instr.setText("Congratulations, " + player + " player won!");
      resetButton.setEnabled(true);
   }
   public void roundLose(String player){
       timer.stop();
       if(player.equals("left"))
       {
           if(numR + 1 == 3)
           {
               gameWin("right");
           }
           else
           {
           numR += 1;
           }
       }
       if(player.equals("right"))
       {
           if(numL + 1 == 3)
           {
               gameWin("left");
           }
           else
           {
           numL += 1;
           }
       }
       if(player.equals("draw"))
       {
           instr.setText("DRAW!!!");
       }
       reset();
   } 
   private void reset()
   {
      timer.stop();
      directionleft = 4;
      directionright = 3;
      Lwins.setText("" + numL);
      Rwins.setText("" + numR);
      instr.setText("Best of 5! Good Luck!");
      for(int r = 0; r < array.length; r++)
         for(int c = 0; c < array[0].length; c++)
         {
            if(r == 0 || r == (array.length - 1) || c == 0 || c == (array[0].length - 1))
               array[r][c] = 5; //wall
            else
               array[r][c] = 0; //background
         }
      array[rows / 2][7] = 1;
      array[rows / 2][columns - 8] = 3;
      update();
      startButton.setEnabled(true);
      resetButton.setEnabled(false);
      requestFocus();
   }
   private class Listener implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
         move();
         update();
      }
   }
   private class ResetListener implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
         reset();
         numL = 0;
         numR = 0;
      }
   }
   private class QuitListener implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
         System.exit(0);
      }
   }
   private class StartListener implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
         start();
      }
   }
   private class Key extends KeyAdapter
   {
      public void keyPressed(KeyEvent e)
      {
         if(e.getKeyCode() == KeyEvent.VK_W){
                directionleft = 1; 
                System.out.println("I'm here");//up
         }
         if(e.getKeyCode() == KeyEvent.VK_S) //down
            directionleft = 2; 
         if(e.getKeyCode() == KeyEvent.VK_A) //left
            directionleft = 3; 
         if(e.getKeyCode() == KeyEvent.VK_D) //right
            directionleft = 4; 
         if(e.getKeyCode() == KeyEvent.VK_UP)
             directionright = 1;
         if(e.getKeyCode() == KeyEvent.VK_DOWN)
             directionright = 2;
         if(e.getKeyCode() == KeyEvent.VK_LEFT)
             directionright = 3;
         if(e.getKeyCode() == KeyEvent.VK_RIGHT)
             directionright = 4;
      }
   }
}

0 个答案:

没有答案