Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --============== Lights 1.0 ==============
- --== copyright (c) 2014 InputUsername ==
- --==========================================</fancy header>
- --=================================
- --== INITIALIZATION
- --=================================
- local w,h=term.getSize()
- if w<26 or h<14 then
- w,h=nil,nil
- print("Sorry, your display is too small.\nLights requires a display of at least 26x14 to work.")
- return
- end
- if not term.isColor then
- print("Sorry, your ComputerCraft version is not supported.\nPlease update to a recent version.")
- return
- elseif not term.isColor() then
- print("Sorry, Lights is currently supported on advanced computers only.")
- return
- end
- --=================================
- --== SOME OOP-esque STUFF
- --=================================
- --new light at (x,y) with size (w,h), state (s) and colors (cOn,cOff)
- local function newLight(x,y,w,h,s,cOn,cOff)
- return {
- x=x,
- y=y,
- w=w,
- h=h,
- s=s,
- cOn=cOn,
- cOff=cOff,
- draw=function(self)
- term.setBackgroundColor(self.s==1 and self.cOn or self.cOff)
- local b=(" "):rep(self.w)
- for ypos=self.y,self.y+self.h-1 do
- term.setCursorPos(self.x,ypos)
- term.write(b)
- end
- end,
- toggle = function(self)
- if self.s==1 then self.s=0 else self.s=1 end
- self:draw()
- end,
- isclicked = function(self,xp,yp)
- return(xp>=self.x)and(xp<=self.x+self.w-1)and(yp>=self.y)and(yp<=self.y+self.h-1)
- end
- }
- end
- --new button at (x,y) with text (s) and colors (cB,cT)
- local function newButton(x,y,s,cB,cT)
- return {
- x=x,
- y=y,
- s=s,
- cB=cB,
- cT=cT,
- draw=function(self)
- term.setBackgroundColor(self.cB)
- term.setTextColor(self.cT)
- term.setCursorPos(self.x,self.y)
- term.write(self.s)
- end,
- isclicked=function(self,xp,yp)
- return(xp>=self.x)and(xp<=self.x+#self.s-1)and(yp==self.y)
- end
- }
- end
- --=================================
- --== VARIABLES
- --=================================
- local colorScheme={
- back=colors.black,
- text=colors.white,
- title=colors.green,
- menuB=colors.green,
- menuT=colors.white,
- on=colors.green,
- off=colors.gray,
- }
- local options = {
- defaultGameLights=6,
- patternGameLights=7
- }
- --either 'mainMenu', 'optionsMenu', 'aboutMenu', 'defaultGame', 'patternGame' or 'exit'
- local state="mainMenu"
- local tw,th=term.getSize()
- local cx,cy=math.floor(tw/2),math.floor(th/2)
- --=================================
- --== FUNCTIONS
- --=================================
- local function saveOptions()
- local h=fs.open("/.lightsdata","w")
- local s=textutils.serialize(options)
- if type(s)=="string" then
- h.write(s)
- end
- h.close()
- end
- local function loadOptions()
- if not fs.exists("/.lightsdata") then
- saveOptions()
- end
- local h=fs.open("/.lightsdata","r")
- local c=h.readAll()
- c=textutils.unserialize(c)
- if type(c)=="table" then
- options=c
- end
- h.close()
- end
- local function clear(x,y,b,t)
- term.setCursorPos(x,y)
- if b then term.setBackgroundColor(b) end
- if t then term.setTextColor(t) end
- term.clear()
- end
- local function initialize()
- clear(1,1,colorScheme.back,colorScheme.text)
- end
- local function reset()
- clear(1,1,colors.black,colors.white)
- end
- local function mainMenu()
- clear(cx-3,cy-6,colorScheme.back,colorScheme.title)
- term.write("Lights")
- local items={" Regular game "," Pattern game "," Options "," About "}
- local buttons={}
- for i=1,#items do
- buttons[i]=newButton(cx-6,cy-4+2*(i-1),items[i],colorScheme.menuB,colorScheme.menuT)
- buttons[i]:draw()
- end
- --buttons[#buttons+1]=newButton(cx-6,cy+5," Exit ",colorScheme.menuB,colorScheme.menuT)
- --this button will crash Sapphari, we cannot allow the user to terminate the program.
- buttons[#buttons]:draw()
- while true do
- local e,_,x,y=sapphUtils.pullEvent"mouse_click"
- for i=1,#buttons do
- if buttons[i]:isclicked(x,y) then
- if i==1 then
- state="defaultGame"
- return
- elseif i==2 then
- state="patternGame"
- return
- elseif i==3 then
- state="optionsMenu"
- return
- elseif i==4 then
- state="aboutMenu"
- return
- elseif i==5 then
- state="exit"
- return
- end
- end
- end
- end
- end
- local function optionsMenu()
- clear(cx-3,cy-6,colorScheme.back,colorScheme.title)
- term.write("Options")
- term.setTextColor(colorScheme.text)
- term.setCursorPos(cx-10,cy-3)
- term.write("Regular game lights:")
- term.setCursorPos(cx-10,cy-1)
- term.write("Pattern game lights:")
- local items={}
- local buttons={}
- buttons[1]=newButton(cx+11,cy-3," "..options.defaultGameLights.." ",colorScheme.menuB,colorScheme.menuT)
- buttons[2]=newButton(cx+11,cy-1," "..options.patternGameLights.." ",colorScheme.menuB,colorScheme.menuT)
- buttons[3]=newButton(cx-6,cy+5," Back ",colorScheme.menuB,colorScheme.menuT)
- for i=1,#buttons do
- buttons[i]:draw()
- end
- while true do
- local e,_,x,y=sapphUtils.pullEvent"mouse_click"
- for i=1,#buttons do
- if buttons[i]:isclicked(x,y) then
- if i==1 then
- if options.defaultGameLights<9 then
- options.defaultGameLights=options.defaultGameLights+1
- else
- options.defaultGameLights=3
- end
- buttons[i].s=" "..options.defaultGameLights.." "
- buttons[i]:draw()
- elseif i==2 then
- if options.patternGameLights<9 then
- options.patternGameLights=options.patternGameLights+1
- else
- options.patternGameLights=3
- end
- buttons[i].s=" "..options.patternGameLights.." "
- buttons[i]:draw()
- elseif i==3 then
- state="mainMenu"
- saveOptions()
- return
- end
- end
- end
- end
- end
- local function aboutMenu()
- clear(cx-3,cy-6,colorScheme.back,colorScheme.title)
- term.write("About")
- local function cwrite(y,s)
- term.setCursorPos(cx-math.floor(#s/2),y)
- term.write(s)
- end
- term.setTextColor(colorScheme.text)
- cwrite(cy-4,"In Regular game mode, the goal is")
- cwrite(cy-3,"to turn every light green.")
- cwrite(cy-2,"In Pattern game mode, you must")
- cwrite(cy-1,"copy a certain pattern to win.")
- cwrite(cy+1,"You can exit with the 'give up' button.")
- cwrite(cy+3,"Copyright (c) 2014 InputUsername")
- cwrite(cy+6, " Ported by Hithere, ALL CREDITS TO ORIGINAL AUTHOR")
- local back=newButton(cx-6,cy+5," Back ",colorScheme.menuB,colorScheme.menuT)
- back:draw()
- while true do
- local e,_,x,y=sapphUtils.pullEvent"mouse_click"
- if back:isclicked(x,y) then
- state="mainMenu"
- return
- end
- end
- end
- local function defaultGame()
- clear(cx-6,cy-6,colorScheme.back,colorScheme.title)
- term.write("Regular game")
- local lights={}
- for i=1,options.defaultGameLights do
- lights[i]=newLight(cx-math.ceil(options.defaultGameLights*2.5)+5*(i-1),cy-4,4,3,math.floor(math.random(0,1)),colorScheme.on,colorScheme.off)
- lights[i]:draw()
- end
- local giveup=newButton(2,3," Give up ",colorScheme.menuB,colorScheme.menuT)
- giveup:draw()
- local function check()
- for i=1,#lights do
- if lights[i].s==0 then return false end
- end
- return true
- end
- while true do
- local e,_,x,y = sapphUtils.pullEvent"mouse_click"
- for i=1,#lights do
- if lights[i]:isclicked(x,y) then
- if lights[i-1] then lights[i-1]:toggle() end
- if lights[i+1] then lights[i+1]:toggle() end
- lights[i]:toggle()
- break
- end
- end
- if giveup:isclicked(x,y) then
- clear(cx-2,cy,colorScheme.back,colorScheme.text)
- term.write("D'oh! :(")
- sleep(0.75)
- state="mainMenu"
- return
- end
- if check() then
- sleep(.2)
- clear(cx-6,cy,colorScheme.back,colorScheme.text)
- term.write("Congratulations!")
- sleep(1)
- state="mainMenu"
- return
- end
- end
- end
- local function patternGame()
- clear(cx-6,cy-6,colorScheme.back,colorScheme.title)
- term.write("Pattern game")
- term.setCursorPos(cx-9,cy+1)
- term.write("Copy this pattern:")
- local lights={}
- for i=1,options.patternGameLights do
- lights[i]=newLight(cx-math.ceil(options.patternGameLights*5/2)+5*(i-1),cy-4,4,3,math.floor(math.random(0,1)),colorScheme.on,colorScheme.off)
- lights[i]:draw()
- end
- local giveup=newButton(2,3," Give up ",colorScheme.menuB,colorScheme.menuT)
- giveup:draw()
- local pattern={}
- for i=1,options.patternGameLights do
- pattern[i]=math.floor(math.random(0,1))
- term.setBackgroundColor(pattern[i]==1 and colorScheme.on or colorScheme.off)
- term.setCursorPos(cx-options.patternGameLights+2*(i-1),cy+3)
- term.write(" ")
- end
- local function check()
- for i=1,#lights do
- if lights[i].s~=pattern[i] then return false end
- end
- return true
- end
- while check() do
- for i=1,options.patternGameLights do
- pattern[i]=math.floor(math.random(0,1))
- term.setBackgroundColor(pattern[i]==1 and colorScheme.on or colorScheme.off)
- term.setCursorPos(cx-options.patternGameLights+2*(i-1),cy+3)
- term.write(" ")
- end
- end
- while true do
- local e,_,x,y = sapphUtils.pullEvent"mouse_click"
- for i=1,#lights do
- if lights[i]:isclicked(x,y) then
- if lights[i-1] then lights[i-1]:toggle() end
- if lights[i+1] then lights[i+1]:toggle() end
- lights[i]:toggle()
- break
- end
- end
- if giveup:isclicked(x,y) then
- clear(cx-2,cy,colorScheme.back,colorScheme.text)
- term.write("D'oh! :(")
- sleep(0.75)
- state="mainMenu"
- return
- end
- if check() then
- sleep(.2)
- clear(cx-6,cy,colorScheme.back,colorScheme.text)
- term.write("Congratulations!")
- sleep(1)
- state="mainMenu"
- return
- end
- end
- end
- --=================================
- --== GAME
- --=================================
- initialize()
- loadOptions()
- while true do
- if state=="mainMenu" then
- mainMenu()
- elseif state=="optionsMenu" then
- optionsMenu()
- elseif state=="aboutMenu" then
- aboutMenu()
- elseif state=="defaultGame" then
- defaultGame()
- elseif state=="patternGame" then
- patternGame()
- elseif state=="exit" then
- break
- end
- end
- reset()
- saveOptions()
Advertisement
Add Comment
Please, Sign In to add comment