2015-04-27 64 views
-2

在java中,我正在嘗試創建一個pong遊戲,到目前爲止,我有一個移動的球從牆上跳起來,並且一個槳停留在同一個地方。我可以添加什麼代碼來使鼠標移動槳。我想讓鼠標點擊並拖動槳。我也想讓球從槳上反彈。請幫助我,並給我建議。我怎樣才能讓這個槳在我的乒乓球遊戲中移動?

import javax.swing.*; 
import java.awt.*; 


public class Pong2 extends JFrame implements Runnable{ 

    int ball_x, ball_y, ball_dx, ball_dy, paddle_y; 
    int ball_r; 

    int x_left, x_right, y_top, y_bottom; 

    /** 
    * Constructor 
    */ 
    public Pong2(){ 
    init(); 

    } 

    /** 
    * this is where we set up the UI 
    */ 
    protected void init(){ 
    this.setSize(300,300); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  

    ball_x = this.getWidth()/2; 
    ball_y = this.getHeight()/2; 

    ball_dx = ball_dy = 2; 

    ball_r = 30; 
    paddle_y = 20; 

    this.setVisible(true); 
    getFocus(this); 

    x_left = this.getInsets().left; 
    x_right = this.getWidth() - this.getInsets().right - ball_r; 
    y_top = this.getHeight() - this.getInsets().top + ball_r/3; 
    y_bottom = this.getInsets().bottom + ball_r; 

    } 

    /** 
    * helper method which we use to get the focus 
    */ 
    public void getFocus(final JFrame frame) 
    { 
     EventQueue.invokeLater(new Runnable() { 
       public void run() { 
        frame.requestFocus(); 
       } 
      }); 
    } 

    /** 
    * implementation of the Runnable interface to be able to move the ball, etc. 
    */ 
    public void run(){ 

    while(true){ 

     ball_x += ball_dx; 
     if(ball_x <= x_left || ball_x >= x_right){ 
     ball_dx *=-1; 
     ball_x += (2*ball_dx); 
     } 

     ball_y += ball_dy; 
     if(ball_y <= y_bottom || ball_y >= y_top){ 
     ball_dy *=-1; 
     ball_y += (2*ball_dy); 
     } 

     repaint(); 

     try{ 
     Thread.sleep(100); 
     }catch(InterruptedException ex){ 
     System.out.println(ex); 
     } 
    } 
    } 

    /** 
    * all rendering occurs here 
    */ 
    public void paint(Graphics g){ 

    g.setColor(Color.white); 
    g.fillRect(0,0,this.getWidth(),this.getHeight()); 

    g.setColor(Color.red); 
    g.fillOval(ball_x,ball_y, ball_r, ball_r); 

    g.setColor(Color.blue); 
    g.fillRect(30,40,10,50); 

    } 

    /** 
    * entry point into the program 
    */ 
    public static void main(String[] args){ 
    // create the class 
    Pong2 application = new Pong2(); 
    new Thread(application).start(); 

    } 

} 
+1

你有沒有取得任何進展?發佈迄今爲止所嘗試的內容通常很有用,這樣您可以獲得更多的吝嗇回答。 – rdonatoiop

回答

2

你應該看看MouseListener。它響應:

  • 當用戶點擊
  • 當用戶進入區域當用戶按下一個鼠標按鈕被聽取
  • 當用戶退出的區域被聽取
  • 向下
  • 當該鼠標按鈕按下用戶釋放

來實現它,根本就

Component.addMouseListener(new MouseListener(){ 
     @Override 
     public void mouseClicked(MouseEvent e){ 
     } 
     @Override 
     public void mouseEntered(MouseEvent arg0) { 
     } 
     @Override 
     public void mouseExited(MouseEvent arg0) { 
     } 
     @Override 
     public void mousePressed(MouseEvent arg0) { 
     } 
     @Override 
     public void mouseReleased(MouseEvent arg0) { 
     } 
0

你應該使用MouseMotionListener當鼠標拖動它通知。