0

I initially instantiated the ball in the center of the window using the initBall function. However after I implemented the code to move the ball randomly across the window, it doesn't begin its motion from the center of the window each time I begin the game like it does in the staff's solution. Could it be a problem my code for the ball's random motion?

double velocityX = drand48() * 5;
double velocityY = 2.0;

while(lives > 0 && bricks > 0)
{

move(ball, velocityX, velcityY)

if (ball touches left or right edge of window)

    velocityX = -velocityX;

if (ball touches top or bottom edge of window)

    velocityY = -velocityY;

pause(10)

}


GOval initBall (Gwindow window)
{
Goval ball = newGOval(0,0, 2*RADIUS, 2*RADIUS)
setColour (ball, "RED");
setFilled (ball, true);
add(window,ball);

double x = (getWidth(window) - getWidth(ball)) / 2;
double y = (getHeight(window) - getHeight(ball)) / 2;
setLocation(ball,x,y);

return ball;
}
5
  • I think it is more likely something in how you create and initiate the ball. Can you please edit the question and add that code there?
    – Cliff B
    May 8 '15 at 18:06
  • Also, it's not exactly clear in your description. Are you saying that at the very beginning, the ball is not starting at the center? Or are you saying that after the ball hits the bottom of the window the first time, it isn't REstarting in the center? If it is the latter, are you repositioning the ball back at the center before you start it moving again?
    – Cliff B
    May 8 '15 at 19:40
  • Hi Cliff, yes, in the beginning itself the ball is not starting from the center, although when I instantiated it before giving it motion, it was positioned at the center of the window. I have edited the code to include the ball instantiation code. Hope this helps. May 13 '15 at 10:41
  • I could not see in your code place when you are loosing "life", at this moment you should set up initialization of ball from the middle and make event null
    – LeVl
    May 17 '15 at 19:59
  • Nothing in your code would cause the ball to not be centered at the start.
    – curiouskiwi
    May 27 '15 at 0:30

You must log in to answer this question.

Browse other questions tagged .