2013-04-15 89 views
-1

所以我試圖通過在按下某個鍵時增加它們來移動這些paddles。我這樣做是因爲當我沒有使用keyrelease時,你不能同時移動它們。在key中使用keyPressed和keyreleased時發生故障pong

現在我遇到的問題是,如果我按一個方向它會去(他們都可以在同一時間去哪裏是好的),但它會停止,一旦我按下相反的鍵,而不會能夠再次移動。有小費嗎?

這裏是所有這些東西我談論

public void paddleMove(){ 
      if(leftDown==true){ 
       y-=10; 
      } 
      if(leftUp ==true){ 
       y+=10; 
      } 
      if(rightDown ==true){ 
       ytwo-=10; 
      } 
      if(rightUp ==true){ 
       ytwo+=10; 
      } 
     } 

     public void keyPressed(KeyEvent e) { 

     if(e.getKeyCode() == KeyEvent.VK_A || e.getKeyCode() == KeyEvent.VK_S || e.getKeyCode() == KeyEvent.VK_QUOTE || e.getKeyCode() == KeyEvent.VK_SEMICOLON){ 
       if(e.getKeyCode() == KeyEvent.VK_A){ 
        // y-=10; 
        leftDown = true; 
       } 

     if(e.getKeyCode() == KeyEvent.VK_S){ 
         // y+=10; 
      leftUp = true; 
       } 

     if(e.getKeyCode() == KeyEvent.VK_QUOTE){ 
       // ytwo-=10; 
      rightDown = true; 
     } 
     if(e.getKeyCode() == KeyEvent.VK_SEMICOLON){ 
         //ytwo+=10; 
      rightUp = true; 
       } 

     } 
     } 
     public void keyRelease(KeyEvent r){ 
      if(r.getKeyCode() == KeyEvent.VK_A){ 
       leftDown = false; 
      } 
      if(r.getKeyCode() == KeyEvent.VK_S){ 
       leftUp = false; 
      } 
      if(r.getKeyCode() == KeyEvent.VK_QUOTE){ 
       rightDown = false; 
      } 
      if(r.getKeyCode() == KeyEvent.VK_SEMICOLON){ 
       rightUp = false; 
      } 
     } 

下面是完整的代碼

import java.awt.Color; 
import java.awt.Event; 
import java.awt.Graphics; 
import java.util.Random; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 


public class Pong extends JFrame implements ActionListener{ 

     //implement constants 

     PongPanel pongPanel = new PongPanel(); 

     //JFrame pong x and y coordinates 
     static final int jfpX = 150; 
     static final int jfpY = 20; 

     // JFrame pong width and height 
     static final int jfpW = 800; 
     static final int jfpH = 600; 

     Thread thrd; 

     public static void main(String[] args) { 
       Pong jfp = new Pong(); 
       jfp.setVisible(true); 

     } 

     public Pong(){ 
       setBounds(jfpX,jfpY,jfpW,jfpH); 
       setTitle("Pong"); 
       setResizable(false); 
       setDefaultCloseOperation(EXIT_ON_CLOSE); 
       setBackground(Color.black); 


       add(pongPanel); 
       addKeyListener(pongPanel); 
       thrd = new Thread (pongPanel); 
     thrd.start(); 
     } 

     public void actionPerformed(ActionEvent e) { 

     } 



} 

class PongPanel extends JPanel implements Runnable, KeyListener{ 
     Random random = new Random(); 
     static final int jpW = 800; 
     static final int jpH = 600; 
     int paddleStart = (jpH/2)-35; 
     int paddleStarttwo = (jpH/2)-35; 
     int ballStartX = (jpW/2)-20; 
     int ballStartY = (jpH/2)-20; 
     int ytwo,x,y; 
     int ballD = 30; 
     int paddleW1 = 20; 
     int paddleH1 = 100; 
     int paddleW2 = 20; 
     int paddleH2 = 100; 
     int min = -2; 
     int max = 2; 
     int randomBallx, randomBally; 

