email me at borlaj@portlandschools.org
notes previous (09/07/06) submit the dump links  
 

HWJ13_Timing

Create an applet that when the user presses the space bar, a ball will appear in a random location and then move at a random direction around the screen. If it hits a wall, it will bounce off it. If the user hits space bar again, it will pause it and then space bar again restarts the action.

Start with the timing code found here.

 

Rubric 8 points HW due Monday beginning of class.

  • 1 comments
  • 1 proper indentation
  • 1 space bar starts the action
  • 1 space bar pauses and starts the action
  • 1 ball starts in a random location
  • 1 ball moves in a random direction
  • 2 ball bounces off wall correctly

 

Hints:

  • Have a ball start at a random location (so get a random x between 0, 500 and same for y)
  • To move the ball, I would have a changeInX and changeInY global variable that would be how much the ball moves each time.
  • Originally I would set this as a random between -5 and 5 or something like that, excluding 0 (otherwise it will move just up and down or left and right) To get a random between -5 and 5, I would get a random between 0-10 and then subtract 5.
    • Question: How do we get a random number between -5 and 5 without 0?
  • If you hit a wall, have the ball change in direction. Ie, changeInX is 4 and it hits the right wall, changeInX would then be -4.