Lesson 6
Events Part2
Review
Here are some questions that you can ask in review:
What did we do last time?
What do you wish we had had a chance to do?
Did you think of any questions after the lesson that you want to ask?
What was your favorite part of the last lesson?
Event.- An action that causes something to happen
Create buttons
Have a volunteer to touch any shape. If the volunteer touched the circle, the class clap one time. if he/she touched the rhombus the class tap the table. If he/she touched the square, the class stamp their feet. Show the following picture and ask them to say something every time you touch a shape
This is an illustration of a button, which is another type of event.
Every time you press a button, something happens.
pencilcode.net
Open the program events from last class
We are going to program some buttons
Add the following code illustrating the use of the buttons, the block for buttons is under the section Control
Here is the code for the full program in CoffeScript:
speed (100)
#Event: The code will run when something occurs
#Event: press the “up arrow” key
#Reaction: turtle moves up 10 steps
keydown ‘Up’, ->
fd 10
#Event: press the “down arrow” key
#Reaction: turtle moves down 10 steps
keydown ‘Down’, ->
bk 10
#Event: press the “left arrow” key
#Reaction: turtle turn left and moves 10 steps
keydown ‘Left’, ->
lt(90)
fd 10
rt(90)
#Event: press the “left arrow” key
#Reaction: turtle turn left and moves 10 steps
keydown ‘Right’, ->
rt(90)
fd 10
lt(90)
#Event: press the button grow
#Action: turtle grow 2 times
button ‘Grow’, ->
grow 2
#Event: press the button shrink
#Action: turtle size is reduced half size
button ‘Shrink’, ->
grow 0.5
#Event: press the button pen
#Action: pen is set with color and thickness
button ‘Pen’, ->
pen purple, 10
#Event: press the button “pen off”
#Action: pen is off
button ‘Pen off’, ->
pu()
Now the students can draw anything by using the events keydown and button.
NOTICE: For first grade you may only have time for two buttons .
Challenge
Create one button that will make a box, one that will make a dot, one that will hide the turtle, and one that will show the turtle.
button ‘Grow’, ->
grow 3
button ‘Shrink’, ->
grow 0.5
button ‘Pen’, ->
pen purple, 10
button ‘Pen off’, ->
pu()
button ‘Box’, ->
box yellow, 50
button ‘Dot’, ->
dot green, 50
button ‘Hide’, ->
hide()
button ‘Show’, ->
show()
Wrap Up
What is an event?
Why do we need to be able to handle events in a program?
Can we write any type of code for an event?
Last Updated:7/24/2019