 Trying to learn and understand more about Turtle • Question

• litdev can you transfer my misplaced discussion to this post plz.

Trying to understand how Turtle is programmed or made to work

for example why does it read the first variable as number of lines per circle, the second variable as number of circles to draw and third variable as number of times to repeat previous steps. Thanks

Friday, August 17, 2012 1:59 AM

• Your code works well.   I made it simplified. 90 degree turn is written in array "rot".

circles = 5
turns   = 360 / circles
sides   = 20
length  = 100 / sides
angle   = 360 / sides
Turtle.Speed = 10
rot="1=0;2=1;3=1;4=0;5=1;6=0;7=1;8=0"      '<-------- 90 degree turn

For K=1 to 9
turtle1()
endfor

sub turtle1
Turtle.PenDown()
For j = 1 To circles
For i = 1 To sides
Turtle.Move (length)
Turtle.Turn (angle)
EndFor
Turtle.Turn (turns)
EndFor
Turtle.Turn (90*rot[k])        '<--------
Turtle.PenUp()
Turtle.Move (100)
endsub

Friday, August 17, 2012 5:28 AM
• Nice one NaochanON! You've merged turtlemove100() + turtlemove90deg() into 1. The only difference between them was the presence or not of Turtle.Turn(90) anyways.

Nice call on putting that information in an array and using another For...EndFor loop w/ its iterator working as an array index.  ^_^

And Vdian, that's what loops are for, to make your code more concise and witty, repeating many times the same lines of code block; and when the opportunity arises, using its iterator as a type of index, where some extra calculations can be derived from its current value within a loop.

Now, here's my hill-billy tackle on this:  :-P

circles = 4
turns   = 360 / circles

lines   = 8
length  = 100 / lines
angle   = 360 / lines

columns = 6
rows    = 4

startX  = GraphicsWindow.Width
startY  = GraphicsWindow.Height
distX   = 90
distY   = 90

GraphicsWindow.BackgroundColor = "Black"
GraphicsWindow.PenWidth = 4
Turtle.Speed = 10

For col = 1 To columns
For row = 1 To rows

GraphicsWindow.PenColor = GraphicsWindow.GetRandomColor()
Turtle.PenUp()
Turtle.MoveTo( startX - distX*col,  startY - distY*row )
Turtle.Angle = 0
Turtle.PenDown()

For circle = 1 To circles
For line = 1 To lines
Turtle.Move(length)
Turtle.Turn(angle)
EndFor
Turtle.Turn(turns)
EndFor

Sound.PlayClick()

EndFor
EndFor

Sound.PlayChimeAndWait()

Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

Friday, August 17, 2012 11:34 AM
• Hey GoToLoop  I wanted to mention that i believe i am beginning to understand the answer to my question concerning how variables understand what to do.

In truth, variables are labels for memory addresses. Each mem address can store one 8-bit value.

In the case of a For loop, the primary function of its variable, in this specific case also called an iterator, is to be a counter, so the For loop knows when to stop.

For example -> For i = 1 To sides, it makes the var i its iterator, with the initial value = 1. Even before starting its code block, the For statement compares the value stored inside i w/ that of sides. As long as i <= sides, the For loop block happens. But when i becomes greater than sides (i>sides), the loop stops, then program execution goes forward with next command just after the EndFor.

But how does var i change its stored value? There's an automatic iterator increment (i = i + 1) after loop reaches EndFor! So it can take many loop repeats, but the iterator i will surely extrapolate the value of sides. So we call the value after the keyword To the condition of the For...EndFor loop. And normally it is the same number of times we want the loop to happen.

But here's a question, how many Turtle.Move (length)   Turtle.Turn (angle)  do we need to repeat to make the turtle draw a full circle?

The answer is stored inside var sides! Because sides * angle = 360, a full circle.

Turtle.Move (length) draws a small straight line, whose distance value is stored within length; then Turtle.Turn (angle) clockwise-rotates the turtle angle degrees.

Just imagine how much we'd have to type  Turtle.Move (length)   Turtle.Turn (angle)  if there was no For...EndFor loop!

I guess it's getting clear to you now how programming works.  :D

Just to finish, a slightly modification of your latest code:

circles = 5
turns   = 360 / circles

sides   = 8
length  = 30  / sides
angle   = 360 / sides

GraphicsWindow.BackgroundColor = "Black"
gw = GraphicsWindow.Width
gh = GraphicsWindow.Height
Turtle.Speed = 10

TurtleLoop:

