Learn to Code Space Invaders – Lesson 2 – Moving Things Around
22nd July 2019Learn to Code Space Invaders – Lesson 4 – Creating and Moving Sprites
23rd July 2019Space Invaders Lesson 3 – Using the Control Buttons
Using the Control Buttons In TIC 80
To make our game easy to play we need to be able to detect when the player has pressed keys on the keyboard or buttons on the gamepad. There are two built in functions which allow us to do this.
btn(<key ID>)
The btn() function needs one parameter, the ID number of the button you want to detect. If that key or button is currently being held down the function will return a true answer, otherwise it will return back false.
btnp(<key ID>)
The btnp() function needs the same parameter but this time it only returns true if the key or button has just been pressed. In other words the last time the TIC () function ran the key was not pressed down, but this time it is pressed down. In this instance the function returns true. If the player keeps the key pressed down this function only returns true the very first time it sees the key being pressed down but will then return false.
Key IDs
Each key or button that TIC 80 can recognise is given an ID number. For keyboard input the ID numbers are shown in the diagram below.
TIC 80 can also detect button presses on up to 4 gamepads using the ID number shown in the table below.
Testing for Key Presses
Now that we have two functions that send us back a true or false answer depending on whether keys have been pressed we can use them as the expressions in our if statements to take action when the player presses the button.
Remember that our if statement looks at the expression you use and will perform its action if the expression is true.
if btn(0) then
— do code for up button presses
end