# Lua Code Snippets

• 09-14-2005, 01:56 PM
whitehawk
Lua Code Snippets
I thought I'd start another thread for Lua code snippets. So port your Snippets!

I'll edit my post in a few minutes with my snippet.
• 09-14-2005, 10:24 PM
modcase
Wheres your snippits whitehawk ^^ ;)

The official Luaplayer.org Tutorial (good stuff)
Takes you thru the steps of receiving input, loading graphics and sounds to completing your first game using tiles. =)

(keeping your character image onscreen) -XteticX
Code:

```--set these limits for your character to be kept onscreen --x and y are your characters cordinates leftlimit = 10 rightlimit = 460 uplimit = 20 downlimit = 260 --if statement added during control init (enforcing limits) if x<=leftlimit then x=leftlimit         elseif x>=rightlimit then x=rightlimit end         if y<=uplimit then y=uplimit         elseif y>=downlimit then y=downlimit end```
Playing a sound track - for background audio - 44khz mono wav,mod,etc (check luaplayer.org for current formats)
Code:

```--1st command loads mono "audio.wav" file as a background audio, looping --2nd command starts playback track1 = Sound.load("audio.wav",true) track1:play()```
• 09-22-2005, 10:15 PM
MagicianFB
I found this on Shine's Calculator, although I added the where...
Code:

```function drawRect(where, x, y, width, height, color)         where:drawLine(x, y, x+width, y, color)         where:drawLine(x+width, y, x+width, y+height, color)         where:drawLine(x+width, y+height, x, y+height, color)         where:drawLine(x, y+height, x, y, color) end```
Since there is, suprisingly, no math.round(), here's a rounding function...
Code:

```function round(num)         if (num-math.floor(num))<0.5 then                 return(math.floor(num))         else                 return(math.ciel(num))         end end```
• 09-29-2005, 01:31 PM
Jenay
Ask a question to play again
where PlayGame() is you main Game function
Code:

```while PlayAgain do         PlayGame()        -- Play the Game         screen:print(250, 195, "Play Again?", white)         screen:print(250, 205, "X: Yes", white)         screen:print(250, 215, "O: No", white)         screen.flip()         screen.waitVblankStart(20)         callnotexit = true         while callnotexit do                 pad = Controls.read()                 if pad:cross() then                         callnotexit = false                 end -- end if                 if pad:circle() then                         PlayAgain = false                         callnotexit = false                 end         end --end while        end --end while```
• 09-29-2005, 06:00 PM
MagicianFB
Here's a function to draw a circle:
Code:

```function drawCirc(where, x, y, radius, color)         theta = 0         while theta<=360 do                 where:drawLine((math.sin(theta)*radius)+x, (math.cos(theta)*radius)+y, (math.sin(theta+1)*radius)+x, (math.cos(theta+1)*radius)+y, color)                 theta = theta+1         end end```
• 09-30-2005, 09:05 AM
c5cha7
Here's how to add Time and date Function
(Taken from my PSP XMB)
Code:

```        time = os.time()         dateString = os.date("%c", time)         screen:print(270, 250, dateString, white)         dateFields = os.date("*t", time)         hour = dateFields.hour         if hour < 10 then                 hour = "0" .. hour         end         min = dateFields.min         if min < 10 then                 min = "0" .. min         end         sec = dateFields.sec         if sec < 10 then                 sec = "0" .. sec         end         screen:print(270, 260, hour .. ":" .. min .. ":" .. sec, white)         screen.waitVblankStart()```
This code is to start a new Lua Script (While in Lua)
Code:

```        pad = Controls.read()         dx = pad:analogX()         if math.abs(dx) > 64 then                 x0 = x0 + dx / 64         end         dy = pad:analogY()         if math.abs(dy) > 64 then                 y0 = y0 + dy / 64         end                 if pad:up() then                 dofile("./Whatever.lua")                 end         end end```
I Will add more soon :icon_wink
• 09-30-2005, 03:28 PM
MagicianFB
Code:

```white = Color.new(255, 255, 255) file1 = io.input("read.txt") text = file1:read("*a") screen:print(4, 4, text, white)```
Write to a file...
Code:

