Astronomy Hour of Code
Activity 18: Adding Keyboard Controls: Where am I?Instructions:
(Advanced Lesson)
One of the common accessibility issues that blind computer users have is called the "Where am I?" problem. This is a problem where the state of the program changes and the user may no longer know where she is. In the next lesson, we are going to learn how to change where our telescope is pointing so before we implement that feature, let's give the user a way to find out what coordinates it is pointed at.
The first thing you need to notice about the starting code is the addition of two new Libraries for an InputMonitor and KeyboardEvent. We also create objects called monitor and keys for these classes. Notice also that we included another action from the Game class called Update. The Update action gives us a variable called seconds which represents the fraction of a second since the last Update action was called by the engine. We can use this variable in formulas to calculate movement. We will learn more about that in the next lesson.
For this lesson, we are going to add a call to monitor to see if the space key is pressed on the keyboard. We do this with the following line of code:
if monitor:IsKeyPressed(keys:SPACE)
Inside this conditional block, we are going to put another conditional block (this is called nesting). The purpose of the second conditional is to make sure that we only act on this key press one time. The condition we will check in this block is whether the pressed variable is false (indicating this is the first time this event has been called since the key was depressed. Inside the block we will output the value of the yaw variable (which is angle of rotation in degrees) and then set pressed to true. The block looks like this:
if pressed = false
output yaw
pressed = true
end
We also need a few additional lines of code to close the first conditional statement to be sure the pressed variable is set to false if the monitor does not detect a press of the SPACE key.
else
pressed = false
end
When you are done, click the green Run Program button to start the game engine. When you click the space key, the current angle of rotation will display in the console window. Since we haven't added any controls yet to move the camera this will always display 0 degrees. In order to have the game engine respond to your keyboard input you need to make sure that the visual output window has focus by either clicking on it or tabbing in to it.
Exploration Challenge (Optional):
Use your imagination to add other keyboard controls to your program. The Input and Events Tutorial is a good place to look for some more ideas and examples. Some things you might want to try are: i) add the correct click sound to play when the user presses either the 1 or 2 key, ii) ask the user for input to set the rotation or iii) add a method to run when the user clicks the right mouse button.
Next Tutorial
In the next tutorial, we will discuss Astronomy Hour of Code | Activity 0, which describes Accessible astronomy themed Hour of Code.