     boolean leftUp = false; 
     boolean leftDown = false; 
     boolean rightUp = false; 
     boolean rightDown = false; 


//  int randomBallx = random.nextInt(max-min+1)+min; 
//  int randomBally = random.nextInt(max-min+1)+min; 

     int rand1 = random.nextInt(2-1 + 1)+1; // random for function to determine ballx and bally 
     int rand2 = random.nextInt(2-1+2)+1; 
     int dx = 4; 
     int dy = 4; //direction of y 

     public void ballNotZero(){// makes sure the ball doesnt go straight up and down 
     if (randomBallx ==0){ 
       randomBallx = random.nextInt(max-min+1)+min; 
      } 
      if(randomBally == 0){ 
       randomBally=random.nextInt(max-min+1)+min; 
      } 
//   if(rand1 ==1){ 
//   randomBallx=-1; 
//   } 
//   if(rand1 ==2){ 
//   randomBallx=1; 
//   } 
//   if(rand2 ==1){ 
//   randomBally =-1; 
//   } 
//   if(rand2==2){ 
//   randomBally = 1; 
//   } 

     } 


     public PongPanel(){ 

     } 

     protected void paintComponent(Graphics g) 
     { 
     super.paintComponent(g); 

     Color ball; 
     Color paddleOne; 
     Color paddleTwo; 
     ball = new Color(255,0,255); 
     paddleOne = new Color(255,0,0); 
     paddleTwo = new Color(0,0,255); 


     g.setColor(ball); 
     g.fillOval(ballStartX+randomBallx,ballStartY+randomBally,ballD,ballD); 

     g.setColor(paddleOne); 
     g.fillRect(20,paddleStart+y,paddleW1,paddleH1); 

     g.setColor(paddleTwo); 
     g.fillRect(760,paddleStarttwo+ytwo,paddleW2,paddleH2); 



     } 
     public void run() { 
       while(true){ 
       ballNotZero(); 
       detectPaddle(); 
       paddleMove(); 
       randomBall(); 
       ballMove(); 
       repaint(); 
     try {Thread.sleep(75); } catch(Exception e){ 

     } 

       } 
     } 
     public static boolean intervallContains(int low, int high, int n) { //determines if something is in a certain range 
      return n >= low && n <= high; 
     } 
     public void detectPaddle(){ //determines if ball is close enough to paddle for detection 
     int withinY = (paddleStart+y) -(ballStartY+randomBally); 
     int withinY1 = (paddleStarttwo+ytwo)-(ballStartY+randomBally); 

     if (ballStartX+randomBallx <=20 && intervallContains(-50,50,withinY)){ 
     dx = -dx; 
     } 
     if(ballStartX+randomBallx >=760 && intervallContains(-50,50,withinY1)){ 
     dx = -dx; 
     } 
     } 

     public void randomBall(){ 
     if(randomBallx >=0){ 
     randomBallx+=dx; 
     } 
     if(randomBallx<0){ 
     randomBallx-=dx; 
     } 
     if(randomBally>=0){ 
     randomBally+=dy; 
     } 
     if(randomBally<0){ 
     randomBally-=dy; 
     } 
//    randomBallx+=randomBallx; 
//    randomBally+=randomBally; 
     } 
     public void ballMove(){ 
     if(ballStartY+randomBally > jpH-60){ 
     dy= -dy; 

     } 
     if(ballStartY+randomBally <0){ 
     dy = -dy; 
     } 
     } 
     public void paddleMove(){ 
      if(leftDown==true){ 
       y-=10; 
      } 
      if(leftUp ==true){ 
       y+=10; 
      } 
      if(rightDown ==true){ 
       ytwo-=10; 
      } 
      if(rightUp ==true){ 
       ytwo+=10; 
      } 
     } 