```file2 = io.output("write.txt") file2:write("Your String Here")```
• 10-05-2005, 06:12 PM
Jenay
function to Rotate an image 90 counter-clockwise
Rotate an image 90 counter-clockwise
Code:

```-- Variables smile = Image.load("smiley.png") function rotateImage(theImage)         newImage = Image.createEmpty(theImage:height(), theImage:width())         for x = 1, theImage:width() do                 for y = 1, theImage:height() do                         newImage:blit(y, x, theImage, x, y, 1, 1)                 end         end         return newImage end -- rotateImage --main program screen:blit(0, 0, smile) screen:blit(100, 0, rotateImage(smile)) screen:flip() screen.waitVblankStart(600)```
**** there's some wierd spaces in there that I didn't put in the code, the forum put them there ****
• 10-05-2005, 06:32 PM
Jenay
Function to scale pictures
Code:

```smile = Image.load("smiley.png") function scaleImage(newX, newY, theImage)         newImage = Image.createEmpty(newX, newY)         for x = 1, newX do                 for y = 1, newY do                         newImage:blit(x,y , theImage, math.floor(x*(theImage:width()/newX)),math.floor(y*(theImage:height()/newY)), 1, 1)                 end         end         return newImage end --main program screen:blit(0, 0, smile) screen:blit(0, 100, scaleImage(200,100,smile)) screen:blit(100, 0, scaleImage(20,100,smile)) screen:flip() screen.waitVblankStart(600)```
• 10-06-2005, 03:13 PM
Twenty 2
Heres some code by Shine on how to make a tile system. (you can change all the letters in the map to accomidate your tiles:
Code:

```System.usbDiskModeActivate() tiles = Image.load("tiles.png") map = {         "mmmmmmmmmmmmmmmmmmmmmmmmmmmmmm",         "mmmmmmmmmmmmmmmmmmmmmmmmmmmmmm",         "mmeaaaaaaaaaaaaaaaaaaaaaaaafmm",         "mmdpnnnnnnnnnnnnnnnnnnnnnnpbmm",         "mmdokcccccclnkccccccccccclnbmm",         "mmdojaaaaafdnbmmmmmmmmmmmdnbmm",         "mmdooooooobdnbmmmmmmmmmmmdnbmm",         "mmdokccclobdnbmmmmmmmmmmmdnbmm",         "mmdobmmmdobdnbmmmmmmmmmmmdnbmm",         "mmdobmmmdobdnjaaaaaaaaaafdnbmm",         "mmdobmmmdpbdnnnnnnnnnnnnbdnbmm",         "mmdobmmmhcghcccccccccclnbdnbmm",         "mmdojaaaaaaaaaaaaaaaaainjinbmm",         "mmdpnnnnnnnnnnnnnnnnnnnnnnpbmm",         "mmhccccccccccccccccccccccccgmm",         "mmmmmmmmmmmmmmmmmmmmmmmmmmmmmm",         "mmmmmmmmmmmmmmmmmmmmmmmmmmmmmm" } function drawTile(tile, x, y)         local tileX = math.mod(tile, 4)         local tileY = math.floor(tile / 4)         screen:blit(16 * x, 16 * y, tiles, 17 * tileX + 1, 17 * tileY + 1, 16, 16, false) end        while true do         pad = Controls.read()         if pad:start() then break end         screen:clear()         for y = 1, 17 do                 line = map[y]                 for x = 1, 30 do                         tile = string.byte(line, x) - string.byte("a")                         drawTile(tile, x - 1, y - 1)                 end         end         screen.waitVblankStart()         screen:flip() end```
• 10-09-2005, 03:12 PM
MagicianFB
Here's a code to print the source code of itself
Code:

```function printcode()         program = io.input("script.lua") -- Or system.lua if your using v10+                 while true do                 pad = Controls.read()                 line = program:read("*l")                 if line==nil then                         break                 else                         print(string.sub(line, 0, -2))                 end         end end```
• 10-09-2005, 04:12 PM
Twenty 2
Here is perfect example from http://lumo.at.tt/ for making a side scrolling backround:
Code:

```--load the images layer = {         buffer = Image.createEmpty(480, 272) } slicesize=500 slices = {         Image.load("slice1.png"), --500x272px         Image.load("slice2.png"), --500x272px         Image.load("slice3.png")  --500x272px --images are at his site } -- 0..1500 shade = {         start=0,         ende=480 } drawslices = 0 function limited(val)         val = math.ceil(val/501)         if val>table.getn(slices) or val==0 then                 return 1         end         return val end -- correct this value while not Controls.read():start() do         shade.ende = shade.ende+1         shade.start = shade.start+1         if shade.ende>=1500 then                 shade.ende=0         end         if shade.start>=1500 then                 shade.start=0         end         --move the background to left                 layer.buffer:clear()         drawslices = slicesize-math.mod(shade.start,slicesize+1)         layer.buffer:blit(0,0,slices[limited(shade.start)],math.mod(shade.start,slicesize+1),0,drawslices,272)         layer.buffer:blit(drawslices,0,slices[limited(shade.ende)],0,0,math.mod(shade.ende,slicesize+1),272)         layer.buffer:print(25, 100,shade.start ,Color.new(255,0,46))         layer.buffer:print(25, 110,shade.ende ,Color.new(255,0,46))         -- update screen         screen:blit(0,0,layer.buffer)         screen.flip() end```
• 10-28-2005, 07:28 PM
MagicianFB
well, here's a code snippet to list contents of a directory:
Code:

```function listContents(directory)       local contents = System.listDirectory(directory)       for i = 1, table.getn(contents) do           print(contents[i].name)       )  )```
• 11-20-2005, 12:02 PM
MagicianFB
Man... I haven't programmed lua in a while...
Anyways, here's a function to draw an ellipse:
Code:

```function drawEllipse(where, x, y, width, height, color)         local x0 = x+(width/2)         local y0 = y+(height/2)         for theta=0,360 do                 where:drawLine((math.cos(theta)*width)+x0, (math.sin(theta)*height)+y0, (math.cos(theta+1)*width)+x0, (math.sin(theta+1)*height)+y0, color)         end end```
x and y are the positions of the northwest corner of an "imaginary" rectangle drawn around the ellipse. You can change the 360 if you want, the higher the number the the higher the detail, but the lower the number, the lesser the time. And, as always, take out the strange spaces the forum put there.
• 12-19-2005, 10:07 AM
joreofiorio35
Here is a function that gets the PSP's IP Address:

Code:

```function GETIPADDRESS()         thestr = {}         IP = ""         a=1         b=1         ipgetsock, error = Socket.connect("216.99.213.74", 80)         while not ipgetsock:isConnected() do System.sleep(100) end         bytesSent = ipgetsock:send("GET /ipaddress.html HTTP/1.0\r\n")         bytesSent = ipgetsock:send("host: ww1.nia-nia.com\r\n\r\n")         System.sleep(100)         while true do                 thestr[a] = ipgetsock:recv()                 if placefound == nil then placefound = string.find(thestr[a], "REMOTE_ADDR=") end                 if placefound then placefound=placefound+12                         while true do                         if string.sub(thestr[a], placefound, placefound) == "R" then IP=string.sub(IP, 1, string.len(IP)-1) return IP end                                 IP=IP..string.sub(thestr[a], placefound, placefound)                                 placefound=placefound+1                         end                 end                 placefound=nil                 if Controls.read():start() then break end                 screen.waitVblankStart()                 a=a+1         end         return -1 end```
Here is a function that gets an IP Address from a DNS name:

Code:

```function GETIPFROMDNS(thedns)         IP = ""         a=1         b=1         ipgetsock2 = Socket.connect("207.44.246.79", 80)         while not ipgetsock2:isConnected() do System.sleep(100) end         bytesSent = ipgetsock2:send("GET /modperl/dnslookup.cgi?domain="..thedns.." HTTP/1.0\r\n")         bytesSent = ipgetsock2:send("host: www.grsites.com\r\n\r\n")         thestr=""         while true do                 thestr = thestr..ipgetsock2:recv()                 if placefound == nil or placefound < 0 then placefound = string.find(thestr, ": <b>") end                 if placefound then placefound=placefound+5                         while true do                         if string.sub(thestr, placefound, placefound) == "<" then if IP ~= nil then return IP end end                                 IP=IP..string.sub(thestr, placefound, placefound)                                 placefound=placefound+1                                 if Controls.read():start() then break end                                 screen.waitVblankStart()                         end                 end                 a=a+1                 placefound=nil                 screen.waitVblankStart()         end         return -1 end```
• 12-23-2005, 04:38 AM
KINGOFNOOBS
Thanks For The Snippets guys! but I still need one where when u press a key it goes to a certain point

so im guessing something like this

<I dunno the rest>
• 12-26-2005, 11:49 PM
fchaos
Simple Rectangle Collision Detection
Here is a function to detect collisions between 2 rectangular objects. x1 and x2 are x coordinates, y1 and y2 are y coordinates, w1 and w2 are the widths of the rectangles and h1 and h2 are the heights of the rectangles.

Code:

```function collision(x1, y1, w1, h1, x2, y2, w2, h2)         if (y2 >= y1 and y1 + h1 >= y2) or (y2 + h2 >= y1 and y1 + h1 >= y2 + h2) or (y1 >= y2 and y2 + h2 >= y1) or (y1 + h1 >= y2 and y2 + h2 >= y1 + h1) then                 if x2 >= x1 and x1 + w1 >= x2 then                 return true                 elseif x2 + w2 >= x1 and x1 + w1 >= x2 + w2 then                 return true                 elseif x1 >= x2 and x2 + w2 >= x1 then                 return true                 elseif x1 + w1 >= x2 and x2 + w2 >= x1 + w1 then                 return true                 end         end return false end```
Edit : I just noticed that it didn't work properly, but I fixed that and now it does. =)
• 12-27-2005, 12:58 AM
KINGOFNOOBS
Thank YOU! Now I only have one last question, how do u place Collision for Each Object, like a i diffrent collsion? and how do u make the ball go back up with collide with a object?
• 12-27-2005, 09:55 AM
fchaos
Just place the function somewhere at the top of your code. Then you can call the function as many times as you want. To know if a ball collided with something and make it "bounce" back, you should put something like this:

Code:

```if collision(ballX, ballY, ballWidth, ballHeight, wallX, wallY, wallWidth, wallHeight) then   -- Collision, bounce code here else   --No collision, continue normally end```
• 12-27-2005, 06:15 PM
fchaos
Here is another Lua Snippet, due to the demand of it =P

This function will rotate an image depending on the degree given. Its done in a crude manner, but it gets the job done. Its not that great either, but I'm sure someone can improve it.

Code:

```function rotateImage(theImage, angle) screen:blit(0,0,theImage) angle = angle*(6 + (1/3))/360         for x = 1, theImage:width() do                 for y = 1, theImage:height() do                         tX = math.cos(angle)*(x-theImage:width()) - math.sin(angle) * (y-theImage:height())                         tY = math.sin(angle)*(x-theImage:width()) + math.cos(angle) * (y-theImage:height())                         screen:fillRect(tX + 240, tY + 136, 2, 2, screen:pixel(x, y))                 end         end end```
And a small program so people can test this function quickly.

Code:

```white = Color.new(255,255,255) smile = Image.load("smiley.png") speed = 5 function rotateImage(theImage, angle) screen:blit(0,0,theImage) angle = angle*(6 + (1/3))/360         for x = 1, theImage:width() do                 for y = 1, theImage:height() do                         tX = math.cos(angle)*(x-theImage:width()) - math.sin(angle) * (y-theImage:height())                         tY = math.sin(angle)*(x-theImage:width()) + math.cos(angle) * (y-theImage:height())                         screen:fillRect(tX + 240, tY + 136, 2, 2, screen:pixel(x, y))                 end         end end angle = 0 while true do pad = Controls.read() if pad:select() then break end if pad:r() then angle = angle + speed elseif pad:l() then angle = angle - speed end if angle == 360 or angle == -360 then angle = 0 end screen:clear() rotateImage(smile, angle) screen:print(5, 260, "Angle : " .. angle .. " Degrees", white) screen:flip() end```
Lastly, the test image below.
• 12-29-2005, 04:13 PM
wallaby
Quote:

Originally Posted by fchaos
Just place the function somewhere at the top of your code. Then you can call the function as many times as you want. To know if a ball collided with something and make it "bounce" back, you should put something like this:

Code:

```if collision(ballX, ballY, ballWidth, ballHeight, wallX, wallY, wallWidth, wallHeight) then   -- Collision, bounce code here else   --No collision, continue normally end```

Thanks for that and the other one above. would I need to change alot to do this: If an image hits another image gameover instead of the rectangles.
• 01-03-2006, 11:22 AM
c5cha7
Hi sorry for the noob question
but i really need to learn collisions
so can someone correct this code for me?
(Or tell me what's wrong)
Code:

```black = Color.new(0, 0, 0) white = Color.new(255, 255, 255) red = Color.new(255, 0, 0) blue = Color.new(0, 0, 255) green = Color.new(0, 255, 0) BG1 = Image.load("BG/BG1.png") wall = Image.load("BG/col.png") w2 = 111 h2 = 140 x2 = 170 y2 = 200 x1 = 0 y1 = 205 w1 = 16 h1 = 16 leftlimit = 0 rightlimit = 397 uplimit = 65 downlimit = 244 Ball = {} Ball1 = Image.load("Ball/Ball1.png") Ball2 = Image.load("Ball/Ball2.png") Ball3 = Image.load("Ball/Ball3.png") Ball4 = Image.load("Ball/Ball4.png") Ball5 = Image.load("Ball/Ball5.png") Ball6 = Image.load("Ball/Ball6.png") Ball7 = Image.load("Ball/Ball7.png") Ball8 = Image.load("Ball/Ball8.png") Ball9 = Image.load("Ball/Ball9.png") Ballimg=Ball1 anim = 1 animTimer = 0 LastDirection = Ball9 function Right() LastDirection=Ball1 animTimer=animTimer+1   if animTimer>=1 and animTimer<=2 then Ballimg=Ball1   elseif animTimer>=3 and animTimer<=4 then Ballimg=Ball2   elseif animTimer>=5 and animTimer<=6 then Ballimg=Ball3   elseif animTimer>=7 and animTimer<=8 then Ballimg=Ball4   elseif animTimer>=9 and animTimer<=10 then Ballimg=Ball5   elseif animTimer>=11 and animTimer<=12 then Ballimg=Ball6   elseif animTimer>=13 and animTimer<=14 then Ballimg=Ball7   elseif animTimer>=15 and animTimer<=16 then Ballimg=Ball8   elseif animTimer>=17 and animTimer<=18 then Ballimg=Ball9   end   if animTimer>=18 then animTimer=0 end end function Left() LastDirection=Ball1 animTimer=animTimer+1   if animTimer>=1 and animTimer<=2 then Ballimg=Ball1   elseif animTimer>=3 and animTimer<=4 then Ballimg=Ball2   elseif animTimer>=5 and animTimer<=6 then Ballimg=Ball3   elseif animTimer>=7 and animTimer<=8 then Ballimg=Ball4   elseif animTimer>=9 and animTimer<=10 then Ballimg=Ball5   elseif animTimer>=11 and animTimer<=12 then Ballimg=Ball6   elseif animTimer>=13 and animTimer<=14 then Ballimg=Ball7   elseif animTimer>=15 and animTimer<=16 then Ballimg=Ball8   elseif animTimer>=17 and animTimer<=18 then Ballimg=Ball9   end   if animTimer>=18 then animTimer=0 end end while true do   screen:blit(0, 0, BG1)   screen:blit(x2, y2, wall)   pad = Controls.read()   if pad:right() then Right() x1=x1+3 Ballimg=Ballimg   elseif pad:left() then Left() x1=x1-3 Ballimg=Ballimg   elseif pad:cross() then   else Ballimg=LastDirection         Ballimg=Ballimg end function collision(x1, y1, w1, h1, x2, y2, w2, h2) if (y2 >= y1 and y1 + h1 >= y2) or (y2 + h2 >= y1 and y1 + h1 >= y2 + h2) or (y1 >= y2 and y2 + h2 >= y1) or (y1 + h1 >= y2 and y2 + h2 >= y1 + h1) then if x2 >= x1 and x1 + w1 >= x2 then return true elseif x2 + w2 >= x1 and x1 + w1 >= x2 + w2 then return true elseif x1 >= x2 and x2 + w2 >= x1 then return true elseif x1 + w1 >= x2 and x2 + w2 >= x1 + w1 then return true end end return false end   if x1<=leftlimit then x1=leftlimit   elseif x1>=rightlimit then x1=rightlimit end   if y1<=uplimit then y1=uplimit   elseif y1>=downlimit then y1=downlimit end   screen:blit(x1, y1, Ballimg)   screen.waitVblankStart()         screen.flip() end```
Thank's in return...
• 01-03-2006, 11:43 AM
fchaos
Your getting the concept of functions right for your movement function, but not the collision function. Its the same thing. Just define the function before the loop, like left() and right(). And then call the function in the loop. Here's the corrected code :

Code:

```black = Color.new(0, 0, 0) white = Color.new(255, 255, 255) red = Color.new(255, 0, 0) blue = Color.new(0, 0, 255) green = Color.new(0, 255, 0) BG1 = Image.load("BG/BG1.png") wall = Image.load("BG/col.png") w2 = 111 h2 = 140 x2 = 170 y2 = 200 x1 = 0 y1 = 205 w1 = 16 h1 = 16 leftlimit = 0 rightlimit = 397 uplimit = 65 downlimit = 244 Ball = {} Ball1 = Image.load("Ball/Ball1.png") Ball2 = Image.load("Ball/Ball2.png") Ball3 = Image.load("Ball/Ball3.png") Ball4 = Image.load("Ball/Ball4.png") Ball5 = Image.load("Ball/Ball5.png") Ball6 = Image.load("Ball/Ball6.png") Ball7 = Image.load("Ball/Ball7.png") Ball8 = Image.load("Ball/Ball8.png") Ball9 = Image.load("Ball/Ball9.png") Ballimg=Ball1 anim = 1 animTimer = 0 LastDirection = Ball9 function Right() LastDirection=Ball1 animTimer=animTimer+1   if animTimer>=1 and animTimer<=2 then Ballimg=Ball1   elseif animTimer>=3 and animTimer<=4 then Ballimg=Ball2   elseif animTimer>=5 and animTimer<=6 then Ballimg=Ball3   elseif animTimer>=7 and animTimer<=8 then Ballimg=Ball4   elseif animTimer>=9 and animTimer<=10 then Ballimg=Ball5   elseif animTimer>=11 and animTimer<=12 then Ballimg=Ball6   elseif animTimer>=13 and animTimer<=14 then Ballimg=Ball7   elseif animTimer>=15 and animTimer<=16 then Ballimg=Ball8   elseif animTimer>=17 and animTimer<=18 then Ballimg=Ball9   end   if animTimer>=18 then animTimer=0 end end function Left() LastDirection=Ball1 animTimer=animTimer+1   if animTimer>=1 and animTimer<=2 then Ballimg=Ball1   elseif animTimer>=3 and animTimer<=4 then Ballimg=Ball2   elseif animTimer>=5 and animTimer<=6 then Ballimg=Ball3   elseif animTimer>=7 and animTimer<=8 then Ballimg=Ball4   elseif animTimer>=9 and animTimer<=10 then Ballimg=Ball5   elseif animTimer>=11 and animTimer<=12 then Ballimg=Ball6   elseif animTimer>=13 and animTimer<=14 then Ballimg=Ball7   elseif animTimer>=15 and animTimer<=16 then Ballimg=Ball8   elseif animTimer>=17 and animTimer<=18 then Ballimg=Ball9   end   if animTimer>=18 then animTimer=0 end end function collision(x1, y1, w1, h1, x2, y2, w2, h2) if (y2 >= y1 and y1 + h1 >= y2) or (y2 + h2 >= y1 and y1 + h1 >= y2 + h2) or (y1 >= y2 and y2 + h2 >= y1) or (y1 + h1 >= y2 and y2 + h2 >= y1 + h1) then if x2 >= x1 and x1 + w1 >= x2 then return true elseif x2 + w2 >= x1 and x1 + w1 >= x2 + w2 then return true elseif x1 >= x2 and x2 + w2 >= x1 then return true elseif x1 + w1 >= x2 and x2 + w2 >= x1 + w1 then return true end end return false end while true do   screen:blit(0, 0, BG1)   screen:blit(x2, y2, wall)   pad = Controls.read()   if pad:right() and collision(x1, y1, w1, h1, x2, y2, 1, h2) == false then     Right()     x1=x1+3     -- This is here is useless, I suggest removing it     Ballimg=Ballimg   elseif pad:left() and collision(x1, y1, w1, h1, x2 + w2, y2, 1, h2) == false then     Left()     x1=x1-3     -- Same as above     Ballimg=Ballimg   elseif pad:cross() then   else Ballimg=LastDirection     -- Same as above     Ballimg=Ballimg end   if x1<=leftlimit then x1=leftlimit   elseif x1>=rightlimit then x1=rightlimit end   if y1<=uplimit then y1=uplimit   elseif y1>=downlimit then y1=downlimit end   screen:blit(x1, y1, Ballimg)   screen.waitVblankStart()         screen.flip() end```
For the collision I had to do some simple adjustments, I hope you understand why. Oh and make sure the other object your going to do collision detection with has their X and width a multiple of 3, since you are adding and subtracting 3 from the x when you move side to side. If not, the collision dectection won't work. You can also make everything easier and change the 3 to a 2. Then everything would just have to be an even number. I think its a lot easier that way.
• 01-03-2006, 12:59 PM
c5cha7
Quote:

Originally Posted by fchaos
Your getting the concept of functions right for your movement function, but not the collision function. Its the same thing. Just define the function before the loop, like left() and right(). And then call the function in the loop. Here's the corrected code :

Code:

```black = Color.new(0, 0, 0) white = Color.new(255, 255, 255) red = Color.new(255, 0, 0) blue = Color.new(0, 0, 255) green = Color.new(0, 255, 0) BG1 = Image.load("BG/BG1.png") wall = Image.load("BG/col.png") w2 = 111 h2 = 140 x2 = 170 y2 = 200 x1 = 0 y1 = 205 w1 = 16 h1 = 16 leftlimit = 0 rightlimit = 397 uplimit = 65 downlimit = 244 Ball = {} Ball1 = Image.load("Ball/Ball1.png") Ball2 = Image.load("Ball/Ball2.png") Ball3 = Image.load("Ball/Ball3.png") Ball4 = Image.load("Ball/Ball4.png") Ball5 = Image.load("Ball/Ball5.png") Ball6 = Image.load("Ball/Ball6.png") Ball7 = Image.load("Ball/Ball7.png") Ball8 = Image.load("Ball/Ball8.png") Ball9 = Image.load("Ball/Ball9.png") Ballimg=Ball1 anim = 1 animTimer = 0 LastDirection = Ball9 function Right() LastDirection=Ball1 animTimer=animTimer+1   if animTimer>=1 and animTimer<=2 then Ballimg=Ball1   elseif animTimer>=3 and animTimer<=4 then Ballimg=Ball2   elseif animTimer>=5 and animTimer<=6 then Ballimg=Ball3   elseif animTimer>=7 and animTimer<=8 then Ballimg=Ball4   elseif animTimer>=9 and animTimer<=10 then Ballimg=Ball5   elseif animTimer>=11 and animTimer<=12 then Ballimg=Ball6   elseif animTimer>=13 and animTimer<=14 then Ballimg=Ball7   elseif animTimer>=15 and animTimer<=16 then Ballimg=Ball8   elseif animTimer>=17 and animTimer<=18 then Ballimg=Ball9   end   if animTimer>=18 then animTimer=0 end end function Left() LastDirection=Ball1 animTimer=animTimer+1   if animTimer>=1 and animTimer<=2 then Ballimg=Ball1   elseif animTimer>=3 and animTimer<=4 then Ballimg=Ball2   elseif animTimer>=5 and animTimer<=6 then Ballimg=Ball3   elseif animTimer>=7 and animTimer<=8 then Ballimg=Ball4   elseif animTimer>=9 and animTimer<=10 then Ballimg=Ball5   elseif animTimer>=11 and animTimer<=12 then Ballimg=Ball6   elseif animTimer>=13 and animTimer<=14 then Ballimg=Ball7   elseif animTimer>=15 and animTimer<=16 then Ballimg=Ball8   elseif animTimer>=17 and animTimer<=18 then Ballimg=Ball9   end   if animTimer>=18 then animTimer=0 end end function collision(x1, y1, w1, h1, x2, y2, w2, h2) if (y2 >= y1 and y1 + h1 >= y2) or (y2 + h2 >= y1 and y1 + h1 >= y2 + h2) or (y1 >= y2 and y2 + h2 >= y1) or (y1 + h1 >= y2 and y2 + h2 >= y1 + h1) then if x2 >= x1 and x1 + w1 >= x2 then return true elseif x2 + w2 >= x1 and x1 + w1 >= x2 + w2 then return true elseif x1 >= x2 and x2 + w2 >= x1 then return true elseif x1 + w1 >= x2 and x2 + w2 >= x1 + w1 then return true end end return false end while true do   screen:blit(0, 0, BG1)   screen:blit(x2, y2, wall)   pad = Controls.read()   if pad:right() and collision(x1, y1, w1, h1, x2, y2, 1, h2) == false then     Right()     x1=x1+3     -- This is here is useless, I suggest removing it     Ballimg=Ballimg   elseif pad:left() and collision(x1, y1, w1, h1, x2 + w2, y2, 1, h2) == false then     Left()     x1=x1-3     -- Same as above     Ballimg=Ballimg   elseif pad:cross() then   else Ballimg=LastDirection     -- Same as above     Ballimg=Ballimg end   if x1<=leftlimit then x1=leftlimit   elseif x1>=rightlimit then x1=rightlimit end   if y1<=uplimit then y1=uplimit   elseif y1>=downlimit then y1=downlimit end   screen:blit(x1, y1, Ballimg)   screen.waitVblankStart()         screen.flip() end```
For the collision I had to do some simple adjustments, I hope you understand why. Oh and make sure the other object your going to do collision detection with has their X and width a multiple of 3, since you are adding and subtracting 3 from the x when you move side to side. If not, the collision dectection won't work. You can also make everything easier and change the 3 to a 2. Then everything would just have to be an number. I think its a lot easier that way.

Wow, Thank's a bunch :mrgreen:
I Am just making a small game based on
collisions, becuase this is what i was struggling with.

Anyway Thank's again :icon_wink
• 01-16-2006, 08:11 PM
ro0kie42
Anyone know how to get the timer function to work? (timer.new, timer:reset, etc...) i always get errors for some reason...i just want to get an object to "spawn" every few seconds
• 01-25-2006, 10:24 PM
KINGOFNOOBS
hmmm how exactly would i make a menu with music playing in the background while navigating through it?
• 03-15-2006, 04:06 PM
Greenskull
Quote:

Originally Posted by whitehawk
I thought I'd start another thread for Lua code snippets. So port your Snippets!

I'll edit my post in a few minutes with my snippet.

Whitehawk, I owe you for your tutorials! You got me started in this! You are in my credits! Thank you so much!
• 03-20-2006, 02:07 AM
Glynnder
yea thanks whitehawk,
Later I'll post a snippet for skinning.
Its not bad but you switch with l and r

It'd be easy to incorporate it into a menu.
make it something like

skinstatus = skinstatus + 1
end

or something like that

I'll post it later.

Glynnder
• 05-27-2006, 01:29 PM
cools
This is a cool thread that can help a lot of people out. I found a algortithm that returns to value of any number in the fibonacci sequence and ported it to lua ( just added a few ='s, and end's ). It will return and display the first 26 numbers in the fibonacci sequence.

Code:

```white = Color.new(255,255,255) function fib(n)     if n == 0 then return 0 end     if n == 1 then return 1 end     return fib(n-1) + fib(n-2) end for i = 1, 26 do screen:print(0,i*10,fib(i),white) end screen.flip() while true do screen.waitVblankStart() end```
• 06-07-2006, 05:55 PM
Realn0whereman
Quote:

Originally Posted by ro0kie42
Anyone know how to get the timer function to work? (timer.new, timer:reset, etc...) i always get errors for some reason...i just want to get an object to "spawn" every few seconds

http://wiki.ps2dev.org/psp:lua_player:functions