getting started with the turtle Find the latest version of this document at
learning objectives o basic editor usage o basic coding skills o publishing and sharing scripts o and lots of turtle fun!
turtle programming o you control a turtle o the turtle has a pen and draws a line as it moves o you have to give commands to the turtle to move forward or turn o see LOGO for reference
square “think like the turtle”…
square forward 100
square forward 100 left turn 90 (degrees)
square forward 100 left turn 90 forward 100
square forward 100 left turn 90 forward 100 left turn 90 …
square forward 100 left turn 90 forward 100 left turn 90 forward 100 left turn 90
square forward 100 left turn 90 forward 100 left turn 90 forward 100 left turn 90 forward 100 left turn 90
scripts o a script is an app created in TouchDevelop o a script can be published to TouchDevelop so that other users can use it o a script can be exported into a Windows app or Windows Phone app.
turtle as a library o scripts use actions from other scripts o a script must be marked as ‘library’ in order to share its actions o only public actions can be shared (i.e. non private)
code editor navigate the script scroll to the bottom
tap to select
tap to delete or press backspace
loading turtle o type for ‘turtle’ in the search bar o Tap on ‘search for libraries’ o tap on ‘turtle’ from the ‘TouchDevelop Samples’ user
and we keep taping to build the code tap to add a new line
type “turtle” and insert type “init” and insert type and select
keep coding then run run your script
the turtle go back to the editor run again
select multiple lines copy to clipboard paste it!
we’ve got a square! that’s how your code should look like
custom pen o change the color of the pen libs->turtle->pen color(colors→random) o change the thickness of the pen libs->turtle->set thickness(20)
exercises o 1. change to color on each stroke o 2. draw a triangle with the turtle o 3. draw a pentagon with the turtle
publishing o publish your script to the cloud anybody can see it on internet o once published, take screenshots (optional)
1. tap here 2. tap here 3. published scripts have a unique id
run the published app take screenshot
for loops o for loop: repeats the same code multiple times definition for 0 ≤ index < count do... index starts at 0, increments by 1 on each iteration and finishes at count-1. index goes through 0, 1, 2, …, count-1
square for 0 ≤ index forward(100) turtle->left turn(90) index = 0
square for 0 ≤ index forward(100) turtle->left turn(90) index = 1
square for 0 ≤ index forward(100) turtle->left turn(90) index = 2
square for 0 ≤ index forward(100) turtle->left turn(90) index = 3
add a new line and tap on ‘for’ use number keypad or type 4
local variables o a local variable holds a value in memory; o a local variable can be read or written too. definition var x := 0 reading x->post to wall update with new value x := 5
spiral var c := 100 for 0 ≤ index forward(c) turtle->left turn(90) c := c + 5 c holds the current distance to move forward increase c by 5 so that the turtle moves a little bit more on the next iteration
if statement: executes different code based on a Boolean condition if... then... else... if.. then.. else.. true or false happens when true happens when false
random walk if math->random(2) = 0 then... else... 50% chance true or false returns a random number between 0 (included) and 2 (excluded)
random walk turtle->forward(10) if math->random(2) = 0 then turtle->left turn(90) else turtle->right turn(90)
actions functions: a collection of lines of code with a name that one can call. Functions can have inputs and outputs. action square(x : Number) returns r : Number r := x * x x is an input, it is a number r is an output, it is a number the action is called ‘square’ storing x*x into r
nested square action square(c : Number) for 0 ≤ index forward(c) turtle->left turn(90) action main() var c := 100 for 0 ≤ index square(c) c := c + 5
drawing a tree
see script /gdmw
o recreate the following turtle drawing homework 1
color gradients o create gradient effects var c := colors→linear gradient(colors→red, colors →blue, index / 3) turtle→pen color(c)
homework 2 You will create a script that uses the turtle library to draw a design of your own creation. The design itself is up to you, but it must include all of the technical elements described below. o Your drawing must be created when you run the Main function. o You must use three other functions, each of which draws something. o You must use at least one for loop and at least one if statement. o Your functions should be appropriately named, and each should do something unique. o At least one function must use a parameter, and it must be called using multiple values for that parameter. o All functions must be used more than once. Using a function within a loop works for this purpose. o You must use at least three colors, and you must change the pen thickness at least once. o Your program should terminate within a reasonable amount of time. o Ideas If you don’t know what to draw, here are a few ideas: o Try drawing a shape, then rotating the turtle some amount, and then doing it again, and so on. o Try writing functions that don’t end in the same place they started. o You can draw irregular shapes, like diamonds, isosceles triangles, or stars. o Try using the pen up and move to functions to draw the same shape on different parts of the screen. o Instead of drawing a geometric shape, draw a stick figure