PDA

View Full Version : Lua's corner



shadowprophet
July 18th, 2005, 21:38
Hi all im posting this thread. For everyone thats interested in dev/ing With lua.
I think its a nifty little program. And I also think its The perfect starters tool For aspireing developers to learn To code.
What I Submit this thread be. Is a learning center, Where coders come in and paste there script code. proven methods that have worked and ideas they wish to share. So others can learn from this. you can also post code with errors in it that you are haveing problems with and maybe somone can help with the answers. for the first entry. I will give An alter verson is shines Snake code.
That I aletred myself in a first attempt. This was just for kicks to see if the program worked. it does!. But here it is. Although I doubt somwhat it will be of any use to anyone. this script Is finished, And 100% Working :p
__________________________________________________ ______________
-- Shadowprophets Fantasy Beta
-- load images
broom2 = loadImage("broom2.png")
mm = loadImage("mm.png")
bodyT = loadImage("sprite8.png")
bodyB = loadImage("sprite1.png")
bodyL = loadImage("sprite3.png")
bodyR = loadImage("sprite6.png")
headR = loadImage("headR.png")
headT = loadImage("headT.png")
headL = loadImage("headL.png")
headB = loadImage("headB.png")
bodyLT = loadImage("bodyLT.png")
bodyLB = loadImage("bodyLB.png")
bodyRT = loadImage("bodyRT.png")
bodyRB = loadImage("bodyRB.png")
bodyLR = loadImage("bodyLR.png")
bodyTB = loadImage("bodyTB.png")
-- define globals
bodys = {}
heads = {}
bodies = {}
RIGHT = 0
TOP = 1
LEFT = 2
BOTTOM = 3
bodys[RIGHT] = bodyR
bodys[TOP] = bodyT
bodys[LEFT] = bodyL
bodys[BOTTOM] = bodyB
heads[RIGHT] = headR
heads[TOP] = headT
heads[LEFT] = headL
heads[BOTTOM] = headB
bodies[RIGHT] = bodyLR
bodies[TOP] = bodyTB
bodies[LEFT] = bodyLR
bodies[BOTTOM] = bodyTB
score = 0
high = 0
target = {}
targetImage = 0
dx = 0
dy = 0
cellHead = {}
cellBody = {}
function createRandomTarget()
target["x"] = math.random(21)
target["y"] = math.random(15)
if math.random(2) == 1 then
targetImage = broom2
else
targetImage = mm
end
end
function newGame()
flipDrawFrame()
fillBackground(0, 0, 480, 270)
dx = 0
dy = 0
x = 12
y = 8
cellBody["x"] = x
cellBody["y"] = y
cellBody["direction"] = RIGHT
x = x+1
cellHead["x"] = x
cellHead["y"] = y
cellHead["direction"] = RIGHT
cellBody.next = cellHead
createRandomTarget()
blitImage(16*cellBody["x"], 16*cellBody["y"], bodyR);
blitImage(16*cellHead["x"], 16*cellHead["y"], headR);
score = 0
flipShowFrameV()
end
function keyboardControl()
waitVblankStart();
pad = ctrlRead()
if isCtrlUp(pad) then
dx = 0
dy = -1
elseif isCtrlDown(pad) then
dx = 0
dy = 1
elseif isCtrlLeft(pad) then
dx = -1
dy = 0
elseif isCtrlRight(pad) then
dx = 1
dy = 0
elseif isCtrlCircle(pad) then
-- screenshot()
end
end
function move()
-- do nothing, if no movement vector defined
if dx == 0 and dy == 0 then
return
end

-- save last head direction and position
lastX = cellHead["x"]
lastY = cellHead["y"]
lastDirection = cellHead["direction"]
-- add new head
cellHead.next = {}
cellHead = cellHead.next
cellHead["x"] = lastX + dx;
cellHead["y"] = lastY + dy;

