View difference between Paste ID: MQhHLbVw and WV7SK6pm
SHOW: | | - or go back to the newest paste.
1-
local gpu=require("component").gpu
1+
local gpu=require("component").gpu
2-
local event=require("event")
2+
local event=require("event")
3-
3+
4-
width=20
4+
width=20
5-
5+
6-
mas={}
6+
mas={}
7-
7+
8-
StockBlocks = { { start_position = { x = 4, y = 1 },
8+
StockBlocks = { { start_position = { x = 4, y = 1 },
9-
                  color = { 175, 175, 0 },
9+
                  color = { 175, 175, 0 },
10-
                  symbol = "T",
10+
                  symbol = "T",
11-
                  transformations = { { { x = -1, y = 0 },
11+
                  transformations = { { { x = -1, y = 0 },
12-
                                        { x = 0, y = 0 },
12+
                                        { x = 0, y = 0 },
13-
                                        { x = 1, y = 0 },
13+
                                        { x = 1, y = 0 },
14-
                                        { x = 0, y = -1 } },
14+
                                        { x = 0, y = -1 } },
15-
                                      { { x = 0, y = 0 },
15+
                                      { { x = 0, y = 0 },
16-
                                        { x = 0, y = -1 },
16+
                                        { x = 0, y = -1 },
17-
                                        { x = 0, y = 1 },
17+
                                        { x = 0, y = 1 },
18-
                                        { x = 1, y = 0 } },
18+
                                        { x = 1, y = 0 } },
19-
                                      { { x = -1, y = 0 },
19+
                                      { { x = -1, y = 0 },
20-
                                        { x = 0, y = 0 },
20+
                                        { x = 0, y = 0 },
21-
                                        { x = 1, y = 0 },
21+
                                        { x = 1, y = 0 },
22-
                                        { x = 0, y = 1 } },
22+
                                        { x = 0, y = 1 } },
23-
                                      { { x = 0, y = 0 },
23+
                                      { { x = 0, y = 0 },
24-
                                        { x = 0, y = -1 },
24+
                                        { x = 0, y = -1 },
25-
                                        { x = 0, y = 1 },
25+
                                        { x = 0, y = 1 },
26-
                                        { x = -1, y = 0 } } } },
26+
                                        { x = -1, y = 0 } } } },
27-
                { start_position = { x = 4, y = 1 },
27+
                { start_position = { x = 4, y = 1 },
28-
                  color = { 0, 255, 0 },
28+
                  color = { 0, 255, 0 },
29-
                  symbol = "Z",
29+
                  symbol = "Z",
30-
                  transformations = { { { x = -1, y = 0 },
30+
                  transformations = { { { x = -1, y = 0 },
31-
                                        { x = 0, y = 0 },
31+
                                        { x = 0, y = 0 },
32-
                                        { x = 0, y = -1 },
32+
                                        { x = 0, y = -1 },
33-
                                        { x = 1, y = -1 } },
33+
                                        { x = 1, y = -1 } },
34-
                                      { { x = 0, y = -1 },
34+
                                      { { x = 0, y = -1 },
35-
                                        { x = 0, y = 0 },
35+
                                        { x = 0, y = 0 },
36-
                                        { x = 1, y = 0 },
36+
                                        { x = 1, y = 0 },
37-
                                        { x = 1, y = 1 } } } },
37+
                                        { x = 1, y = 1 } } } },
38-
                { start_position = { x = 4, y = 1 }, 
38+
                { start_position = { x = 4, y = 1 }, 
39-
                  color = { 100, 100, 175 },
39+
                  color = { 100, 100, 175 },
40-
                  symbol = "S",
40+
                  symbol = "S",
41-
                  transformations = { { { x = 1, y = 0 },
41+
                  transformations = { { { x = 1, y = 0 },
42-
                                        { x = 0, y = 0 },
42+
                                        { x = 0, y = 0 },
43-
                                        { x = 0, y = -1 },
43+
                                        { x = 0, y = -1 },
44-
                                        { x = -1, y = -1 } },
44+
                                        { x = -1, y = -1 } },
45-
                                      { { x = 0, y = 1 },
45+
                                      { { x = 0, y = 1 },
46-
                                        { x = 0, y = 0 },
46+
                                        { x = 0, y = 0 },
47-
                                        { x = 1, y = 0 },
47+
                                        { x = 1, y = 0 },
48-
                                        { x = 1, y = -1 } } } },
48+
                                        { x = 1, y = -1 } } } },
49-
                { start_position = { x = 4, y = 0 }, 
49+
                { start_position = { x = 4, y = 0 }, 
50-
                  color = { 255, 255, 100 },
50+
                  color = { 255, 255, 100 },
51-
                  symbol = "J",
51+
                  symbol = "J",
52-
                  transformations = { { { x = -1, y = 0 },
52+
                  transformations = { { { x = -1, y = 0 },
53-
                                        { x = 0, y = 0 },
53+
                                        { x = 0, y = 0 },
54-
                                        { x = 1, y = 0 },
54+
                                        { x = 1, y = 0 },
55-
                                        { x = -1, y = 1 } },
55+
                                        { x = -1, y = 1 } },
56-
                                      { { x = 0, y = -1 },
56+
                                      { { x = 0, y = -1 },
57-
                                        { x = 0, y = 0 },
57+
                                        { x = 0, y = 0 },
58-
                                        { x = 0, y = 1 },
58+
                                        { x = 0, y = 1 },
59-
                                        { x = 1, y = 1 } },
59+
                                        { x = 1, y = 1 } },
60-
                                      { { x = -1, y = 0 },
60+
                                      { { x = -1, y = 0 },
61-
                                        { x = 0, y = 0 },
61+
                                        { x = 0, y = 0 },
62-
                                        { x = 1, y = 0 },
62+
                                        { x = 1, y = 0 },
63-
                                        { x = 1, y = -1 } },
63+
                                        { x = 1, y = -1 } },
64-
                                      { { x = 0, y = -1 },
64+
                                      { { x = 0, y = -1 },
65-
                                        { x = 0, y = 0 },
65+
                                        { x = 0, y = 0 },
66-
                                        { x = 0, y = 1 },
66+
                                        { x = 0, y = 1 },
67-
                                        { x = -1, y = -1 } } } },
67+
                                        { x = -1, y = -1 } } } },
68-
                { start_position = { x = 4, y = 0 },
68+
                { start_position = { x = 4, y = 0 },
69-
                  color = { 255, 0, 255 },
69+
                  color = { 255, 0, 255 },
70-
                  symbol = "L",
70+
                  symbol = "L",
71-
                  transformations = { { { x = -1, y = 0 },
71+
                  transformations = { { { x = -1, y = 0 },
72-
                                        { x = 0, y = 0 },
72+
                                        { x = 0, y = 0 },
73-
                                        { x = 1, y = 0 },
73+
                                        { x = 1, y = 0 },
74-
                                        { x = 1, y = 1 } },
74+
                                        { x = 1, y = 1 } },
75-
                                      { { x = 0, y = -1 },
75+
                                      { { x = 0, y = -1 },
76-
                                        { x = 0, y = 0 },
76+
                                        { x = 0, y = 0 },
77-
                                        { x = 0, y = 1 },
77+
                                        { x = 0, y = 1 },
78-
                                        { x = 1, y = -1 } },
78+
                                        { x = 1, y = -1 } },
79-
                                      { { x = -1, y = 0 },
79+
                                      { { x = -1, y = 0 },
80-
                                        { x = 0, y = 0 },
80+
                                        { x = 0, y = 0 },
81-
                                        { x = 1, y = 0 },
81+
                                        { x = 1, y = 0 },
82-
                                        { x = -1, y = -1 } },
82+
                                        { x = -1, y = -1 } },
83-
                                      { { x = 0, y = -1 },
83+
                                      { { x = 0, y = -1 },
84-
                                        { x = 0, y = 0 },
84+
                                        { x = 0, y = 0 },
85-
                                        { x = 0, y = 1 },
85+
                                        { x = 0, y = 1 },
86-
                                        { x = -1, y = 1 } } } },
86+
                                        { x = -1, y = 1 } } } },
87-
                { start_position = { x = 4, y = 0 },
87+
                { start_position = { x = 4, y = 0 },
88-
                  color = { 255, 0, 0 },
88+
                  color = { 255, 0, 0 },
89-
                  symbol = "I",
89+
                  symbol = "I",
90-
                  transformations = { { { x = -1, y = 0 },
90+
                  transformations = { { { x = -1, y = 0 },
91-
                                        { x = 0, y = 0 },
91+
                                        { x = 0, y = 0 },
92-
                                        { x = 1, y = 0 },
92+
                                        { x = 1, y = 0 },
93-
                                        { x = 2, y = 0 } },
93+
                                        { x = 2, y = 0 } },
94-
                                      { { x = 0, y = -1 },
94+
                                      { { x = 0, y = -1 },
95-
                                        { x = 0, y = 0 },
95+
                                        { x = 0, y = 0 },
96-
                                        { x = 0, y = 1 },
96+
                                        { x = 0, y = 1 },
97-
                                        { x = 0, y = 2 } } } },
97+
                                        { x = 0, y = 2 } } } },
98-
                { start_position = { x = 4, y = 0 },
98+
                { start_position = { x = 4, y = 0 },
99-
                  color = { 0, 0, 255 },
99+
                  color = { 0, 0, 255 },
100-
                  symbol = "[]",
100+
                  symbol = "[]",
101-
                  transformations = { { { x = 0, y = 0 },
101+
                  transformations = { { { x = 0, y = 0 },
102-
                                        { x = 0, y = 1 },
102+
                                        { x = 0, y = 1 },
103-
                                        { x = 1, y = 0 },
103+
                                        { x = 1, y = 0 },
104-
                                        { x = 1, y = 1 } } } }
104+
                                        { x = 1, y = 1 } } } }
105-
             }
105+
             }
