The Basics Part 3

From PSP Developer wiki
Revision as of 19:12, 2 March 2019 by GregoryRasputin (talk | contribs) (Created page with "Lesson 02 The Basics Lua Programming Techniques This is part three of Whitehawk's second intoductory Lua tutorial. If you haven't already read the first part and the second pa...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Lesson 02 The Basics Lua Programming Techniques This is part three of Whitehawk's second intoductory Lua tutorial. If you haven't already read the first part and the second part, go back now.

First things first, let's display the background. screen:blit(0, 0, splash, false) screen.waitVblankStart() screen.flip() This step is simple. To display the background, we use the "screen:blit" command. This is much like the "screen:print" command in that it writes to the offscreen buffer. But, in this case it is writing an image rather than text. The first two numbers (as in "screen:print") are the x and y coordinates. Since our image is the since our image is the size of the screen, we left these two numbers as zero (so that the picture would start in the top-left corner). "splash" is the variable name that we used for the image. And then we will pass "false" for the alpha parameter. Our image doesn't have transparency, so we dont need this flag set.

Now we have the background displayed on our intro screen! But, of course, the intro screen is not done. We need... MUSIC! Or... at least some sound. To accomplish this, we simply need to add one line: boltsnd:play() This is where Lua really shines. It may not provide a whole lot of flexibility for advanced coders, but it is lines like this that make it such a great programming language. In C, you would have had to install all sorts of libraries, linked them, and not to mention programmed in a bunch of routines to handle the sound. With Lua, this is all done for you, and you can start playing sound with just a single line of code.

The line is fairly straightforward, we just call the "play()" function for our "boltsnd" variable (which we already loaded our sound clip into above). That's really all there is to it!

Now we are going to flip the screen again, set the volume, and wait on the screen until the sound is finished. Put this in your code: screen.waitVblankStart(240) Music.volume(128) As we touched on before, there are sixty Vblanks in a second, so we are pausing the program for four seconds (240/60 = 4). "screen.flip()" just updates the screen. And, Music.volume(128) sets the volume to 128; this is the highest that the volume goes (0 is the lowest). Now our loading screen (and our intro) are done! Hooray! Give your self a big hug. (Aside: whitehawk is a little weird for adding that in, but hey who am I, the editor, to judge?)

But it's not all fun and games yet, we still need to code the actual program. So, let's go.

The first thing we're going to do is start our game loop and display the background. while true do

         screen:blit(0, 0, background, false)

Remember from the first tutorial when we used the infinite loop at the end? Well, this uses the same concept, except we are going to do the main bulk of our program until the user exits the script rather than just pausing. The other line (obviously, since this is about the gagillionth time we've done this) writes the image into the offscreen buffer.

We are almost to the part of our code where we will interpret button input, but first we need to get the state of the buttons. We do that through the "Controls.read()" function, like this:

         pad = Controls.read()

This saves the button states to a variable, "pad." It is like taking a snapshot, if we never redid this line, it would always contain the button status that we have at this specific moment in time. But, remember, this is still in the game loop, so every time our loop goes through it will update the variable.

Ready for interpretting the input? Yeah, I'll bet you are, but hold your pants on. We need to print the instructions on the screen first (how else are our users going to know what to do?).

         screen:print(135, 251, "Press 'Start' to restart", blue)
         screen:print(110, 261, "Press 'Select' for a screenshot", blue)
         screen:print(383, 35, "Support:", black)
         screen:print(365, 45, "PS3Lounge.net", orange)
         screen:print(365, 55, "scriptscribbler.com/psp", gray)
         screen.flip()

Now we have our instructions on the screen (along with a little bit of self promotion).

Ok, so we now have a loading screen, some background music and an intro screen. So go on to part four to continue with button input.