Turtle.PenUp()
Turtle.MoveTo ( Math.GetRandomNumber(gw)  Math.GetRandomNumber(gh) )
Turtle.Angle = Math.GetRandomNumber(361) - 1
Turtle.PenDown()

GraphicsWindow.PenColor = GraphicsWindow.GetRandomColor()

For j = 1 To circles
For i = 1 To sides
Turtle.Move (length)
Turtle.Turn (angle)
EndFor
Turtle.Turn (turns)
EndFor

Goto TurtleLoop

Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

Friday, August 17, 2012 4:48 PM

All replies

• Hi , i wanted to post my newest circle program, they arent exactly circles but i changed the number of sides per circle in order to hopefully speed up the process. Also I used subroutines which is about as far as i have read in the introducing small basic file.

circles = 5
turns   = 360 / circles

sides   = 8
length  = 100 / sides
angle   = 360 / sides

Turtle.Speed = 10

sub turtle1
For j = 1 To circles

For i = 1 To sides
Turtle.Move (length)
Turtle.Turn (angle)
EndFor
Turtle.Turn (turns)
EndFor
endsub

turtle1()
turtlemove100()
Sub turtlemove100
Turtle.PenUp()
Turtle.Move (100)
Turtle.PenDown()
turtle1()
EndSub

Sub turtlemove90deg
Turtle.PenUp()
Turtle.Turn (90)
Turtle.Move (100)
Turtle.PenDown()
turtle1()
endsub

turtlemove90deg()
turtlemove90deg()
turtlemove100()
turtlemove90deg()
turtlemove100()
turtlemove90deg()
turtlemove100()

If anyone can improve upon this so its not so much typing plz do so.

Friday, August 17, 2012 2:53 AM
• Your code works well.   I made it simplified. 90 degree turn is written in array "rot".

circles = 5
turns   = 360 / circles
sides   = 20
length  = 100 / sides
angle   = 360 / sides
Turtle.Speed = 10
rot="1=0;2=1;3=1;4=0;5=1;6=0;7=1;8=0"      '<-------- 90 degree turn

For K=1 to 9
turtle1()
endfor

sub turtle1
Turtle.PenDown()
For j = 1 To circles
For i = 1 To sides
Turtle.Move (length)
Turtle.Turn (angle)
EndFor
Turtle.Turn (turns)
EndFor
Turtle.Turn (90*rot[k])        '<--------
Turtle.PenUp()
Turtle.Move (100)
endsub

Friday, August 17, 2012 5:28 AM
• Nice one NaochanON! You've merged turtlemove100() + turtlemove90deg() into 1. The only difference between them was the presence or not of Turtle.Turn(90) anyways.

Nice call on putting that information in an array and using another For...EndFor loop w/ its iterator working as an array index.  ^_^

And Vdian, that's what loops are for, to make your code more concise and witty, repeating many times the same lines of code block; and when the opportunity arises, using its iterator as a type of index, where some extra calculations can be derived from its current value within a loop.

Now, here's my hill-billy tackle on this:  :-P

circles = 4
turns   = 360 / circles

lines   = 8
length  = 100 / lines
angle   = 360 / lines

columns = 6
rows    = 4

startX  = GraphicsWindow.Width
startY  = GraphicsWindow.Height
distX   = 90
distY   = 90

GraphicsWindow.BackgroundColor = "Black"
GraphicsWindow.PenWidth = 4
Turtle.Speed = 10

For col = 1 To columns
For row = 1 To rows

GraphicsWindow.PenColor = GraphicsWindow.GetRandomColor()
Turtle.PenUp()
Turtle.MoveTo( startX - distX*col,  startY - distY*row )
Turtle.Angle = 0
Turtle.PenDown()

For circle = 1 To circles
For line = 1 To lines
Turtle.Move(length)
Turtle.Turn(angle)
EndFor
Turtle.Turn(turns)
EndFor

Sound.PlayClick()

EndFor
EndFor

Sound.PlayChimeAndWait()

Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

Friday, August 17, 2012 11:34 AM
• Thanks for your replies , and the introduction of using sound, which i havent incorporated into my newest program...

I got it to do what i wanted to do, but i had to pull up a previous program from GoToLoop and then add in my messed up code a piece or two at a time. Anyhow here it is, btw its really slow.  Also I think i could take the subroutine out but i havent tried to yet.

GraphicsWindow.BackgroundColor = "Black"

circles = 5
turns   = 360 / circles

sides   = 25
length  = 25/ sides
angle   = 360 / sides

move()

Turtle.Speed = 10
Sub move

