1

我有一个图像在开始撞到墙上时会向随机方向移动。图像在执行过程中总是开始或出现在左上角,你可以在我的代码中看到。现在我希望图像在执行过程中出现在随机位置,这是我的问题,有人能给我一个想法吗?提前致谢。

public class Ball extends JPanel implements Runnable
{

private Image ball;
private Thread animator;
private int x, y;
private final int DELAY = 20;
private int speedX = 1;
private int speedY = 1;
private static final int RIGHT_WALL = 200;
private static final int LEFT_WALL = 1;
private static final int DOWN_WALL = 200;
private static final int UP_WALL = 1;

public Ball()
{
    setBackground(Color.BLACK);
    setDoubleBuffered(true);

    ImageIcon ii = new ImageIcon(this.getClass().getResource("ball.gif"));
    ball = ii.getImage();

    x = y = 10;
}

public void addNotify()
{
    super.addNotify();
    animator = new Thread(this);
    animator.start();
}

public void paint(Graphics g)
{
    super.paint(g);

    Graphics2D g2d = (Graphics2D) g;
    g2d.drawImage(ball, x, y, this);
    Toolkit.getDefaultToolkit().sync();
    g.dispose();
}

public void move()
{
    x += speedX;
    y += speedY;
    if (x >= RIGHT_WALL)
    {
        x = RIGHT_WALL;
        moveRandomDirection();
    }
    if (y > DOWN_WALL)
    {
        y = DOWN_WALL;
        moveRandomDirection();
    }
    if (x <= LEFT_WALL)
    {
        x = LEFT_WALL;
        moveRandomDirection();
    }
    if (y < UP_WALL)
    {
        y = UP_WALL;
        moveRandomDirection();
    }
}

public void moveRandomDirection()
{
    double direction = Math.random() * 2.0 * Math.PI;
    double speed = 10.0;
    speedX = (int) (speed * Math.cos(direction));
    speedY = (int) (speed * Math.sin(direction));
}

public void run()
{
    long beforeTime, timeDiff, sleep;
    beforeTime = System.currentTimeMillis();
    while (true)
    {
        move();
        repaint();

        timeDiff = System.currentTimeMillis() - beforeTime;
        sleep = DELAY - timeDiff;

        if (sleep > 2)
        {
            sleep = 1;
        }
        try
        {
            Thread.sleep(sleep);
        }
        catch (InterruptedException e)
        {
            System.out.println("interrupted");
        }

        beforeTime = System.currentTimeMillis();
    }
}
}
4

2 回答 2

1

这涵盖了一个随机位置

计算允许球出现的区域:

int x = RIGHT_WALL - LEFT_WALL;
int y = DOWN_WALL - UP_WALL;

减去球的大小:

x -= ball.getWidth(null);
y -= ball.getHeight(null);

选择一个随机位置:

Random r = new Random(); // java.util.Random
x = r.nextInt(x);
y = r.nextInt(y);

将坐标移动到墙左上角的起点:

x += LEFT_WALL;
y += UP_WALL;

现在xy是球出现的有效位置。

注意:请注意,这里使用的xy不是您的班级成员。这些应该是局部变量。


似乎您自己解决了随机方向的算法。


您的方法有一个错误move():检查碰撞时您不关心球的大小。ifs 应该如下所示:

if (x + ball.getWidth(null) >= RIGHT_WALL)
{
    x = RIGHT_WALL - ball.getWidth(null);
    moveRandomDirection();
}
if (y + ball.getHeight(null) >= DOWN_WALL)
{
    y = DOWN_WALL - ball.getHeight(null);
    moveRandomDirection();
}
if (x <= LEFT_WALL)
{
    x = LEFT_WALL;
    moveRandomDirection();
}
if (y <= UP_WALL)
{
    y = UP_WALL;
    moveRandomDirection();
}
于 2011-11-01T08:31:37.840 回答
0

您也可以生成随机值,而不是构造函数中的 x=y=10。我会使用java.util.Random类来做到这一点。看看 nextInt 方法。您可以使用参数在那里设置最大值。

于 2011-11-01T08:35:01.750 回答