Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local running = true
- local cx,cy = 0, 0
- local drawx = 11
- local drawy = -2
- local xscale = 5
- local yscale = 4
- local cubexsize = 6
- local cubeysize = 4
- local score = 2
- local function calcdragpos(sx,sy,ex,ey,dx,dy)
- if(ex < sx-dx)then return -1,0
- elseif(ex > sx+dx)then return 1,0
- elseif(ey < sy-dy)then return 0,-1
- elseif(ey > sy+dy)then return 0,1 end
- end
- local map = {
- {2,0,0,0},
- {0,0,0,0},
- {0,0,0,0},
- {0,0,0,0},
- }
- local numcols = {
- ['2'] = "white",
- ['4'] = "lightGray",
- ['8'] = "brown",
- ['16'] = "orange",
- ['32'] = "pink",
- ['64'] = "red",
- ['128'] = "yellow",
- ['256'] = "yellow",
- ['512'] = "yellow",
- ['1024'] = "yellow",
- ['2048'] = "yellow",
- ['4096'] = "magenta",
- ['8192'] = "purple",
- }
- local function drawtile(x,y)
- local sx = (drawx)+(x)*xscale
- local sy = (drawy)+(y)*yscale
- local ex = (drawx+x*xscale)+cubexsize
- local ey = (drawy+y*yscale)+cubeysize
- if(map[y][x] ~= 0)then
- -- drawing code form empty
- bcol = numcols[tostring(map[y][x])]
- if(bcol == nil)then col = colors.lightBlue end
- paintutils.drawFilledBox(sx+1,sy+1,ex-(cubexsize/2)+1,ey-(cubeysize/2)+1,colors[bcol])
- term.setTextColor(colors.black)
- term.setCursorPos((sx+3) - #tostring(map[y][x])/2 ,sy+2)
- write(map[y][x])
- else
- paintutils.drawFilledBox(sx+1,sy+1,ex-(cubexsize/2)+1,ey-(cubeysize/2)+1,colors.black)
- end
- end
- local function drawtiles()
- term.current().setVisible(false)
- --paintutils.drawBox(drawx,drawy,drawx+(#map[1]*xscale),drawy+(#map*yscale),colors.gray)
- for y = 1, #map do
- for x = 1, #map[y] do
- local tile = map[y][x]
- local sx = (drawx)+(x)*xscale
- local sy = (drawy)+(y)*yscale
- local ex = (drawx+x*xscale)+cubexsize
- local ey = (drawy+y*yscale)+cubeysize
- paintutils.drawBox(sx,sy,ex-1,ey,colors.gray)
- drawtile(x,y)
- end
- end
- term.current().setVisible(true)
- end
- local function setTile(x,y,n)
- if(map[y][x] ~= 0 and n ~= 0)then
- score = score + (n-map[y][x])
- elseif(map[y][x] ~= 0 and n == 0)then
- score = score - map[y][x]
- else
- score = score + n
- end
- map[y][x] = n
- drawtile(x,y)
- end
- local function getRandTile()
- local rx = math.ceil(math.random(1,#map[1]))
- local ry = math.ceil(math.random(1,#map))
- local rn = math.ceil(math.random(1,10))
- if(rn == 10)then
- rn = 4
- else
- rn = 2
- end
- return rx,ry,rn
- end
- local function slidetiles(xd,yd)
- -- 1, UP: 2, DOWN, 3: LEFT, 4: RIGHT
- local rna = math.ceil(math.random(1,2))
- local moved=false
- local cancombine = true
- for li = 1, #map[1] do
- for y = 1, #map do
- for x = 1, #map[y] do
- local to = nil
- if(y+yd < 0 or x+xd < 0)then break end
- if(map[y+yd] ~= nil)then
- if(map[y+yd][x+xd] ~= nil)then
- to = map[y+yd][x+xd]
- end
- end
- if(to == 0)then
- setTile(x+xd,y+yd,map[y][x])
- setTile(x,y,0)
- moved = true
- elseif(to == map[y][x] and cancombine and to ~= 8192)then
- setTile(x+xd,y+yd,map[y+yd][x+xd]+map[y][x])
- setTile(x,y,0)
- cancombine = false
- end
- end
- end
- end
- local rx,ry,rn = getRandTile()
- if(map[ry][rx] == 0 and moved)then
- setTile(rx,ry,rn)
- end
- term.setCursorPos(1,1)
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- term.clearLine()
- print(score)
- end
- term.clear()
- drawtiles()
- while running do
- local e = {os.pullEvent()}
- if(e[1] == "mouse_click")then
- cx = e[3]
- cy = e[4]
- end
- if(e[1] == "mouse_drag" and cx ~= 0 and cy ~= 0)then
- local xdir,ydir = calcdragpos(cx,cy,e[3],e[4],2,1)
- if(type(xdir) == "number" and type(ydir) == "number")then
- slidetiles(tonumber(xdir),tonumber(ydir))
- cx = 0
- cy = 0
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement