logologologologo
  • home
  • learn to code
    • Space Invaders – Beginner’s Course
    • Asteroids – Beginner’s Level 2
    • Space Commander – Beginners Level 3
    • Snake – a Beginner’s Challenge
  • learn to make
    • Arduino Tutorials
    • Raspberry Retro Games Console
  • blog
    • Computer Science
    • Coding
      • Collision Detection
    • Retro Gaming
    • Computer Games for Vision Therapy
  • contact me
✕
Learn to Code Space Invaders – Lesson 2 – Moving Things Around
22nd July 2019
Learn to Code Space Invaders – Lesson 4 – Creating and Moving Sprites
23rd July 2019
Published by Bob at 22nd July 2019
Categories
  • Learn to Code Space Invaders
Tags

Space 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.

TIC80 Button IDs

TIC 80 can also detect button presses on up to 4 gamepads using the ID number shown in the table below.

TIC80 gamepad button IDs

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

 

Code for Lesson 3

click to download Lesson 3 code
Share
6
Bob
Bob

Related posts

17th November 2019

Learn to Code Space Invaders – Lesson 25 – Level Up!


Read more
17th November 2019

Learn to Code Space Invaders – Lesson 24 – The Aliens Have Landed


Read more
16th November 2019

Learn to Code Space Invaders – Lesson 23 – Player Lives and Game States


Read more

Comments are closed.

  • Privacy Policy
  • Contact Me
© 2020 Bytes N Bits