Can't figure out sprites

,

I am a Playdate SDK newbie, and need help making a PNG in the "images" file into a sprite. My code looks like this:

import 'CoreLibs/graphics'
import 'CoreLibs/sprites'
-- import 'CoreLibs/input'

local gfx = playdate.graphics

-- playdate.inputHandlers.push(playdate.input)

local player = 1

local player = gfx.image.new('images/player.png')
local playerX = 0
local playerY = 0

local bullet = gfx.image.new('images/bullet.png')
local bulletX = 0
local bulletY = 0

local score = 0

AButtonDown = function()

BButtonDown = function()

end
end

function playdate.downButtonDown()
playerY = playerY + 10
down = true
up = false
left = false
right = false
end

function playdate.upButtonDown()
playerY = playerY - 10
down = false
up = true
left = false
right = false
end

function playdate.rightButtonDown()
    playerX = playerX + 10
    down = false
    up = false
    left = false
    right = true
    end

function playdate.leftButtonDown()
playerX = playerX - 10
down = false
up = false
left = true
right = false
end

function playdate.BButtonDown()
bulletY = playerY
bulletX = playerX
bulletTime = 1
end

function playdate.AButtonDown()
bulletTime = 0
end

function playdate.update()
gfx.clear()
gfx.image.draw(player,playerX, playerY)
if bulletTime==1 then
bulletY = bulletY - 3
gfx.image.draw(bullet,bulletX, bulletY)
elseif bulletTime==0 then
bulletY = playerY
bulletX = playerX
end
end

I have tried multiple different things, but have had no luck, so it would really help if someone could reply with the code I have, but modify it so the "bullet" and "player" images are sprites. Or just reply with an in-depth explanation of how to use sprites.

PS: I have tried "inside playdate" if you were wondering, and it didn't help either. Also if someone could explain collision rectangles, that would be a huge help.

To create a sprite:

  • Load the image you want the sprite to display, usually with gfx.image.new().
  • Pass the image as a parameter to gfx.sprite.new(), which returns the sprite as a gfx.sprite.
  • By default, the sprite is not displayed. To display it, use gfx.sprite:add() to add it to the list of sprites.
  • Remember to call gfx.sprite.update() in the main playdate.update() function to keep drawing the sprites to the framebuffer.

To move a sprite:

  • The anchor (or "drawing center" as it's referred to in the docs) is the location on the sprite where the coordinates of the sprite equal the coordinates of the current pixel. The default anchor position is the middle of the sprite, so if you want the sprite to have the same effect as calling gfx.image:draw(), you need to set the anchor to the top left by calling :setCenter(0, 0).
  • For a relative move, call gfx.sprite:moveBy() with the amount of pixels that should be added to the position of the sprite's anchor.
  • For an absolute move, call gfx.sprite:moveTo() with the coordinates that the sprite's anchor should be at.

Hi CS,
You are importing an image but not creating a sprite. Look at this example below.

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

local gfx = playdate.graphics

local player_image = gfx.image.new('images/player')
player = gfx.sprite.new( player_image )
player:moveTo(200,100)
player:add() --adds the sprite to the display list

local bullet_image = gfx.image.new('images/bullet.png')
bullet = gfx.sprite.new( bullet_image )
bullet:moveTo(player.x+40,player.y)
bullet:add() --adds the sprite to the display list

function playdate.update()
gfx.sprite.update() -- need to update and render your sprites
playdate.timer.updateTimers()
end

Oh! And don't use the PNG extension for gfx.image.new, since the PNG files are compiled to a different format. Just use gfx.image.new('images/bullet')

I have tried all the suggestions, and the code works (I tested using collide rects) , but my game is freezing 1 frame after I boot it up, I can run the code in the script once, but anything after that doesn't work. And it isn't the simulator, because other games, (and hence different code) works. So if anyone could suggest a fix, or some new code, that would be great.

Your game isn't freezing. The example given above simply doesn't contain the collision detection code, or the code that makes the bullet shoot and the player move.

I think what you want is something like this:

import 'CoreLibs/graphics'
import 'CoreLibs/sprites'

local gfx <const> = playdate.graphics

local playerImage = gfx.image.new('images/player')
local player = gfx.sprite.new(playerImage)
player:moveTo(200, 120)
player:add()

local bulletImage = gfx.image.new('images/bullet')
local bullet = gfx.sprite.new(bulletImage)
bullet:setVisible(false)
bullet:add()

local score = 0
local bulletTime

function playdate.downButtonDown()
	player:moveBy(0, 10)
	down = true
	up = false
	left = false
	right = false
end

function playdate.upButtonDown()
	player:moveBy(0, -10)
	down = false
	up = true
	left = false
	right = false
end

function playdate.rightButtonDown()
	player:moveBy(10, 0)
	down = false
	up = false
	left = false
	right = true
end

function playdate.leftButtonDown()
	player:moveBy(-10, 0)
	down = false
	up = false
	left = true
	right = false
end

function playdate.BButtonDown()
	bullet:moveTo(player.x, player.y)
	bullet:setVisible(true)
	bulletTime = 1
end

function playdate.AButtonDown()
	bullet:setVisible(false)
	bulletTime = 0
end

function playdate.update()
	if bulletTime == 1 then
		bullet:moveBy(0, -3)
	end
	
	gfx.sprite.update()
end