begin:
r = Math.GetRandomNumber(180)
Turtle.Turn(r)
f = Math.GetRandomNumber(100)
GraphicsWindow.PenColor = "Blue"
Turtle.Move(f)
TurtlePositionCheck()
If r >= 90 then
neg()
EndIf
Goto begin
Sub neg
r = Math.GetRandomNumber(180)
Turtle.Turn(-r)
r = Math.GetRandomNumber(100)
GraphicsWindow.PenColor = "Red"
Turtle.Move(r)
TurtlePositionCheck()
EndSub
Sub TurtlePositionCheck
'variables used:
'bbm = Bounce Back Movement: amount turtle went off the screen is the amount that the turtle should bounce back from the edge.
'bba = Bounce Back Angle: angle to bounce turtle off edge of screen - based on angle turtle was at when it left the screen
'tpc = Turtle Postion Check: used to repeat the off screen check in case the bounced back position ends up off the screen on the other axis
'overedge = flag used to loop turtle back up movement
tpc = "True"
While (tpc = "True")
tpc = "False"
'check if turtle is off edge
If Turtle.X < 0 Or Turtle.X > GraphicsWindow.Width Or Turtle.Y < 0 or Turtle.Y > GraphicsWindow.Height Then
OverEdge = "Yes"
If Turtle.Y < 0 or Turtle.Y > GraphicsWindow.Height Then
'figure out the angle to bounce back
If math.Remainder(Turtle.Angle,360) < 180 Then
bba = 180 - math.Remainder(Turtle.Angle,360)
Else
bba = 180 + (360 - math.Remainder(Turtle.Angle,360))
EndIf
EndIf
'same as above but for the X axis
If Turtle.X < 0 Or Turtle.X > GraphicsWindow.Width Then
'figure out the angle to bounce back angle
bba = 360 - math.Remainder(Turtle.Angle,360)
EndIf
bbm = 0
While (OverEdge = "Yes")
'neg. amount moves turtle backward and While
'contiues until turtle is at the screen coordinates
'it was when it went over the edge - not exactly, but close enough.
Turtle.Move(-1)
bbm=bbm + 1
If Turtle.X > 0 And Turtle.X < GraphicsWindow.Width And Turtle.Y > 0 And Turtle.Y < GraphicsWindow.Height Then
OverEdge = "No"
EndIf
EndWhile
Turtle.Angle = bba 'turn turtle to bounce angle and
Turtle.Move(bbm) 'move turtle the bounce back amount
're-run this sub by using While (tpc) loop, if the new location is off edge of screen
If Turtle.X < 0 Or Turtle.X > GraphicsWindow.Width Or Turtle.Y < 0 or Turtle.Y > GraphicsWindow.Height Then
tpc = "True"
EndIf