Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

math.rand()

807598May 9 2006 — edited May 9 2006
Im 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;
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 6 2006
Added on May 9 2006
2 comments
120 views