Step 5¶
For our game to engage and interact with users, we need it to be responsive to user input.
In this chapter, we will be looking at how user input is handled in Shards.
User Input¶
User input is managed within the GFX.Window
of your game. For this game, the only user input required would be the ↑ and ↓ directional key.
We can use Inputs.KeyDown
to execute code whenever the user presses down on a specified key.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
- This code is executed when the user presses the ↑ directional key.
- This code is executed when the user presses the ↓ directional key.
Inputs.KeyDown
will run the code in itsAction
parameter when theKey
specified is pressed down by the user.
In order to prevent code within the Inputs.Keydown
shards from executing whenever the user presses the specified key, we define a variable .input-received
to track if we have already received the user's input.
Define the variable in initialize-variables
and add code to reset it in reset-round-variables
.
1 2 3 4 5 6 7 8 9 10 11 |
|
- Variable that tracks if an input from the user has been received.
- Resets the variable tracking whether user input has been received for each round.
Now that we are able to obtain the user's input, we can proceed to check if the user pressed the correct button.
Checking the Input¶
Based on the images chosen, we can determine whether the user needs to select the ↑ or ↓ directional key.
In initialize-variables
, create a variable named .same-image
. This will be used to check if the user pressed the correct key later.
1 2 3 4 5 6 7 8 9 |
|
- Tracks whether the same image is used.
Navigate to where we chose the images in initialize-round
.
Check if the chosen images are the same, and assign true
or false
to .same-image
accordingly.
1 2 3 4 5 6 7 8 9 10 |
|
If
checks thePredicate
given and runs the code withinThen
if it is true. If false, the code withinElse
is run instead.- Check if the images on the left and right are the same or different.
Create a shard named check-answer
which will take in the user's input and check it against .same-image
.
The user is correct if they:
-
Pressed ↑ when
.same-image
is true -
Pressed ↓ when
.same-image
is false
We award them a point by increasing the value of .total-score
.
Set .input-received
to true to prevent further user input, and end the round if .game-over
is false.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
- Take in the user's input.
- Check if a user input has already been received.
- If the user pressed the ↑ directional key when the same images are being shown...
- ... increase the user's total score.
- Prevents this segment of code from running again until it is reset.
- If it is not Game Over yet...
- ... end the round.
Step
runs a wire inline.nil
ensures that any previous input (e.g. Bool fromtrue > .input-received
) is not propagated into the wire. The first use of a wire defines its input type, and subsequent calls to the wire with different input types will be a violation.
Step vs Do
Step behaves similarly to Do
, except that it allows you to use a Looped Wire like a function. Step
will run one iteration of the Loop before returning control back to the Wire that called it. If you try to use Do
on a Loop, the Loop will run indefinitely.
Use a stepped Loop when you want a variable within the Loop to persist. When a Wire finishes, any changes made to the variables within it will be lost. A Looped Wire lives on, thereby retaining any changes within it.
Check out the primer here for more information.
We can now employ the check-answer
shard in our Inputs.KeyDown
logic.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
- Check-answer is called, with true being passed in to indicate that the user chose "Yes".
- Check-answer is called, with false being passed in to indicate that the user chose "No".
Cheers! Your game can now receive user input, tabulate the score, and allows players to play up to 10 rounds each time.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
|