Wednesday 15 June 2011

java - basic bouncing ball in graphics window -


I am trying to create a simple animation that bounces a goole ball from the bottom of the graphics window and then Again I do not get out of the window until I get out of the window. The problem is that I do not understand how to get the ball to recognize the lower part of the graphics window and to put it on the other side. In other words, the ball never bounces from the floor, it just falls and eventually disappears under the screen. Is this the right approach to thinking about this problem, as long as I & lt; The ball should go down that step I do not understand what the situation should be to bowl the ball, and then again down again.

  Import acm.program. *; Import acm.graphics *; Public Class Bouncingball Graphics window {Public Zero Run) (Goal Ball Expands New Goal (0, 0, OVL_SIIS, OVL_SEGEE); ball.setFilled (right); add the ball; int dx = 0; Int dy = 1; while (true) {int i = 0; if (i & lt; STEPS) {ball.move (dx, dy); pausing (PAUSE_TIME);} if (??) {ball.move (dx, - Dy;; pauses (PAUSE_TIME);} I ++;}} Private Static Last STEPS = 1000; Private Stable Last OVAL_SIZE = 25; Private Stable Last PAUSE_TIME = 7;}    < P> 

you mean by instead of GraphicsProgram Is GraphicsWindow ? GraphicsProgram s is a getHeight () method that will tell you how long your window is. In this case:

  if (ball.getY) + ball.getHeight ()> = getHyight ()) {dy = -dy; }   

It goes down to the bottom of the screen when it reverses the y-velocity of the ball. If you want to bounce it back and forth then you can write the same code for the top of the screen.

No comments:

Post a Comment