     public void keyPressed(KeyEvent e) { 

     if(e.getKeyCode() == KeyEvent.VK_A || e.getKeyCode() == KeyEvent.VK_S || e.getKeyCode() == KeyEvent.VK_QUOTE || e.getKeyCode() == KeyEvent.VK_SEMICOLON){ 
       if(e.getKeyCode() == KeyEvent.VK_A){ 
        // y-=10; 
        leftDown = true; 
       } 

     if(e.getKeyCode() == KeyEvent.VK_S){ 
         // y+=10; 
      leftUp = true; 
       } 

     if(e.getKeyCode() == KeyEvent.VK_QUOTE){ 
       // ytwo-=10; 
      rightDown = true; 
     } 
     if(e.getKeyCode() == KeyEvent.VK_SEMICOLON){ 
         //ytwo+=10; 
      rightUp = true; 
       } 

     } 
     } 
     public void keyRelease(KeyEvent r){ 
      if(r.getKeyCode() == KeyEvent.VK_A){ 
       leftDown = false; 
      } 
      if(r.getKeyCode() == KeyEvent.VK_S){ 
       leftUp = false; 
      } 
      if(r.getKeyCode() == KeyEvent.VK_QUOTE){ 
       rightDown = false; 
      } 
      if(r.getKeyCode() == KeyEvent.VK_SEMICOLON){ 
       rightUp = false; 
      } 
     } 



     public void keyTyped(KeyEvent e) { 

     } 

     @Override 
     public void keyReleased(KeyEvent e) { 
       // TODO Auto-generated method stub 

     } 

} 
+1

你的第一個問題幾乎與此相同。 http://stackoverflow.com/questions/16025533/java-pong-cant-move-both-paddles-at-once。 – AxGryndr

+0

我跟着提示那裏,然後我遇到了這個問題 –

+0

這是兩個人類球員? – helsont

回答

1

看看你的邏輯......

if (e.getKeyCode() == KeyEvent.VK_A) { 
    // y-=10; 
    leftDown = true; 
} 

if (e.getKeyCode() == KeyEvent.VK_S) { 
    // y+=10; 
    leftUp = true; 
} 

這基本上說,如果我按A AND 小號然後leftDownleftUp可以true同時,這一點,顯然是不可能的(你想要做什麼),他們需要相互抵消...

應該讀起來更像...

if (e.getKeyCode() == KeyEvent.VK_A) { 
    // y-=10; 
    leftDown = true; 
    leftUp = false; 
} 

if (e.getKeyCode() == KeyEvent.VK_S) { 
    // y+=10; 
    leftUp = true; 
    leftDown = false 
} 

取而代之。這意味着,如果我按一個leftDowntrueleftUpfalse,但如果我隨後也按小號,然後leftDownfalseleftUptrue。這意味着玩家只能在一個方向上移動,而不管他們按什麼鍵。

雖然我在這裏...

if (e.getKeyCode() == KeyEvent.VK_A || e.getKeyCode() == KeyEvent.VK_S || e.getKeyCode() == KeyEvent.VK_QUOTE || e.getKeyCode() == KeyEvent.VK_SEMICOLON) {是多餘的,增加了你的代碼沒有價值,你要檢查這些狀態中的任何方式......

而是然後用一堆斷開if之類的語句的...

if (e.getKeyCode() == KeyEvent.VK_A) { 
    //... 
} 
if (e.getKeyCode() == KeyEvent.VK_S) { 
    //... 
} 
if (e.getKeyCode() == KeyEvent.VK_QUOTE) { 
    //... 
} 
if (e.getKeyCode() == KeyEvent.VK_SEMICOLON) { 
    //... 
} 

您應該使用if-else語句....

if (e.getKeyCode() == KeyEvent.VK_A) { 
    //... 
} else if (e.getKeyCode() == KeyEvent.VK_S) { 
    //... 
} else if (e.getKeyCode() == KeyEvent.VK_QUOTE) { 
    //... 
} else if (e.getKeyCode() == KeyEvent.VK_SEMICOLON) { 
    //... 
} 

這是真的,真的,小的,但使它更加明顯,你打算檢查單的狀態,因爲KeyEvent是永遠只能將成爲一個關鍵...

哦,我應該還請添加,使用Key Bindings而不是KeyListener s。 KeyListener患有許多焦點相關的問題,你不會想要處理...