

補足:操作キャラクターの画像リソースを自作する場合は、最低でも "32px" はないと視認性が低くなってしまうとのことです。
local pd = playdate
local gfx = pd.graphics
-- Player.
--- プレイヤーの位置.
local playerX = 40
local playerY = 120
--- プレイヤー画像の読み込み (".png" の指定はあってもなくても良いらしい).
local playerImage = gfx.image.new("images/capybara")
-- ゲーム更新.
function pd.update()
-- プレイヤー画像の描画.
playerImage:draw(playerX, playerY)
end

■注意点:
プレイヤー画像の描画は「playerImage.draw()」と他の言語で見慣れた書き方「. (ピリオド)」にしてしまうとエラーとなります。
main.lua:14: bad argument #1 to 'draw' (playdate.graphics.image expected, got number) stack traceback: [C]: in field 'draw' main.lua:14: in function <main.lua:12>正しくは「:(コロン)」でメソッドを指定する必要があります。
local pd = playdate
local gfx = pd.graphics
-- Player.
--- プレイヤーの位置.
local playerX = 40
local playerY = 120
--- プレイヤーの速度.
local playerSpeed = 3
--- プレイヤー画像の読み込み.
local playerImage = gfx.image.new("images/capybara")
-- ゲーム更新.
function pd.update()
-- クランクの位置で移動する.
local crankPosition = pd.getCrankPosition()
if crankPosition <= 90 or crankPosition >= 270 then
-- クランクが水平方向より上にあるときはプレイヤーを上に移動させる.
playerY -= playerSpeed
else
-- クランクが水平方向より下にあるときはプレイヤーを下に移動させる.
playerY += playerSpeed
end
-- プレイヤー画像の描画.
playerImage:draw(playerX, playerY)
end

...
-- ゲーム更新.
function pd.update()
-- 画面をクリア.
gfx.clear() -- これを追加.
-- クランクの位置で移動する.
local crankPosition = pd.getCrankPosition()
...

import "CoreLibs/sprites" -- spriteの機能を使うために必要.
local pd = playdate
local gfx = pd.graphics
-- Player.
--- プレイヤーの開始位置.
local playerStartX = 40
local playerStartY = 120
--- プレイヤーの速度.
local playerSpeed = 3
--- プレイヤー画像の読み込み.
local playerImage = gfx.image.new("images/capybara")
--- プレイヤースプライトの作成.
local playerSprite = gfx.sprite.new(playerImage) -- 画像をもとに生成.
playerSprite:setCollideRect(0, 0, 64, 48) -- 64x48の当たり判定を設定.
playerSprite:moveTo(playerStartX, playerStartY) -- プレイヤーを開始位置に移動.
playerSprite:add() -- プレイヤースプライトをゲームに追加.
...
import "CoreLibs/sprites" -- spriteの機能を使うために必要.
local pd = playdate
local gfx = pd.graphics
-- Player.
--- プレイヤーの開始位置.
local playerStartX = 40
local playerStartY = 120
--- プレイヤーの速度.
local playerSpeed = 3
--- プレイヤー画像の読み込み.
local playerImage = gfx.image.new("images/capybara")
--- プレイヤースプライトの作成.
local playerSprite = gfx.sprite.new(playerImage) -- 画像をもとに生成.
playerSprite:setCollideRect(0, 0, 64, 48) -- 64x48の当たり判定を設定.
playerSprite:moveTo(playerStartX, playerStartY) -- プレイヤーを開始位置に移動.
playerSprite:add() -- プレイヤースプライトをゲームに追加.
-- ゲーム更新.
function pd.update()
-- spriteの描画だけであれば画面クリアは不要.
-- gfx.clear()
-- クランクの位置で移動する.
local crankPosition = pd.getCrankPosition()
if crankPosition <= 90 or crankPosition >= 270 then
-- クランクが水平方向より上にあるときはプレイヤーを上に移動させる.
playerSprite:moveBy(0, -playerSpeed)
else
-- クランクが水平方向より下にあるときはプレイヤーを下に移動させる.
playerSprite:moveBy(0, playerSpeed)
end
-- すべてのスプライトを更新して描画.
gfx.sprite.update()
end