106-
106+
107-
function toXY(num,wid)
107+
function toXY(num,wid)
108-
  wid=wid or width
108+
  wid=wid or width
109-
  y=math.floor((num-1)/wid)+1
109+
  y=math.floor((num-1)/wid)+1
110-
  return num-(y-1)*width,y
110+
  return num-(y-1)*width,y
111-
end
111+
end
112-
function fromXY(x,y,wid)
112+
function fromXY(x,y,wid)
113-
  wid=wid or width
113+
  wid=wid or width
114-
  return (y-1)*wid+x
114+
  return (y-1)*wid+x
115-
end
115+
end
116-
116+
117-
for i=1,1000 do
117+
for i=1,1000 do
118-
  mas[i]=0
118+
  mas[i]=0
119-
end
119+
end
120-
--print(StockBlocks[1].transformations[1][1].y)
120+
--print(StockBlocks[1].transformations[1][1].y)
121-
--[[f=7
121+
--[[f=7
122-
tr=1
122+
tr=1
123-
for i=1,4 do
123+
for i=1,4 do
124-
  gpu.set(StockBlocks[f].transformations[tr][i].x+5,StockBlocks[f].transformations[tr][i].y+5,"!")
124+
  gpu.set(StockBlocks[f].transformations[tr][i].x+5,StockBlocks[f].transformations[tr][i].y+5,"!")
125-
end]]--
125+
end]]--
126-
obj={
126+
obj={
127-
  id=-1,
127+
  id=-1,
128-
  x=-1,
128+
  x=-1,
129-
  y=-1,
129+
  y=-1,
130-
  dX=0,
130+
  dX=0,
131-
  dY=0,
131+
  dY=0,
132-
  dR=0,
132+
  dR=0,
133-
  rotate=-1,
133+
  rotate=-1,
134-
}
134+
}
135-
function newTile(tile)
135+
function newTile(tile)
136-
  tile=tile or (math.random(1,7))
