2013-12-09 148 views
1

我試圖做一個pong遊戲,並有一個問題。當我嘗試按下鍵時,程序使用我的按鍵之前有一個延遲。我認爲問題在KeyBindings因爲如果我使用定時器200ms,沒有延遲如何減少按鍵的延遲?

PingPong.java

import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 
import java.util.ArrayList; 

public class PingPong{ 
    JFrame theFrame;//главное окно 
    MyPanel myPanel;//окно с игрой,в котором все рисуется 
    boolean drawCell=true; 
    int speed =90; //скорость таймера,по которому происхоит перерисовка 
    int width =600;//ширина и высота в пикселях 
    int height=400; 
    int wLength=20;//ширина x 
    int hLength=20;//длина y 
    int maxWidth = width/wLength;//колво квадратов ширины 
    int maxHeight = height/hLength; //кол-во квадратов в высоте 
    public static void main(String[] args){ 
     PingPong game= new PingPong(); 
     game.start(); 
    } 
    private void start(){ 
     buildGui(); 
     myPanel.game(); 
    } 
    private void buildGui(){ 
     theFrame = new JFrame("PingPong"); 
     theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     theFrame.setResizable(false); 
     theFrame.setVisible(true); 
     theFrame.setBackground(Color.BLACK); 
     theFrame.setSize(width+6,height+29); 
     myPanel = new MyPanel(); 
     theFrame.getContentPane().add(myPanel); 

    } 
    class MyPanel extends JPanel implements ActionListener{//переопределяем панель для поля сдесь рисуем и все действиея связанные с игрой 
     class UpAction extends AbstractAction{ 
      Player player=new Player(); 
      public UpAction(Player player0) 
      { 
       player = player0; 
      } 
     public void actionPerformed(ActionEvent e) 
      { 
       player.up(); 
      } 
     } 
     class DownAction extends AbstractAction{ 
      Player player=new Player(); 
      public DownAction(Player player0) 
      { 
       player = player0; 
      } 
      public void actionPerformed(ActionEvent e) 
      { 
       player.down(); 
      } 
     } 
     Timer t = new Timer(speed, this); 
     Ball ball = new Ball(width/2,height/2,1,1,width,height); 
     Player player1 = new Player(KeyStroke.getKeyStroke(KeyEvent.VK_UP,0), 
            KeyStroke.getKeyStroke(KeyEvent.VK_DOWN,0),width-20,height/2); 
     Player player2 = new Player(KeyStroke.getKeyStroke(KeyEvent.VK_W,0), 
            KeyStroke.getKeyStroke(KeyEvent.VK_S,0),0,height/2) ;      
     public void game(){ 
      addBindings(); 
      t.start(); 
     } 
     protected void addBindings() {//добавляем клавиши!Когда клавиша нажата вызывается инпут 
      InputMap im =getInputMap(WHEN_IN_FOCUSED_WINDOW);//который ишет значения в актионмапе 
      ActionMap am = getActionMap(); 

      im.put(player1.keyUp,"player1Up"); 
      UpAction player1up = new UpAction(player1); 
      am.put("player1Up",player1up); 

      im.put(player2.keyUp,"player2Up"); 
      UpAction player2up = new UpAction(player2); 
      am.put("player2Up",player2up); 

      im.put(player1.keyDown,"player1Down"); 
      DownAction player1down = new DownAction(player1); 
      am.put("player1Down",player1down); 

      im.put(player2.keyDown,"player2Down"); 
      DownAction player2down = new DownAction(player2); 
      am.put("player2Down",player2down); 
      } 
     public void actionPerformed(ActionEvent e){//действие по тику таймера 
      player1.PlayerTurn(); 
      player2.PlayerTurn(); 
      ball.nextTurnBall(); 
      repaint(); 
     } 
     public void paintComponent(Graphics g){ 
      g.setColor(Color.BLACK); 
      g.fillRect(0,0,width,height); 
      g.fillOval(ball.x-ball.speedx,ball.y-ball.speedy,ball.rad,ball.rad); 
      if (drawCell){ 
      g.setColor(Color.GRAY); 
      for (int i=0; i<=maxWidth;i++){ 
       g.drawLine(i*wLength,0,i*wLength,height); 
      } 
      for (int i=0; i<=maxHeight;i++){ 
       g.drawLine(0,i*hLength,width,i*hLength); 
      } 

       g.setColor(Color.YELLOW); 
       g.fillOval(ball.x,ball.y,ball.rad,ball.rad); 
      } 
      g.setColor(Color.WHITE); 
      g.fillRect(player1.x,player1.y,player1.width,player1.height); 
      g.fillRect(player2.x,player2.y,player2.width,player2.height); 
     } 
    } 
} 

Player.java

import javax.swing.*; 
import java.awt.event.*; 
public class Player{ 
KeyStroke keyUp = KeyStroke.getKeyStroke(KeyEvent.VK_UP,0); 
KeyStroke keyDown = KeyStroke.getKeyStroke(KeyEvent.VK_UP,0); 
int x=0; 
int y=0; 
int width=20; 
int height=80; 
int speedy=0; 

public Player(KeyStroke keyUp0,KeyStroke keyDown0,int x0,int y0,int width0,int height0){ 
    keyUp=keyUp0; 
    keyDown=keyDown0; 
    x=x0; 
    y=y0; 
    width=width0; 
    height=height0; 
} 
    public Player() 
    { 

    } 
    public Player(KeyStroke keyUp0,KeyStroke keyDown0,int x0,int y0){ 
    keyUp=keyUp0; 
    keyDown=keyDown0; 
    x=x0; 
    y=y0; 
} 
public void up(){ 
    speedy=-4; 
} 
public void down(){ 
    speedy=4; 
} 
public void PlayerTurn(){ 
    y += speedy; 
    speedy=0; 
} 

}

+0

您是否已經找到答案或者是否想要解決它(200 ms延遲)? – Freakyuser

回答

1

我覺得延遲喲重複按鍵延遲你的系統。您將UpAction和DownAction綁定到KeyStroke.getKeyStroke(keyCode, 0),其中according to the javadoc是「按鍵」事件。當您按下鍵時會發生按鍵事件,並且每次操作系統生成該鍵的自動重複時都會發生按鍵事件。

而不是每次播放器移動時都執行speedy = 0,將該行移到播放器中的一個單獨的方法,只有在釋放移動鍵時纔會調用該方法。您可以創建一個單獨的密鑰版本綁定,例如,KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, true)