-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTurtleDemo.pde
76 lines (66 loc) · 2.68 KB
/
TurtleDemo.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//Show the demo(s).
// Wiggly moving things + random wavy lines
// Also show that you can just issue commands in the setup to mess with turtles to start.
//Priorities:
//1. Students Familiarize themselves with turtles by creating a single variable turtle.
// They then issue commands in the setup (without a draw for now) to see how it behaves.
//2. Write a method doStuff(Turtle t) that takes a Turtle as a parameter and moves it
// around the screen drawing lines.
//3. Convert the method into a member of the Turtle class.
//4. Students make a method in the Turtle class: polygon(numSides,sideLength)
//5. After working with the Turtles for a while, talk about fractal tree.
//6. Students make a method in the Turtle class: tree(depth)
//7. Improve the tree command by adding parameters for branchLength, turnAngle, etc.
//8. Further improve it by adding branch decay and random angle changes.
Turtle[] boxOfTurtles;
Turtle fred;
void setup() {
size(800, 600);
background(0);
boxOfTurtles = new Turtle[20];
for (int i = 0; i < boxOfTurtles.length; i++) {
boxOfTurtles[i]= new Turtle(); //random color turtle at the center of the screen
boxOfTurtles[i].penDown();//make them draw
boxOfTurtles[i].setSize( 0.5+random(20)/10.0);
}
//issue single commands to a turtle to test code: (if you don't use a draw)
fred = new Turtle();
fred.penDown();
fred.forward(50.0);
fred.right(50.0);
fred.forward(50.0);
fred.left(90.0);
fred.forward(25.0);
fred.draw();
}
void draw() {
if(frameCount < 500){
//disable animation for 200 frames to demo the fred stuff
}else if ( mousePressed ) {
draw2();
} else {
draw1();
}
fill(255);
text("Click the mouse on the screen to see alternate demo", 20, 20);
}
public void draw1() {
background(0);//if you use the background procedure, you must redraw
//what the turtles have drawn. This is only good if you want to
//animate turtles moving around, and have them redraw things.
for (int i = 0; i < boxOfTurtles.length; i++) {
boxOfTurtles[i].wiggle(); //random motion
boxOfTurtles[i].draw(); //to draw them every frame run this method
boxOfTurtles[i].screenWrap(); //to keep them on screen run this method
}
}
public void draw2() {
//Instead of drawing the turtles, you can just draw the path they take.
//This allows you to see the wiggle motion step by step.
//By not drawing the background you can see where they went when their pen is down.
for (int i = 0; i < boxOfTurtles.length; i++) {
boxOfTurtles[i].wiggle(); //random motion
//do NOT draw the turtles, instead let them draw lines!
boxOfTurtles[i].screenWrap();//to keep them on screen run this method
}
}