0

我有一个代码,当在参数中输入时,它会复制对鼠标光标的引力。当鼠标单击时,它会创建一个反向效果推动对象(矩形远离它)。我正在尝试设置它,因此当您单击并按住时,当对象在 x 或 y 坐标中达到某个数字时,它将随机更改该对象的 x 和 y。这是我的代码。注释区域是我尝试使 x 和 y 在达到 500 个参数时随机化的地方。

import java.awt.*;

import java.util.Random;
    public class Ball
{

private Color col;
private double x, y;         // location
private double vx, vy;       // velocity

public Ball(int new_x, int new_y, int new_vx, int new_vy)
{
     x = new_x; 
     y = new_y; 
     vx = new_vx; 
     vy = new_vy;
}

public Ball()
{
    Random gen = new Random();
    x = gen.nextInt(480);
    y = gen.nextInt(480);
    vx = gen.nextInt(10);
    vy = gen.nextInt(10);

    col = new Color(gen.nextInt(255),gen.nextInt(255),gen.nextInt(255));

}

void paint( Graphics h)
{
    h.setColor(col);
    h.fillRect((int)x,(int)y,20,20);  
}

void move(int currentX, int currentY, boolean isButtonPressed )
{
    double dvx, dvy, rx, ry;
    double r_mag;


    x = x + vx;
    y = y + vy;  

    //bounce
    if  (x > 480 || x < 0)
       vx = -vx;
    if (y > 480 || y < 0)
        vy = -vy;



   if ( currentX <500 && currentY <500)   // mouse is on canvas, apply "gravity" 
   {
      rx = currentX - x;
      ry = currentY - y;
      r_mag = Math.sqrt((rx*rx) + (ry*ry));

//           if ( x = 500 || y = 500)
//             Random x = new Random();
//             x.nextDouble();
//             Random y = new Random();
//             y.nextDouble();

      if (r_mag < 1)
        r_mag = 1;

      dvx = (rx / r_mag);
      dvy = (ry / r_mag);

      if (isButtonPressed)
      {
        vx = vx - dvx;   // + makes balls move to cursor.
        vy = vy - dvy;   // - makes balls move away from cursor. 
      }
      else 
      {
        vx = vx + dvx;   // + makes balls move to cursor.
        vy = vy + dvy;   // - makes balls move away from cursor.

      }
    }

    // reduce speed slowly
    vx = .99*vx;
    vy = .99*vy;
}

}
4

1 回答 1

2

The commented area is where I tried to make x and y go random when it hits 500 parameters.

So when x or y reaches 500, you want to randomly relocate the object?

Instead of

//           if ( x = 500 || y = 500)

this is assignment, not comparison

//             Random x = new Random();

redeclares x, not what you want

//             x.nextDouble();

statement without effect

//             Random y = new Random();
//             y.nextDouble();

see above

you could use Math.random(), as in

if (x == 500 || y == 500) {
    x = Math.random()*480;
    y = Math.random()*480;
}

(note: Math.random() returns a double in the half-open interval [0,1), so you have to scale it; the scaling factor of 480 I used is a guess), or (less good, IMO) create a new Random instance each time you enter the if.

But,

  1. x, y, and the velocities vx and vy are doubles, so it is very unlikely that the movements let x or y become exactly 500, so you should probably test for >= rather than == in the condition.

  2. In the code, you flip the velocity when either coordinate passes 480, so getting to 500 or farther is difficult and can only be achieved by judicious acceleration using the mouse, so a smaller threshold may be wanted.

于 2012-12-12T22:03:29.877 回答