--- プレイヤースプライトの作成.
local playerSprite = gfx.sprite.new(playerImage) -- 画像をもとに生成.
playerSprite:setCollideRect(0, 0, 56, 40) -- 56x40の当たり判定を設定.
playerSprite:moveTo(playerStartX, playerStartY) -- プレイヤーを開始位置に移動.

...
--- プレイヤースプライトの作成.
local playerSprite = gfx.sprite.new(playerImage) -- 画像をもとに生成.
playerSprite:setCollideRect(0, 0, 56, 40) -- 56x40の当たり判定を設定.
playerSprite:moveTo(playerStartX, playerStartY) -- プレイヤーを開始位置に移動.
playerSprite:add() -- プレイヤースプライトをゲームに追加.
-- Gameの状態を追加.
local gameState = "stopped"
-- ゲーム更新.
function pd.update()
if gameState == "stopped" then
-- "Press A to start" の文字を表示.
gfx.drawText("Press A to Start", 200, 40)
if pd.buttonJustPressed(pd.kButtonA) then
-- Aボタンが押されたらゲームを開始.
gameState = "playing"
end
elseif gameState == "playing" then
-- ゲームが開始されたら、プレイヤーを移動させる.
-- クランクの位置で移動する.
local crankPosition = pd.getCrankPosition()
if crankPosition <= 90 or crankPosition >= 270 then
-- クランクが水平方向より上にあるときはプレイヤーを上に移動させる.
playerSprite:moveBy(0, -playerSpeed)
else
-- クランクが水平方向より下にあるときはプレイヤーを下に移動させる.
playerSprite:moveBy(0, playerSpeed)
end
-- すべてのスプライトを更新して描画.
gfx.sprite.update()
end
end
import "CoreLibs/graphics" -- graphicsの機能を使うために必要.
import "CoreLibs/sprites" -- spriteの機能を使うために必要.
...
if gameState == "stopped" then
-- "Press A to start" の文字を中央揃えで表示.
gfx.drawTextAligned("Press A to Start", 200, 40, kTextAlignment.center)
...
...
elseif gameState == "playing" then
-- ゲームが開始されたら、プレイヤーを移動させる.
-- クランクの位置で移動する.
local crankPosition = pd.getCrankPosition()
if crankPosition <= 90 or crankPosition >= 270 then
-- クランクが水平方向より上にあるときはプレイヤーを上に移動させる.
playerSprite:moveBy(0, -playerSpeed)
else
-- クランクが水平方向より下にあるときはプレイヤーを下に移動させる.
playerSprite:moveBy(0, playerSpeed)
end
if playerSprite.y < -30 or playerSprite.y > 270 then
-- プレイヤーが画面外に出たらゲームオーバー.
gameState = "stopped"
playerSprite:moveTo(playerStartX, playerStartY) -- プレイヤーを開始位置に戻す.
end
-- すべてのスプライトを更新して描画.
gfx.sprite.update()
end
...
...
-- Gameの状態を追加.
local gameState = "stopped"
-- 障害物.
local objstacleSpeed = 5 -- 移動速度.
local objstacleImage = gfx.image.new("images/rock") -- 岩画像の読み込み.
local objstacleSprite = gfx.sprite.new(objstacleImage) -- 岩スプライトの作成.
objstacleSprite:setCollideRect(0, 0, 48, 48) -- 48x48の当たり判定を設定.
objstacleSprite:moveTo(450, 240) -- 岩を右端に配置.
objstacleSprite:add() -- 岩スプライトをゲームに追加.
...
...
if gameState == "stopped" then
-- "Press A to start" の文字を中央揃えで表示.
gfx.drawTextAligned("Press A to Start", 200, 40, kTextAlignment.center)
if pd.buttonJustPressed(pd.kButtonA) then
-- Aボタンが押されたらゲームを開始.
gameState = "playing"
-- 障害物をランダムな位置に移動.
local randomY = math.random(40, 200)
objstacleSprite:moveTo(450, randomY)
end
...
elseif gameState == "playing" then
-- ゲームが開始されたら、プレイヤーを移動させる.
-- クランクの位置で移動する.
local crankPosition = pd.getCrankPosition()
if crankPosition <= 90 or crankPosition >= 270 then
-- クランクが水平方向より上にあるときはプレイヤーを上に移動させる.
playerSprite:moveBy(0, -playerSpeed)
else
-- クランクが水平方向より下にあるときはプレイヤーを下に移動させる.
playerSprite:moveBy(0, playerSpeed)
end
-- 障害物の移動.
objstacleSprite:moveBy(-objstacleSpeed, 0)
-- 障害物が画面外に出たら、右端に戻す.
if objstacleSprite.x < -20 then
local randomY = math.random(40, 200)
objstacleSprite:moveTo(450, randomY)
end
if playerSprite.y < -30 or playerSprite.y > 270 then
-- プレイヤーが画面外に出たらゲームオーバー.
gameState = "stopped"
playerSprite:moveTo(playerStartX, playerStartY) -- プレイヤーを開始位置に戻す.
end
-- すべてのスプライトを更新して描画.
gfx.sprite.update()
...