136+
  tile=tile or (math.random(1,7))
137-
  obj.id=tile
137+
  obj.id=tile
138-
  obj.x,obj.y=StockBlocks[tile].start_position.x,StockBlocks[tile].start_position.y
138+
  obj.x,obj.y=StockBlocks[tile].start_position.x,StockBlocks[tile].start_position.y
139-
  obj.rotate=1
139+
  obj.rotate=1
140-
--  print("!")
140+
--  print("!")
141-
end
141+
end
142-
function drawBlock(x,y,color)
142+
function drawBlock(x,y,color)
143-
  gpu.set((x-1)*2+1,y,"  ")
143+
  gpu.set((x-1)*2+1,y,"  ")
144-
end
144+
end
145-
function drawTile(color)
145+
function drawTile(color)
146-
  gpu.setBackground(color or 0x005500)
146+
  gpu.setBackground(color or 0x005500)
147-
--  print(obj.id,obj.rotate)
147+
--  print(obj.id,obj.rotate)
148-
  for i=1,4 do
148+
  for i=1,4 do
149-
    drawBlock(StockBlocks[obj.id].transformations[obj.rotate][i].x+obj.x,StockBlocks[obj.id].transformations[obj.rotate][i].y+obj.y)
149+
    drawBlock(StockBlocks[obj.id].transformations[obj.rotate][i].x+obj.x,StockBlocks[obj.id].transformations[obj.rotate][i].y+obj.y)
