Explore what CodeHS has to offer for districts, schools, and teachers.
Click on one of our programs below to get started coding in the sandbox!
View All
In the following program:
let dx = 5; let dy = 5; let circle; function main() { circle = new Circle(50); circle.setPosition(getWidth() / 2, getHeight() / 2); keyDownMethod(down); add(circle); } function down(e) { if (e.key == "a") { circle.move(-dx, 0); } else if (e.key == "d") { circle.move(dx, 0); } else if (e.key == "w") { circle.move(0, -dy); } else if (e.key == "s") { circle.move(0, dy); } else { circle.setColor(Randomizer.nextColor()); } } main();
How can the user move the circle left on the canvas?
By pressing the left arrow key
left arrow
By pressing the d key
d
By pressing the a key
a
None of the keys will move the circle since the down() function needs to be called move() in order to move the graphic.
down()
move()