Lesson 5:
Events
Introduction
Try this activity to show events: Have Student A go out, ask him to close the door behind him, wait a few seconds, and then knock on the door (preferably, you want to do this without the rest of the class noticing). Another student is likely to hear the knock and open the door. If no one opens the door, you can ask someone to do so. Tell the students that knocking on the door is an event and the reaction to that event was opening the door. Ask them for other examples of events in their daily life.
Let’s think about some examples of events in our daily life
When there’s a red light, cars stop
You feel hungry so you eat
Stubbing your toe and yelling “Ouch!”
Getting the basketball in the basket and scoring a point for your team!
You click on a Youtube video so it plays
What is an Event?
Click below to watch a video
Event.- An action that causes something to happen.
pencilcode.net
Review about the comments in pencilcode.net
What is a comment in Programming?
Comments are used to document your code so it is easier for humans to understand. You should include a comment any time you think there is something that needs an explanation. Especially if you are part of a team, another programmer can more easily maintain or modify the code in the future.
Place symbol # at the beginning of the coding line to comment in CoffeeScript
Create events for the arrow keys
Have them identify the arrow keys of the keyboard
Notice that every time you hit a key is an event and we need to write the code for the action to be taken by the computer.
New File: events
show them how to get the block “keydown” from the Control section for the “up arrow” key, then let them get the block to move forward. Write the comments.
Let them figure out how to create the event for the “down arrow” key.
Repeat the process to the right and left turn
speed 10
#EVENT: Press “up arrow” key
#ACTION: Turtle moves 10 steps forward
keydown ‘up’, ->
fd 10
#EVENT: Press “down arrow” key
#ACTION: Turtle moves 10 steps backwards
keydown ‘down’, ->
bk 10
#EVENT: Press “right arrow” key
#ACTION: Turtle turns right
keydown ‘right’, ->
rt 90
#EVENT: Press “left arrow” key
#ACTION: Turtle turns left
keydown ‘left’, ->
lt 90
NOTICE: When you run the program the turtle won’t move unless you press one of the arrow keys.
Challenge
Add a color to each keydown when our turtle moves forward and back.
Wrap Up
What is an event?
Why do we need to be able to handle events in a program?
What are some other kinds of events that you can think of?
Last Updated:7/25/2019