Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Building Your Own Turtle Functions For making really cool pictures!

Similar presentations


Presentation on theme: "1 Building Your Own Turtle Functions For making really cool pictures!"— Presentation transcript:

1 1 Building Your Own Turtle Functions For making really cool pictures!

2 2 5 Cool Things… 1.Variables and Arithmetic 2.Branches for variety 3.Using Functions 4.Building your own Functions (Today) 5.Loops to repeat (Next week)

3 3 Review: 1) Variables and Arithmetic var n = 4; var y = 2; var x = n + y; y = n * 2; document.write(“x=” + x + “, y=” + y);

4 4 2) Using Functions Math Functions: x = Math.sqrt(81); y = Math.sqrt(x); document.write(“x=” + x + “, y=” + y); Turtle Functions: forward(20); left(90); forward(20); right(135); backward(40); Identify the Function calls and the arguments

5 5 Which command(s) uses “relative” positioning, and which “absolute” forward -- move turtle forward some number of pixels left – turn left some number of degrees moveTo -- move to an x,y coordinate turnTo – turn to a particular degree heading home – send turtle back to center of screen, facing up

6 6 Today—3) Building your own functions Lets you “abstract” a collection of moves For example, these lines make a square: forward(20); right(90);

7 7 If you want to draw a lot of squares, put this at the top of your script… function square( ) { forward(40); right(90); } This is a Function Definition

8 8 Now you can ‘call’ your square function square(); -------------------- left(30); square(); left(180); square(); moveTo(-300, -100); left(30); square(); left(180); square();

9 9 Functions help manage complexity You can do interesting patterns without a lot of repetition of code They save time and effort Functions can use other functions

10 10 What if you want different sizes of squares? function square( n ) { forward(n); right(90); } n is called a parameter It’s a variable that receives the size of the square (given as the argument in the function call)

11 11 Now when you call the square function, just say how big you want it to be square(100); square(50); square(25);

12 12 You can generate random sized squares… var size, angle; size=rand(50,100); square(size); angle=rand(0,180); turnTo(angle); size=rand(50,100); square(size); rand(low, high) gives random number between low and high

13 13 Every time you run the code from last slide, it gives a different result It’s interesting to observe the variations and similarities

14 14 Capture your images in a “screen shot” Press Alt and PrtScr at same time Open Paint Edit/Paste You can chop out image using select tool Dotted line box Then paste into Microsoft Word Or save as a.jpg file (project 2)

15 15 That’s it! Have fun in lab Next week…branches and loops


Download ppt "1 Building Your Own Turtle Functions For making really cool pictures!"

Similar presentations


Ads by Google