Back to AI Flashcard MakerEducation /Animation and Games CodeHS Part 4

Animation and Games CodeHS Part 4

Education10 CardsCreated 4 months ago

This deck covers key concepts and questions from the Animation and Games CodeHS Part 4, focusing on global variables, collision detection, drag and drop, animation, and reusability.

Which of the following are good examples of things that should be stored as global variables?
I - A ball for a game that is used in multiple functions
II - A counter that keeps track of how many times the user has clicked the mouse
III - A for loop counter variable
IV - The color of a rectangle that is only used in one function

I and II only

Tap or swipe ↕ to flip
Swipe ←→Navigate
1/10

Key Terms

Term
Definition

Which of the following are good examples of things that should be stored as global variables?
I - A ball for a game that is used in multiple functions
II - A counter that keeps track of how many times the user has clicked the mouse
III - A for loop counter variable
IV - The color of a rectangle that is only used in one function

I and II only

In the following code
function start(){
mouseClickMethod(clickHandler);
}
function clickHandler(e){
//your code here
}
How can we get the graphical element at the location of the user click?

var elem = getElementAt(e.getX(), e.getY());

9.9.7: Click for Collision

var blueCircle;
var redCircle;
var RADIUS = 25;
var DX_RED = 6;
var DX_BLUE = 4;
var DELAY = 40;
var click = 0;
var paused = f...

9.9.8: Drag and Drop

var NUM_CIRCLES = 3;
var RADIUS = 30;
function start(){
drawCircles();
// Add your drag and drop code
mouseDownMethod(drag);
mous...

What does this program do?
var ball;
function start(){
ball = new Circle(40);
add(ball);
setTimer(draw, 20);
}
function draw(){
ball.move(2, 2);
}

Animates a ball by moving it down and to the right once every 20 milliseconds.

In the following code:
var ball;
function start(){
ball = new Circle(40);
add(ball);
setTimer(draw, 20);
}
function draw(){
ball.move(2, 2);
}
Which of the following statements are true about ball?
I - ball is a local variable
II - the ball variable in draw is different from the ball variable in start
III - ball is a global variable
IV - ball's scope includes both start and draw

III and IV

Related Flashcard Decks

Study Tips

  • Press F to enter focus mode for distraction-free studying
  • Review cards regularly to improve retention
  • Try to recall the answer before flipping the card
  • Share this deck with friends to study together
TermDefinition

Which of the following are good examples of things that should be stored as global variables?
I - A ball for a game that is used in multiple functions
II - A counter that keeps track of how many times the user has clicked the mouse
III - A for loop counter variable
IV - The color of a rectangle that is only used in one function

I and II only

In the following code
function start(){
mouseClickMethod(clickHandler);
}
function clickHandler(e){
//your code here
}
How can we get the graphical element at the location of the user click?

var elem = getElementAt(e.getX(), e.getY());

9.9.7: Click for Collision

var blueCircle;
var redCircle;
var RADIUS = 25;
var DX_RED = 6;
var DX_BLUE = 4;
var DELAY = 40;
var click = 0;
var paused = false;
function start(){
blueCircle();
redCircle();
setTimer(collide, DX_BLUE);
mouseClickMethod(collide);
}
function collide(e){
setTimer(blueCircle, DX_BLUE);
}
function pause(e){
if(paused){
paused = false;
setTimer(collide, DX_BLUE);
}else{
paused = true;
stopTimer(collide)
}
}
function blueCircle(){
var circle = new Circle(RADIUS);
circle.setColor(Color.blue);
circle.setPosition(50, getHeight()/2);
add(circle);
}
function redCircle(){
var circle = new Circle(RADIUS);
circle.setColor(Color.red);
circle.setPosition(getWidth()/2, getHeight()/2);
add(circle);
}

9.9.8: Drag and Drop

var NUM_CIRCLES = 3;
var RADIUS = 30;
function start(){
drawCircles();
// Add your drag and drop code
mouseDownMethod(drag);
mouseDragMethod(up);
mouseUpMethod(drop);
}
// This function draws a number of random colored circles at random points
// on the screen based on the variable NUM_CIRCLES
function drawCircles() {
for (var i = 0; i < NUM_CIRCLES; i++) {
var circle = new Circle(RADIUS);
var x = Randomizer.nextInt(RADIUS, getWidth() - RADIUS);
var y = Randomizer.nextInt(RADIUS, getHeight() - RADIUS);
circle.setPosition(x, y);
circle.setColor(Randomizer.nextColor());
add(circle);
}
}
var newPosition = null;
function drag(e){
newPosition = getElementAt(e.getX(), e.getY());
}
function drop(e){
newPosition = null;
}
function up(e){
newPosition.setPosition(e.getX(), e.getY());
}

What does this program do?
var ball;
function start(){
ball = new Circle(40);
add(ball);
setTimer(draw, 20);
}
function draw(){
ball.move(2, 2);
}

Animates a ball by moving it down and to the right once every 20 milliseconds.

In the following code:
var ball;
function start(){
ball = new Circle(40);
add(ball);
setTimer(draw, 20);
}
function draw(){
ball.move(2, 2);
}
Which of the following statements are true about ball?
I - ball is a local variable
II - the ball variable in draw is different from the ball variable in start
III - ball is a global variable
IV - ball's scope includes both start and draw

III and IV

The following program animates a ball by setting a timer to move the ball across the screen. We want the animation to stop whenever the user clicks the mouse:
var ball;
function start(){
ball = new Circle(40);
add(ball);
setTimer(draw, 20);
mouseClickMethod(stopAnimation);
function draw(){
ball.move(2, 2);
}
function stopAnimation(e){
//your code here ...
}
Which statement would stop the animation in this program?

stopTimer(draw);

We extend our draw function to include:
1 var ball;
2 var moveCounter = 0;
3 function draw(){
4
5 ball.move(2, 2);
6 moveCounter++;
7
8 if(moveCounter == 25){
9 stopTimer(draw);
10 }
11 }
12
13 function start(){
14 ball = new Circle(40);
15 add(ball);
14 setTimer(draw, 20);
15 }
How many times will the ball be moved before the animation stops?

25

Which of the following are techniques that make our code more reusable?
I - Using constants instead of magic numbers
II - Using specific values in our functions rather than using parameters
III - Writing multiple functions that solve small subproblems rather than one function that solves the entire problem.
IV - Writing as few lines of code as possible

I and III, and IV

Which function has better reusability?

function drawCircle(x, y, radius, color){
var circle = new Circle(radius);
circle.setPosition(x, y);
circle.setColor(color);
add(circle);
}