Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local gpu=require("component").gpu
- local event=require("event")
- width=20
- mas={}
- StockBlocks = { { start_position = { x = 4, y = 1 },
- color = { 175, 175, 0 },
- symbol = "T",
- transformations = { { { x = -1, y = 0 },
- { x = 0, y = 0 },
- { x = 1, y = 0 },
- { x = 0, y = -1 } },
- { { x = 0, y = 0 },
- { x = 0, y = -1 },
- { x = 0, y = 1 },
- { x = 1, y = 0 } },
- { { x = -1, y = 0 },
- { x = 0, y = 0 },
- { x = 1, y = 0 },
- { x = 0, y = 1 } },
- { { x = 0, y = 0 },
- { x = 0, y = -1 },
- { x = 0, y = 1 },
- { x = -1, y = 0 } } } },
- { start_position = { x = 4, y = 1 },
- color = { 0, 255, 0 },
- symbol = "Z",
- transformations = { { { x = -1, y = 0 },
- { x = 0, y = 0 },
- { x = 0, y = -1 },
- { x = 1, y = -1 } },
- { { x = 0, y = -1 },
- { x = 0, y = 0 },
- { x = 1, y = 0 },
- { x = 1, y = 1 } } } },
- { start_position = { x = 4, y = 1 },
- color = { 100, 100, 175 },
- symbol = "S",
- transformations = { { { x = 1, y = 0 },
- { x = 0, y = 0 },
- { x = 0, y = -1 },
- { x = -1, y = -1 } },
- { { x = 0, y = 1 },
- { x = 0, y = 0 },
- { x = 1, y = 0 },
- { x = 1, y = -1 } } } },
- { start_position = { x = 4, y = 0 },
- color = { 255, 255, 100 },
- symbol = "J",
- transformations = { { { x = -1, y = 0 },
- { x = 0, y = 0 },
- { x = 1, y = 0 },
- { x = -1, y = 1 } },
- { { x = 0, y = -1 },
- { x = 0, y = 0 },
- { x = 0, y = 1 },
- { x = 1, y = 1 } },
- { { x = -1, y = 0 },
- { x = 0, y = 0 },
- { x = 1, y = 0 },
- { x = 1, y = -1 } },
- { { x = 0, y = -1 },
- { x = 0, y = 0 },
- { x = 0, y = 1 },
- { x = -1, y = -1 } } } },
- { start_position = { x = 4, y = 0 },
- color = { 255, 0, 255 },
- symbol = "L",
- transformations = { { { x = -1, y = 0 },
- { x = 0, y = 0 },
- { x = 1, y = 0 },
- { x = 1, y = 1 } },
- { { x = 0, y = -1 },
- { x = 0, y = 0 },
- { x = 0, y = 1 },
- { x = 1, y = -1 } },
- { { x = -1, y = 0 },
- { x = 0, y = 0 },
- { x = 1, y = 0 },
- { x = -1, y = -1 } },
- { { x = 0, y = -1 },
- { x = 0, y = 0 },
- { x = 0, y = 1 },
- { x = -1, y = 1 } } } },
- { start_position = { x = 4, y = 0 },
- color = { 255, 0, 0 },
- symbol = "I",
- transformations = { { { x = -1, y = 0 },
- { x = 0, y = 0 },
- { x = 1, y = 0 },
- { x = 2, y = 0 } },
- { { x = 0, y = -1 },
- { x = 0, y = 0 },
- { x = 0, y = 1 },
- { x = 0, y = 2 } } } },
- { start_position = { x = 4, y = 0 },
- color = { 0, 0, 255 },
- symbol = "[]",
- transformations = { { { x = 0, y = 0 },
- { x = 0, y = 1 },
- { x = 1, y = 0 },
- { x = 1, y = 1 } } } }
- }
- function toXY(num,wid)
- wid=wid or width
- y=math.floor((num-1)/wid)+1
- return num-(y-1)*width,y
- end
- function fromXY(x,y,wid)
- wid=wid or width
- return (y-1)*wid+x
- end
- for i=1,1000 do
- mas[i]=0
- end
- --print(StockBlocks[1].transformations[1][1].y)
- --[[f=7
- tr=1
- for i=1,4 do
- gpu.set(StockBlocks[f].transformations[tr][i].x+5,StockBlocks[f].transformations[tr][i].y+5,"!")
- end]]--
- obj={
- id=-1,
- x=-1,
- y=-1,
- dX=0,
- dY=0,
- dR=0,
- rotate=-1,
- }
- function newTile(tile)
- tile=tile or (math.random(1,7))
- obj.id=tile
- obj.x,obj.y=StockBlocks[tile].start_position.x,StockBlocks[tile].start_position.y
- obj.rotate=1
- -- print("!")
- end
- function drawBlock(x,y,color)
- gpu.set((x-1)*2+1,y," ")
- end
- function drawTile(color)
- gpu.setBackground(color or 0x005500)
- -- print(obj.id,obj.rotate)
- for i=1,4 do
- drawBlock(StockBlocks[obj.id].transformations[obj.rotate][i].x+obj.x,StockBlocks[obj.id].transformations[obj.rotate][i].y+obj.y)
- end
- gpu.setBackground(0x000000)
- end
- function test()
- for i=1,4 do
- local x,y=StockBlocks[obj.id].transformations[obj.rotate][i].x+obj.x,StockBlocks[obj.id].transformations[obj.rotate][i].y+obj.y
- if (y==30)or(mas[fromXY(x,y)]~=0) then
- return true
- end
- end
- return false
- end
- function putInMap()
- for j=1,4 do
- mas[fromXY(StockBlocks[obj.id].transformations[obj.rotate][j].x+obj.x,StockBlocks[obj.id].transformations[obj.rotate][j].y+obj.y)]=1
- end
- end
- function mapUpdate()
- drawTile(0x000000)
- obj.y=obj.y+1
- -- obj.x=obj.x+obj.dX
- if test() then
- -- obj.x=obj.x-obj.dX
- obj.y=obj.y-1
- drawTile()
- putInMap()
- newTile()
- else
- if obj.dY==1 then
- drawTile(0x000000)
- obj.y=obj.y+1
- if test() then
- obj.y=obj.y-1
- drawTile()
- putInMap()
- newTile()
- end
- end
- if obj.dX~=0 then
- drawTile(0x000000)
- obj.x=obj.x+obj.dX
- if test() then
- obj.x=obj.x-obj.dX
- end
- end
- if obj.dR~=0 then
- local tmp=obj.rotate
- drawTile(0x000000)
- obj.rotate=(obj.rotate+#StockBlocks[obj.id].transformations)%(#StockBlocks[obj.id].transformations)+1
- if test() then
- obj.rotate=tmp
- end
- -- obj.rotate=obj.rotate+1
- end
- -- drawTile()
- -- os.sleep(0.3)
- -- drawTile(0x000000)
- -- obj.y=obj.y+1
- --[[ if test() then
- drawTile()
- putInMap()
- newTile()
- end
- end]]--
- end
- obj.dX=0
- obj.dY=0
- obj.dR=0
- drawTile()
- end
- function onKey(ev,_,code1,code2,player)
- if (code1==113)and(code2==16) then
- isRunning=false
- elseif (code1==0) then
- if code2==203 then--left
- obj.dX=-1
- elseif code2==200 then--up
- obj.dR=1
- elseif code2==205 then--right
- obj.dX=1
- elseif code2==208 then--down
- obj.dY=1
- end
- end
- end
- newTile()
- drawTile()
- isRunning=true
- event.ignore("key_down",onKey)
- event.listen("key_down",onKey)
- while isRunning do
- os.sleep(0.3)
- mapUpdate()
- end
- event.ignore("key_down",onKey)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement