« Return to Thread: You are welcome.. (and some more demo code)

You are welcome.. (and some more demo code)

by Berkant Atay :: Rate this Message:

Reply to Author | View in Thread

Happy to see you having started writing your home code, jimmyjoejack.. I also code on my Treo in bed (till my wife kicks me out)  :)

Since I cannot upload files to the files section any more, I copy-pasted here some more demonstration codes I wrote. There are 9 programs separated with asterices. Just copy them in a new .txt file on your desktop, then copy these text files on your SD card's PALM\Programs\Plua\SRC folder, then change the extensions to lower case ".lua". You can access and run them in Plua after changing to "Card"...

Here we go...


*********************************************************************

-- ** Combinatorial Mathematics **

function fact(x)
   local mult=1
   for i=1,x do
   mult=mult*i
   end
  return mult
end

function getnumbers()
  local p=gui.gettext(num1field)
  if p=="" then gui.alert("You cannot leave a field blank.") return nil,nil end
  if tonumber(p)==nil then gui.alert("Only numbers.") return end
  p=tonumber(p)
  if p~=tonumber(string.format("%d", p)) then gui.alert("Integral numbers please.") return nil,nil end
  local q=gui.gettext(num2field)
  if q=="" then gui.alert("You cannot leave a field blank.") return nil,nil end
  if tonumber(q)==nil then gui.alert("Only numbers.") return end
  q=tonumber(q)
  if q~=tonumber(string.format("%d", q)) then gui.alert("Integral numbers please.") return nil,nil end
  if p<q then gui.alert("First argument cannot be smaller than the second.") return nil,nil end
  return p,q
end
 
function permu()
  local p,q=getnumbers()
  if p==nil or q==nil then return end
  screen.moveto(0,200)
  print("                                                                       ")
  print("                                                                       ")
  print("                                                                       ")
  screen.moveto(0,200)
  local k=fact(p)/fact(p-q)
  s="   The permutation of  ( "..tostring(p).." , "..tostring(q).." )  is "
  print(s)
  print()
  print("     ",k)
  return
end

function combi()
   local p,q=getnumbers()
   if p==nil or q==nil then return end
   screen.moveto(0,200)
   print("                                                                        ")
   print("                                                                        ")        
   print("                                                                        ")
   screen.moveto(0,200)
   local k=fact(p)/(fact(q)*fact(p-q))
   s="   The combination of  ( "..tostring(p).." , "..tostring(q).." )  is "
   print(s)
   print()
   print("     ",k)
   return
end

gui.title("COMBINATORIAL MATHEMATICS")
screen.moveto(10,50)
num1field=gui.field(1,10,10,"",not nil,nil)
screen.moveto(140,50)
num2field=gui.field(1,10,10,"",not nil,nil)
screen.moveto(5,100)
butcomb=gui.button("combination")
screen.moveto(133,100)
butperm=gui.button("permutation")
screen.moveto(180,270)
exitbtn=gui.button("Quit")
screen.rect(6,48,92,28)
screen.rect(132,48,92,28)
gui.setfocus(num1field)

while true do
 ev,id=gui.event()
  if ev==appStop then break end
  if ev==ctlSelect and id==butperm then permu() end
  if ev==ctlSelect and id==butcomb then combi() end
  if ev==ctlSelect and id==exitbtn then os.exit() end
end


*********************************************************************


-- ** Random colored disks, fun to watch.. **

screen.clear()
while true do
x=math.random(320)
y=math.random(320)
c=screen.rgb(math.random()*256,math.random()*256,math.random()*256)
r1=math.random(160)
r2=math.random(160)
screen.disc(x,y,r1,r2,c)
ev=gui.event(0)
if ev==appStop then break end
end


**********************************************************************


--   **  Simple demostration game **


function  initfile()
points=0
person={}
f,numrec=io.open("db:/Gamehigh.ber","r+")
if numrec==0 then person={"nobody",0}
        data=bin.pack("SB", person)
        f:createrec(string.len(data))
        f:openrec(0)
        f:write(data)
        high=0
        f:closerec() end
f:openrec(0)
data=f:read("*a")
person=bin.unpack("SB", data)
high=person[2]
f:closerec()
f:close()
end

function play()
shots={}
gui.destroy()
for n=1,10 do
gui.title("Points: "..points)
  for i=1,n do
     if shots[i]==1 then screen.disc(120+i*16,14,8,6,screen.rgb(0,255,0))
     else screen.disc(120+i*16,14,8,6,screen.rgb(255,0,0))
     end
  end  
os.sleep(0.1+3*math.random())
x=math.random(300)+10
y=math.random(270)+40
screen.disc(x,y,10,10,screen.rgb(255,0,0))
t=os.clock()
while true do
ev,inx,iny=gui.event(0)  
   if ev==appStop then os.exit() end
   if os.clock()-t>0.5 then break end
   if    ((ev==penDown)
   and (inx>x-10) and (inx<x+10)
   and (iny>y-10) and (iny<y+10))
   then
       points=points+1
       screen.disc(x,y,10,10,screen.rgb(0,0,255))
       os.sleep(0.1)
       shots[n]=1
       break end
   end
   end
end

function trunk()
if points>high then
      newrecname=gui.input("You broke the record !  Enter your name:")
      person[1]=newrecname
      person[2]=points
      f=io.open("db:/Gamehigh.ber","r+")
      f:removerec(0)
      data=bin.pack("SB", person)
      f:createrec(string.len(data))
      f:openrec(0)
      f:write(data)
      f:closerec()
      f:close()
      high=points
      end
screen.clear()
gui.title("Hunt 'em down ! !")
screen.moveto(130,180)
print("Last game points: "..points)
screen.moveto(130,210)
print("High score: "..high.." by "..person[1])
screen.moveto(130,130)
startbtn=gui.control{type="button",text="Start",width=100}
screen.moveto(250,270)
quitbtn=gui.button("Quit")
screen.moveto(80,80)
gui.label("Press Start to begin...")
points=0
end


initfile()
trunk()
while true do
e,v=gui.event()
if e==appStop then os.exit() end
if e==ctlSelect and v==startbtn then
    play()
    trunk()
    end
if e==ctlSelect and v==quitbtn then os.exit() end
end


*********************************************************************


-- ** Reflecting ball on screen **  
-- ** You can also draw a full circle, but buffering makes it slightly faster.**

screen.clear(0)
bufempty=buffer.get(0,0,10,10)
screen.disc(5,5,5,5,16777215)
buffull=buffer.get(0,0,10,10)

function makecircle()
buffer.put(bufempty,xo,yo)
buffer.put(buffull,x,y)
end

screen.clear(0)
x=50+math.random(130)
y=50+math.random(130)
repeat
xd=math.random(3)-1
until xd~=0
repeat
yd=math.random(3)-1
until yd~=0

while true do
xo,yo=x,y
x=x+xd
y=y+yd
if x>315 then xd=math.random(3)-3
   elseif x<5 then xd=math.random(3)
   elseif y>315 then yd=math.random(3)-3
   elseif y<5 then yd=math.random(3)
   end
if gui.event(0)==appStop then break end
makecircle()
end


*********************************************************************


-- ** Draw on the screen **

screen.clear(0)
white=screen.rgb(255,255,255)
while true do  
  ev,x,y = gui.event()
  if ev==penDown then xo=x yo=y end
  if ev ==penMove  then
  screen.line(xo,yo,x,y,white)
  xo=x yo=y
  elseif ev == appStop then os.exit()
  end  
end


*********************************************************************


-- **  Using sound.tone and slider **

function ex()
screen.clear()
screen.moveto(70,150)
print("Thanks for using me...")
os.sleep(1)
os.exit()
end

function act()
screen.font(7)
screen.moveto(10,280)
print("                                      ")
screen.moveto(10,280)
screen.color(screen.rgb(200,0,0),bckgrnd)
print("Now playing...")
sound.tone(freq,dur,vol)
screen.moveto(10,280)
print("                                      ")
screen.moveto(10,280)
screen.color(screen.rgb(0,160,0),bckgrnd)
print("Finished.")
end

function pr()
screen.font(0)
screen.color(0,bckgrnd)
screen.moveto(240,147)
print("        ")
screen.moveto(240,147)
print(freq-1)
screen.moveto(260,187)
print("        ")
screen.moveto(260,187)
print(math.floor(dur/100)/10)
screen.moveto(260,227)
print("        ")
screen.moveto(260,227)
print(math.floor(vol*1.5625))
end

