Day 1 β Dragging & Rotating the Putter π―
Objective: Students will implement interactive behaviors (dragging and rotating the putter) and write pseudocode.
π€ Think & Discuss:
"In video games, how do you think characters know when you're clicking or dragging them with your mouse?"
Students share ideas, then teacher connects to Boolean expressions and mouse interactions.
What You're Building:
You'll make the putter follow your mouse when you drag it across the screen.
1 Copy the Starter Code
Copy and paste the starter code provided into CodeHS.
2 Find the draw() Function
Look for the draw() function. This function runs over and over (every frame) to animate your game.
3 Add the Dragging Code
Inside the draw() function, add this if/else statement:
β Answer Key - Exercise 4.5.2
What this code does:
putter.mouse.dragging()checks if the mouse is dragging the putter (returns true or false)- If TRUE:
putter.moveTowards(mouse, 0.1)makes the putter move toward the mouse position - If FALSE:
putter.speed = 0stops the putter from moving - The number
0.1controls how fast the putter follows the mouse (smaller = slower)
What You're Building:
You'll make the putter rotate when you press the 'r' key on your keyboard.
1 Copy Your Previous Code
Copy and paste your code from Exercise 4.5.2 (Dragging the Putter).
2 Add Rotation Code
In the draw() function, AFTER the dragging code, add this:
β Answer Key - Exercise 4.5.3
What this code does:
kb.presses('r')checks if the 'r' key was just pressed (returns true or false)putter.rotate(45, 10)rotates the putter by 45 degrees over 10 frames- First number (45) = how many degrees to rotate
- Second number (10) = how many frames the rotation takes (higher = slower rotation)
βοΈ Pseudocode Practice:
Write pseudocode to create an if block that has a couch sprite rotate by 20 degrees at a rate of 10 degrees/sec when the user clicks on it.
β Pseudocode Answer:
π» Code Practice:
Write code to create an if block that has a couch sprite rotate by 20 degrees at a rate of 10 degrees/sec when the user clicks on it. Use your pseudocode to help you!
β Code Answer:
π€ Reflection Questions:
- How did you make the putter move and rotate?
- What problem did you face, and how did you fix it?
Day 2 β Ball Movement & Slowing Mechanics β‘
Objective: Students will control ball behavior using comparison operators and if/else statements to slow the ball.
π€ Think & Discuss:
"When you roll a real golf ball on grass, it slows down and stops. What do you think causes this to happen? How could we recreate this effect in code?"
Students discuss friction and forces. Teacher introduces comparison operators and how to check if something is moving.
What You're Building:
You'll add friction to make the ball slow down naturally when it rolls on the grass, just like in real mini golf!
1 Copy Your Previous Code
Copy and paste your code from Exercise 4.5.3 (Rotating the Putter).
2 Add Structure for New If Statement
At the END of your draw() function (after all other code), add an empty if statement structure:
3 Write the Boolean Expression
In the parentheses, check if the ball's speed is greater than 0:
4 Decrease the Ball's Speed
Inside the curly braces, subtract 0.01 from the ball's speed:
β Answer Key - Exercise 4.5.4
What this code does:
ball.speed > 0is a comparison operator that checks if the ball is moving- The
>symbol means "greater than" ball.speed -= 0.01is shorthand forball.speed = ball.speed - 0.01- This code runs every frame, so the ball slows down gradually
- The number 0.01 is the friction amount (larger = more friction = faster slowdown)
βοΈ Pseudocode Practice:
Write pseudocode to create an if block that checks if a car sprite's speed is greater than 5, and if so, decreases the speed by 0.5.
β Pseudocode Answer:
π» Code Practice:
Write code to create an if block that checks if a car sprite's speed is greater than 5, and if so, decreases the speed by 0.5. Use your pseudocode to help you!
β Code Answer:
Note: car.speed -= 0.5 is shorthand for car.speed = car.speed - 0.5
π€ Reflection Questions:
- How did you implement the slowing behavior?
- Did the ball always slow down correctly? Why or why not?
Day 3 β Removing the Ball & Game Completion π
Objective: Students will implement sprite removal logic to complete the game.
π€ Think & Discuss:
"In mini golf, what happens when the ball goes into the hole? How do you think a computer game 'knows' when two objects are touching each other?"
Students share ideas about collision detection. Teacher connects to the overlaps() method and sprite removal.
What You're Building:
You'll make the ball disappear when it goes into the hole, just like scoring in real mini golf!
1 Write Pseudocode First!
Before coding, write pseudocode on paper or as a comment:
2 Copy Your Previous Code
Copy and paste your code from Exercise 4.5.4 (Slowing Down the Ball).
3 Add Structure for New If Statement
At the END of your draw() function, add an empty if statement:
4 Check if Ball Overlaps Hole
Use the overlaps() method to check collision:
5 Remove the Ball
Inside the if statement, use the remove() method:
overlaps(), sprites will no longer collide with each other - they will overlap instead! This is what we want for the ball and hole.
β Answer Key - Exercise 4.5.5
What this code does:
ball.overlaps(hole)checks if the ball sprite is touching the hole sprite- Returns true when they touch, false when they don't
ball.remove()deletes the ball sprite from the canvas completely- This creates the effect of the ball "falling into" the hole
6 Add Comments to All If Statements
Make sure each if statement in your draw() function has a comment explaining what it does!
β Final Complete Code
Your final draw() function should look like this:
βοΈ Pseudocode Practice:
Write pseudocode to create an if block that removes a coin sprite when a player sprite overlaps with it.
β Pseudocode Answer:
π» Code Practice:
Write code to create an if block that removes a coin sprite when a player sprite overlaps with it. Use your pseudocode to help you!
β Code Answer:
Remember: When using overlaps(), sprites will no longer collide - they will pass through each other!
π€ Reflection Questions:
- How does removing the ball affect gameplay?
- Which interaction was the most challenging to implement and why?
- How would you add a second hole or additional interactions?
| Term | Definition |
|---|---|
| Boolean | True/false value used to control logic. |
| If/Else Statement | Runs one block of code or another depending on a condition. |
| Comparison Operator | Compares two values (>, <, ===, etc.). |
| Pseudocode | Written description of code logic before coding. |
| moveTowards() | p5play method to move a sprite toward a target. |