440 likes | 663 Views
getting started. with the turtle. l earning objectives. basic editor usage basic coding skills publishing and sharing scripts and lots of turtle fun!. turtle programming. you control a turtle the turtle has a pen and draws a line as it moves
E N D
getting started with the turtle
learning objectives • basic editor usage • basic coding skills • publishing and sharing scripts • and lots of turtle fun!
turtle programming • you control a turtle • the turtle has a pen and draws a line as it moves • you have to give commands to the turtle to move forward or turn • 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 • a script is an app created in TouchDevelop • a script can be published to TouchDevelop so that other users can use it • a script can be exportedinto a Windows app or Windows Phone app.
navigate the script code editor scroll to the bottom
tap to select tap to select
tap to select tap to delete or press backspace
loading turtle • type for ‘turtle’ in the search bar • Tap on ‘search for libraries’ • tap on ‘turtle’ from the ‘TouchDevelop Samples’ user
and we keep taping to build the code tap to add a new line
type and select type “turtle” and insert type “init” and insert
keep coding then run run your script
run again go back to the editor the turtle
select multiple lines copy to clipboard paste it!
that’s how your code should look like we’ve got a square!
custom pen • change the color of the pen libs->turtle->pen color(colors→random) • change the thickness of the pen libs->turtle->set thickness(20)
exercises • change to color on each stroke • (optional) draw a triangle with the turtle • (optional) draw a pentagon with the turtle
publishing • publish your script to the cloud • anybody can see it on internet • once published, take screenshots • (optional)
1. tap here 3. published scripts have a unique id 2. tap here
run the published app take screenshot
for loops • 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 < 4 do turtle->forward(100) turtle->left turn(90) index = 0
square for 0 ≤ index < 4 do turtle->forward(100) turtle->left turn(90) index = 1
square for 0 ≤ index < 4 do turtle->forward(100) turtle->left turn(90) index = 2
square for 0 ≤ index < 4 do turtle->forward(100) turtle->left turn(90) index = 3
add a new line and tap on ‘for’ use number keypad or type 4
local variables • alocal variable holds a value in memory; • 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 c holds the current distance to move forward var c := 100 for 0 ≤ index < 10 do turtle->forward(c) turtle->left turn(90)c := c + 5 increase c by 5 so that the turtle moves a little bit more on the next iteration
if .. then .. else .. if statement: executes different code based on a Boolean condition if ... then ... else ... true or false happens when true happens when false
random walk returns a random number between 0 (included) and 2 (excluded) ifmath->random(2) = 0 then ... else ... 50% chance true or false
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 the action is called ‘square’ r is an output, it is a number storing x*x into r
nested square action square(c : Number) for 0 ≤ index < 4 do turtle->forward(c) turtle->left turn(90) action main() var c := 100 for 0 ≤ index < 10 docode->square(c)c := c + 5
homework 1 • recreate the following turtle drawing
color gradients • create gradient effects var c := colors→linear gradient(colors→red, colors →blue, index / 3) ♺turtle→pencolor(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. • Your drawing must be created when you run the Main function. • You must use three other functions, each of which draws something. • You must use at least one for loop and at least one if statement. • Your functions should be appropriately named, and each should do something unique. • At least one function must use a parameter, and it must be called using multiple values for that parameter. • All functions must be used more than once. Using a function within a loop works for this purpose. • You must use at least three colors, and you must change the pen thickness at least once. • Your program should terminate within a reasonable amount of time. • Ideas If you don’t know what to draw, here are a few ideas: • Try drawing a shape, then rotating the turtle some amount, and then doing it again, and so on. • Try writing functions that don’t end in the same place they started. • You can draw irregular shapes, like diamonds, isosceles triangles, or stars. • Try using the pen up and move to functions to draw the same shape on different parts of the screen. • Instead of drawing a geometric shape, draw a stick figure