GoalPlan the instructions for an object by answering questions about its purpose, input, condition and conclusion, and then write its algorithm.
New words
object · a sprite or other thing in a programstatic object · an object that has no code and stays where it isinput · the event that starts a script, such as pressing a keyalgorithm · a set of steps in order that solves a problem
Explanation
Programs with selection are bigger, so we plan each object before we write the code. Ask these questions. Is the object static, with no code? What actions does it need to do? What is the input, the event that starts its script? What is the condition? What is the conclusion? Then write the algorithm in numbered steps and build it from the steps. Last, test the program and debug it. A plan on paper is easy to change, so mistakes cost less time.
purpose→input→condition→conclusion→algorithm→code and test
Worked examples
Question
Kite
Tree
Is it static?
no
yes
What actions?
flies 25 steps right
none
Input
right arrow key pressed
none
Condition
touching the Tree
none
Conclusion
says Stuck! for 2 seconds
none
A program has a Kite and a Tree. The player presses the right arrow key to make the Kite fly. If the Kite touches the Tree, it says ‘Stuck!’. Here is the plan for each object.
Step
Algorithm for the Kite
1
Start when the right arrow key is pressed.
2
Move 25 steps.
3
If the Kite is touching the Tree, then say ‘Stuck!’ for 2 seconds.
4
Stop.
Next, the plan becomes an algorithm. The Kite also has a start script, which puts it at the left of the stage when the green flag is clicked.
Last, each step becomes a block. The Kite starts at x: -160. After 9 presses it is at x: 65. The Tree stands at x: 65, y: 60. Then the condition is true.
Class activity
Plan a pair (2 or 3 players). Choose one moving object and one static object, for example a boat and a lighthouse, or a robot and a charging station. Fill in a planning sheet with the questions from the lesson for each object. Then swap sheets with another group. Cut out two paper objects, put them on a grid on the table and act out the plan: one player presses a ‘key’ by tapping the table, and the other moves the paper object and checks the condition each time. Does the plan work? Is any question left blank?
Practice
1
A Boat sails to a Lighthouse. When the player presses the space key, the Boat moves, and if it touches the Lighthouse it says ‘Safe!’. Which object is static? Which object has an input?
The Lighthouse is static. The Boat has the input: the space key being pressed.
2
A Camel moves 20 steps each time that the space key is pressed. If it touches the Oasis, it says ‘Water!’. The Oasis stays still. Complete the plan.
Question
Camel
Oasis
Is it static?
?
?
What actions?
?
?
Input
?
?
Condition
?
?
Conclusion
?
?
Camel: static no, action moves 20 steps, input space key pressed, condition touching the Oasis, conclusion says Water!. Oasis: static yes, no action, no input, no condition, no conclusion.
3
Write the algorithm for the Camel in numbered steps.
1 Start when the space key is pressed. 2 Move 20 steps. 3 If the Camel is touching the Oasis, then say ‘Water!’ for 2 seconds. 4 Stop.
4
Match each part of the plan to a block in this script: input, action, condition and conclusion.
Input: when up arrow key pressed. Action: change y by 20. Condition: touching Leaf. Conclusion: hide.