Download presentation
Presentation is loading. Please wait.
Published byViolet Small Modified over 9 years ago
1
getting started with the turtle Find the latest version of this document at http://touchdevelop.com/slides.http://touchdevelop.com/slides
2
learning objectives o basic editor usage o basic coding skills o publishing and sharing scripts o and lots of turtle fun!
3
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
4
square “think like the turtle”…
5
square forward 100
6
square forward 100 left turn 90 (degrees)
7
square forward 100 left turn 90 forward 100
8
square forward 100 left turn 90 forward 100 left turn 90 …
9
square forward 100 left turn 90 forward 100 left turn 90 forward 100 left turn 90
10
square forward 100 left turn 90 forward 100 left turn 90 forward 100 left turn 90 forward 100 left turn 90
11
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.
12
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)
13
code editor navigate the script scroll to the bottom
14
tap to select
15
tap to delete or press backspace
16
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
17
and we keep taping to build the code tap to add a new line
18
type “turtle” and insert type “init” and insert type and select
19
keep coding then run run your script
20
the turtle go back to the editor run again
21
select multiple lines copy to clipboard paste it!
22
we’ve got a square! that’s how your code should look like
23
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)
24
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
25
publishing o publish your script to the cloud anybody can see it on internet o once published, take screenshots (optional)
26
1. tap here 2. tap here 3. published scripts have a unique id
27
run the published app take screenshot
28
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
29
square for 0 ≤ index forward(100) turtle->left turn(90) index = 0
30
square for 0 ≤ index forward(100) turtle->left turn(90) index = 1
31
square for 0 ≤ index forward(100) turtle->left turn(90) index = 2
32
square for 0 ≤ index forward(100) turtle->left turn(90) index = 3
33
add a new line and tap on ‘for’ use number keypad or type 4
34
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
35
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
36
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
37
random walk if math->random(2) = 0 then... else... 50% chance true or false returns a random number between 0 (included) and 2 (excluded)
38
random walk turtle->forward(10) if math->random(2) = 0 then turtle->left turn(90) else turtle->right turn(90)
39
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
40
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
41
drawing a tree
42
see script /gdmw
43
o recreate the following turtle drawing homework 1
44
color gradients o create gradient effects var c := colors→linear gradient(colors→red, colors →blue, index / 3) turtle→pen color(c)
45
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.