SHOW:
|
|
- or go back to the newest paste.
| 1 | --============== Lights 1.0 ============== | |
| 2 | --== copyright (c) 2014 InputUsername == | |
| 3 | --==========================================</fancy header> | |
| 4 | ||
| 5 | --================================= | |
| 6 | --== INITIALIZATION | |
| 7 | --================================= | |
| 8 | local w,h=term.getSize() | |
| 9 | if w<26 or h<14 then | |
| 10 | w,h=nil,nil | |
| 11 | print("Sorry, your display is too small.\nLights requires a display of at least 26x14 to work.")
| |
| 12 | return | |
| 13 | end | |
| 14 | if not term.isColor then | |
| 15 | print("Sorry, your ComputerCraft version is not supported.\nPlease update to a recent version.")
| |
| 16 | return | |
| 17 | elseif not term.isColor() then | |
| 18 | print("Sorry, Lights is currently supported on advanced computers only.")
| |
| 19 | return | |
| 20 | end | |
| 21 | ||
| 22 | --================================= | |
| 23 | --== SOME OOP-esque STUFF | |
| 24 | --================================= | |
| 25 | --new light at (x,y) with size (w,h), state (s) and colors (cOn,cOff) | |
| 26 | local function newLight(x,y,w,h,s,cOn,cOff) | |
| 27 | return {
| |
| 28 | x=x, | |
| 29 | y=y, | |
| 30 | w=w, | |
| 31 | h=h, | |
| 32 | s=s, | |
| 33 | cOn=cOn, | |
| 34 | cOff=cOff, | |
| 35 | draw=function(self) | |
| 36 | term.setBackgroundColor(self.s==1 and self.cOn or self.cOff) | |
| 37 | local b=(" "):rep(self.w)
| |
| 38 | for ypos=self.y,self.y+self.h-1 do | |
| 39 | term.setCursorPos(self.x,ypos) | |
| 40 | term.write(b) | |
| 41 | end | |
| 42 | end, | |
| 43 | toggle = function(self) | |
| 44 | if self.s==1 then self.s=0 else self.s=1 end | |
| 45 | self:draw() | |
| 46 | end, | |
| 47 | isclicked = function(self,xp,yp) | |
| 48 | return(xp>=self.x)and(xp<=self.x+self.w-1)and(yp>=self.y)and(yp<=self.y+self.h-1) | |
| 49 | end | |
| 50 | } | |
| 51 | end | |
| 52 | ||
| 53 | --new button at (x,y) with text (s) and colors (cB,cT) | |
| 54 | local function newButton(x,y,s,cB,cT) | |
| 55 | return {
| |
| 56 | x=x, | |
| 57 | y=y, | |
| 58 | s=s, | |
| 59 | cB=cB, | |
| 60 | cT=cT, | |
| 61 | draw=function(self) | |
| 62 | term.setBackgroundColor(self.cB) | |
| 63 | term.setTextColor(self.cT) | |
| 64 | term.setCursorPos(self.x,self.y) | |
| 65 | term.write(self.s) | |
| 66 | end, | |
| 67 | isclicked=function(self,xp,yp) | |
| 68 | return(xp>=self.x)and(xp<=self.x+#self.s-1)and(yp==self.y) | |
| 69 | end | |
| 70 | } | |
| 71 | end | |
| 72 | ||
| 73 | --================================= | |
| 74 | --== VARIABLES | |
| 75 | --================================= | |
| 76 | local colorScheme={
| |
| 77 | back=colors.black, | |
| 78 | text=colors.white, | |
| 79 | title=colors.green, | |
| 80 | ||
| 81 | menuB=colors.green, | |
| 82 | menuT=colors.white, | |
| 83 | ||
| 84 | on=colors.green, | |
| 85 | off=colors.gray, | |
| 86 | } | |
| 87 | ||
| 88 | local options = {
| |
| 89 | defaultGameLights=6, | |
| 90 | patternGameLights=7 | |
| 91 | } | |
| 92 | ||
| 93 | --either 'mainMenu', 'optionsMenu', 'aboutMenu', 'defaultGame', 'patternGame' or 'exit' | |
| 94 | local state="mainMenu" | |
| 95 | ||
| 96 | local tw,th=term.getSize() | |
| 97 | local cx,cy=math.floor(tw/2),math.floor(th/2) | |
| 98 | ||
| 99 | --================================= | |
| 100 | --== FUNCTIONS | |
| 101 | --================================= | |
| 102 | local function saveOptions() | |
| 103 | local h=fs.open("/.lightsdata","w")
| |
| 104 | local s=textutils.serialize(options) | |
| 105 | if type(s)=="string" then | |
| 106 | h.write(s) | |
| 107 | end | |
| 108 | h.close() | |
| 109 | end | |
| 110 | ||
| 111 | local function loadOptions() | |
| 112 | if not fs.exists("/.lightsdata") then
| |
| 113 | saveOptions() | |
| 114 | end | |
| 115 | local h=fs.open("/.lightsdata","r")
| |
| 116 | local c=h.readAll() | |
| 117 | c=textutils.unserialize(c) | |
| 118 | if type(c)=="table" then | |
| 119 | options=c | |
| 120 | end | |
| 121 | h.close() | |
| 122 | end | |
| 123 | ||
| 124 | local function clear(x,y,b,t) | |
| 125 | term.setCursorPos(x,y) | |
| 126 | if b then term.setBackgroundColor(b) end | |
| 127 | if t then term.setTextColor(t) end | |
| 128 | term.clear() | |
| 129 | end | |
| 130 | ||
| 131 | local function initialize() | |
| 132 | clear(1,1,colorScheme.back,colorScheme.text) | |
| 133 | end | |
| 134 | ||
| 135 | local function reset() | |
| 136 | clear(1,1,colors.black,colors.white) | |
| 137 | end | |
| 138 | ||
| 139 | local function mainMenu() | |
| 140 | clear(cx-3,cy-6,colorScheme.back,colorScheme.title) | |
| 141 | term.write("Lights")
| |
| 142 | ||
| 143 | local items={" Regular game "," Pattern game "," Options "," About "}
| |
| 144 | local buttons={}
| |
| 145 | ||
| 146 | for i=1,#items do | |
| 147 | buttons[i]=newButton(cx-6,cy-4+2*(i-1),items[i],colorScheme.menuB,colorScheme.menuT) | |
| 148 | buttons[i]:draw() | |
| 149 | end | |
| 150 | - | buttons[#buttons+1]=newButton(cx-6,cy+5," Exit ",colorScheme.menuB,colorScheme.menuT) |
| 150 | + | --buttons[#buttons+1]=newButton(cx-6,cy+5," Exit ",colorScheme.menuB,colorScheme.menuT) |
| 151 | --this button will crash Sapphari, we cannot allow the user to terminate the program. | |
| 152 | buttons[#buttons]:draw() | |
| 153 | ||
| 154 | - | local e,_,x,y=os.pullEvent"mouse_click" |
| 154 | + | |
| 155 | local e,_,x,y=sapphUtils.pullEvent"mouse_click" | |
| 156 | for i=1,#buttons do | |
| 157 | if buttons[i]:isclicked(x,y) then | |
| 158 | if i==1 then | |
| 159 | state="defaultGame" | |
| 160 | return | |
| 161 | elseif i==2 then | |
| 162 | state="patternGame" | |
| 163 | return | |
| 164 | elseif i==3 then | |
| 165 | state="optionsMenu" | |
| 166 | return | |
| 167 | elseif i==4 then | |
| 168 | state="aboutMenu" | |
| 169 | return | |
| 170 | elseif i==5 then | |
| 171 | state="exit" | |
| 172 | return | |
| 173 | end | |
| 174 | end | |
| 175 | end | |
| 176 | end | |
| 177 | end | |
| 178 | local function optionsMenu() | |
| 179 | clear(cx-3,cy-6,colorScheme.back,colorScheme.title) | |
| 180 | term.write("Options")
| |
| 181 | ||
| 182 | term.setTextColor(colorScheme.text) | |
| 183 | term.setCursorPos(cx-10,cy-3) | |
| 184 | term.write("Regular game lights:")
| |
| 185 | term.setCursorPos(cx-10,cy-1) | |
| 186 | term.write("Pattern game lights:")
| |
| 187 | ||
| 188 | local items={}
| |
| 189 | local buttons={}
| |
| 190 | ||
| 191 | buttons[1]=newButton(cx+11,cy-3," "..options.defaultGameLights.." ",colorScheme.menuB,colorScheme.menuT) | |
| 192 | buttons[2]=newButton(cx+11,cy-1," "..options.patternGameLights.." ",colorScheme.menuB,colorScheme.menuT) | |
| 193 | buttons[3]=newButton(cx-6,cy+5," Back ",colorScheme.menuB,colorScheme.menuT) | |
| 194 | ||
| 195 | for i=1,#buttons do | |
| 196 | buttons[i]:draw() | |
| 197 | end | |
| 198 | ||
| 199 | - | local e,_,x,y=os.pullEvent"mouse_click" |
| 199 | + | |
| 200 | local e,_,x,y=sapphUtils.pullEvent"mouse_click" | |
| 201 | ||
| 202 | for i=1,#buttons do | |
| 203 | if buttons[i]:isclicked(x,y) then | |
| 204 | if i==1 then | |
| 205 | if options.defaultGameLights<9 then | |
| 206 | options.defaultGameLights=options.defaultGameLights+1 | |
| 207 | else | |
| 208 | options.defaultGameLights=3 | |
| 209 | end | |
| 210 | buttons[i].s=" "..options.defaultGameLights.." " | |
| 211 | buttons[i]:draw() | |
| 212 | elseif i==2 then | |
| 213 | if options.patternGameLights<9 then | |
| 214 | options.patternGameLights=options.patternGameLights+1 | |
| 215 | else | |
| 216 | options.patternGameLights=3 | |
| 217 | end | |
| 218 | buttons[i].s=" "..options.patternGameLights.." " | |
| 219 | buttons[i]:draw() | |
| 220 | elseif i==3 then | |
| 221 | state="mainMenu" | |
| 222 | saveOptions() | |
| 223 | return | |
| 224 | end | |
| 225 | end | |
| 226 | end | |
| 227 | end | |
| 228 | end | |
| 229 | local function aboutMenu() | |
| 230 | clear(cx-3,cy-6,colorScheme.back,colorScheme.title) | |
| 231 | term.write("About")
| |
| 232 | ||
| 233 | local function cwrite(y,s) | |
| 234 | term.setCursorPos(cx-math.floor(#s/2),y) | |
| 235 | term.write(s) | |
| 236 | end | |
| 237 | ||
| 238 | term.setTextColor(colorScheme.text) | |
| 239 | cwrite(cy-4,"In Regular game mode, the goal is") | |
| 240 | cwrite(cy-3,"to turn every light green.") | |
| 241 | cwrite(cy-2,"In Pattern game mode, you must") | |
| 242 | cwrite(cy-1,"copy a certain pattern to win.") | |
| 243 | ||
| 244 | cwrite(cy+1,"You can exit with the 'give up' button.") | |
| 245 | ||
| 246 | cwrite(cy+3,"Copyright (c) 2014 InputUsername") | |
| 247 | cwrite(cy+6, " Ported by Hithere, ALL CREDITS TO ORIGINAL AUTHOR") | |
| 248 | ||
| 249 | local back=newButton(cx-6,cy+5," Back ",colorScheme.menuB,colorScheme.menuT) | |
| 250 | back:draw() | |
| 251 | - | local e,_,x,y=os.pullEvent"mouse_click" |
| 251 | + | |
| 252 | while true do | |
| 253 | local e,_,x,y=sapphUtils.pullEvent"mouse_click" | |
| 254 | if back:isclicked(x,y) then | |
| 255 | state="mainMenu" | |
| 256 | return | |
| 257 | end | |
| 258 | end | |
| 259 | end | |
| 260 | local function defaultGame() | |
| 261 | clear(cx-6,cy-6,colorScheme.back,colorScheme.title) | |
| 262 | term.write("Regular game")
| |
| 263 | ||
| 264 | local lights={}
| |
| 265 | for i=1,options.defaultGameLights do | |
| 266 | 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) | |
| 267 | lights[i]:draw() | |
| 268 | - | local giveup=newButton(2,2," Give up ",colorScheme.menuB,colorScheme.menuT) |
| 268 | + | |
| 269 | ||
| 270 | local giveup=newButton(2,3," Give up ",colorScheme.menuB,colorScheme.menuT) | |
| 271 | giveup:draw() | |
| 272 | ||
| 273 | local function check() | |
| 274 | for i=1,#lights do | |
| 275 | if lights[i].s==0 then return false end | |
| 276 | end | |
| 277 | return true | |
| 278 | end | |
| 279 | - | local e,_,x,y = os.pullEvent"mouse_click" |
| 279 | + | |
| 280 | while true do | |
| 281 | local e,_,x,y = sapphUtils.pullEvent"mouse_click" | |
| 282 | for i=1,#lights do | |
| 283 | if lights[i]:isclicked(x,y) then | |
| 284 | if lights[i-1] then lights[i-1]:toggle() end | |
| 285 | if lights[i+1] then lights[i+1]:toggle() end | |
| 286 | lights[i]:toggle() | |
| 287 | break | |
| 288 | end | |
| 289 | end | |
| 290 | ||
| 291 | if giveup:isclicked(x,y) then | |
| 292 | clear(cx-2,cy,colorScheme.back,colorScheme.text) | |
| 293 | term.write("D'oh! :(")
| |
| 294 | sleep(0.75) | |
| 295 | state="mainMenu" | |
| 296 | return | |
| 297 | end | |
| 298 | ||
| 299 | if check() then | |
| 300 | sleep(.2) | |
| 301 | clear(cx-6,cy,colorScheme.back,colorScheme.text) | |
| 302 | term.write("Congratulations!")
| |
| 303 | sleep(1) | |
| 304 | state="mainMenu" | |
| 305 | return | |
| 306 | end | |
| 307 | end | |
| 308 | end | |
| 309 | local function patternGame() | |
| 310 | clear(cx-6,cy-6,colorScheme.back,colorScheme.title) | |
| 311 | term.write("Pattern game")
| |
| 312 | ||
| 313 | term.setCursorPos(cx-9,cy+1) | |
| 314 | term.write("Copy this pattern:")
| |
| 315 | ||
| 316 | local lights={}
| |
| 317 | for i=1,options.patternGameLights do | |
| 318 | 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) | |
| 319 | lights[i]:draw() | |
| 320 | - | local giveup=newButton(2,2," Give up ",colorScheme.menuB,colorScheme.menuT) |
| 320 | + | |
| 321 | ||
| 322 | local giveup=newButton(2,3," Give up ",colorScheme.menuB,colorScheme.menuT) | |
| 323 | giveup:draw() | |
| 324 | ||
| 325 | local pattern={}
| |
| 326 | for i=1,options.patternGameLights do | |
| 327 | pattern[i]=math.floor(math.random(0,1)) | |
| 328 | term.setBackgroundColor(pattern[i]==1 and colorScheme.on or colorScheme.off) | |
| 329 | term.setCursorPos(cx-options.patternGameLights+2*(i-1),cy+3) | |
| 330 | term.write(" ")
| |
| 331 | end | |
| 332 | ||
| 333 | local function check() | |
| 334 | for i=1,#lights do | |
| 335 | if lights[i].s~=pattern[i] then return false end | |
| 336 | end | |
| 337 | return true | |
| 338 | end | |
| 339 | ||
| 340 | while check() do | |
| 341 | for i=1,options.patternGameLights do | |
| 342 | pattern[i]=math.floor(math.random(0,1)) | |
| 343 | term.setBackgroundColor(pattern[i]==1 and colorScheme.on or colorScheme.off) | |
| 344 | term.setCursorPos(cx-options.patternGameLights+2*(i-1),cy+3) | |
| 345 | term.write(" ")
| |
| 346 | end | |
| 347 | end | |
| 348 | - | local e,_,x,y = os.pullEvent"mouse_click" |
| 348 | + | |
| 349 | while true do | |
| 350 | local e,_,x,y = sapphUtils.pullEvent"mouse_click" | |
| 351 | for i=1,#lights do | |
| 352 | if lights[i]:isclicked(x,y) then | |
| 353 | if lights[i-1] then lights[i-1]:toggle() end | |
| 354 | if lights[i+1] then lights[i+1]:toggle() end | |
| 355 | lights[i]:toggle() | |
| 356 | break | |
| 357 | end | |
| 358 | end | |
| 359 | ||
| 360 | if giveup:isclicked(x,y) then | |
| 361 | clear(cx-2,cy,colorScheme.back,colorScheme.text) | |
| 362 | term.write("D'oh! :(")
| |
| 363 | sleep(0.75) | |
| 364 | state="mainMenu" | |
| 365 | return | |
| 366 | end | |
| 367 | ||
| 368 | if check() then | |
| 369 | sleep(.2) | |
| 370 | clear(cx-6,cy,colorScheme.back,colorScheme.text) | |
| 371 | term.write("Congratulations!")
| |
| 372 | sleep(1) | |
| 373 | state="mainMenu" | |
| 374 | return | |
| 375 | end | |
| 376 | end | |
| 377 | end | |
| 378 | ||
| 379 | --================================= | |
| 380 | --== GAME | |
| 381 | --================================= | |
| 382 | initialize() | |
| 383 | loadOptions() | |
| 384 | ||
| 385 | while true do | |
| 386 | if state=="mainMenu" then | |
| 387 | mainMenu() | |
| 388 | elseif state=="optionsMenu" then | |
| 389 | optionsMenu() | |
| 390 | elseif state=="aboutMenu" then | |
| 391 | aboutMenu() | |
| 392 | elseif state=="defaultGame" then | |
| 393 | defaultGame() | |
| 394 | elseif state=="patternGame" then | |
| 395 | patternGame() | |
| 396 | elseif state=="exit" then | |
| 397 | break | |
| 398 | end | |
| 399 | end | |
| 400 | ||
| 401 | reset() | |
| 402 | saveOptions() |