Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Created by Tomlacko
- --recreation of the mobile game Shades
- function clr()
- m.clear()
- m.setCursorPos(1,1)
- end
- function clrterm()
- term.clear()
- term.setCursorPos(1,1)
- end
- function setColor(color)
- m.setBackgroundColor(color)
- end
- function draw(x, y)
- m.setCursorPos(x, y)
- m.write(" ")
- end
- function genBlock()
- a = math.random(1, diff+2)
- if a == 1 then return colors.white
- elseif a == 2 then return colors.lime
- elseif a == 3 then return colors.green
- elseif a == 4 then return colors.brown
- else return colors.black
- end
- end
- function stackBlock(curColor)
- if curColor == colors.white then return colors.lime
- elseif curColor == colors.lime then return colors.green
- elseif curColor == colors.green then return colors.brown
- else return colors.black
- end
- end
- function drawPanel()
- m.setCursorPos(1,1)
- setColor(colors.black)
- m.write("Blocks: "..cycle.." Score: "..score)
- m.setCursorPos(sizeX-7,1)
- m.write("Level: "..diff)
- setColor(nxt)
- for i=1, sizeX do
- draw(i,2)
- end
- end
- function drawStatic()
- for col=1, 4 do
- for row=2, 10 do
- if blocks[col][row] == 0 then
- if quick and col==posX then
- setColor(colors.lightGray)
- else
- setColor(colors.gray)
- end
- else
- setColor(blocks[col][row])
- end
- for i=(col*9)-8, col*9 do
- draw(i, row*2)
- draw(i, (row*2)-1)
- end
- end
- end
- end
- function drawBlock()
- setColor(cur)
- for i=(posX*9)-8, posX*9 do
- draw(i, posY)
- draw(i, posY-1)
- end
- end
- function drawPause()
- setColor(colors.black)
- m.setCursorPos(16, 10)
- m.write("Paused")
- setColor(colors.lightGray)
- m.write(" ")
- m.setCursorPos(15, 9)
- m.write(" ")
- m.setCursorPos(15, 10)
- m.write(" ")
- m.setCursorPos(15, 11)
- m.write(" ")
- end
- function drawExit()
- setColor(colors.black)
- m.setCursorPos(15, 14)
- m.write("Continue")
- setColor(colors.lightGray)
- m.write(" ")
- m.setCursorPos(14, 14)
- m.write(" ")
- m.setCursorPos(14, 16)
- m.write(" ")
- m.setCursorPos(14, 13)
- m.write(" ")
- m.setCursorPos(14, 15)
- m.write(" ")
- m.setCursorPos(14, 17)
- m.write(" ")
- setColor(colors.black)
- m.setCursorPos(15, 16)
- m.write(" Exit ")
- setColor(colors.lightGray)
- m.write(" ")
- end
- ----------------------------------------------------------------------------
- ----------------------------------------------------------------------------
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- clrterm()
- repeat
- print("Touch the monitor to start the game...")
- event, side = os.pullEvent("monitor_touch")
- m = peripheral.wrap(side)
- m.setTextScale(2)
- sizeX, sizeY = m.getSize()
- if not(sizeX == 36 and sizeY == 20) then
- clrterm()
- print("Monitor doesn't have the right size!")
- print("You need an 7x6 monitor to play this game.")
- print(" ")
- else
- setColor(colors.black)
- m.setTextColor(colors.white)
- clr()
- end
- until sizeX == 36 and sizeY == 20
- clrterm()
- print("Touch the top of the monitor to pause the game...")
- score = 0
- diff = 1
- cycle = 0
- cur = genBlock()
- nxt = genBlock()
- blocks = {}
- for i=1, 4 do
- blocks[i] = {}
- for j=2, 10 do
- blocks[i][j] = 0
- end
- end
- posY = 4
- posX = 2
- new = true
- stack = 0
- speed = 0.3
- quick = false
- while true do
- if (posY%2 == 0 and blocks[posX][(posY/2)+1] ~= 0 and (blocks[posX][(posY/2)+1] ~= cur or blocks[posX][(posY/2)+1] == colors.black)) or posY == 20 then
- cycle = cycle+1
- score = score+2
- if stack>0 then
- score = score + (((stack+1)*2)*stack)
- stack = 0
- end
- blocks[posX][posY/2] = cur
- if blocks[1][(posY/2)] == cur and blocks[2][(posY/2)] == cur and blocks[3][(posY/2)] == cur and blocks[4][(posY/2)] == cur then
- if cur == colors.white then score = score + 20
- elseif cur == colors.lime then score = score + 40
- elseif cur == colors.green then score = score + 60
- elseif cur == colors.brown then score = score + 80
- elseif cur == colors.black then score = score + 100
- end
- for x = 1, 4 do
- for y = posY/2, 3, -1 do
- blocks[x][y] = blocks[x][y-1]
- end
- blocks[x][2] = 0
- end
- end
- cur = nxt
- nxt = genBlock()
- posY = 4
- new = true
- quick = false
- end
- if new then
- new = false
- else
- posY = posY+1
- end
- if cycle == 50 then
- diff = 2
- speed = 0.2
- elseif cycle == 100 then
- diff = 3
- speed = 0.15
- end
- if posY%2 == 0 and blocks[posX][(posY/2)] == cur then
- cur = stackBlock(cur)
- stack = stack+1
- blocks[posX][(posY/2)] = 0
- end
- if quick then
- timID = os.startTimer(0.05)
- else
- timID = os.startTimer(speed)
- end
- event, side, x, y = os.pullEvent()
- while not(event == "monitor_touch" or (event == "timer" and side == timID)) do
- event, side, x, y = os.pullEvent()
- end
- if event == "monitor_touch" then
- if y == 1 then
- drawPause()
- drawExit()
- ev, si, xd, yd = os.pullEvent("monitor_touch")
- while true do
- if xd > 14 and xd < 23 and yd == 14 then
- break
- elseif xd > 14 and xd < 23 and yd == 16 then
- setColor(colors.black)
- m.clear()
- m.setTextScale(1)
- error("Game stopped.")
- end
- ev, si, xd, yd = os.pullEvent("monitor_touch")
- end
- elseif math.floor((x+8)/9) ~= posX then
- if not(blocks[math.floor((x+8)/9)][(posY+(posY%2))/2] ~= 0 or stack>0) then
- posX = math.floor((x+8)/9)
- end
- else
- quick = true
- end
- end
- drawPanel()
- drawStatic()
- drawBlock()
- if posY == 4 and blocks[posX][(posY/2)+1] ~= 0 and blocks[posX][(posY/2)+1] ~= cur then break end
- end
- setColor(colors.lightGray)
- m.setCursorPos(16, 9)
- m.write(" ")
- m.setCursorPos(15, 10)
- m.write(" ")
- m.setCursorPos(15, 11)
- m.write(" ")
- m.setCursorPos(16, 12)
- m.write(" ")
- m.setCursorPos(21, 10)
- m.write(" ")
- m.setCursorPos(21, 11)
- m.write(" ")
- setColor(colors.black)
- m.setCursorPos(17, 10)
- m.write("GAME")
- m.setCursorPos(17, 11)
- m.write("OVER")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement