-- =============================================================
-- 以下は、クランクを使って円を動かすことができる簡単なサンプルプログラムです。
-- このファイルの内容はすべて削除しても構いませんが、
-- Playdate.update 関数は必ず追加してください。
-- Playdate ゲームには必ずこの関数が必要です!
-- =============================================================
-- drawCircleAtPointとcrankIndicatorで使用されるライブラリをインポートします.
import "CoreLibs/graphics"
import "CoreLibs/ui"
-- ライブラリの機能の名前を短くして使いやすくします.
local pd <const> = playdate
local gfx <const> = playdate.graphics
-- プレイヤーの変数を定義します.
local playerSize = 10 -- プレイヤーのサイズ
local playerVelocity = 3 -- プレイヤーの速度
local playerX, playerY = 200, 120 -- プレイヤーの初期位置
-- プレイヤーの画像を描画します.
local playerImage = gfx.image.new(32, 32)
gfx.pushContext(playerImage)
-- アウトラインを描画
gfx.drawRoundRect(4, 3, 24, 26, 1)
-- 画面を描画
gfx.drawRect(7, 6, 18, 12)
-- 目を描画
gfx.drawLine(10, 12, 12, 10)
gfx.drawLine(12, 10, 14, 12)
gfx.drawLine(17, 12, 19, 10)
gfx.drawLine(19, 10, 21, 12)
-- クランクを描画
gfx.drawRect(27, 15, 3, 9)
-- A/Bボタンを描画
gfx.drawCircleInRect(16, 20, 4, 4)
gfx.drawCircleInRect(21, 20, 4, 4)
-- D-Padを描画
gfx.drawRect(8, 22, 6, 2)
gfx.drawRect(10, 20, 2, 6)
gfx.popContext()
-- valueをmin〜max範囲内でループ.
local function ring(value, min, max)
if (min > max) then
-- 最小と最大が逆になっている場合は入れ替えます.
min, max = max, min
end
return min + (value - min) % (max - min)
end
-- playdate.update 関数は必ず追加しなければいけません.
function playdate.update()
-- 画面をクリア
gfx.clear()
if pd.isCrankDocked() then
-- クランクがドックされている (収納されている) 場合はクランクインジケーターを描画します.
pd.ui.crankIndicator:draw()
else
-- クランクの角度から速度を計算します.
-- getCrankPosition()は上始まりの時計回り.
-- -90で右から開始。時計回りなのでsinにマイナスをかけない.
local crankPosition = pd.getCrankPosition() - 90
local xVelocity = math.cos(math.rad(crankPosition)) * playerVelocity
local yVelocity = math.sin(math.rad(crankPosition)) * playerVelocity
-- プレイヤーを移動します.
playerX += xVelocity
playerY += yVelocity
-- プレイヤーの位置をループさせます.
playerX = ring(playerX, -playerSize, 400 + playerSize)
playerY = ring(playerY, -playerSize, 240 + playerSize)
end
-- テキストを描画します.
gfx.drawTextAligned("Template configured!", 200, 30, kTextAlignment.center)
-- プレイヤーを描画します.
playerImage:drawAnchored(playerX, playerY, 0.5, 0.5)
end