Sony Playstation Portable Lua Programming: Button Input
In this tutorial we are going to learn how to take button input for your PSP, and make a simple program that will display text to the screen when you press certain buttons.
Without button input, there is no user interaction, which is well....quite boring. So we're going to go ahead and learn a little about it now. We will make use of the
commands we learned in the earlier tutorials, including variables. We will slack off a little with comments in this tutorial, and introduce another method of keeping your
code in order, by splitting it up in sections.
Make Use of Button Input
Let's start by making a section that we will use for variables. Feel free to make your own design instead of the stars!
-- ******* Variables ********
Once again, we need our color object so that we can write to the screen. If you like, make several different color objects, each on their own line. You will be able to use them.
But, I will leave that to you. I will only make one myself. (Learning to do things on your own early on is a plus)
green = Color.new(0,255,0)
Next, we are going to create some variables that will store text that will be printed when we press a button. This we tell what button you are pressing.
We will only make half of the buttons we use with variables. The other half we will directly print out with the print command. Why? For one so you can practice your variable use,
and two, to see that either way works fine for a program this small. We are going to be using ten buttons. These will be the directional buttons, the shape buttons, and the shoulder buttons.
We are also going to use the print command slightly different this time. When we print what button we're pressing to the screen we want the button to be in quotes and in order to
do that we'll use single quotes to surround our string of text, instead of quotes. If we use the normal quotes then the program will think we are ending the string of text where
our button word is.
Let's make our five variables (remember I said half).
upPressed = 'You are pressing the "UP" button'
downPressed = 'You are pressing the "DOWN" button'
leftPressed = 'You are pressing the "LEFT" button'
rightPressed = 'You are pressing the "RIGHT" button'
LPressed = 'You are pressing the "L" button'
Now that we've gotten that over with let's move on. Remember that loop thing that we keep using... and avoiding?! Well, it's time to learn a little more about it. Every game
you make will have a main program loop. What this is, is a section of code that will repeat over and over and over and over until a certain condition is met, whatever that
may be. Inside the loop there are sections of code that will be performed. Some of them will be performed every single time the program loops, some will check to see if the
condition is right, and then choose whether or not to perform that piece of code. Let's start our loop.
-- *****Main Loop******
while true do
From this tutorial on we will start coding inside of the loop, which we haven't done yet. Let's put in our next line of code, which is something new. Clearing the screen.
screen:clear()
This command will clear the screen, erasing everything you've put there. Every time the loop repeats everything on the screen will be cleared. By the way, these loops run at speeds so fast you'll never know it's happening! Without doing this everything we print would get pasted on top of what we printed before, creating a big ugly blob.
Let's move on to something else new.
pad = Controls.read()
Put simply, this will read button input each time the program loops. Also, note that pad is a variable and that you could use any other name, such as buttons. pad seems to be
the standard, so we'll use it.
Now it's time to learn your first conditional expression, the if statement. It's not as bad as it sounds, trust me. Also, this will introduce the button input command.
Let's get it in our program, then I'll explain.
if pad:up() then
screen:print(100,100,upPressed,green)
end
What this is saying is "if the up button is pressed then print the text in the variable upPressed with the color green". This returns either true or false.
Every if statement starts with the word "if", then gives something to check for. In this case, is up being pressed? Next, it has the word "then" followed by the code
that will perform if the condition is true. Finally the statement always ends with the word "end". The terms are so easy you can read the statement and tell instantly
what it's doing. This is a quick intro into this, and we will dive more into it later. Now, let's add the code for the other three directional buttons, and one shoulder button, L.
if pad:down() then
screen:print(100,100,downPressed,green)
end
if pad:left() then
screen:print(100,100,leftPressed,green)
end
if pad:right() then
screen:print(100,100,rightPressed,green)
end
if pad:l() then
screen:print(100,100,LPressed,green)
end
Now for those other five buttons. We will write the text directly in the print statement for these, just to practice different methods.
if pad:r() then
screen:print(100,100,'You are pressing the "R" button',green)
end
if pad:triangle() then
screen:print(100,100,'You are pressing the "TRIANGLE" button',green)
end
if pad:circle() then
screen:print(100,100,'You are pressing the "CIRCLE" button',green)
end
if pad:cross() then
screen:print(100,100,'You are pressing the "X" button',green)
end
if pad:square() then
screen:print(100,100,'You are pressing the "SQUARE" button',green)
end
Phew! Now let's finish up our loop. We need to flip the screen so that the offscreen buffer goes to onscreen. Note, the "end" is to end our "while loop".
screen.waitVblankStart()
screen.flip()
end
Run it and press some buttons to see what happens!
Variables |
Tables
|