...
elseif gameState == "playing" then
-- ゲームが開始されたら、プレイヤーを移動させる.
-- クランクの位置で移動する.
local crankPosition = pd.getCrankPosition()
if crankPosition <= 90 or crankPosition >= 270 then
-- クランクが水平方向より上にあるときはプレイヤーを上に移動させる.
playerSprite:moveBy(0, -playerSpeed)
else
-- クランクが水平方向より下にあるときはプレイヤーを下に移動させる.
playerSprite:moveBy(0, playerSpeed)
end
-- 衝突を考慮した障害物の移動.
--objstacleSprite:moveBy(-objstacleSpeed, 0)
local nextX = objstacleSprite.x - objstacleSpeed
local nextY = objstacleSprite.y
local actualX, actualY, collsions, length = objstacleSprite:moveWithCollisions(nextX, nextY);
-- 障害物が画面外に出たら、右端に戻す.
if objstacleSprite.x < -20 then
local randomY = math.random(40, 200)
objstacleSprite:moveTo(450, randomY)
end
if length > 0 or playerSprite.y < -30 or playerSprite.y > 270 then
-- 衝突 or プレイヤーが画面外に出たらゲームオーバー.
gameState = "stopped"
playerSprite:moveTo(playerStartX, playerStartY) -- プレイヤーを開始位置に戻す.
end
-- すべてのスプライトを更新して描画.
gfx.sprite.update()
...
import "CoreLibs/graphics" -- graphicsの機能を使うために必要.
import "CoreLibs/sprites" -- spriteの機能を使うために必要.
local pd = playdate
local gfx = pd.graphics
-- Player.
--- プレイヤーの開始位置.
local playerStartX = 40
local playerStartY = 120
--- プレイヤーの速度.
local playerSpeed = 3
--- プレイヤー画像の読み込み.
local playerImage = gfx.image.new("images/capybara")
--- プレイヤースプライトの作成.
local playerSprite = gfx.sprite.new(playerImage) -- 画像をもとに生成.
playerSprite:setCollideRect(0, 0, 56, 40) -- 56x40の当たり判定を設定.
playerSprite:moveTo(playerStartX, playerStartY) -- プレイヤーを開始位置に移動.
playerSprite:add() -- プレイヤースプライトをゲームに追加.
-- Gameの状態を追加.
local gameState = "stopped"
-- 障害物.
local objstacleSpeed = 5 -- 移動速度.
local objstacleImage = gfx.image.new("images/rock") -- 岩画像の読み込み.
local objstacleSprite = gfx.sprite.new(objstacleImage) -- 岩スプライトの作成.
objstacleSprite:setCollideRect(0, 0, 48, 48) -- 48x48の当たり判定を設定.
objstacleSprite:moveTo(450, 240) -- 岩を右端に配置.
objstacleSprite:add() -- 岩スプライトをゲームに追加.
-- ゲーム更新.
function pd.update()
if gameState == "stopped" then
-- "Press A to start" の文字を中央揃えで表示.
gfx.drawTextAligned("Press A to Start", 200, 40, kTextAlignment.center)
if pd.buttonJustPressed(pd.kButtonA) then
-- Aボタンが押されたらゲームを開始.
gameState = "playing"
-- プレイヤーを開始位置に戻す.
playerSprite:moveTo(playerStartX, playerStartY)
-- 障害物をランダムな位置に移動.
local randomY = math.random(40, 200)
objstacleSprite:moveTo(450, randomY)
end
elseif gameState == "playing" then
-- ゲームが開始されたら、プレイヤーを移動させる.
-- クランクの位置で移動する.
local crankPosition = pd.getCrankPosition()
if crankPosition <= 90 or crankPosition >= 270 then
-- クランクが水平方向より上にあるときはプレイヤーを上に移動させる.
playerSprite:moveBy(0, -playerSpeed)
else
-- クランクが水平方向より下にあるときはプレイヤーを下に移動させる.
playerSprite:moveBy(0, playerSpeed)
end
-- 衝突を考慮した障害物の移動.
--objstacleSprite:moveBy(-objstacleSpeed, 0)
local nextX = objstacleSprite.x - objstacleSpeed
local nextY = objstacleSprite.y
local actualX, actualY, collsions, length = objstacleSprite:moveWithCollisions(nextX, nextY);
-- 障害物が画面外に出たら、右端に戻す.
if objstacleSprite.x < -20 then
local randomY = math.random(40, 200)
objstacleSprite:moveTo(450, randomY)
end
if length > 0 or playerSprite.y < -30 or playerSprite.y > 270 then
-- 衝突 or プレイヤーが画面外に出たらゲームオーバー.
gameState = "stopped"
end
-- すべてのスプライトを更新して描画.
gfx.sprite.update()
end
end
...
-- 障害物.
local objstacleSpeed = 5 -- 移動速度.
local objstacleImage = gfx.image.new("images/rock") -- 岩画像の読み込み.
local objstacleSprite = gfx.sprite.new(objstacleImage) -- 岩スプライトの作成.
-- 衝突はオーバーラップ判定 (重なりのみ判定).
objstacleSprite.collisionResponse = gfx.sprite.kCollisionTypeOverlap
objstacleSprite:setCollideRect(0, 0, 48, 48) -- 48x48の当たり判定を設定.
objstacleSprite:moveTo(450, 240) -- 岩を右端に配置.
objstacleSprite:add() -- 岩スプライトをゲームに追加.
...

