(10.3)積み上げる

コンピューター・ビュー:

-- 10.3 積み上げる
local w = platform.window.width()
local h = platform.window.height()
local marginLeft = 20
local marginTop = math.floor(h/200) 
local yokoCount = 5
local tateCount = 7
local dansa = math.floor(h/tateCount)
local sideLen = math.floor(dansa/1.3)

local black = 0x000000
local orange = 0xCD8500 
local red = 0xDC143C
local blue = 0x4169E1

local initYoko = math.floor(yokoCount/2)
local initTate = 0


-- 各マス目の状態を覚えておくテーブルを作る
local statusMasu = {}
for i = 0, tateCount do
   statusMasu[i] = {}
   for j = 0, yokoCount do
      statusMasu[i][j] = 0
   end
end

-- 左右の壁に最初からフラグを立てておく
for i = 1, tateCount do
   for j = 1, yokoCount, yokoCount - 1 do
      statusMasu[i][j] = 1 
   end
end

-- 底に最初からフラグを立てておく
for j = 1, yokoCount do
   statusMasu[tateCount][j] = 1 
end

-- フラグが 1 なら四角を描く(左上の座標, 1 辺の長さ, 色, gc) 
-- statusMasu[i][i] の左上の座標は (marginLeft + dansa * (j - 1), marginTop + dansa * (i - 1))
function drawBlocks(x, y, sideLen, color, gc)
   for i = 1, tateCount do
      for j = 1, yokoCount do
         if statusMasu[i][j] == 1 then
            gc:setColorRGB(color)
            gc:fillRect(x, y, sideLen, sideLen)
         end
      end
   end
end


function on.paint(gc)
   for i = 1, tateCount do
      for j = 1, yokoCount do
         gc:setFont("sansserif", "r", 7)
         gc:setColorRGB(black)
         --gc:drawString(statusMasu[i][j], marginLeft + dansa * (j - 1), marginTop + dansa * (i - 1), "top") -- 確認のためマス目テーブルの内容を表示する
         if statusMasu[i][j] == 1 then
            drawBlocks(marginLeft + dansa * (j - 1), marginTop + dansa * (i - 1), sideLen,orange, gc) -- "1" のところに四角を描く
         end
      end
   end
   -- 落ちる四角を描く
   function on.arrowKey(key)
      if key == "left" and statusMasu[initTate][initYoko] == 0 then initYoko = initYoko - 1
      elseif key == "right" and statusMasu[initTate][initYoko + 2] == 0 then initYoko = initYoko + 1
      end
   end
   gc:setColorRGB(blue)
   gc:fillRect(marginLeft + dansa * (initYoko), marginTop + dansa * (initTate - 1),sideLen, sideLen)
      if statusMasu[initTate + 1][initYoko + 1] == 0 then -- 直下が 0 なら
         initTate = initTate + 1  -- 1 マスずつ落ちる
      elseif statusMasu[initTate + 1][initYoko + 1] == 1 then -- 直下が 1 なら
         statusMasu[initTate][initYoko + 1] = 1  -- マス目テーブルに1を代入して
         initYoko = math.floor(yokoCount/2); initTate = 0     -- 落ちる四角を初期位置に戻す
      end
end

timer.start(0.1)
function on.timer()
   platform.window:invalidate()
end

参考文献: