Geeks Who Shower



Palm game programming
posted September 27, 2006 by eedok
This is a news article that was recovered from the server crash
----
Palm Game Programming
----
One of the big complaints I hear from people after buying a palm is how hard it is to create games for the thing, which is usually followed by the other party agreeing with the statements. Now the toolset that palm actively promotes isn't exactly the friendliest or make much sense for the matter(especially when compared to toolsets offered by the competitors), you don't have to use what palm pushes you to use.

Thankfully there's one of the best kept secrets among those who program for the palm known as plua. In short plua is a customized version of lua 4.0 tailored for the palm. It has 2 modes, first off is a repl mode where you can play around with it, and the second one lets you run files and when you're satisfied with the way they run, you can compile them to a prc which you can beam to others easily.

But how do I make games with this do you ask? Rather than a lengthy explanation I'll give short overly commented examples, and then a link to the documentation:

this first example is a simple demonstration of the way plua handles events in the drawing canvas which you move the pen across to draw and hit a button to clear the screen:
Code:
-- draw.lua
-- Creates a simple canvas to draw on, shows basics of events

-- clear the screen to a blank canvas
pclear()
--loop until the house button is hit
while 1 do
    --wait for an event to occur, the x and y aren't always returned, see documentation for pevent() in the plua docs for more details
    event,x,y=pevent()
    --if the pen is touched to the screen
    if event==penDown then
        --set the pixel the pen is on to the foreground color, sets the cursor position
        pset(x,y)
    --if the pen is moved while touching the screen
    elseif event==penMove then
        --draw a line from the last cursor position to where the pen ended up, moves the cursor to the end of the line
        plineto(x,y)
    --if a button was pushed
    elseif event==keyDown then
        pclear()
    end
end
This second example demonstrates how to have events nonblocking and how to do double buffering in plua
Code:
--bouncingbox.lua
--Creates a box that bounces around the screen

--get the resolution of the device
width,height=pmode()
--create a backbuffer to draw to so the screen doesn't flicker
buffer=pnewbuffer(width,height)
--set the box's starting location
x=random()*width
y=random()*height
--set the speed which the box goes
xmod=1
ymod=1
--loop until home button is hit
while 1 do
    --set drawing to happen on the backbuffer
    pusebuffer(buffer)
    --clear it off to make it ready to be drawn on
    pclear()
    --make sure it's in the screen(doesn't account for width&height for simplicity purposes)
    if x + xmod > width or x + xmod < 0 then
        xmod = xmod * -1
    end
    if y + ymod > height or y + ymod < 0 then
        ymod = ymod * -1
    end
    --move the box
    x=x+xmod
    y=y+ymod
    --draw the box with height & width of 10 to make it easy to see
    pbox(x,y,10,10)
    --set drawing to happen on the screen
    pusebuffer()
    --put the backbuffer on the screen
    pputbuffer(buffer,0,0)
    --not used but wait 1ms for an event to occur before looping
    pevent(1)
end

If you're wondering what a function does in more detail, take a look at the plua documentation, the lua 4.0 manual and some plua samples.

I haven't used plua extensively but I have noticed a couple of drawbacks to it:
1. a millisecond timer doesn't seem to exist
2. you can't change your application icon easily
But other than that game programming for palms doesn't have to be hard.

 



Write Comment
Name:
Put the letter y here:
Comment: