Author Topic: How can l make it ?  (Read 4384 times)

0 Members and 1 Guest are viewing this topic.

Offline cypressx1

  • LV3 Member (Next: 100)
  • ***
  • Posts: 45
  • Rating: +0/-0
    • View Profile
How can l make it ?
« on: July 22, 2011, 07:11:59 am »
Code:(lua)
Code: [Select]
----------block1-------------
block1=class()
bw,bh=80,70
cc=20 ff=25     t1="start" t2="stop"
tp=0.02
st=1
n=1/2.5

-----st must =ff or ff≥st+1------
function block1:init()
end
function block1:set()
wbx,wby=platform.window:width(),platform.window:height()
bx=wbx/2-bw/2
by=wby/2-bh/2
end

function block1:setc1()
block1:set()
c1x,c1y,c1w,c1h=bx,by+bh/2-cc/2,bw,cc
end
function block1:setc2()
block1:set()
c2x,c2y,c2w,c2h=bx+bw/2-cc/2,by,cc,bh
end

function block1:paint(gc)
gc:setColorRGB(150,200,100)
block1:set()
gc:fillRect(bx,by,bw,bh)
-------c1------
gc:setColorRGB(0,0,0)
block1:setc1()
gc:fillRect(c1x,c1y,c1w,c1h)
-----c2-------
gc:setColorRGB(0,0,0)
block1:setc2()
gc:fillRect(c2x,c2y,c2w,c2h)
end
function block1:timer()
platform.window:invalidate()
end
--------mouse------
Mouse=class()
mx=0
my=0
function Mouse:init()
end
function Mouse:paint(gc)
gc:setFont("sansserif","i",10)
gc:drawString("MouseLocation : ( "..mx.." , "..my.." )",60,0,"top")
end
function on.mouseMove(x,y)
mx=x
my=y
end
function Mouse:send()
--Ox=mx
end
function Mouse:timer()
platform.window:invalidate()
end
--------------------------
--[[
function ST()
if B1x<140 or B1x>171
then st=1
elseif B1x>140 and B1x<171
then st=1
end end
--]]
-----------Steps----------
function step1()
  if B1x==c2x-ff  --and B2y==c1y
   then t1="start"
end
if B1x==c2x+ff
   then t1="stop"
return step2()
   end
--ST()
B1x=B1x+st
 B2y=B1y-(ff^2-(B1x-B2x)^2)^0.5
 --B2y=math.eval("round("..B2y..",0)")
end
 
function step2()
  if B1x==c2x+ff --and B2y==c1y
   then t2="start"
end
if B1x==c2x-ff
   then t2="stop" B1x=c2x-ff
--timer.stop()
return step1() 
 end
--ST()
B1x=B1x-st
 B2y=B1y+(ff^2-(B1x-B2x)^2)^0.5
--B2y=math.eval("round("..B2y..",0)")
end






B1=class()
block1:setc1()
 block1:setc2()
B1x,B1y,B2x,B2y=c2x-ff,c1y,c2x,c1y
function B1:paint(gc)
step1()
gc:fillArc(B1x,B1y,cc,cc,0,360)
gc:fillArc(B2x,B2y,cc,cc,0,360)
end
B2=class()
block1:setc1()
 block1:setc2()
function B2:paint(gc)
step2()
gc:fillArc(B1x,B1y,cc,cc,0,360)
gc:fillArc(B2x,B2y,cc,cc,0,360)
end



-----------------------
function on.paint(gc)
block1():paint(gc)
Mouse():paint(gc)
gc:setColorRGB(255,255,255)
if t1=="start" then
B1():paint(gc)
end
if t2=="start" then
B2():paint(gc)
end
gc:setColorRGB(0,0,0)
gc:setFont("sansserif","i",10)
gc:drawString(t1.." "..t2,0,0,"top")
gc:drawString("( "..B1x.." , "..B1y.." )",100,199)
gc:drawString(" ( "..B2x.." , "..B2y.." )",100,212)
d=((B1x-B2x)^2+(B1y-B2y)^2)^0.5
d=math.eval("iPart("..d..")")
gc:drawString(d..st,100,180)
-------o-------
--Mouse:send()
--l=math.eval("linsolve("..B1y.."=k*"..B1x.."+b and "..B2y.." =k*"..B2x.."+b,{k,b})")
Ox=(B2x*(1+n)-B1x)/n
Oy=(B2y*(1+n)-B1y)/n
gc:fillArc(Ox,Oy,cc,cc,0,360)
end


function on.timer()
timer.start(tp)
block1():timer()
Mouse():timer()
platform.window:invalidate()
end
on.timer()

l'm leaning the lua. l want to make a prgm to draw the oval. But l don't know how to draw the ball's locus on the screen.in addition, l also want to make the vertical ball move more smoothly? Please give me some ideas :)

Offline cypressx1

  • LV3 Member (Next: 100)
  • ***
  • Posts: 45
  • Rating: +0/-0
    • View Profile
Re: How can l make it ?
« Reply #1 on: July 23, 2011, 08:37:19 am »
No one ?

Offline calcdude84se

  • Needs Motivation
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2272
  • Rating: +78/-13
  • Wondering where their free time went...
    • View Profile
Re: How can l make it ?
« Reply #2 on: July 23, 2011, 11:35:39 am »
It's only been one day since your post. Give everyone a bit of time ;)
I myself do not know Lua, especially not w/r/t the Nspire, otherwise I would help :/
"People think computers will keep them from making mistakes. They're wrong. With computers you make mistakes faster."
-Adam Osborne
Spoiler For "PartesOS links":
I'll put it online when it does something.

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: How can l make it ?
« Reply #3 on: July 23, 2011, 02:07:17 pm »
Hi, I'm still looking at your code, because its so big and I can't test it right now.
But here already a suggestio:
Use Lua native functions instead of calling them through math.eval() (if they are available). This will improve the speed.

I'm still looking at your code, and will post an update as soon as I test it :)

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: How can l make it ?
« Reply #4 on: July 24, 2011, 09:24:26 am »
Quote
l'm leaning the lua. l want to make a prgm to draw the oval.

I may be wrong, but can't that be done with ellipse?

Offline cypressx1

  • LV3 Member (Next: 100)
  • ***
  • Posts: 45
  • Rating: +0/-0
    • View Profile
Re: How can l make it ?
« Reply #5 on: July 26, 2011, 08:08:52 am »
ok,thanks! by the way, l have another question, can we use two different timer.start() at one time? for example,  l start the  timer with timer.start(XX)  "XX will change", and l also want the clock function which l programmed work well ?

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: How can l make it ?
« Reply #6 on: July 26, 2011, 03:30:21 pm »
timer.start will change the time (invalidate the other one) between the ticks. The best would be to make it go faster and check yourself if its time to do something.