RHS   Allan   Game Techniques Exercises Solution03


 

Solution for Exercise03

In class Worm change the method nextPoint(int, int) to:

private Point nextPoint(int prevPosn, int bearing) {
  // get the increments for the compass bearing
  Point2D.Double incr = incrs[bearing];

  int newX = cells[prevPosn].x + (int)(DOTSIZE * incr.x);
  int newY = cells[prevPosn].y + (int)(DOTSIZE * incr.y);

  // modify newX/newY if < 0, or > pWidth/pHeight;
  if (newX < 0) //leaving to the left
    newX = 0;
  else if (newX > pWidth - DOTSIZE) //leaving to the right
    newX = pWidth - DOTSIZE;

  if (newY < 0) // leaving at the top
    newY = 0;
  else if (newY > pHeight - DOTSIZE) //leaving at the buttom
    newY = pHeight - DOTSIZE;

  return new Point(newX,newY);
} // end of nextPoint()


Maintained by: Allan Helboe Nielsen
Updated: 3 November, 2005 1:04