137k views
5 votes
This graphics program should draw a caterpillar. A caterpillar has NUM_CIRCLES circles. Use a for loop to draw the caterpillar, centered vertically in the screen. Every other circle is a different color. When i is even, the circle should be red. When i is odd, the circle should be green. Remember that 0 is an even number. Be sure that the caterpillar is still drawn across the whole canvas even if the value of NUM_CIRCLES is changed. (PYTHON, CODEHS 4.6.6 Caterpillar.)

User Bdd
by
4.5k points

2 Answers

7 votes

Answer:

var NUM_CIRCLES = 15;

var radius=getWidth()/(NUM_CIRCLES*2);

var x=radius;

function start(){

for (var i=0; i<NUM_CIRCLES; i++){

if (i%2==0){

drawRedWorm();

}else{

drawGreenWorm();

}

var xPosition=x;

x=xPosition+(radius*2)

}

}

function drawRedWorm(){

var circle=new Circle(getWidth()/(NUM_CIRCLES*2));

circle.setPosition(x , getHeight()/2 );

circle.setColor(Color.red);

add (circle);

}

function drawGreenWorm(){

var circle=new Circle(getWidth()/(NUM_CIRCLES*2));

circle.setPosition(x , getHeight()/2 );

circle.setColor(Color.green);

add (circle);

}

Step-by-step explanation:

codeHs

User Roasm
by
4.4k points
7 votes

Answer:

radius = (get_width() / NUM_CIRCLES / 2)

x = radius

for i in range(NUM_CIRCLES):

circ = Circle(radius)

if i % 2 == 0:

circ.set_color(Color.red)

else:

circ.set_color(Color.green)

circ.set_position(x, get_height() / 2)

add(circ)

current_spot = x

x = current_spot + (radius * 2)

Step-by-step explanation:

User The Jug
by
3.9k points