-- set direction for new head
direction = 0
if dx == 1 then
direction = RIGHT
elseif dy == -1 then
direction = TOP
elseif dx == -1 then
direction = LEFT
elseif dy == 1 then
direction = BOTTOM
end
cellHead["direction"] = direction

-- check which body to draw at the place of the old head
if lastDirection == RIGHT and direction == TOP then
blitImage(16*lastX, 16*lastY, bodyLT)
elseif lastDirection == TOP and direction == LEFT then
blitImage(16*lastX, 16*lastY, bodyLB)
elseif lastDirection == LEFT and direction == BOTTOM then
blitImage(16*lastX, 16*lastY, bodyRB)
elseif lastDirection == BOTTOM and direction == RIGHT then
blitImage(16*lastX, 16*lastY, bodyRT)
elseif lastDirection == RIGHT and direction == BOTTOM then
blitImage(16*lastX, 16*lastY, bodyLB)
elseif lastDirection == TOP and direction == RIGHT then
blitImage(16*lastX, 16*lastY, bodyRB)
elseif lastDirection == LEFT and direction == TOP then
blitImage(16*lastX, 16*lastY, bodyRT)
elseif lastDirection == BOTTOM and direction == LEFT then
blitImage(16*lastX, 16*lastY, bodyLT)
else
blitImage(16*lastX, 16*lastY, bodies[direction])
end

-- draw new head
blitImage(16*cellHead["x"], 16*cellHead["y"], heads[cellHead["direction"]])

-- check for target
if cellHead["x"] == target["x"] and cellHead["y"] == target["y"] then
createRandomTarget()
score = score + 1
else
-- remove body
fillBackground(16*cellBody["x"], 16*cellBody["y"], 16, 16)
cellTBody = cellBody.next
-- draw new Body image
if cellBody["direction"] ~= cellBody.next["direction"] then
cellBody["direction"] = cellBody.next["direction"]
end
blitImage(16*cellBody["x"], 16*cellBody["y"], bodys[cellBody["direction"]])
end
end
function isGameOver()
lastX = cellHead["x"]
lastY = cellHead["y"]
gameOver = false
if lastX >= 22 then
gameOver = true
end
if lastX < 1 then
gameOver = true
end
if lastY >= 16 then
gameOver = true
end
if lastY < 1 then
gameOver = true
end
cell = cellBody
while cell ~= cellHead do
if cell["x"] == lastX and cell["y"] == lastY then
gameOver = true
break
end
cell = cell.next
end
return gameOver
end

-- init
math.randomseed(os.time())
screenFrame(1, 1)
screenFrame(1, 0)
newGame()
-- game loop
while true do
for i=0,4 do
keyboardControl()
end
move()
blitImage(16 * target["x"], 16 * target["y"], targetImage)
if score > high then
high = score
end
if isGameOver() then
for i=0,50 do
waitVblankStart()
end
newGame()
end
printDecimal(410, 81, score, 0);
printDecimal(429, 129, high, 0);
end
__________________________________________________ ________________

If you have any script that works with lua please post it here. Im sure there are a lot of people who could learn from it:D

"Shadowprophet" Cause he thinks lua is just the complete bomb-digity

This thread could really help the aspireing developers comunity:D
So please by all means contrubute!! :cool:

alex_dsnews
July 20th, 2005, 00:10
This might be useful. Lua's actually pretty fast, and I struggled for a bit to handle the joypad input without it repeating when you held a button down. Start with allowpad=0 before your main loop, and have two ctrlRead()'s - one is just fake and only lets the real one read the pad once you've 'let go' of any buttons.

This would go in your main loop, and then you'd read from the 'pad' variable for your input.



if allowpad==0 then
clearpad=ctrlRead()
end

if clearpad==0 then
allowpad=1
end

if allowpad==1 then
pad = ctrlRead()
end

