platform.apilevel = '1.0' -- Author : Jim Bauwens -- More TI Downloads on http://tiplanet.org -------------------------------------------------- -- Jim Bauwens implentation of a Screen manager -- -- Original Idea from Levak, so many credits to -- -- him! -- -------------------------------------------------- Screen = class() Screens = {} function push_screen(screen) table.insert(Screens, screen) end function remove_screen(screen) return table.remove(Screens) end function current_screen() return Screens[#Screens] end function Screen:init() end function Screen.paint(gc) end function Screen.timer() end function Screen.arrowUp() end function Screen.arrowDown() end function Screen.arrowLeft() end function Screen.arrowRight()end function Screen.enterKey() end function Screen.escapeKey() end function Screen.tabKey() end function Screen.charIn(char) end function Screen.mouseMove(x,y) end function Screen.mouseUp(x,y) end function Screen.mouseDown(x,y) end function Screen.backspaceKey() end function on.paint(gc) for _, screen in pairs(Screens) do screen.paint(gc) end end function on.timer() current_screen().timer() end function on.arrowUp() current_screen().arrowUp() end function on.arrowDown() current_screen().arrowDown() end function on.arrowLeft() current_screen().arrowLeft() end function on.arrowRight()current_screen().arrowRight() end function on.enterKey() current_screen().enterKey() end function on.escapeKey() current_screen().escapeKey() end function on.tabKey() current_screen().tabKey() end function on.charIn(ch) current_screen().charIn(ch) end function on.backspaceKey() current_screen().backspaceKey() end function on.mouseMove(x, y) current_screen().mouseMove(x,y) end function on.mouseUp(x, y) current_screen().mouseUp(x, y) end function on.mouseDown(x, y) current_screen().mouseDown(x, y) end function getScreenSize() local x = platform.window:width() local y = platform.window:height() return x,y end menu = Screen() menu.index = 1 menu.sy = 0 menu.dy = 0 function menu.paint(gc) timer.start(0.01) local sw, sh = getScreenSize() gc:setColorRGB(100,255,100) gc:fillRect(sw/2-100, 20, 200, 180) gc:setColorRGB(0,0,0) gc:drawRect(sw/2-100, 20, 200, 180) title = "Leaping Kangaroos" text1 = "Two Pegs" text2 = "Four Pegs" text3 = "Six Pegs" text4 = "Eight Pegs" gc:setFont("sansserif", "b", 11) gc:drawString(title, sw/2-gc:getStringWidth(title)/2, 40, "top") gc:drawRect(sw/2-50, 82 + menu.index*30-30, 100, 20) gc:setFont("sansserif", "r", 11) gc:drawString(text1, sw/2-gc:getStringWidth(text1)/2, 80, "top") gc:drawString(text2, sw/2-gc:getStringWidth(text2)/2, 110, "top") gc:drawString(text3, sw/2-gc:getStringWidth(text3)/2, 140, "top") gc:drawString(text4, sw/2-gc:getStringWidth(text4)/2, 170, "top") end function menu.charIn(ch) if ch=="2" or ch=="4" or ch=="6" or ch=="8" then Screens={game} game.newGame(tonumber(ch)) platform.window:invalidate() end end function menu.arrowUp() if menu.index>1 then menu.index = menu.index - 1 platform.window:invalidate() end end function menu.arrowDown() if menu.index<4 then menu.index = menu.index + 1 platform.window:invalidate() end end function menu.enterKey() Screens={game} game.newGame(menu.index*2) platform.window:invalidate() end game = Screen() function game.newGame(n) game.steps = 0 game.genPegs(n) end --game.pegs = {1,1,1,1,1,1,1,1,0,2,2,2,2,2,2,2,2} --game.pegs = {1,1,0,2,2} --game.pegs = {1,0,2} function game.genPegs(n) game.pegs={} for i=1, n/2 do table.insert(game.pegs, 1) end table.insert(game.pegs, 0) for i=1, n/2 do table.insert(game.pegs, 2) end end function game.checkMove() for i, p in pairs(game.pegs) do if p==1 and (game.pegs[i+1]==0 or (game.pegs[i+1]~=0 and game.pegs[i+2]==0)) then return true elseif p==2 and (game.pegs[i-1]==0 or (game.pegs[i-1]~=0 and game.pegs[i-2]==0)) then return true end end return false end function game.checkWin() for i, p in pairs(game.pegs) do if 2-math.floor((i/#game.pegs)+0.5)~=p and i~=(#game.pegs-1)/2+1 then return false end end return true end function game.peg(gc, x, y, w, h, color) local r,g,b = unpack(color) gc:setColorRGB(r,g,b) gc:fillRect(x,y+w/4,w,h) gc:setColorRGB(0, 0, 0) gc:drawRect(x,y+w/4,w,h) gc:setColorRGB(r,g,b) gc:fillArc(x+1, y+h, w-1, w/2, 0, 360) gc:setColorRGB(0, 0, 0) gc:drawArc(x, y+h, w, w/2, 0, -180) gc:setColorRGB(r,g,b) gc:fillArc(x, y, w, w/2, 0, 360) gc:setColorRGB(0, 0, 0) gc:drawArc(x, y, w, w/2, 0, 360) end function game.paint(gc) local sw, sy = getScreenSize() local pd = (sw - 20)/math.max(#game.pegs,5) local pw = pd/1.5 local ph = pw*2 local bx = 20 local bh = (ph-5)/2 local bw = 3*pw/2 local ey = sy-ph*2 local by = ey+(ph-10+pw/4)-bw/2 local bl = sw-bx game.ey = ey game.pd = pd game.ph = ph game.pw = pw gc:drawString("Steps: " .. tostring(game.steps), 2, 2, "top") gc:setColorRGB(100,255,100) gc:fillPolygon({bx, by, bx+bl, by, bl, by+bw, 0, by+bw, bx, by}) gc:fillRect(0, by+bw, bl, bh) gc:fillPolygon({bx+bl, by, bx+bl, by+bh, bl, by+bw+bh, bl, by+bw, bx+bl, by}) gc:setColorRGB(0,0,0) gc:drawPolyLine({bx, by, bx+bl, by, bl, by+bw, 0, by+bw, bx, by}) gc:drawRect(0, by+bw, bl, bh) gc:drawPolyLine({bx+bl, by, bx+bl, by+bh, bl, by+bw+bh, bl, by+bw, bx+bl, by}) for p, peg in pairs(game.pegs) do if peg~=0 then local color = peg==1 and {255,100,100} or {100,100,255} game.peg(gc, 10 + (p*pd - pd) + pd/2 -pw/2, ey, pw, ph-10, color) else gc:setColorRGB(100,100,100) gc:fillArc(10 + (p*pd - pd) + pd/2 -pw/2, ey+ph-10, pw, pw/2, 0, 360) gc:setColorRGB(0, 0, 0) gc:drawArc(10 + (p*pd - pd) + pd/2 -pw/2, ey+ph-10, pw, pw/2, 0, 360) end end if game.selected then local color = game.selected.peg==1 and {255,100,100} or {100,100,255} game.peg(gc, game.selected.x, game.selected.y, pw, ph, color) end end function game.mouseUp(x, y) local pos = math.ceil((x-10)/game.pd) if game.selected then local ppos = game.selected.pos if game.pegs[pos]==0 and ((pos==ppos+1 or pos==ppos-1) or ((pos==ppos+2 and game.pegs[ppos+1]~=0) or (pos==ppos-2 and game.pegs[ppos-1]~=0)) or pos==ppos) and ((game.selected.peg==1 and pos>=ppos) or (game.selected.peg==2 and pos<=ppos)) then if ppos~=pos then game.steps = game.steps + 1 end game.pegs[pos]=game.selected.peg game.selected=nil if not game.checkMove() then if game.checkWin() then push_screen(gwin) else push_screen(gover) end end platform.window:invalidate() end elseif y>game.ey and y