Back to AI Flashcard MakerInformation Technology /Animation and Games CodeHS Part 6

Animation and Games CodeHS Part 6

Information Technology10 CardsCreated 5 months ago

This flashcard deck covers key concepts and problem-solving strategies from Part 6 of the Animation and Games module on CodeHS. It includes questions on variable scope, keyboard events, and graphical programming challenges.

This code creates a new circle on the canvas every time the mouse is pressed down and the circle follows the mouse when the mouse is dragged. The result should look like this:
But there is one problem. What is wrong with the following code?

Line 4 declares a new local variable var ball. The local variable ball in down is different from the global variable ball in drag, but we need these functions to be affecting the same ball

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

Key Terms

Term
Definition

This code creates a new circle on the canvas every time the mouse is pressed down and the circle follows the mouse when the mouse is dragged. The result should look like this:
But there is one problem. What is wrong with the following code?

Line 4 declares a new local variable var ball. The local variable ball in down is different from the global variable ball in drag, but we need thes...

How can we fix the problem in this code from question 18? The same code is shown again for reference.

Get rid of the word var in line 4 so that the ball in down refers to the same global ball in drag rather than a local variable.

Describe the result of the following line of code. What will happen in our program when this line is in our start function?

The function moveBall will be called every time any key on the keyboard is pressed down.

In the following program:
var STEP = 5; var SQUARE_SIZE = 20; var square; function start(){ square = new Rectangle(SQUARE_SIZE, SQUARE_SIZE); square.setPosition(getWidth() / 2, getHeight() / 2); keyDownMethod(down); add(square); } function down(e){ if(e.keyCode Keyboard.LEFT){ square.move(-STEP, 0); } else if(e.keyCode Keyboard.RIGHT){ square.move(STEP, 0); } else if(e.keyCode Keyboard.letter('R')){ square.move(0, -STEP); } else if(e.keyCode Keyboard.letter('F')){ square.move(0, STEP); } else{ square.setColor(Randomizer.nextColor()); } }
How can a user move the square up on the screen?

The square changes color to a random color

The following code draws stacks of alternating red and black circles across the window one at a time, filling the window from left to right.Example:After first call to draw ...................... After second call to draw
...
What should the value of xPosition be changed to on line 16 so that the next column drawn is touching the right side of the previous column?

xPosition = xPosition + 2*RADIUS;

In the code from the last question that draws columns of red and black circles across the window:
...
What should the value of y be on line 27 so that the first circle added is touching the top of the window, and each subsequent circle is touching the bottom of the previous circle. The circles should not overlap and there should be no space in between circles: After first call to draw ...................... After second call to draw

var y = RADIUS + (2 i RADIUS);

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

This code creates a new circle on the canvas every time the mouse is pressed down and the circle follows the mouse when the mouse is dragged. The result should look like this:
But there is one problem. What is wrong with the following code?

Line 4 declares a new local variable var ball. The local variable ball in down is different from the global variable ball in drag, but we need these functions to be affecting the same ball

How can we fix the problem in this code from question 18? The same code is shown again for reference.

Get rid of the word var in line 4 so that the ball in down refers to the same global ball in drag rather than a local variable.

Describe the result of the following line of code. What will happen in our program when this line is in our start function?

The function moveBall will be called every time any key on the keyboard is pressed down.

In the following program:
var STEP = 5; var SQUARE_SIZE = 20; var square; function start(){ square = new Rectangle(SQUARE_SIZE, SQUARE_SIZE); square.setPosition(getWidth() / 2, getHeight() / 2); keyDownMethod(down); add(square); } function down(e){ if(e.keyCode Keyboard.LEFT){ square.move(-STEP, 0); } else if(e.keyCode Keyboard.RIGHT){ square.move(STEP, 0); } else if(e.keyCode Keyboard.letter('R')){ square.move(0, -STEP); } else if(e.keyCode Keyboard.letter('F')){ square.move(0, STEP); } else{ square.setColor(Randomizer.nextColor()); } }
How can a user move the square up on the screen?

The square changes color to a random color

The following code draws stacks of alternating red and black circles across the window one at a time, filling the window from left to right.Example:After first call to draw ...................... After second call to draw
...
What should the value of xPosition be changed to on line 16 so that the next column drawn is touching the right side of the previous column?

xPosition = xPosition + 2*RADIUS;

In the code from the last question that draws columns of red and black circles across the window:
...
What should the value of y be on line 27 so that the first circle added is touching the top of the window, and each subsequent circle is touching the bottom of the previous circle. The circles should not overlap and there should be no space in between circles: After first call to draw ...................... After second call to draw

var y = RADIUS + (2 i RADIUS);

Which of the following are good examples of things that should be stored as global variables?
I - The parameter e to a callback function
II - The ball in the game the user is playing
III - A counter keeping track of how many times the user has pressed the spacebar
IV - A for loop counter
V - The color of a rectangle that is only used in one function

II and III

Animations and Games Challenges:
19.2.3: Blinking Rectangles

var NUM_RECTANGLES_ACROSS = 4;
var NUM_RECTANGLES_DOWN = 10;
var RECT_LENGTH = getWidth()/NUM_RECTANGLES_ACROSS;
var RECT_WIDTH = getHeight()/NUM_RECTANGLES_DOWN;
function start(){
mouseMoveMethod(drawRect);
}
function drawRect(e){
var x = Math.floor(e.getX()/RECT_LENGTH)* RECT_LENGTH;
var y = Math.floor(e.getY()/RECT_WIDTH)* RECT_WIDTH;
rect = new Rectangle(RECT_LENGTH, RECT_WIDTH);
rect.setColor(Randomizer.nextColor);
rect.setPosition(x, y);
add(rect);
}

Animations and Games Challenges:
10.1.2: Increasing Number of Shapes

1; create a start function that sets timers for drawCircle and drawSquare; in drawCircle, create a Circle of radius RADIUS, set a random color, position it randomly within the canvas width and half the height, then add it; in drawSquare, create a Rectangle of 50x50, set a random color, position it randomly within a fraction of the canvas width and height, then add it. Use Randomizer.nextColor() and Randomizer.nextInt() for randomization. Both functions ignore input parameters since random values are generated internally. Timers repeatedly call these functions every DELAY milliseconds. This approach increases the number of shapes on screen dynamically over time.

In the same program as before:

...

What happens if the user presses the spacebar?

The square changes color to a random color