17. Loops
-- A numeric for has the following syntax
for i = 1, 5 do
print(i)
end
-- Output:
-- 1
-- 2
-- 3
-- 4
-- 5
Watchout:InLua,indicesstartsat1(ugh!)
17
19. Conditionals
-- If statements are not so different from other languages
if a < 0 then a = 0 end
-- If else statement
if a < b then
return a
else
return b
end
-- But I personally prefer the inlined style
if a < b then return a else return b end
19
21. LoadingaSprite
-- Importing Playdate core libs
import "CoreLibs/graphics"
import "CoreLibs/sprites"
gfx = playdate.graphics -- Shortcut to gfx library
player = gfx.sprite:new() -- Creates a new sprite object
player:setImage(gfx.image.new('player')) -- 'player.png' is being loaded
player:moveTo(200, 120) -- Moving the sprite in the center
player:addSprite() -- Adds the sprite to the display list
function playdate.update()
gfx.sprite.update() -- Calls update() on every sprite
end
21
23. -- Make the player sprite five time bigger
player:setScale(5)
23
24. -- Rotating a sprite (in degrees)
player:setRotation(135)
24
25. -- Flipping a sprite (on the horizontal side)
player:setImageFlip(gfx.kImageFlippedX)
25
26. TransformingaSprite
-- Setting the z-index for the sprite (high value = draw first)
player:setZIndex(10)
-- Sprites that aren’t visible don’t get their draw() method called
player:setVisible(false)
Formoresprite-relatedmethodshavealookattheirInsidePlaydatesection
26
27. Movingasprite
function playdate.leftButtonDown()
player:moveTo(player.x - 10, player.y) -- Player is moving to the left
end
function playdate.rightButtonDown()
player:moveTo(player.x + 10, player.y) -- Player is moving to the right
end
function playdate.upButtonDown()
player:moveTo(player.x, player.y - 10) -- Player is moving up
end
function playdate.downButtonDown()
player:moveTo(player.x, player.y + 10) -- Player is moving down
end
27