java - 乒乓球碰撞检测器( Racket 到球)

标签 java user-interface pong

我想向这个程序(乒乓球游戏)添加一个碰撞检测器,我不知道碰撞检测的语法。我目前有一个桨、球和一个用于桨的鼠标监听器(感谢 s/o)。目前我正在尝试在 Racket 上添加一个碰撞检测器,这样我就可以将球以随机方向击回。这是我的代码;

问题:在球和 Racket 之间添加碰撞检测器的语法是什么?

非常感谢。

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


public class PongGame extends JFrame implements Runnable,MouseMotionListener{

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

  int x_left, x_right, y_top, y_bottom;

  int paddle_y = 30;

  /**
   * Constructor
   */

  public PongGame(){
    init();
  }

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

    ball_x = this.getWidth()/2;
    ball_y = this.getHeight()/2;
    ball_dx = ball_dy = 2;
    ball_r = 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(50);
      }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.black);
    g.fillOval(ball_x,ball_y, ball_r, ball_r);

    g.setColor(Color.black);
    g.fillRect(20,paddle_y,20,70);
  }

     public void mouseMoved(MouseEvent e) {
      }

       public void mouseDragged(MouseEvent e){
         paddle_y = e.getY(); 
       } 

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

最佳答案

找到答案了!

       if(ball_x >paddle_x && ball_x < paddle_x + paddle_width)
   {
     if(ball_y>paddle_y && ball_y < paddle_y + paddle_height)
      {
         ball_dx = -ball_dx;
      }
   }

来源:https://www.youtube.com/watch?v=GpM8yvDP21o

关于java - 乒乓球碰撞检测器( Racket 到球),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30106199/

相关文章:

java - 乒乓 Racket 不停地晃动,不停留在一个位置上

JavaScript,无法绘制乒乓球中的 Racket 和球。有人可以解释一下为什么吗?

java - JTable 的 RowSorter 未正确排序整数 (1,10,100...2,20...3)

java - 对于可序列化的父类(super class),如果我们序列化子类,为什么会调用 super 构造函数

java - mockito + easymock - NoClassDefFoundError :net/sf/cglib/proxy/Enhancer

java - 多个用户同时访问的 Web 应用程序的 ExecutorService 线程池大小

java - 如何用 Java 中的数据加载已经实例化的 JComboBox?

wpf - WPF中的“Please wait” Windows

java - 在全屏应用程序中生成 JOptionPane 会显示任务栏

c# - 球不能在桨/墙上正确弹跳