bckgrnd=screen.rgb(190,190,250)
screen.clear(bckgrnd)
screen.moveto(10,30)
screen.color(screen.rgb(0,0,200),bckgrnd)
screen.font(7)
print("H      I      G      H      A      U      D      I      O")
screen.line(10,55,310,55)
screen.font(0)
screen.color(0,bckgrnd)
btnbeep=gui.control{type="button",text="S o u n d",x=150,y=80,width=80,height=40,font=1}
btnquit=gui.control{type="button",text="QUIT",x=270,y=280,width=36,height=20,font=3}
screen.moveto(40,220)
v=gui.slider(200,64)
screen.moveto(40,180)
d=gui.slider(200,10000)
screen.moveto(40,140)
f=gui.slider(200,9999)
screen.moveto(2,227) print("Vol")
screen.moveto(2,187) print("Time")
screen.moveto(2,147) print("Freq")
screen.moveto(294,227) print("%")
screen.moveto(294,187) print("sec")
screen.moveto(294,147) print("Hz")
freq,dur,vol=10001,0,1
pr()
gui.setfocus(btnbeep)

while true do
ev,id,value=gui.event()
if ev==appStop then ex() end
if ev==ctlSelect and id==f then freq=10000+value end
if ev==ctlSelect and id==d then dur=value end
if ev==ctlSelect and id==v then vol=value end
if ev==ctlSelect and id==btnbeep then act() end
if ev==ctlSelect and id==btnquit then ex() end
pr()
end


**********************************************************************


-- ** Classic "Life"  loop **
-- ** Ofcourse, having a 16x16 grid makes the loop faster.. **

function init()
screen.clear()
cell={}
cellnext={}
for i=1,32 do cell[i]={} end
for i=1,32 do cellnext[i]={} end
for i=1,250 do
cell[math.random(32)][math.random(32)]=1
end
end

function drawworld()
screen.clear()
for x=1,32 do
for y=1,32 do
if cell[x][y]==1 then screen.box((x-1)*10,(y-1)*10,8,8) end
end
end
end

function tick()
for x=1,32 do
for y=1,32 do
sum=0
if x>1 and y>1 and cell[x-1][y-1]==1 then sum=sum+1 end
if y>1 and cell[x][y-1]==1 then sum=sum+1 end
if x<32 and y>1 and cell[x+1][y-1]==1 then sum=sum+1 end
if x>1 and cell[x-1][y]==1 then sum=sum+1 end
if x<32 and cell[x+1][y]==1 then sum=sum+1 end
if x>1 and y<32 and cell[x-1][y+1]==1 then sum=sum+1 end
if y<32 and cell[x][y+1]==1 then sum=sum+1 end
if x<32 and y<32 and cell[x+1][y+1]==1 then sum=sum+1 end
if sum<2 then cellnext[x][y]=0 end
if sum>3 then cellnext[x][y]=0 end
if (sum==2 or sum==3) and cell[x][y]==1 then cellnext[x][y]=1 end
if sum==3 and cell[x][y]==0 then cellnext[x][y]=1 end
end
end
cell=cellnext
end

init()
while true do
if gui.event(0)==appStop then os.exit() end
drawworld()
tick()
end


**********************************************************************


-- ** Buffers can be used to show BMP
-- ** files, but not jpg's...

screen.clear()
screen.moveto(70,50)
screen.font(7)
print("This is it !")
id,height,width=buffer.read("vfs0:/PALM/PROGRAMS/Plua/SRC/SUZUKIGSR600A-K7.bmp")  -- for example, my bike's pic..
buffer.put(id,0,0,3)
screen.moveto(100,100)
screen.font(0)
g=gui.button("This") -- you can have gui elements on a foreground..
screen.line(0,0,320,320) --...or draw something on them..
gui.main()


**********************************************************************


--  **  scrolling text like in eReader etc..  **

function newline()
buffer.use(vbuf)
screen.moveto(math.random(180),320)
print(math.random())
end

vbuf=buffer.new(320,340)
newline()

while true do
buffer.use()
for i=1,22 do
buffer.put(vbuf,0,-i)
--os.sleep(0.02) -- set the scrolling speed
if gui.event(0)==appStop then os.exit() end
end
buffer.free(vbuf)
vbuf=buffer.get(0,0,320,340)
newline()
end


**********************************************************************

END OF FILES...

 « Return to Thread: You are welcome.. (and some more demo code)