import "CoreLibs/object" import "CoreLibs/graphics" import "CoreLibs/sprites" import "CoreLibs/timer" local gfx = playdate.graphics local playerSprite = nil function myGameSetUp() local playerImage = gfx.image.new("img/player.png") assert(playerImage) playerSprite = gfx.sprite.new(playerImage) playerSprite:moveTo(200, 120) -- (200,120) is the center of the Playdate screen playerSprite:add() -- We want an environment displayed behind our sprite. -- There are generally two ways to do this: -- 1) Use setBackgroundDrawingCallback() to draw a background image. (This is what we're doing below.) -- 2) Use a tilemap, assign it to a sprite with sprite:setTilemap(tilemap), -- and call :setZIndex() with some low number so the background stays behind -- your other sprites. -- local backgroundImage = gfx.image.new("Images/background") -- assert(backgroundImage) -- gfx.sprite.setBackgroundDrawingCallback( -- function(x, y, width, height) -- -- x,y,width,height is the updated area in sprite-local coordinates -- -- The clip rect is already set to this area, so we don't need to set it ourselves -- backgroundImage:draw(0, 0) -- end -- ) local table = gfx.imagetable.new("img/tilemap-table-16-16.png") local map = gfx.tilemap.new() map:setImageTable(table) map:draw(0, 0) end -- Now we'll call the function above to configure our game. -- After this runs (it just runs once), nearly everything will be -- controlled by the OS calling `playdate.update()` 30 times a second. myGameSetUp() -- `playdate.update()` is the heart of every Playdate game. -- This function is called right before every frame is drawn onscreen. -- Use this function to poll input, run game logic, and move sprites. function playdate.update() -- Poll the d-pad and move our player accordingly. -- (There are multiple ways to read the d-pad; this is the simplest.) -- Note that it is possible for more than one of these directions -- to be pressed at once, if the user is pressing diagonally. 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 -- Call the functions below in playdate.update() to draw sprites and keep -- timers updated. (We aren't using timers in this example, but in most -- average-complexity games, you will.) gfx.sprite.update() playdate.timer.updateTimers() end