2013-04-14 68 views
2

當它碰到牆時,它只是沿着牆壁滾動,但我正在反轉Y座標。此外,您無法同時移動槳。任何提示?我應該創建2個線程嗎?乒乓球沒有從牆上彈開

這裏是我改變Y線後撞到牆上的功能。

public void ballMove(){ 
      if(ballStartY+randomBally > jpH){ 
       randomBally -=4; 

      } 
      if(ballStartY+randomBally <0){ 
       randomBally +=4; 
      } 
     } 

下面是完整的代碼

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 = random.nextInt(max-min+1)+min; 
     int randomBally = random.nextInt(max-min+1)+min; 

     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){ 

       randomBall(); 
       ballMove(); 
       repaint(); 
     try {Thread.sleep(75); } catch(Exception e){ 

     }; 

       } 
     } 

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

      } 
      if(ballStartY+randomBally <0){ 
       randomBally +=4; 
      } 
     } 

     public void keyPressed(KeyEvent e) { 

     if(e.getKeyCode() == KeyEvent.VK_A){ 
       y-=10; 
     } 
     else if(e.getKeyCode() == KeyEvent.VK_S){ 
         y+=10; 
       } 

     if(e.getKeyCode() == KeyEvent.VK_QUOTE){ 
       ytwo-=10; 
     } 
     else if(e.getKeyCode() == KeyEvent.VK_SEMICOLON){ 
         ytwo+=10; 
       } 

     } 



     public void keyTyped(KeyEvent e) { 

     } 

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

     } 

} 
+1

那些變量名......哦,上帝爲什麼...... –

+0

randomY = -randomY。讓我知道你是否想要一個徹底的解釋,我會爲你輸入一個例子。 –

+1

哦,我只是看看你的代碼,我相信你使用的是錯誤的操作符。 randomY - = 4將讀取randomY的值減去4,然後將新值存儲在randomY中,它與randomY = randomY - 4相同。我不認爲這就是您要做的。我會使用速度變量dx和dy,然後當發生碰撞時根據牆壁設置dx = -dx或dy = -dy,那麼x + = dx和y + = dy應該始終保持球向正確的方向移動。 –

回答

0

而不是做randomBally -=4;randomBally +=4;你可以做randomBally *=-1。除了增加速度,你應該將速度乘以-1,這會改變運動方向。