For z = 1 To 500
x = Math.GetRandomNumber ( 500 )
y = Math.GetRandomNumber ( 500 )
GraphicsWindow.PenColor = GraphicsWindow.GetRandomColor()
For j = 1 To circles

For i = 1 To sides
Turtle.Move (length)
Turtle.Turn (angle)
EndFor

Turtle.Turn (turns)

EndFor
Turtle.PenUp()
Turtle.MoveTo (x,y)
Turtle.PenDown()
EndFor
EndSub

rot="1=0;2=1;3=1;4=0;5=1;6=0;7=1;8=0"    NaochanON, thanks for rewriting my program.   I understand that =0 mean go straight and = 1 means

Turtle.Turn (90*rot[k])                                     turn 90deg , but I dont get (90*rot[k])  thats 90 x rotation = 0 or 90 but why is the [k] in there ? I havent written any arrays yet myself only read about them in the introducing small basic file..     Thanks for your reply !

• Edited by Friday, August 17, 2012 2:54 PM
Friday, August 17, 2012 2:53 PM
• Hey GoToLoop  I wanted to mention that i believe i am begining to understand the answer to my question concerning how variables understand what to do
Friday, August 17, 2012 2:57 PM
• rot="1=0;2=1;3=1;4=0;5=1;6=0;7=1;8=0"    NaochanON, thanks for rewriting my program.   I understand that =0 mean go straight and = 1 means

Turtle.Turn (90*rot[k])                                     turn 90deg , but I dont get (90*rot[k])  thats 90 x rotation = 0 or 90 but why is the [k] in there ? I havent written any arrays yet myself only read about them in the introducing small basic file..     Thanks for your reply !

Hi Vdian

rot="1=0;2=1;3=1;4=0;5=1;6=0;7=1;8=0"  means  rot=0, rot=1,.....,rot=0 .  That is rot[k].

(rot=0 is not written here,      because it is unnecessary  this case)

This method avoids to write many array.  One useful technic

* About  "btw its really slow " ......

Turtle.Speed = 10  should be written before  move()

Friday, August 17, 2012 3:07 PM
• Hey GoToLoop  I wanted to mention that i believe i am beginning to understand the answer to my question concerning how variables understand what to do.

In truth, variables are labels for memory addresses. Each mem address can store one 8-bit value.

In the case of a For loop, the primary function of its variable, in this specific case also called an iterator, is to be a counter, so the For loop knows when to stop.

For example -> For i = 1 To sides, it makes the var i its iterator, with the initial value = 1. Even before starting its code block, the For statement compares the value stored inside i w/ that of sides. As long as i <= sides, the For loop block happens. But when i becomes greater than sides (i>sides), the loop stops, then program execution goes forward with next command just after the EndFor.

But how does var i change its stored value? There's an automatic iterator increment (i = i + 1) after loop reaches EndFor! So it can take many loop repeats, but the iterator i will surely extrapolate the value of sides. So we call the value after the keyword To the condition of the For...EndFor loop. And normally it is the same number of times we want the loop to happen.

But here's a question, how many Turtle.Move (length)   Turtle.Turn (angle)  do we need to repeat to make the turtle draw a full circle?

The answer is stored inside var sides! Because sides * angle = 360, a full circle.

Turtle.Move (length) draws a small straight line, whose distance value is stored within length; then Turtle.Turn (angle) clockwise-rotates the turtle angle degrees.

Just imagine how much we'd have to type  Turtle.Move (length)   Turtle.Turn (angle)  if there was no For...EndFor loop!

I guess it's getting clear to you now how programming works.  :D

Just to finish, a slightly modification of your latest code:

circles = 5
turns   = 360 / circles

sides   = 8
length  = 30  / sides
angle   = 360 / sides

GraphicsWindow.BackgroundColor = "Black"
gw = GraphicsWindow.Width
gh = GraphicsWindow.Height
Turtle.Speed = 10

TurtleLoop:

Turtle.PenUp()
Turtle.MoveTo ( Math.GetRandomNumber(gw)  Math.GetRandomNumber(gh) )
Turtle.Angle = Math.GetRandomNumber(361) - 1
Turtle.PenDown()

GraphicsWindow.PenColor = GraphicsWindow.GetRandomColor()

For j = 1 To circles
For i = 1 To sides
Turtle.Move (length)
Turtle.Turn (angle)
EndFor
Turtle.Turn (turns)
EndFor

Goto TurtleLoop

Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

Friday, August 17, 2012 4:48 PM