Lesson 2
(Key Moves)

Learning Intention
- I can program program a robot to move when keys are pressed.
Introduction
How do you control players when playing computer games? One way is using a computer keyboard with different keys triggering the computer game sprite to carry out different movements such as jumping. We are going to write programs to make Ohbot carry out different sequences of movements when different keys are pressed.
How do you control players when playing computer games? One way is using a computer keyboard with different keys triggering the computer game sprite to carry out different movements such as jumping. We are going to write programs to make Ohbot carry out different sequences of movements when different keys are pressed.

Let’s make Ohbot nod once whenever the N key is pressed. First we will start with an event instruction. In the last lesson we used the When green flag clicked event block to start our sequence. This time we are going to use When___ Key Pressed. Use the drop down menu to select N as the trigger key.
The scripts will be similar to those created in the previous lesson. Use 10 and 0 for maximum movements in either direction and 5 to bring Ohbot back to the middle point. No add a second set of blocks that uses the S key to make Ohbot shake its head. Select Save As from the File menu and save the program with the name ‘ keyboard N and S’.
The scripts will be similar to those created in the previous lesson. Use 10 and 0 for maximum movements in either direction and 5 to bring Ohbot back to the middle point. No add a second set of blocks that uses the S key to make Ohbot shake its head. Select Save As from the File menu and save the program with the name ‘ keyboard N and S’.

Activity
1. Write a program to make Ohbot’s eyes look up and down when U is pressed and look left and right when L is pressed.
Extension
Can you write the code to control Ohbot’s lips using the keyboard? T for top lip and B for bottom lip.
1. Write a program to make Ohbot’s eyes look up and down when U is pressed and look left and right when L is pressed.
Extension
Can you write the code to control Ohbot’s lips using the keyboard? T for top lip and B for bottom lip.
Plenary
When we use the keyboard to control Ohbot, Ohbot’s head will continue to move up and down as long as the N (for nod) key is being pressed. Consequently, if you press N again and again Ohbot will nod again and again. However, in programming this is not the most efficient way to get a robot to perform an instruction multiple times. Have a look around in the Ohbot instructions to see if you can find a more efficient way to perform movements again and again?
We need to use the repeat instruction from the Control palette. In the next lesson we will learn to write code using repeat.
When we use the keyboard to control Ohbot, Ohbot’s head will continue to move up and down as long as the N (for nod) key is being pressed. Consequently, if you press N again and again Ohbot will nod again and again. However, in programming this is not the most efficient way to get a robot to perform an instruction multiple times. Have a look around in the Ohbot instructions to see if you can find a more efficient way to perform movements again and again?
We need to use the repeat instruction from the Control palette. In the next lesson we will learn to write code using repeat.