...
-- Gameの状態を追加.
local gameState = "stopped"
-- スコア.
local score = 0
-- 障害物.
...
...
if pd.buttonJustPressed(pd.kButtonA) then
-- Aボタンが押されたらゲームを開始.
gameState = "playing"
-- スコア変数の初期化.
score = 0
-- プレイヤーを開始位置に戻す.
...
...
-- 障害物が画面外に出たら、右端に戻す.
if objstacleSprite.x < -20 then
local randomY = math.random(40, 200)
objstacleSprite:moveTo(450, randomY)
-- スコアを加算.
score += 100
end
...
...
-- すべてのスプライトを更新して描画.
gfx.sprite.update()
end
-- スコアの描画 (右揃え).
gfx.drawTextAligned("Score: " .. score, 390, 10, kTextAlignment.right)
...

if gameState == "stopped" then
-- "Press A to start" の文字を中央揃えで表示.
gfx.drawTextAligned("Press A to Start", 200, 40, kTextAlignment.center)
if pd.buttonJustPressed(pd.kButtonA) then
-- Aボタンが押されたらゲームを開始.
gameState = "playing"
-- スコア変数の初期化.
score = 0
-- プレイヤーを開始位置に戻す.
playerSprite:moveTo(playerStartX, playerStartY)
-- 障害物をランダムな位置に移動.
local randomY = math.random(40, 200)
objstacleSprite:moveTo(450, randomY)
print("Game Started")
end
...




name=Capy Run author=takashi-kun games description=Super Capybara Action bundleID=com.kakashi-kun-games.capyrun version=1.0.0 buildNumber=1
| 項目名 | 説明 |
|---|---|
| name | アプリケーション名 (ゲームタイトル) |
| author | 作者名 または 開発チーム名 |
| description | 詳細説明 |
| bundleID | ユニークなドメイン。 ゲームの識別子となるので被らないIDにする必要があります |
| buildNumber | ゲームビルドバージョン。アップデート時にはインクリメントします |

※Playdate本体側のロックを解除する必要があります
