1 of 30

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:

  • add sprites and background
  • code a sprite to point toward and move towards the mouse pointer.
  • code sprites to hide and show in random locations on the screen
  • use a conditional to control response to a sensing block

Objectives/Outcome

Predator/Prey

Prerequisites:

Students should have foundational understanding of:

  • adding sprites
  • basic movement blocks
  • say blocks

Vocabulary

Conditionals

Algorithm

Event

2 of 30

Project Sample

3 of 30

conditional

variables

  • A place to store a piece of data that can change.
  • A container for storing a value that can be used and updated. Variables can store numbers, text (strings) or other values.

$

24

code.org : Course 4, Lesson 6

4 of 30

conditional

conditional

  • commands that only run under certain conditions
  • an action that occurs based on whether a condition is true or false

5 of 30

Conditionals

6 of 30

Conditionals

you like

ice cream

raise your hand

7 of 30

Conditionals

you have brown hair

stand up

8 of 30

Conditionals

you have shoes with laces

jog in place

9 of 30

Conditionals

you

jump

10 of 30

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.

11 of 30

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.

12 of 30

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.

13 of 30

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.

14 of 30

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)

15 of 30

Step-by-Step

Go to Clever

Open Code HS

Select the “Predator/Prey” Project

16 of 30

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.

17 of 30

Step-by-Step

3. Make the predator sprite always point toward the mouse pointer and move toward it.

18 of 30

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

19 of 30

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?

20 of 30

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?

21 of 30

Step-by-Step

7. Add a starting location for the sprite and a “show” block.

22 of 30

Step-by-Step

8. Create a variable to track how many fish you have caught. Name your variable “Fish Count”

23 of 30

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.

24 of 30

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.

25 of 30

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.

26 of 30

Tutorials

27 of 30

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.

28 of 30

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

  • Create an algorithm
  • Make sprite follow mouse pointer
  • Make a sprite appear and disappear in random locations
  • Use conditionals with sensing blocks to control events

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

29 of 30

Resources

Hide and Seek

30 of 30

Slidesgo

THANKS

Sources:

scratch.mit.edu

thenounproject.com

freepik.com

code.org

Pam Hyer

CREDITS: This presentation template was created by Slidesgo, including icons by Flaticon, infographics & images by Freepik