Keyboard and Mouse

Goals
 Understanding Keyboard and Mouse Input
 Single key press, and held down state
 Mouse position and mouse click
 Controlling sprites using the keyboard or mouse
Objectives
 Students are introduced to the topic and given
examples of keyboard and mouse input
 Students are given real problems to code and solve
using the keyboard and mouse
 Results are demonstrated through demos of
running code and displayed results
Pre Requisites
 Understanding of Scratch, Sprites, Animation, IF, hit
detection, looping
Materials
 Slides with examples (or present on whiteboard)
 PCs with development environments installed
Lesson Description
 Introduce Scratch blocks for Key N pressed, MouseX,






MouseY, Mouse down
Give examples/demo of handling key presses, and key
held down
Give examples/demo of handling mouse movement and
click
Give sample problems to solve in class
Students practice and extend examples to their own
wishes
Students present their solutions, typically F2F with the
instructor(s) at their desk
Students analyze their and others’ solutions for bugs
Lesson Procedure
 Review previous lessons: Animation, x position, y position
of sprites
 Give exercises for in class practice (move a sprite based
on keyboard input)
 Direct movement on key press, key held, OR, constant
movement with key input to change direction
 Direct movement of sprite to current mouse position
 Chase movement of sprite to follow (move towards) mouse
position
 Walk the room answering questions, looking over the
shoulder, asking questions, etc.
Closure/Conclusion
 Discussion
 How do you apply this to your Galaga project ship?
 What other kinds of games can you make with
keyboard and mouse input?
 Summary
 Key press and key down handling
 Mouse input
 Ask questions on what is confusing or needs more
time/practice
Keyboard and Mouse Input
Lesson 7
Example Demo
 Today we’re going to learn more about keyboard
and mouse inputs (how to get your sprite to move
on command).
 We’re also going to revisit the concept of variable
and see how we can keep score.
 Demo CheesePuffGirl mini-game
Variables
 What is a variable?
 Stores a value that can be
reused in code.
 Pros: if you want to change
a number, you will only have
to change it in one place.
 Also use for score,
movement speed, health
level, etc.
Variables Continued…
 How do you create a variable?
 How do you deleted a variable?
 How do you show a variable on the screen?
 How do you set the value of the variable?
 How can you increment/decrement the
value of a variable?
 What do these two variable blocks do?
(Same as the third bullet)
Keyboard Input
 We’ve already seen:
 What does this do?
 What if we wanted to move
a sprite to the left with our
left arrow key?
 Right arrow?
 Up/Down arrow?
Keyboard Input (part 2)
0 When you use these blocks:
0 The sprite pauses briefly
0 You can't press 2 keys at the same
time (diagonals)
0 Instead, you can do it another way:
0 May need to adjust speed by adding
wait, or by moving fewer steps
0 Can also use
Change X by and Change Y by
to avoid using Point in Direction
Mouse Input
 We’ve already seen this control:
 What does it do?
 What about this one?
 This is how we can have sprites
follow the mouse.
 What does this control block do?
 Use with Move block to make a sprite
chase your mouse pointer, or another sprite
 What about these blocks?
 Show the x-coordinate of the mouse
 Show the y-coordinate of the mouse
 Condition statement to check if the mouse
was clicked.
Example Demo
 Show how to create the CheesePuffGirl Demo
Daily project
 By the end of class create a mini-game where a sprite chases other
sprites (either by using keyboard or mouse inputs) and on clicking
the chased sprites, the score is increased by 1 point.
 Worth 10 points total. Save your project:
 File format: LP7-YourLastName_YourFirstName.sb
Grade Breakdown – Must Include
Points
Sprite movement on either keyboard or mouse input (must move
correctly in all 4 directions)
2 (1/2 for each direction)
Contains at least two other moving sprite (besides the one being
controlled by keyboard/mouse input)
2
Chased sprites must bounce off screen edges
1
Chased sprites have random starting position
1
Chased sprites have random movement direction
1
On mouse-click the chased sprite must hide and reappear later
1
Score is kept correctly
2
Summary
 Discussion
 How do you apply this to your Galaga project ship?
 What other kinds of games can you make with
keyboard and mouse input?
 Summary
 Key press and key down handling
 Mouse input
 Ask questions on what is confusing or needs more
time/practice