150-
  end
150+
  end
151-
  gpu.setBackground(0x000000)
151+
  gpu.setBackground(0x000000)
152-
end
152+
end
153-
function test()
153+
function test()
154-
  for i=1,4 do
154+
  for i=1,4 do
155-
    local x,y=StockBlocks[obj.id].transformations[obj.rotate][i].x+obj.x,StockBlocks[obj.id].transformations[obj.rotate][i].y+obj.y
155+
    local x,y=StockBlocks[obj.id].transformations[obj.rotate][i].x+obj.x,StockBlocks[obj.id].transformations[obj.rotate][i].y+obj.y
156-
    if (y==30)or(mas[fromXY(x,y)]~=0) then
156+
    if (y==30)or(mas[fromXY(x,y)]~=0) then
157-
      return true
157+
      return true
158-
    end
158+
    end
159-
  end
159+
  end
160-
  return false
160+
  return false
161-
end
161+
end
162-
function putInMap()
162+
function putInMap()
163-
  for j=1,4 do
163+
  for j=1,4 do
164-
    mas[fromXY(StockBlocks[obj.id].transformations[obj.rotate][j].x+obj.x,StockBlocks[obj.id].transformations[obj.rotate][j].y+obj.y)]=1
164+
    mas[fromXY(StockBlocks[obj.id].transformations[obj.rotate][j].x+obj.x,StockBlocks[obj.id].transformations[obj.rotate][j].y+obj.y)]=1
165-
  end
165+
  end
166-
end
166+
end
167-
function mapUpdate()
167+
function mapUpdate()
168-
  drawTile(0x000000)
168+
  drawTile(0x000000)
169-
  obj.y=obj.y+1
169+
  obj.y=obj.y+1
170-
--  obj.x=obj.x+obj.dX
170+
--  obj.x=obj.x+obj.dX
171-
  if test() then
171+
  if test() then
172-
--    obj.x=obj.x-obj.dX
172+
--    obj.x=obj.x-obj.dX
173-
    obj.y=obj.y-1
173+
    obj.y=obj.y-1
174-
    drawTile()
174+
    drawTile()
175-
    putInMap()
175+
    putInMap()
176-
    newTile()
176+
    newTile()
177-
  else
177+
  else
178-
    if obj.dY==1 then
178+
    if obj.dY==1 then
179-
      drawTile(0x000000)
179+
      drawTile(0x000000)
180-
      obj.y=obj.y+1
180+
      obj.y=obj.y+1
