math.rand()
807598May 9 2006 — edited May 9 2006Im programming a game for uni and I want to make the ball move in a random direction at the begining of the game. I have 2 velocities xvelocity and yvelocity. some code below:
class Ball extends Sprite
{
Ball(int a, int b)
{
super(a, b, 5, 5);
color = Color.white;
xvelocity = 0;
yvelocity = 0;
}
void show (Graphics g)
{
g.fillRect(x*SIZE,y*SIZE,5*SIZE,5*SIZE);
}
void move()
{
x += xvelocity;
y += yvelocity;
}
}
// code to start the ball moving.
case KeyEvent.VK_SPACE : ball.xvelocity = ball.yvelocity = 1; break;