Starting Simple

Im trying to get into the SDK and just wrapping my head into simple stuff.
What am I missing here to display the triangle on screen ?

import "CoreLibs/object"
import "CoreLibs/graphics"
import "CoreLibs/sprites"
import "CoreLibs/timer"

local gfx <const> = playdate.graphics
local playerShip = nil

function myGameSetUp()

    local playerShip = gfx.drawTriangle(100, 200, 300, 200, 200, 50)
    playerShip:moveTo( 200, 120 ) 
    playerShip:add() 

end

myGameSetUp()

function playdate.update()

    playdate.timer.updateTimers()

end

Hello!

As someone who only started with Lua and Playdate recently, I hope I can help explain what's wrong here and how to fix it!

You want to make a sprite that's a triangle. A sprite is its own kind of object; assigning it to the result of a draw function won't quite work. Sprites have their own draw functions. You wan't something like this:

import "CoreLibs/object"
import "CoreLibs/graphics"
import "CoreLibs/sprites"
import "CoreLibs/timer"

local gfx <const> = playdate.graphics
local playerShip = nil

function myGameSetUp()

	playerShip = gfx.sprite.new() -- this is how create a new sprite, almost always
	-- by the way, playerShip has already been created above, as a file-local var
	-- we probably don't want to make this sprite local to just this function,
	-- or we couldn't control it from anywhere else in the code
	
	playerShip:setSize(50, 50) -- unless we make the sprite a bitmap image, we need to set its size
	
	-- this is the function that will run whenever this sprite is drawn
	function playerShip:draw()
		gfx.drawTriangle(0,0, 50,20, 20, 50)
	end
	-- …another way to give a sprite an image is to either load a bitmap file,
	-- OR, draw INTO a bitmap file and set that as the sprite's image
	-- with sprite:setImage. But let's stick with this for now!
	 
	playerShip:moveTo(200, 120) 
	playerShip:add() 

end

myGameSetUp()

function playdate.update()
	playdate.timer.updateTimers() -- we're not actually using timers for anything yet…
	gfx.sprite.update()           -- …but we do have to update all sprites in the update loop!
end
1 Like

Thank you. Makes total sense when reading the docs I was assume sprite related to loading in image files.

I want to draw not use pngs if I can and draw it all programmatically.

Thanks

1 Like