181-
      if test() then
181+
      if test() then
182-
        obj.y=obj.y-1
182+
        obj.y=obj.y-1
183-
        drawTile()
183+
        drawTile()
184-
        putInMap()
184+
        putInMap()
185-
        newTile()
185+
        newTile()
186-
      end
186+
      end
187-
    end
187+
    end
188-
    if obj.dX~=0 then
188+
    if obj.dX~=0 then
189-
      drawTile(0x000000)
189+
      drawTile(0x000000)
190-
      obj.x=obj.x+obj.dX
190+
      obj.x=obj.x+obj.dX
191-
      if test() then
191+
      if test() then
192-
        obj.x=obj.x-obj.dX
192+
        obj.x=obj.x-obj.dX
193-
      end
193+
      end
194-
    end
194+
    end
195-
    if obj.dR~=0 then
195+
    if obj.dR~=0 then
196-
      local tmp=obj.rotate
196+
      local tmp=obj.rotate
197-
      drawTile(0x000000)
197+
      drawTile(0x000000)
198-
      obj.rotate=(obj.rotate+#StockBlocks[obj.id].transformations)%(#StockBlocks[obj.id].transformations)+1
198+
      obj.rotate=(obj.rotate+#StockBlocks[obj.id].transformations)%(#StockBlocks[obj.id].transformations)+1
199-
      if test() then
199+
      if test() then
200-
        obj.rotate=tmp
200+
        obj.rotate=tmp
201-
      end
201+
      end
202-
--      obj.rotate=obj.rotate+1
202+
--      obj.rotate=obj.rotate+1
203-
    end
203+
    end
204-
--      drawTile()
204+
--      drawTile()
205-
--      os.sleep(0.3)
205+
--      os.sleep(0.3)
206-
--      drawTile(0x000000)
206+
--      drawTile(0x000000)
207-
--      obj.y=obj.y+1
207+
--      obj.y=obj.y+1
208-
--[[      if test() then
208+
--[[      if test() then
209-
        drawTile()
209+
        drawTile()
210-
        putInMap()
210+
        putInMap()
211-
        newTile()
211+
        newTile()
212-
      end
212+
      end
213-
    end]]--
213+
    end]]--
214-
  end
214+
  end
215-
  obj.dX=0
215+
  obj.dX=0
216-
  obj.dY=0
216+
  obj.dY=0
217-
  obj.dR=0
217+
  obj.dR=0
218-
  drawTile()
218+
  drawTile()
219-
end
219+
end
220-
function onKey(ev,_,code1,code2,player)
220+
function onKey(ev,_,code1,code2,player)
221-
  if (code1==113)and(code2==16) then
221+
  if (code1==113)and(code2==16) then
222-
    isRunning=false
222+
    isRunning=false
223-
  elseif (code1==0) then
223+
  elseif (code1==0) then
224-
    if code2==203 then--left
224+
    if code2==203 then--left
225-
      obj.dX=-1
225+
      obj.dX=-1
226-
    elseif code2==200 then--up
226+
    elseif code2==200 then--up
227-
      obj.dR=1
227+
      obj.dR=1
228-
    elseif code2==205 then--right
228+
    elseif code2==205 then--right
229-
      obj.dX=1
229+
      obj.dX=1
230-
    elseif code2==208 then--down
230+
    elseif code2==208 then--down
231-
      obj.dY=1
231+
      obj.dY=1
232-
    end
232+
    end
233-
  end
233+
  end
234-
end
234+
end
235-
newTile()
235+
newTile()
236-
drawTile()
236+
drawTile()
237-
isRunning=true
237+
isRunning=true
238
event.ignore("key_down",onKey)
239-
event.listen("key_down",onKey)
239+
event.listen("key_down",onKey)
240-
while isRunning do
240+
while isRunning do
241-
  os.sleep(0.3)
241+
  os.sleep(0.3)
242-
  mapUpdate()
242+
  mapUpdate()
243-
end
243+
end
244
event.ignore("key_down",onKey)