-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
5.5.6 Caterpillar.js
39 lines (36 loc) · 1.23 KB
/
5.5.6 Caterpillar.js
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
var NUM_CIRCLES = 15;
var RADIUS = getWidth() / NUM_CIRCLES / 2;
var circleColor = Color.red;
// This graphics program should draw a caterpillar. A caterpillar has NUM_CIRCLES
// circles. Every other circle is a different color, the even circles are red, and
// the odd circles are green. Use a for loop to draw the caterpillar, centered
// vertically in the screen.
//start function
//after this function I have included an alternate color sequence for fun...
function start() {
for (var i = 0; i < NUM_CIRCLES; i++) {
var circle = new Circle(RADIUS);
circle.setColor(circleColor);
circle.setPosition(RADIUS * i * 2 + RADIUS, getHeight() / 2);
add(circle);
if (circleColor == Color.green) {
circleColor = Color.red;
} else {
circleColor = Color.green;
}
}
}
/*function start() {
for (var i = 0; i < NUM_CIRCLES; i++) {
var circle = new Circle(RADIUS);
circle.setColor(circleColor);
circle.setPosition(RADIUS * i * 2 + RADIUS, getHeight() / 2);
add(circle);
if (circleColor == Color.red) {
circleColor = Color.black;
} else {
circleColor = Color.red;
}
}
}
*/