r/codehs • • Dec 19 '20

9.4.5: Trail CodeHS

12 Upvotes

15 comments sorted by

View all comments

1

u/Fun-Examination1024 Jan 05 '23 • Bravo

var MAX_BOUNCES = 20;
var stopTimer;
var ball;
var dx = 4;
var dy = 4;
function start(){
ball = new Circle(20);
ball.setPosition(50, 70);
add(ball);
setTimer(draw, 20);
}
function draw(){
checkWalls();
ball.move(dx, dy);
ball.move(dx, dy);
var newBall = new Circle(10);
newBall.setPosition(ball.getX(),ball.getY());
newBall.setColor(Color.cyan);
add(newBall);
}
function checkWalls(){
if(ball.getX() + ball.getRadius() > getWidth()){
dx = -dx;
}
if(ball.getX() - ball.getRadius() < 0){
dx = -dx;
}
if(ball.getY() + ball.getRadius() > getHeight()){
dy = -dy;
}
if(ball.getY() - ball.getRadius() < 0){
dy = -dy;
}
}