The Basics Part 4
Lesson 02 The Basics Lua Programming Techniques This is part four of Whitehawk's second intoductory Lua tutorial. If you haven't already read the first part, the second part, and the third part, go back now.
Now it is time for the interpretation of the button input. Did you keep your pants on? Oh God, I didn't want to hear that. It was a rhetorical question, rhetorical!
What is going to happen in our little application is this:
When we press [X], it will display a picture of the PSP's X button on the screen. When we press [^], it will display a picture of the PSP's UP button on the screen. When we press [L], it will display a picture of the PSP's Left Trigger on the screen. And so on and so forth.
Now that it's understood what we are attempting to do, let's take a gander at trying to understand how we will write the code. We are going to use a series of "if" statements. "If" statements are perhaps the most widely used programming convention. What they allow your program to do is make decisions. "If this, do that. If not this, then do something else." Now that you understand what "if" statements do, let's look at how we make them do it. Here's the code we are going to use for the first button:
if pad:cross() then screen:blit(50, 228, x) screen.flip() end
First of all, "pad:cross()" is the way that we check to see if when the "pad" variable was set, the [X] button was pressed. It will return "true" if it was, and "false" if it wasn't. If it is returned true, the code between "then" and "end" is run. It it is returned false, the code in between those two identifiers is skipped. The code, of course, is our code to display the image of the [X] button.
The code for the first button is set, now we just need to do the same thing for each of the other buttons. Here's the code:
if pad:circle() then screen:blit(90, 195, circle) screen.flip() end
if pad:triangle() then screen:blit(45, 165, triangle) screen.flip() end
if pad:square() then screen:blit(15, 195, square) screen.flip() end
if pad:up() then screen:blit(60, 40, up) screen.flip() end
if pad:right() then screen:blit(90, 65, right) screen.flip() end
if pad:down() then screen:blit(60, 80, down) screen.flip() end
if pad:left() then screen:blit(30, 65, left) screen.flip() end
if pad:l() then screen:blit(4, 6, l) screen.flip() end
if pad:r() then screen:blit(403, 4, r) screen.flip() end
Congratulations! You have finished your first true Lua Application. So we're done... or are we? (*cue dramatic music*)
Continue to part five to find out if we're truly done.