/**
 *  The BouncingTurtle class moves and bounces off the sides 
 *  of the Display.
 *
 */
import java.awt.*;
import com.otherwise.jurtle.*;

public class BouncingTurtle extends Turtle
{
    static final int TOTAL_MOVES = 400000; // Total number of moves before quiting.
    
    int numBounces;                       // Number of bounces off the walls.   

    /**
     * Entry point to the Turtle's code.  
     */
    public void runTurtle()
    {
        setHeading( 30 );
        int numMoves = 0;
        while ( numMoves < TOTAL_MOVES )
        {
            move();
            numMoves = numMoves + 1;
        }
    }


    private void move()
    {
        // Move the turtle
        forward( 20 );

        // Get current location.  If we have hit a wall then turn.
        Point pt = getPosition();
        Dimension size = getDisplaySize();

        if ( pt.y < 0 || pt.y > size.height )
        {
            setHeading( 180 - getHeading() );
            numBounces = numBounces + 1;
        }
        if ( pt.x < 0 || pt.x > size.width )
        {
            setHeading( 360 - getHeading() );
            numBounces = numBounces + 1;
        }

    }


}
