Home ← TI-Nspire Authoring ← Lua Scripting HQ ← Lua and JavaScript
Todo: - Image support - Font size support - Physics support - Toolpallete support - Clipboard - D2Editor - coroutine (possibly not possible) - string functions - keyboard improvements
platform.apilevel = 2.0 platform.apilevel = 2.0 -- Cameron Harris @ ImperialMatrix.com local w local h local runningSim local timerCount local simOver local ratio5 local xPoints local yPoints local pointIndex local clockArc local displayTime local pointX local pointY local ratio local hummingbird local Birdy function init(gc) w = platform.window:width() h = platform.window:height() runningSim = 0 timerCount = 0 simOver = 0 ratio = (w/h)*5 xPoints = {} yPoints = {} pointIndex = 1 clockArc = 0 displayTime = 0 pointX = w*(10/64) pointY = h*(27/32) Birdy = Bird(w*(1/4),h*(1/4),0) timer.start(1/15) on.paint = paint paint(gc) end function on.timer() if timerCount == 60 then runningSim = 0 timerCount = 0 simOver = 1 clockArc = 0 end--end if to stop sim once 4 sec have elapsed if runningSim == 1 then if timerCount%15==0 then displayTime = displayTime + 1 end--if to control display clock pointY = Birdy.y + h*(2/32) pointX = pointX + ((w*(3/4))/60) timerCount = timerCount + 1 if pointX>w then pointX = 5 end--end if to reset block if clockArc == 360 then clockArc = 0 end--end if to reset clock arc clockArc =clockArc + (360/16) end--emd if to look for runningSim platform.window:invalidate() end --==================== BIRD CODE =================== Bird = class() function Bird:init(x,y,width,height,clicked) self.x = w*(1/16) self.y = h*(27/32) self.clicked = 0 self.width = 3*ratio self.height = 3*ratio pointY = self.y + h*(2/32) pointX = pointX + ((w*(3/4))/60) end--end Bird class function Bird:contains(x, y) local sw = self.width local sh = self.height return x >= self.x - sw and x <= self.x + sw and y >= self.y - sh and y <= self.y + sh end--end Bird:contains --=================== END BIRD CODE ========================= --=================== TIMER CODE =========================== function on.enterKey() if simOver == 1 then xPoints = {} yPoints = {} pointIndex = 1 simOver = 0 pointX = w*(10/64) pointY = Birdy.y + h*(3/32) clockArc = 0 displayTime = 0 end--end if simOver, resets simulation runningSim = 1 end-- end on.enterKey() function on.escapeKey() on.paint = init end --=============== END TIMER CODE ========================= function on.mouseDown(x,y) if runningSim == 0 then xPoints = {} yPoints = {} pointIndex = 1 simOver = 0 pointX = w*(10/64) pointY = Birdy.y + h*(3/32) clockArc = 0 displayTime = 0 end--end if to reset variables on click afer sim over if Birdy:contains(x,y) then Birdy.clicked = 1 runningSim = 1 end--end if to listen for clicks on bird end-- end on.mouseDown(x,y) function on.mouseUp(x,y) Birdy.clicked = 0 end--end on.mouseUp(x,y) function on.mouseMove(x,y) if Birdy.clicked == 1 then if yh*(1/64) then Birdy.y = y end-- end if to assure valid x,y end-- end if to move bird with mouse on y axis end--end on.mouseMove(x,y) function paint(gc) w = platform.window:width() h = platform.window:height() --=========== GRAPH DISPLAY =============== gc:drawLine(w*(9/64),0,w*(9/64),h)-- screen divider gc:drawRect(w*(1/16),h*(30/32),w*(4/64),h*(2/64)) -- radar box gc:drawLine(w*(1/64),h*(2/64),w*(1/64),h*(30/32))--bird y-axis yAxis = h*(30/32) gc:setFont("sansserif", "r", 6) for k = 0,12,2 do gc:drawLine(w*(1/64),yAxis,w*(2/64),yAxis) gc:drawString(k,w*(2/64),yAxis,bottom) yAxis = yAxis - (h*(1/7)) end--end for loop to print bird yaxis ticks gc:drawLine(w*(10/64),h*(2/64),w*(10/64),h*(30/32))--graph y-axis yAxis = h*(30/32) for k = 0,12,2 do gc:drawLine(w*(10/64),yAxis,w*(11/64),yAxis) gc:drawString(k,w*(11/64),yAxis,bottom) yAxis = yAxis - (h*(1/7)) end--end for loop to print graph yaxis ticks gc:drawLine(w*(11/64),h*(30/32),w*(15/16),h*(30/32)) -- horizontal tick marks xAxis = w*(10/64) for k = 1,4,1 do xAxis = xAxis + (w*(25/128)) gc:drawLine(xAxis,h*(30/32),xAxis,h*(29/32)) gc:drawString(k,xAxis-2,h*(32/32)) end--end for to print horizontal ticka --=========== END GRAPH DISPLAY =========== --=========== TIMR DIDPLAY ================ if(ratio*7)>6 then gc:setFont("sansserif", "r", ratio*7) else gc:setFont("sansserif", "r", 6) end gc:drawArc(w*(110/128),h*(3/128),w*(1/9),h*(3/17),0,clockArc) gc:drawString(displayTime,w*(113/128),h*(26/128)) if simOver == 1 then gc:drawArc(w*(110/128),h*(3/128),w*(1/9),h*(3/17),0,360) gc:setFont("sansserif", "r", 6) gc:drawString("Click",w*(112/128),h*(34/128)) gc:drawString("to",w*(116/128),h*(40/128)) gc:drawString("Reset",w*(112/128),h*(46/128)) gc:drawArc(w*(110/128),h*(25/128),w*(1/9),h*(3/17),0,360) end --=========== END TIME DISPLAY ============= -- gc:drawImage(hummingBird,Birdy.x,Birdy.y) gc:setColorRGB(100, 200, 230) gc:fillArc(Birdy.x,Birdy.y, Birdy.width, Birdy.height, 0, 360) xPoints[pointIndex] = pointX yPoints[pointIndex] = pointY pointIndex = pointIndex + 1 if runningSim == 1 or simOver == 1 then for k=1,pointIndex-1,1 do tempX = xPoints[k] tempY = yPoints[k] if k<=pointIndex then gc:drawRect(tempX,tempY,2,2) end--end if end--end for to print points end--end if to display only points generated during sim --tempX = xPoints[1] -- tempY = yPoints[1] --gc:drawRect(tempX,tempY,2,2) gc:setFont("sansserif", "r", 12) gc:setColorRGB(0, 0, 0) local str = "Grab the blue 'bird' and drag it up and down" local sw = gc:getStringWidth(str) gc:drawString(str, w/2 - sw/2, h*0.1, "middle") end function on.resize() on.paint = init end