如何随机指导?

时间:2011-11-01 08:26:08

标签: java random direction

我的图像在开始撞墙时会随机移动。图像始终在执行期间开始或出现在左上角,您可以在我的代码中看到它。现在我希望图像在执行期间出现在随机位置,这是我的问题,有人可以给我一个关于此的想法吗?提前谢谢。

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();
    }
}
}

2 个答案:

答案 0 :(得分: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()方法有误:检查碰撞时你不关心球的大小。 if应如下所示:

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();
}

答案 1 :(得分:0)

在构造函数中,您可以生成随机值,而不是x = y = 10。我会使用java.util.Random类来执行此操作。看看nextInt方法。您可以使用参数在那里设置最大值。