shadowprophet
July 20th, 2005, 08:45
Thanx alex That little bit of code will come in handy over and over again :p
its about time you got your coders bagde bro :D
. A useful idea ive had which isnt very helpful unless your simply tired of looking at the silly snake image on the lua loader. I went to the lua site grabed the logo images sized them down and put them in the eboot useing psp brew.
It somhow feels much more offical this way.
I wanted to upload the file but I tried like times, once in rar once in zip and once as the raw eboot. none of them show up in my attachments list:confused:. At any rate its very simple to do. I recomend to everyone thats interested in developing with lua. to get psp brew so they can alter there own eboots. with pspbrew and lua. you really have a well rounded and sound method for creating homebrew. Thanx again alex for that little bit of code. like all interesting snippets of code I run into . its saved on my psp in a text directory. for future use :D You da man alex keep it up. if you happen to come across anymore exampels or create them yourself. id love to see them, im learning from you bro :p

PSP_Newbie
July 20th, 2005, 09:08
hey shadowprophet, i think this site only allow files as attachements w/ a certain extension, one of which is .zip, so .pbp and .rar wont work

shadowprophet
July 20th, 2005, 17:59
hey shadowprophet, i think this site only allow files as attachements w/ a certain extension, one of which is .zip, so .pbp and .rar wont work

