A chase game where a predator chases prey and appears to consume them.
Objective: Students will be able to create algorithms to make a sprite (predator) follow mouse movements. Students will use a conditional so that if it comes in contact with another sprite (prey), that sprite will disappear.
Expected Outcome:
Students will be able to:
Objectives/Outcome
Predator/Prey
Prerequisites:
Students should have foundational understanding of:
Vocabulary
Conditionals
Algorithm
Event
Project Sample
conditional
variables
$
24
code.org : Course 4, Lesson 6
conditional
conditional
Conditionals
Conditionals
you like
ice cream
raise your hand
Conditionals
you have brown hair
stand up
Conditionals
you have shoes with laces
jog in place
Conditionals
you
jump
Conditionals
Optional game: Play “Over the Mountain” (conditionals)
Over the Mountain
Participants sit in chairs that are arranged in a circle. One participant stands at the center of the circle to start the activity. The person in the center of the circle makes a statement that applies to them (and hopefully others!) in the following format: “Over the mountain if you…” An example of this might be: “Over the mountain if you have ever gotten a speeding ticket.” Anyone sitting in the circle and this statement applies to them, must get up and move to a new chair (not one adjacent to their current seat!). The last person standing who does not find a chair makes the next statement, and the activity continues.
Conditionals
In coding, an “If / Then” statement is called a “conditional”
You build the conditional block by adding other pieces to create the condition and the actions.
What are Conditionals?
New Block Type - Conditionals
| If the condition is true, the blocks held inside it will run. If the condition is false, the blocks held inside will be ignored. The condition is only checked once. |
| Use one of the many diamond shaped sensing blocks or operators to fill in the condition of the “if” statement. |
| This conditional is an if/then/else conditional. When the script runs it will check if the condition is met. If the condition is true, the first section of code will run. If the condition is false, the “else” section will run |
| A common error with conditionals is not understanding that the condition is only checked once. To make the program repeatedly check a condition, it needs to be in some kind of loop. |
Conditional blocks are used to check to see if a condition is true or false. If it is true, then an action will happen. Block are only checked once (unless contained in a forever loop). They can be used to compare values, check for input (i.e. mouse click/color sensing), control objects, etc.
A New Movement Block
New Block - Point Towards
Another way to move sprites on your screen is to have theme point towards something. This can be combined with other movement blocks to create and interactive game type element.
Other sprites added to the project will show up in the drop down.
Put in a forever loop and pair with motion blocks to make a sprite follow the mouse.
Predator/Prey
Decompose
Add sprites (predator and prey) and background
Make sprite (prey) hide when touched
Make sprite (prey) reappear in a random spot
Make sprite (predator) follow the mouse
Note: Not all blocks that might be used are shown here. There are multiple ways to code this project.
Add sounds
What are some of the code blocks I need?
Add variable (score)
Step-by-Step
Go to Clever
Open Code HS
Select the “Predator/Prey” Project
Step-by-Step
1. Add one sprite to represent a predator and another sprite to represent its prey.
2. Add a background that represents a habitat.
Step-by-Step
3. Make the predator sprite always point toward the mouse pointer and move toward it.
Step-by-Step
4. Create an algorithm (including a loop) to make the prey hide if it is touched by the predator.
Insert the “touching” block from the sensing category into the diamond of the if/then statement
Step-by-Step
5. Add code to make the prey reappear in a random spot on the screen.
Where would you add these blocks to the code sequence you have already created?
Step-by-Step
6. Add sound effects that will start when the prey is touched by the predator sprite.
Where would you add these blocks to the code sequence you have already created?
Step-by-Step
7. Add a starting location for the sprite and a “show” block.
Step-by-Step
8. Create a variable to track how many fish you have caught. Name your variable “Fish Count”
Step-by-Step
9. Add your variable code “change Fish Count by 1” to fish sprite. This will change the variable on the stage each time the shark catches a fish.
Step-by-Step
10. Add “set Fish Count to 0” code to the when flag clicked code string so the “fish count” variable resets to 0 when the flag is clicked.
Step-by-Step
11. Duplicate the prey sprite to make multiple prey.
12. Drag the copies of the prey sprites to different locations on the stage to spread them out. Change the “go to” block numbers to give each sprite a different starting location.
Right Click on a sprite and select “duplicate”.
A copy will be made of the sprite and all its code.
Tutorials
Revise
Can you add a scorekeeping variable?
Can you make your predator move with the arrow keys?
Can you add other sound effects. (Ocean noises, music, etc)
Predator/Prey
Can you make different levels?
Debug
Does everything work the way you want it to?
Is there anything you want to change?
score
5
Animate the predator to do something when he eats the prey.
CODE BLOCKS
Show
Hide
Point towards
Move
Wait
Go to random
If/then
Sensing operator
Start sound
SKILLS
VOCABULARY
Conditionals
Random
Algorithm
Event
Vocabulary
Code Blocks
Skills
Standards
STANDARDS
CS 3AP-3b Create programs that include sequences, events, loops, and conditionals
CS 3AP-4a Break down (deconstruct) algorithms and list the steps needed to solve a problem into a sequence of tasks and sub-tasks.
CS 3AP-5e Test and debug (identify and fix errors) a program or algorithm to ensure it runs as intended
Resources
Hide and Seek