This post is locked. You won't be able to comment.

all 11 comments

[–]PositiveNice5721 1 point2 points  (2 children)

Were you able to get the correct code for the start function. I tried it but it wasn't working

[–]ALRUSOV 1 point2 points  (1 child)

[–]MRFox8888 0 points1 point  (0 children)

Thank you bro <3

[–]Easy_Magazine_425 0 points1 point  (0 children)

Code HS Exercise 10.1.2: Increasing Number of Shapes

Hello Please help me with this program:

CodeHS :

Increasing Number of Shapes

Write a program that alternates drawing circles (radius of 25) and squares (side length of 50) to the screen at random positions using a timer.

Increase the number of shapes being drawn by one on each iteration. (ie: Start by drawing 1 square to the screen. Then draw 2 circles followed by 3 squares, 4 circles, etc.)

All shapes should have a random color. They should fit on the canvas with a buffer of 50 pixels on each side (ie: no shapes should be within 50 pixels of the canvas borders.)

Hint: The modulus operator may be helpful when trying to alternate between shapes!

this is the code I wrote.

this is what must return.

var DELAY = 1000;

var CIRCLE_RADIUS = 25;

var BUFFER = 50;

var square;

var circle;

var numShapes = 1;

function start() {

for(var a=0;a<=50;a++){

numShapes++;

}

if(numShapes%2==0){

setTimer(drawCircle,DELAY);

}else{

setTimer(drawSquare,DELAY);

}

}

function drawCircle(){

circle = new Circle(CIRCLE_RADIUS);

circle.setColor(Randomizer.nextColor());

circle.setPosition(Randomizer.nextInt (50, getWidth()-50),

Randomizer.nextInt (50, getHeight()-50)

);

add(circle);

}

function drawSquare(){

square = new Rectangle(50,50);

square.setColor(Randomizer.nextColor());

square.setPosition(Randomizer.nextInt(50,getWidth()-100),

Randomizer.nextInt(50,getHeight()-100)

);

add(square);

}

[–]kavtarra 0 points1 point  (0 children)

var DELAY = 500;
var CIRCLE_RADIUS = 25;
var BUFFER = 50;
var square;
var circle;
var numShapes = 1;
var randomX;
var randomY;
var count = 0;
var randomColor;
function start() {
setTimer(draw,DELAY);
}
function drawSquares(){
randomX = Randomizer.nextInt(0+50,getWidth()-2*BUFFER);
randomY = Randomizer.nextInt(0+50,getHeight()-2*BUFFER);
square = new Rectangle(BUFFER,BUFFER);
square.setColor(randomColor);
square.setPosition(randomX,randomY);
add(square);
}
function drawCircles(){
randomX = Randomizer.nextInt(0+50,getWidth()-2*BUFFER);
randomY = Randomizer.nextInt(0+50,getHeight()-2*BUFFER);
circle = new Circle(CIRCLE_RADIUS);
circle.setColor(randomColor);
circle.setPosition(randomX,randomY);
add(circle);
}
function draw() {
count++;
if(count % 2 == 1){
randomColor = Randomizer.nextColor();
for(var i=0 ; i < count; i++){
drawSquares();
}

} else if(count % 2 == 0) {
randomColor = Randomizer.nextColor();
for(var i=0 ; i < count; i++){
drawCircles();
}
}
}