You've already been shown how to do basic player control in previous PlayBasic tutorials. In this tutorial, you'll learn how it works and start on your way to more complex forms of player control.
Introduction to Player Control in PlayBasic Tutorial
Player control is built using If statements. This is true whether you program player control that uses the keyboard, the mouse, or a gamepad or joystick. The concept is easy to understand if you consider how you would design the control for a remote bomb.
A remote bomb only needs one control — a single button that makes it explode. When the user presses the button, the bomb goes boom. To make this work you will need something that can detect when the remote bomb user presses the button and be able to make the bomb explode when he does.
Let's make a bomb that explodes when the player presses the SPACE arrow key.
First, we need something to detect when the user presses the SPACE arrow key. This is the function named SpaceKey(). All it does is wait for the user to press the SPACE arrow key and reports when he does.
Secondly, we need something that can flip the switch that makes the bomb explode in response to the SPACE key being pressed. This is the If statement. If statements do exactly what they look like. Here's the code that controls the bomb :
If SpaceKey() Then bomb = 1
Essentially, this means :
If the user presses the space key then set the variable bomb to 1
The bomb variable is being used as a switch. If bomb is set to 0, the bomb hasn't gone off. If bomb is set to 1, then the bomb goes off. Here's the entire code that simulates a simple bomb control :
bomb = 0 SetFPS 60 Do If SpaceKey() Then bomb = 1 Cls 0 If bomb = 1 Then Print "Boom! The bomb has exploded." Sync Loop
Detecting Arrow Keys
Most player control that uses the keyboard uses the four arrow keys. PlayBasic has four functions similar to SpaceKey() that detect one of the four arrow keys. They are :
It's not hard to guess which arrow key each one of these functions detect.
It comes as a surprise to many people to learn that 8-direction player control is easier to program than 4-direction player control.
The below code example allows you to move a dot in 8 directions in the PlayBasic window :
x# = 400 y# = 300 SetFPS 60 Do ; user input ; if key is pressed then move circle If LeftKey() Then x# = x# - 1 If RightKey() Then x# = x# + 1 If UpKey() Then y# = y# - 1 If DownKey() Then y# = y# + 1 Cls 0 ; display useful information Print " x# : " + str$(x#) Print " LeftKey() : " + str$(LeftKey()) Print "RightKey() : " + str$(RightKey()) Print " y# : " + str$(y#) Print " UpKey() : " + str$(UpKey()) Print " DownKey() : " + str$(DownKey()) Dot x#, y# Sync Loop
You may find it odd that the dot can move in 8-directions even though only four keys are detected. However, the reason this is possible is easy to understand :
Each If statement is independent of the other If statements. Therefore, if you press two keys that are linked to x# and y#, both x# and y# are changed. This results in diagonal motion.
You may have also noticed that if you press each opposing key at the same time the dot doesn't move. Again, this is because each If statement is independent. For example, if you press LEFT and RIGHT, the If statement that detects and reacts to LEFT is subtract 1 from x#. The If statement linked to RIGHT will add 1 to x# at the same time and as a result, leaving the dot exactly where it was before anything happened.