setPattern
を使うと2進数で表現されたバイナリパターンをグラフィックの描画時にセットできます。
こちらのプロジェクトをベースにsetPattern
を用いて動く罫線を描画します。
今回のプロジェクトは以下devフォーラムのスレッドがベースです。
main.lua
import "CoreLibs/object"
import "CoreLibs/graphics"
import "CoreLibs/sprites"
import "CoreLibs/timer"
local gfx <const> = playdate.graphics
local playerSprite = nil
local function b(e)
return tonumber(e, 2)
end
local pattern = {
b('11110000'),
b('11100001'),
b('11000011'),
b('10000111'),
b('00001111'),
b('00011110'),
b('00111100'),
b('01111000'),
}
local ticks = 0
function myGameSetUp()
local playerImage = gfx.image.new("Images/playerImage")
assert( playerImage )
playerSprite = gfx.sprite.new( playerImage )
playerSprite:moveTo( 200, 120 )
playerSprite:add()
local backgroundImage = gfx.image.new( "Images/background" )
assert( backgroundImage )
gfx.sprite.setBackgroundDrawingCallback(
function( x, y, width, height )
backgroundImage:draw( 0, 0 )
if ticks % 10 == 0 then
table.insert(pattern, 1, table.remove(pattern)) -- パターンをローテーション
end
gfx.setPattern(pattern)
gfx.drawRect(10,10,380,220) -- 矩形の線を描画
end
)
end
myGameSetUp()
function playdate.update()
if playdate.buttonIsPressed( playdate.kButtonUp ) then
playerSprite:moveBy( 0, -2 )
end
if playdate.buttonIsPressed( playdate.kButtonRight ) then
playerSprite:moveBy( 2, 0 )
end
if playdate.buttonIsPressed( playdate.kButtonDown ) then
playerSprite:moveBy( 0, 2 )
end
if playdate.buttonIsPressed( playdate.kButtonLeft ) then
playerSprite:moveBy( -2, 0 )
end
ticks += 1
gfx.sprite.redrawBackground()
gfx.sprite.update()
playdate.timer.updateTimers()
end
b(e)
というローカル関数が tonumber を実行するラッパー関数となります。
pattern
テーブルには2進数の文字列が記述されたパターンが記述されています。
myGameSetUp
の setBackgroundDrawingCallback
内でパターンをローテーションするためにtable.insert(pattern, 1, table.remove(pattern))
でpattern
内の値を入れ替えています。
前行のif ticks % 10 == 0 then
はplaydate.update()
内でカウントアップしているtick
が10で割り切れる値になる毎に実行するようにしています。
入れ替えたpattern
はgfx.setPattern(pattern)
で描画時のパターン設定をしています。