How the p5.js Snake Game Works — PTF50 Handout

0.0(0)
Studied by 0 people
call kaiCall Kai
Locked
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/42

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 8:03 AM on 9/15/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

43 Terms

1
New cards

What is the frame rate set in the setup() function of the Snake game?

a) 5 frames per second b) 10 frames per second c) 20 frames per second d) 30 frames per second

a

2
New cards

What is the purpose of the scale(rez) command in the draw() function?

a) It resizes the canvas to fit the browser window b) It zooms so that 1 grid-square equals 20 real pixels c) It changes the speed of the game loop d) It rotates the entire game board

b

3
New cards

What value is assigned to rez in the global variables of sketch.js?

a) 10 b) 15 c) 20 d) 25

c

4
New cards

Which file loads the ml5 library for machine learning functionality?

a) style.css b) snake.js c) sketch.js d) index.html

d

5
New cards

What is stored in this.body within the Snake class?

a) The snake's current direction b) The snake's current score c) An array of squares representing the snake's body d) The food position

c

6
New cards

Which function in sketch.js is responsible for placing the first piece of food at the start of the game?

a) setup() b) draw() c) foodLocation() d) keyPressed()

b

7
New cards

In keyPressed(), what direction does snake.setDir(0, 1) represent?

a) Left b) Right c) Up d) Down

c

8
New cards

Where is the snake's head located within the this.body array?

a) At the front (index 0) b) At the middle c) At the end (last index) d) It changes randomly each frame

c

9
New cards

What does the grow() method do to the snake?

a) It doubles the snake's speed b) It adds one extra square to the snake's body c) It resets the snake to its starting length d) It shrinks the snake by one square

b

10
New cards

Which two numbers are passed to setDir() to make the snake move upward?

a) (0, 1) b) (1, 0) c) (0, -1) d) (-1, 0)

c

11
New cards

What is the minimum confidence threshold used in gotResults() to ignore uncertain voice predictions?

a) 0.50 b) 0.75 c) 0.85 d) 0.95

c

12
New cards

How many classes are recommended for the Teachable Machine audio project (including Background Noise)?

a) 3 b) 4 c) 5 d) 6

d

13
New cards

What is the total number of grid squares across the width of the canvas (w) when rez = 20 and createCanvas(400, 400) is used?

a) 10 b) 20 c) 30 d) 40

b

14
New cards

Which p5.js function runs once at the very beginning of the game, before setup()?

a) draw() b) keyPressed() c) preload() d) foodLocation()

c

15
New cards

What does the endGame() method check for when determining if the snake has crashed?

a) Whether the snake is moving too fast b) Whether the snake's head is off the board or touching its own body c) Whether the food has disappeared d) Whether the snake has grown too long

b

16
New cards

What does the eat(pos) method return to draw() when the snake's head is on the food?

a) false b) true c) The new food position d) The snake's new length

b

17
New cards

In snake.js, what is the starting position of the snake's body when a new Snake is created?

a) The top-left corner (0, 0) b) The middle of the board c) The bottom-right corner d) A random position

a

18
New cards

Which line of code in index.html must come BEFORE sketch.js to avoid a "Snake is not defined" error?

a) <script src="style.css"></script> b) <script src="snake.js"></script> c) <script src="ml5.min.js"></script> d) <script src="p5.js"></script>

b

19
New cards

What color is the food drawn as in the draw() function?

a) Black b) Green c) Red d) Blue

c

20
New cards

What does the classifier.classify(gotResults) line do when added to setup()?

a) It loads the Teachable Machine model from the URL b) It starts the microphone and classifies sounds continuously c) It trains the model on new audio samples d) It stops the classifier from running

b

21
New cards

What is the recommended action if the model outputs a word with confidence below 0.85?

a) Change the snake's direction immediately b) Ignore the prediction and do nothing c) Print an error message to the console d) Restart the game

a

22
New cards

In the update() method, what is the first step in moving the snake?

a) Remove the tail using this.body.shift() b) Add a new head using this.body.push() c) Copy the current head position d) Change the direction using setDir()

c

23
New cards

What is the purpose of the style.css file in the Snake game project?

a) To style the game canvas with colors b) To remove default margins and padding from the page c) To load the p5.js library d) To define the Snake class

b

24
New cards

Which function is called when the user presses a key in the Snake game?

a) keyIsPressed() b) keyDown() c) keyPressed() d) onKey()

c

25
New cards

What is the effect of changing frameRate(10) to frameRate(5)?

a) The game runs faster b) The game runs slower c) The canvas size changes d) The snake grows faster

b

26
New cards

In the Teachable Machine setup, what is the first class that must be created in an Audio Project?

a) up b) down c) Background Noise d) left

c

27
New cards

What does the noLoop() function do when called in draw()?

a) It restarts the game loop b) It stops the draw() loop from repeating c) It pauses the game temporarily d) It clears the canvas

b

28
New cards

Which of the following is NOT one of the four directional classes recommended for the Teachable Machine model?

a) up b) down c) stop d) right

c

29
New cards

What is the value of rez when the game has a 10×10 grid on a 400×400 canvas?

a) 10 b) 20 c) 30 d) 40

d

30
New cards

What does the this.len variable track in the Snake class?

a) The number of squares in the snake b) The score or length counter c) The current frame rate d) The number of foods eaten

b

31
New cards

In draw(), what happens immediately after the snake eats the food?

a) The game ends b) The food moves to a new random location c) The snake shrinks d) The frame rate doubles

b

32
New cards

What does the show() method in snake.js do?

a) It updates the snake's position b) It draws every square of the snake as a black rectangle c) It displays the current score on the canvas d) It checks if the snake has eaten the food

b

33
New cards

Which of the following is a valid reason to increase the confidence threshold above 0.85?

a) To make the snake turn more often b) To reduce false positives from background noise c) To make the game run faster d) To allow more words to be recognized

b

34
New cards

In the gotResults() function, what does results[0].label contain?

a) The confidence level of the prediction b) The error message if classification fails c) The model's best guess of the spoken word d) The raw audio data from the microphone

c

35
New cards

What is the purpose of the copy() method used on the head position in update() and grow()?

a) To delete the original head position b) To create a new independent vector instead of referencing the original c) To double the snake's speed d) To reverse the snake's direction

b

36
New cards

What happens when the snake crashes into a wall in the game?

a) The screen turns red and noLoop() stops the game b) The snake wraps around to the opposite side c) The game speed increases d) The snake shrinks by one square

a

37
New cards

Which function in sketch.js is called when the browser first loads the game, before setup()?

a) draw() b) preload() c) keyPressed() d) foodLocation()

b

38
New cards

What color is the snake drawn as in the default show() method?

a) Red b) Green c) Black d) Blue

c

39
New cards

What is the recommended number of short audio samples to record for each voice command class in Teachable Machine?

a) 3 or fewer b) 5 c) 8 or more d) 15

c

40
New cards

In the keyPressed() function, what does the spacebar do when pressed?

a) It pauses the game b) It changes the snake's direction to up c) It makes the snake grow by one d) It restarts the game

c

41
New cards

What is the effect of changing rez = 20 to rez = 40 on the game board?

a) The canvas size doubles b) The number of grid squares decreases c) The snake moves faster d) The food becomes larger

b

42
New cards

Which method in the Snake class is called by draw() to check if the head is on the food?

a) endGame() b) update() c) eat() d) show()

c

43
New cards

In the Teachable Machine model, what happens if a class is named "Up" with a capital U but the code checks for "up"?

a) The code will automatically correct it b) The snake will still turn correctly c) The snake will never turn for that command d) The game will crash

c