Access_Denied's Forums

Site Resources => Tutorials => Topic started by: Access_Denied on June 10, 2007, 12:01:03 PM



Title: Tutorial Two: Printing Text
Post by: Access_Denied on June 10, 2007, 12:01:03 PM
Alright! Welcome to the second Lua programming tutorial. Finally, we're actually going to be doing some programming! Woo-hoo!

Remember that you will need a Text Editor for your computer. how to program, but if you do not have a text editor, you cannot program the application. First thing's first. We're going to learn how to take notes in Lua. In Lua, taking notes is the easiest thing to do. ever. For example:

--I learned from ProgramLua!

To take notes all you need is the two dashes. Notes are very important in your tutorials. If you write a game now, and then come back to it in 2 months, you won't remember anything about your game. So remember to leave notes if you’re going somewhere far away and coming back a long time later. Notice that the note can only be on one line. If you want multiple lines of notes, you need to put the dashes at the beginning of each line. Or you can do this:

--[[ notes
notes
notes
notes
Notes
notes]]

By using the two brackets before and after your notes, it skips over all the writing in those brackets. That is what happens when you take notes. The PSP skips over all the lines after the two dashes.
Now let's take that note taking practice and put it to use. Create a new text document and name it "index.lua", with no .txt extension. Now inside the text document is where you will write your code. Remember, the text in blue is the code you put in your code.

--My First LUA Program
--July 19,2006

Now, FINALLY we'll be starting our first program. Ready?

COLOR VARIABLES
Color Variables are what we store colors in. To do this, you need to be familiar with the RGB color scale. RGB is an abbreviation of Red, Green, and Blue. To find more colors, use an RGB color finder.

blue = Color.new(0,0,255)

There are three things needed to be explained here. 1) the color variables name. This is where the name is stored, so when you want to use the color, just use the name.
-The name has nothing to do with the actual color. So I could name this color "Red", but it would still be blue. The actual color lies in the RGB value. 2) This second part tells the PSP that this is a color and not a number or word.
- The "C" in Color MUST be capitalized. Lua is a case sensitive language. "color" is not the same as "Color" 3) I named this color blue because, well, it is blue. Take a look. The first two numbers are 0, because those are the red and green values, and we don't need any red or green. The third number is the blue value. 255 is the highest a number can go on the RGB scale. So by setting the blue value to 255, you are getting the "truest" blue you can.
Here are some other colors.

Red = (255,0,0)
Green = (0,255,0)
Black = (0,0,0)
White = (255,255,255)

Use the RGB color finder for more colors.

MAIN LOOP
Our main loop comes after all our variables. To signify our loop, use this line:

while true do

What happens in our main loop? Everything. All your button presses and images and text all come together in the main loop. For almost all programs, the first line of your loop will be:

screen:clear()

What this does is pretty self-explanatory. It clears the screen so your program can be seen. If you didn't put this in if would be like writing on a chalkboard and NEVER erasing it. Everything goes on top of each other and isn't readable.
Also, remember that blue color we created earlier? Well if you put the name of a color (i.e. "blue") inside the parentheses, it will clear the screen blue instead of black.

TEXT
Now we'll learn to print text. It's very easy. Something like:

screen:print(100,100,"Hellow World",blue)

Now, the first part of that. The first part "screen:print" tells the PSP that we will be printing to the screen. DUH. Now in the parentheses. The first number: the x coordinate. It tells how many pixels to the right we want our text. The second number: the Y coordinate. It tells how many pixels down from the top you want your text. (BTW The PSP's screen is 480 by 272) The third thing: our text to print. It must be in quotes unless you have a variable set up. (Which we will cover in the next lesson) The last part: Hey it's our color variable. This just tells the PSP to make the text blue, or whatever other color you picked.

Now three more lines and we'll be done. You don't have to understand this last part. Just know it HAS to be there.

screen.waitVblankStart()
screen.flip()
end

Now, in a nutshell, the first line makes our text stay on the screen. Without this our text would stay for a millisecond and then disappear. The second line puts everything on the screen. The PSP draws everything off-screen first. This puts in on screen. The last thing tells the PSP, that we are done with our code.
Now run your code. If you notice, your script works in LuaPlayer for your PSP, but not in the windows version. So here's what you need to do this. Right-Click on "test.cmd" and click "edit" You should see this line:

luaplayer test.lua

Change that to

luaplayer index.lua

Now save it.
Now you should be able to run your code in Windows.
Note: In the “screen.waitVblankStart()”, if you put a number in the parentheses, it will wait that long. For example if you put "60" in there, it will wait one second. 120 for two second, etc. etc. etc. This wraps up our tutorial. Tune in next time for: VARIABLES.


Hosted by www.Geocities.ws

1