0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Playdate】paturnを使って動く罫線アニメーションを作る

Posted at

capture_221224.gif

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 thenplaydate.update()内でカウントアップしているtickが10で割り切れる値になる毎に実行するようにしています。
入れ替えたpatterngfx.setPattern(pattern)で描画時のパターン設定をしています。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?