But ive tried everything. I got flooded with spyware the other day when I went to this rom site. Ever since then ive been haveing problems. I zaped the spyware like a hundred times. it always sais it worked. and upon reboot. its all back :( .
I virus scanned. I recomend (F-prot Dos ) anyway I had a few viruses. smallaf
/ strange worm 32. trojen/ you know normal stuff. But one effected my download mod. I believe it was called download.exe. which by the way I never downloaded to begin with :p. Anyway everything's stable on my system now. but the damage has been done. Even in my systembackup files. I fear things arnt gonna function correctly untill I reinstall. And I refuse to do so at this time. because i just reinstalled like two weeks ago :mad: . im gonna ride this one out untill I cant get any more use from windows. Then reinstall:p
But I truely wish i could upload that altered eboot for lua it would be a nice compliment to this thread. But its nothing that anyone with pspbrew couldnt esily do even without any codeing skill. so its not truely necessary anyway :p

shadowprophet
July 21st, 2005, 10:48
Has anybody looked at the lua script in that The troubles of the middle earth (alpha) :confused: Who programed this God!?! I like to try to follow source codeing to learn new methods. But dayum!!! :eek:

ProTH
July 21st, 2005, 22:03
I struggled for a bit to handle the joypad input without it repeating when you held a button down. Start with allowpad=0 before your main loop, and have two ctrlRead()'s - one is just fake and only lets the real one read the pad once you've 'let go' of any buttons.
That's a pretty good suggestion. When I ran across the same problem, I just made it pause for a fraction of a second whenever a key had been pressed... of course, my programs aren't exactly button mashers, so that didn't present any problems.

Here's something I did last night. Again it was just an old joke program of mine that made for a quick conversion. It does little more than shove random bits of text together, but it might get you a television job: the Dennis Miller Reference Generator. It's also more fun with the special background image and eboot as found in this ZIP (http://rapidshare.de/files/3226590/DMiller05PSPLua.zip.html). Basically hit any face button, d-pad direction, or start to generate a new sentence. Hitting select will clear the text and take it out of the "waiting for input" loop, allowing one to hit Home and exit the program.


background=loadImage("background.png")

Person = { "Abraham", "Adolf Hitler", "Agamemnon", "Alfonso Ribeiro", "Ariel Sharon", "Barbara Walters", "Benedict Arnold", "Bob Saget", "Bruce Vilanch", "Buford Pusser", "Charlemagne", "Conan the Barbarian", "Dennis Kucinich", "Eleanor Roosevelt", "Elle McPherson", "Ernest Hemingway", "Ezra Pound", "Fred Rogers", "Galileo", "Gary Condit", "Geronimo", "Hera", "Jack the Ripper", "Jethro Bodine", "John Quincy Adams", "Judge Judy", "Julius Caesar", "Kid Rock", "King Arthur", "King Kong", "Lando Calrissian", "Lex Luthor", "Lucille Ball", "Mahatma Gandhi", "Marshall Applewhite", "Mark Felt", "Meriweather Lewis", "Mother Teresa", "Napoleon", "Osama bin Laden", "Patrick Ewing", "Philo T. Farnsworth", "Ray Charles", "Robert DeNiro", "Robert Novak", "Salvador Dali", "Samuel Clemens", "Stanley Kubrick", "Steve Jobs", "Thomas Edison" }



EventsA = { "assassinating", "at the crucifixion of", "comparing scars with", "doing the Vulcan nerve pinch on", "engaging in fisticuffs with", "feeling up", "finding a potato chip shaped like", "giving a black eye to", "giving the Heimlich maneuver to", "going bowling with", "going on a road trip with", "playing chess with", "putting the moves on", "running a three-legged race with", "running over", "saying a eulogy for", "sharing a Big Mac with", "singing a duet with", "snorting coke with", "starring in a TV movie about the life of", "stealing the election from", "swapping baseball cards with", "waxing philosophical with" }


EventsB = { "at the Astrodome", "at the burning of Rome", "at Woodstock", "crossing the Delaware", "eating a balanced breakfast", "ending the War of 1812", "getting abducted by a UFO", "in detention", "in the nude", "inventing a better mouse trap", "jacked into the Matrix", "on the grassy knoll", "on steroids", "proclaiming that Dewey has defeated Truman", "riding around on a go-cart", "singing karaoke", "visiting a pet store", "walking on the moon" }



math.randomseed( os.time() )
math.random()



function gameover()
clearscreen()
os.exit(0)
end


function presskeytocontinue()
--print("\n<Press Key to Continue>\n")
repeat
pad = ctrlRead()
until isCtrlUp(pad) or isCtrlDown(pad) or isCtrlLeft(pad) or isCtrlRight(pad) or isCtrlCircle(pad) or isCtrlCross(pad) or isCtrlTriangle(pad) or isCtrlSquare(pad) or isCtrlStart(pad) or isCtrlSelect(pad)
if isCtrlSelect(pad) then
gameover()
end
end

function clearscreen()
--flipDrawFrame()
--fillBackground(0, 0, 480, 272)
blitImage(0,0, background)
end

function wait(blinks)
for i=0,blinks do
waitVblankStart()
end
end



while true do
Sentence = "It's like "

Sentence = Sentence .. Person[math.random(table.getn(Person))] .. " "

if math.random() < 0.4 then
Sentence = Sentence .. "and " .. Person[math.random(table.getn(Person))] .. " "
end

if math.random() < 0.5 then
Sentence = Sentence .. EventsA[math.random(table.getn(EventsA))] .. " " .. Person[math.random(table.getn(Person))]
if math.random() < 0.2 then
Sentence = Sentence .. " " .. EventsB[math.random(table.getn(EventsB))]
end
else
Sentence = Sentence .. EventsB[math.random(table.getn(EventsB))]
end

Sentence = Sentence .. "."

print(Sentence)

presskeytocontinue()
clearscreen()
wait(25)
end

shadowprophet
July 31st, 2005, 06:07
does anybody know how to make a scrolling background in lua?
Ive looked all over the place and cant find any examples:mad:

If anybody had any ideas I would be very grateful :p

alex_dsnews
August 1st, 2005, 09:55
There's one guy over at the ps2dev.org forums working on a tile engine game, but Lua 0.3/0.4 is actually pretty nifty at blitting images from one screen to another. Shouldn't be too hard to scroll some images along.

shadowprophet
August 5th, 2005, 16:34
yeah lua will be able to do what I need it to just fine :) Ive learned a lot about it lately :D

F34R
August 7th, 2005, 03:06
ok, i tried to load a png using the : background=loadImage("background.png") line... it doesnt seem to load the background file. Any particular specs I need to working with here ? I sized it 480 x 272...