---------- ColorTetris made by Nick ---------- This is the sourcecode for ColorTetris.tns and may be studied and adapted for personal use ---------- Do not claim it as yours, even by changing some of the code ---------- 14/11/2011 ---------- more info at http://www.omnimaga.org/index.php?topic=11258.0 mode = "marathon" function on.create() ----------DECLARE FIELD---------- blocks = {{0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0}} ----------DECLARE BLOCKS---------- Tblock = {{0,1,0,0},{1,1,1,0},{0,0,0,0},{0,0,0,0},5,1,1,3,2} Lblock = {{1,0,0,0},{1,0,0,0},{1,1,0,0},{0,0,0,0},5,1,2,2,3} LLblock = {{0,1,0,0},{0,1,0,0},{1,1,0,0},{0,0,0,0},5,1,3,2,3} Iblock = {{1,0,0,0},{1,0,0,0},{1,0,0,0},{1,0,0,0},5,1,4,1,4} cublock = {{1,1,0,0},{1,1,0,0},{0,0,0,0},{0,0,0,0},5,1,5,2,2} Lstairblock = {{1,1,0,0},{0,1,1,0},{0,0,0,0},{0,0,0,0},5,1,6,3,2} Rstairblock = {{0,1,1,0},{1,1,0,0},{0,0,0,0},{0,0,0,0},5,1,7,3,2} --block[1,4] = bock or not {{},{},{},{},...} --block[5] = x-value --block[6] = y-value --block[7] = color {1,2,3,4,5,6,7} --block[8] = width {1,2,3,4} --block[9] = height {1,2,3,4} ----------DECLARE FIELD POLYGONS---------- field1 = {100,3,200,0,219,3,219,9,259,13,251,61,219,65,219,180,251,183,250,210,219,216,100,216,100,68,60,65,57,15,97,10,100,3} field2 = {104,6,210,2,220,12,255,18,249,58,211,65,215,186,250,192,245,209,209,216,108,216,101,66,61,65,62,17,97,14,104,6} hs1 = {15,100,83,95,85,97,89,142,85,145,21,148,18,143,15,100,15,100} hs2 = {17,103,19,101,83,95,86,138,80,140,24,145,20,141,16,106,17,103} mode1 = {20+210,100,88+210,95,90+210,97,94+210,142,90+210,145,26+210,148,23+210,143,20+210,100,20+210,100} mode2 = {22+210,103,24+210,101,88+210,95,91+210,138,85+210,140,29+210,145,25+210,141,21+210,106,22+210,103} ----------DECLARE OTHER TABLES---------- transpon = {{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},0,0,0,0,0} ghostblock = {{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},0,0,0,0,0} colortable3 = {{{157,54,225},{135,34,198}}, {{216,47,48},{176,15,20}}, {{43,225,237},{28,205,213}}, {{249,123,43},{227,94,1}}, {{56,209,78},{29,185,58}},{{241,216,102},{252,196,14}},{{67,69,229},{23,27,214}}} hold = {{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},0,0,0,0,0} --colortable = {0,0,0,0,0,0} texthold = {"H","O","L","D"," "} textnext = {"N","E","X","T"," "} textlevel = {"L","E","V","E","L"} ----------CHECK FOR HIGHSCORE---------- highscoremar = (var.recall("TetrismarHS") or {0,0,0,"name","name","name"}) highscore40l = (var.recall("Tetris40HS") or {0,0,0,"name","name","name"}) highscoreTT = (var.recall("TetristtHS") or {0,0,0,"name","name","name"}) ----------SET ALL VARIABLES USED---------- level = 1 lines = 0 chooseblock = 1 lastchoice = 0 linescore = 0 stop = false done = false stopright = false stopleft = false noturn = false pause = false holdfull = false checkforlevel = 0 count = 0 ghostblockon = true ghoststop = false newblock = false ghostoff = false alphapause = 255 highest = 21 savefalling = {{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},0,0,0,0,0} name = "" hs = false --mode = "marathon" --modesave = "marathon" menu = true --"marathon", "40 lines" or "Time trial" linesleft = 40 time40 = 0 timeleft = 100 a = 0 k = 0 timer.start(1/level) chooseblock = math.random(1,7) if chooseblock == 1 then fallingblock = Tblock nextblock = Lblock elseif chooseblock == 2 then fallingblock = Lblock nextblock = LLblock elseif chooseblock == 3 then fallingblock = LLblock nextblock = Iblock elseif chooseblock == 4 then fallingblock = Iblock nextblock = cublock elseif chooseblock == 5 then fallingblock = cublock nextblock = Rstairblock elseif chooseblock == 6 then fallingblock = Rstairblock nextblock = Lstairblock elseif chooseblock == 7 then fallingblock = Lstairblock nextblock = Tblock end --boven:7, onder:199, rechts:197 links:109 end function on.timer() if not menu then on.arrowDown() if mode=="Time trial" and not pause and not menu and not done then timeleft=timeleft-1 elseif mode=="40 lines" and not pause and not menu and not done then time40 = time40+1 end end end function on.paint(gc) -----------CHECK IF DONE---------- if mode=="40 lines" then if linescore==40 then done = true on.checkIfHigh() end elseif mode=="Time trial" then if timeleft<=0 then done = true on.checkIfHigh() end end -----------DRAW BACKGROUND---------- if platform.isColorDisplay() then gc:setColorRGB(108,41,206) gc:fillRect(0,0,318,215) gc:setColorRGB(0,0,0) gc:setColorRGB(0,0,0) gc:fillPolygon(field1) gc:setColorRGB(70,70,70) gc:fillPolygon(field2) gc:setColorRGB(2,54,144) gc:setColorRGB(142,142,142) gc:fillRect(109,7,100,200) else gc:setColorRGB(100,100,100) gc:fillRect(0,0,318,215) gc:setColorRGB(0,0,0) gc:fillPolygon(field1) gc:setColorRGB(70,70,70) gc:fillPolygon(field2) gc:setColorRGB(255,255,255) gc:fillRect(109,7,100,200) end ----------SET ALPHA FOR FADING BLOCKS---------- if pause then gc:setAlpha(alphapause) end if not menu then ----------DRAW BLOCKS---------- for i=1,20 do for j=1,10 do if blocks[i][j] ~=0 then gc:setColorRGB(unpack(colortable3[blocks[i][j]][1])) gc:fillRect(109+10*(j-1),7+10*(i-1),10,10) gc:setColorRGB(unpack(colortable3[blocks[i][j]][2])) gc:fillRect(109+10*(j-1)+2,7+10*(i-1)+2,8,8) end end end for i=1,4 do for j=1,4 do if fallingblock[i][j] == 1 then gc:setColorRGB(unpack(colortable3[fallingblock[7]][1])) gc:fillRect((fallingblock[5]-1)*10+10*(j-1)+109,(fallingblock[6]-1)*10+10*(i-1)+7,10,10) -----ghostblock----- if ghostblockon and not ghostoff then on.ghostBlock() --gc:drawRect((fallingblock[5]-1)*10+10*(j-1)+109,207-fallingblock[9]*10+(i-1)*10,9,9) gc:setAlpha("60") gc:fillRect((ghostblock[5]-1)*10+10*(j-1)+109,(ghostblock[6]-1)*10+10*(i-1)+7,9,9) gc:setAlpha(alphapause) end gc:setColorRGB(unpack(colortable3[fallingblock[7]][2])) gc:fillRect((fallingblock[5]-1)*10+10*(j-1)+109+2,(fallingblock[6]-1)*10+10*(i-1)+7+2,8,8) end if nextblock[i][j] == 1 then gc:setColorRGB(unpack(colortable3[nextblock[7]][1])) gc:fillRect(209+j*10,i*10+7+20-(nextblock[9]*5),10,10) gc:setColorRGB(unpack(colortable3[nextblock[7]][2])) gc:fillRect(209+j*10+2,i*10+7+20-(nextblock[9]*5)+2,8,8) end end end end ----------SET ALPHA FOR NOT FADING TEXT---------- if pause then gc:setAlpha("255") end ----------PRINT TEXT (score/level)---------- gc:setColorRGB(255,255,255) gc:setFont("sansserif","b","9") for i=1,5 do gc:drawString(texthold[i],55+i*10,5-i,"top") gc:drawString(textnext[i],210+i*10,i,"top") gc:drawString(textlevel[i],210+i*8,172+i,"top") end if mode=="marathon" then gc:setFont("sansserif","b","9") if level>9 then gc:drawString(level,220,190,"top") gc:setColorRGB(0,0,0) gc:drawString(level,221,191,"top") else gc:drawString(level,230,190,"top") gc:setColorRGB(0,0,0) gc:drawString(level,231,191,"top") end elseif mode=="40 lines" then gc:drawString(linesleft,220,191,"top") gc:setColorRGB(0,0,0) gc:drawString(linesleft,221,191,"top") elseif mode=="Time trial" then gc:drawString(timeleft,220,191,"top") gc:setColorRGB(0,0,0) gc:drawString(timeleft,221,191,"top") end gc:setColorRGB(200,200,200) gc:setFont("sansserif","b","10") --gc:drawString("SCORE: ",180,0,"top") if linescore<=9 then gc:drawString(linescore,195,-5,"top") elseif linescore<=99 then gc:drawString(linescore,185,-5,"top") elseif linescore<=999 then gc:drawString(linescore,175,-5,"top") elseif linescore<=9999 then gc:drawString(linescore,165,-5,"top") end if mode=="40 lines" then gc:drawString(math.floor(time40/60)..":"..math.floor(time40-math.floor(time40/60)*60),115,-5,"top") end --gc:drawString("HIGHSCORE",220,56,"top") --gc:drawString(highscoremar,220,65,"top") --gc:drawString("Nick Steen",272,199,"top") ----------DRAW HIGHSCORE---------- gc:setColorRGB(0,0,0) gc:fillPolygon(hs1) gc:setColorRGB(70,70,70) gc:fillPolygon(hs2) --gc:drawString(highscoremar[1],30,110,"top") gc:setFont("sansserif","b","9") gc:setColorRGB(255,255,255) gc:drawString("HIGH",24,89,"top") gc:setFont("sansserif","r","7") if mode=="marathon" then for i=1,3 do gc:drawString(highscoremar[i].." - "..highscoremar[i+3],27,108+10*i) end elseif mode=="40 lines" then for i=1,3 do gc:drawString(highscore40l[i].." - "..highscore40l[i+3],27,108+10*i) end else for i=1,3 do gc:drawString(highscoreTT[i].." - "..highscoreTT[i+3],27,108+10*i) end end ----------DRAW MODE---------- gc:setColorRGB(0,0,0) gc:fillPolygon(mode1) gc:setColorRGB(70,70,70) gc:fillPolygon(mode2) gc:setColorRGB(255,255,255) gc:setFont("sansserif","b","9") gc:drawString("MODE",239,89,"top") gc:drawString(mode,241,110,"top") ----------DRAW HOLD BLOCK---------- gc:setColorRGB(0,0,0) if holdfull then for i=1,4 do for j=1,4 do if hold[i][j]==1 then --gc:drawRect(110+j*10,60+i*10,10,10) gc:setColorRGB(unpack(colortable3[hold[7]][1])) gc:fillRect(75+j*10-hold[8]*5,i*10+7+22-(hold[9]*5),10,10) gc:setColorRGB(unpack(colortable3[hold[7]][2])) gc:fillRect(75+j*10+2-hold[8]*5,i*10+7+22-(hold[9]*5)+2,8,8) end end end end ----------DRAW ARROWS FOR MENUCHOICE---------- if menu then gc:setColorRGB(255,255,255) gc:fillPolygon({262,115,267,110,272,115,262,115}) gc:fillPolygon({262,130,267,135,272,130,262,130}) --gc:fillPolygon({263,129,267,133,271,129,263,129}) end ----------SET ALPHA PAUSE AND DRAW RECT---------- if pause then gc:setColorRGB(255,255,255) gc:setAlpha("150") gc:fillRect(90,82,138,50) gc:setFont("sansserif","b","25") gc:setColorRGB(0,0,0) gc:setAlpha("255") gc:drawString("PAUSE",105,83,"top") end ----------SET ALPHA DONE AND DRAW RECT---------- if done then gc:setColorRGB(255,255,255) gc:setAlpha("150") gc:fillRect(90,82,138,50) gc:setFont("sansserif","b","25") gc:setColorRGB(0,0,0) gc:setAlpha("255") gc:drawString("it's done",92,83,"top") gc:setFont("sansserif","b","6") --gc:drawString("THE GAME",135,118,"top") if hs then gc:drawString("Name:"..name,135,118,"top") end end if newblock then newblock = false on.newBlock() end end function on.arrowLeft() if not done and not pause and not menu then for i=1,fallingblock[9] do if fallingblock[i][1]==1 then if blocks[fallingblock[6]+i-1][fallingblock[5]-1] ~= 0 then stopleft = true end end end if fallingblock[5] > 1 and not stopleft then fallingblock[5] = fallingblock[5] - 1 end stopleft = false stopright = false platform.window:invalidate() end end function on.arrowRight() if not done and not pause and not menu then for i=1,fallingblock[9] do if fallingblock[i][fallingblock[8]]==1 then if blocks[fallingblock[6]+i-1][fallingblock[5]+fallingblock[8]] ~= 0 then stopright = true end end end if fallingblock[5] < 11-fallingblock[8] and not stopright then fallingblock[5] = fallingblock[5] + 1 end stopleft = false stopright = false platform.window:invalidate() end end function on.checkIfHigh() if mode=="marathon" then if linescore>highscoremar[1] then highscoremar[3] = highscoremar[2] highscoremar[2] = highscoremar[1] highscoremar[1] = linescore highscoremar[6] = highscoremar[5] highscoremar[5] = highscoremar[4] hs = true var.store("TetrismarHS",highscoremar) end elseif mode=="40 lines" then if time40highscoreTT[1] and timeleft==0 then highscoreTT[3] = highscoreTT[2] highscoreTT[2] = highscoreTT[1] highscoreTT[1] = linescore highscoreTT[6] = highscoreTT[5] highscoreTT[5] = highscoreTT[4] hs = true var.store("TetristtHS",highscoreTT) end end platform.window:invalidate() end function on.arrowDown() if not done and not pause and not menu then for i=1,10 do if blocks[1][i]~=0 then done = true if mode=="mararthon" then on.checkIfHigh() end platform.window:invalidate() end end ymax = 20-fallingblock[9] for i=1,4 do for j=1,4 do if fallingblock[6]~=21-fallingblock[9] and fallingblock[i][j]==1 and fallingblock[9]~=4 then if fallingblock[i+1][j]~=1 and blocks[i+2+fallingblock[6]-fallingblock[9]+1*(fallingblock[9]-2)][j+fallingblock[5]-1]~=0 then stop = true end end if fallingblock[6]~=21-fallingblock[9] and fallingblock[i][j]==1 and fallingblock[9]==4 then if blocks[i+2+fallingblock[6]-fallingblock[9]+1*(fallingblock[9]-2)][j+fallingblock[5]-1]~=0 then stop = true end end end end if fallingblock[6]<21-fallingblock[9] and not stop then fallingblock[6] = fallingblock[6] + 1 elseif fallingblock[6]>=21-fallingblock[9] or stop then ghostblockon=false stop = false for i=1,4 do for j=1,4 do if fallingblock[i][j] == 1 then blocks[i+1+fallingblock[6]-fallingblock[9]+1*(fallingblock[9]-2)][j+fallingblock[5]-1] = fallingblock[7] end end end on.checkLines() newblock = true end platform.window:invalidate() end if menu then if mode=="marathon" then mode = "40 lines" textlevel = {"L","E","F","T",""} elseif mode=="40 lines" then mode = "Time trial" textlevel = {"L","E","F","T",""} else mode = "marathon" textlevel = {"L","E","V","E","L"} end platform.window:invalidate() end platform.window:invalidate() end function on.enterKey() newblock = false ghostblockon = false if not done and not pause and not menu then k = -1 repeat k = k + 1 for i=1,4 do for j=1,4 do if fallingblock[6]~=21-fallingblock[9] and fallingblock[i][j]==1 and fallingblock[9]~=4 then if fallingblock[i+1][j]~=1 and blocks[i+2+fallingblock[6]-fallingblock[9]+1*(fallingblock[9]-2)][j+fallingblock[5]-1]~=0 then stop = true end end if fallingblock[6]~=21-fallingblock[9] and fallingblock[i][j]==1 and fallingblock[9]==4 then if blocks[i+2+fallingblock[6]-fallingblock[9]+1*(fallingblock[9]-2)][j+fallingblock[5]-1]~=0 then stop = true end end end end if fallingblock[6]<21-fallingblock[9] and not stop then fallingblock[6] = fallingblock[6] + 1 else stop = false for i=1,4 do for j=1,4 do if fallingblock[i][j] == 1 then blocks[i+1+fallingblock[6]-fallingblock[9]+1*(fallingblock[9]-2)][j+fallingblock[5]-1] = fallingblock[7] stop = true end end end end until stop stop = false on.checkLines() platform.window:invalidate() newblock = true elseif menu then on.create() pause=false menu = false done = false platform.window:invalidate() elseif not pause var.store("TetrismarHS",highscoremar) var.store("Tetris40HS",highscore40l) var.store("TetristtHS",highscoreTT) on.create() pause = false platform.window:invalidate() else pause = false end end function on.checkLines() multi = 0 for k=1,20 do lines = 0 for l=1,10 do if blocks[k][l]~=0 then lines = lines + 1 end end if lines == 10 then multi = multi + 1 table.remove(blocks,k) table.insert(blocks,1,{0,0,0,0,0,0,0,0,0,0}) fallingblock[5] = 40 linescore = linescore + 1 lines=0 else lines = 0 end end platform.window:invalidate() if multi>1 then linescore = linescore + 2*multi end if mode=="40 lines" then linesleft=linesleft-multi if linesleft<=0 then done=true end end checkforlevel = checkforlevel + multi if checkforlevel>=5+level then level = level + 1 timer.stop() timer.start(1/level) checkforlevel = 0 end end function on.ghostBlock() highest = 21 for l=1,20 do for m=1,fallingblock[8] do if (fallingblock[6]+l)<21 then if blocks[fallingblock[6]+l][fallingblock[5]+m-1]~=0 then if (fallingblock[6]+l)=11 then noturn = true end if fallingblock[9]<=fallingblock[8] then noturn = false end end if fallingblock[9]==4 and (fallingblock[5]+4)>11 then noturn = true end if noturn then return 0 else return 1 end end function on.tabKey() if not holdfull then hold = fallingblock hold[5]=5 hold[6]=1 holdfull=true on.newBlock() else nextblock = fallingblock fallingblock = hold holdfull = false end end function on.charIn(char) if not done and not pause and not menu then if char == '+' and level <= 19 then level = level + 1 timer.stop() timer.start(1/level) end if char == '4' then on.arrowLeft() end if char == '6' then on.arrowRight() end if char =='8' then on.arrowUp() end if char == '2' then on.arrowDown() end if char == 'g' then if ghostoff==true then ghostoff = false else ghostoff = true end end end if char == 'p' and not menu then if pause then pause = false timer.stop() timer.start(1/level) alphapause = 255 else pause = true alphapause = 80 end end if char == 'r' then on.create() end if done and hs then if string.len(name) <= 20 then name = name..char end if mode=="marathon" then highscoremar[4] = name elseif mode=="40 lines" then highscore40l[4] = name elseif mode=="Time trial" then highscoreTT[4] = name end end platform.window:invalidate() end function on.backspaceKey() if done then name = string.usub(name,0,string.len(name)-1) if mode=="marathon" then highscoremar[4] = name elseif mode=="40 lines" then highscore40l[4] = name elseif mode=="Time trial" then highscoreTT[4] = name end if mode=="marathon" then var.store("TetrismarHS",highscoremar) elseif mode=="40 lines" then var.store("Tetris40HS",highscore40l) else var.store("TetristtHS",highscoreTT) end platform.window:invalidate() end end function on.newBlock() ghostblockon=true for i=1,10 do if blocks[1][i]~=0 then done = true on.checkIfHigh() end end if not done and not pause then lastchoice = chooseblock repeat chooseblock = math.random(1,7) until chooseblock ~= lastchoice fallingblock = nextblock if chooseblock == 1 then nextblock = Tblock elseif chooseblock == 2 then nextblock = Lblock elseif chooseblock == 3 then nextblock = LLblock elseif chooseblock == 4 then nextblock = Iblock elseif chooseblock == 5 then nextblock = cublock elseif chooseblock == 6 then nextblock = Lstairblock elseif chooseblock == 7 then nextblock = Rstairblock end fallingblock[5] = 6-math.floor(fallingblock[8]/2) fallingblock[6] = 1 platform.window:invalidate() end end