My collection of demos / example code for PlayDate (Lua, some C)

Hi all -
I've been collecting the examples I've been writing as I learn the SDK in one repository, here: https://gitlab.com/RachelWilShaSingh/playdate-tests/-/tree/main/

In the "Lua" folder is the super basics, each with their own example "in a vacuum" (drawing text, getting button presses, drawing an image, etc.) - and same for the "C" folder, though I have fewer examples there.

In the "Demos_Lua" folder I have some bigger examples, such as a sample Brick Breaker game, a Pickin' Sticks game, a Space Invaders style game, and an example of a state changer / menus.

All of it is free to use for whatever reason. Just thought I'd post it here!

screenshot

screenshot2

screenshot

8 Likes

Hi!
Thanks for all these examples, they are great! I am using the Panorama example and I would like to toggle between two images by pressing A and B. Here is my code but it doesn't work, do you have any idea what the problem is? Thanks a lot!

-- https://gitlab.com/RachelWilShaSingh/playdate-tests
-- Rachel Singh

import 'CoreLibs/graphics.lua'
import 'CoreLibs/crank.lua'

local gfx = playdate.graphics
playdate.display.setRefreshRate( 50 )

gfx.setBackgroundColor( gfx.kColorWhite )
screenSize = { width = 400, height = 240 }

speed = 1

xOffset = 0

background = gfx.image.new( "Backyard.png" )
background2 = gfx.image.new( "Backyard2.png" )
imageWidth = 1313

function playdate.update()
  
  gfx.clear( gfx.kColorWhite )
  
  crankAngleDegrees = playdate.getCrankPosition()
  
  crankRatio = crankAngleDegrees / 360
  xOffset = imageWidth * crankRatio
  
  background:draw( xOffset, 0 )  
  background:draw( xOffset - imageWidth, 0 ) 
  
  
  -- buttons
  if playdate.buttonJustPressed("B") then
    print("B button")
    background:draw(0, 0)
  end
  
  if playdate.buttonJustPressed("A") then
    print("A button")
    background2:draw(0, 0)
  end
  --
  
  
end

Docs

Returns true for just one update cycle if button was pressed.

So what you could do is set a variable in your button checking if statements and then draw the background outside of those if statements.

I'd even go so far as setting a current background variable to the one that corresponds to the button that was just pressed.

Kind of thing


  if playdate.buttonJustPressed("B") then
   activebackground = background
  end
  
  if playdate.buttonJustPressed("A") then
    activebackground = background2
  end

  activebackground:draw(0, 0)

Hi @matt
thank you very much for your answer! I'm not sure I understand your example. I replaced my code with yours but it doesn't work. I think I didn't understand how to adapt it.

-- https://gitlab.com/RachelWilShaSingh/playdate-tests
-- Rachel Singh

import 'CoreLibs/graphics.lua'
import 'CoreLibs/crank.lua'

local gfx = playdate.graphics
playdate.display.setRefreshRate( 50 )

gfx.setBackgroundColor( gfx.kColorWhite )
screenSize = { width = 400, height = 240 }

speed = 1

xOffset = 0

background = gfx.image.new( "Backyard.png" )
background2 = gfx.image.new( "Backyard2.png" )
imageWidth = 1313



function playdate.update()
  
  gfx.clear( gfx.kColorWhite )
  
    
  crankAngleDegrees = playdate.getCrankPosition()
  
  crankRatio = crankAngleDegrees / 360
  xOffset = imageWidth * crankRatio
  
  background:draw( xOffset, 0 )  
  background:draw( xOffset - imageWidth, 0 ) 
  
  

  if playdate.buttonJustPressed("B") then
   activebackground = background
  end
  
  if playdate.buttonJustPressed("A") then
    activebackground = background2
  end
  
  activebackground:draw(0, 0)
  
  
end

  • i've commented out a place where you draw background unnecessarily.
  • i've added setting of active background to a blank image so that nothing is displayed initially.
    • you might prefer to default to one of your 2 backgrounds.
  • i think the code below could be further simplified, to set a good foundation to build on.
-- https://gitlab.com/RachelWilShaSingh/playdate-tests
-- Rachel Singh

import 'CoreLibs/graphics.lua'
import 'CoreLibs/crank.lua'

local gfx = playdate.graphics
playdate.display.setRefreshRate( 50 )

gfx.setBackgroundColor( gfx.kColorWhite )
screenSize = { width = 400, height = 240 }

speed = 1

xOffset = 0

background = gfx.image.new( "Backyard.png" )
background2 = gfx.image.new( "Backyard2.png" )
blank = gfx.image.new(400,240, gfx.kColorClear)
activebackground = blank
imageWidth = 1313



function playdate.update()
  
  gfx.clear( gfx.kColorWhite )
  
    
  crankAngleDegrees = playdate.getCrankPosition()
  
  crankRatio = crankAngleDegrees / 360
  xOffset = imageWidth * crankRatio
  
  -- background:draw( xOffset, 0 )  
  -- background:draw( xOffset - imageWidth, 0 ) 
  
  

  if playdate.buttonJustPressed("B") then
   activebackground = background
  end
  
  if playdate.buttonJustPressed("A") then
    activebackground = background2
  end
  
  activebackground:draw(0, 0)
  
  
end```

thank you very much for your help! Your code works, I just adapted it a little bit because the crank was not scrolling the image correctly anymore. I put here the final code that works as I want :slight_smile:

-- https://gitlab.com/RachelWilShaSingh/playdate-tests
-- Rachel Singh

import 'CoreLibs/graphics.lua'
import 'CoreLibs/crank.lua'

local gfx = playdate.graphics
playdate.display.setRefreshRate( 50 )

gfx.setBackgroundColor( gfx.kColorWhite )
screenSize = { width = 400, height = 240 }

speed = 1

xOffset = 0

background = gfx.image.new( "Backyard.png" )
background2 = gfx.image.new( "Backyard2.png" )
blank = gfx.image.new(400,240, gfx.kColorClear)
activebackground = background
imageWidth = 1313



function playdate.update()
  
  gfx.clear( gfx.kColorWhite )
  
    
  crankAngleDegrees = playdate.getCrankPosition()
  
  crankRatio = crankAngleDegrees / 360
  xOffset = imageWidth * crankRatio
  
  -- background:draw( xOffset, 0 )  
  -- background:draw( xOffset - imageWidth, 0 ) 
  
  

  if playdate.buttonJustPressed("B") then
   activebackground = background
  end
  
  if playdate.buttonJustPressed("A") then
    activebackground = background2
  end
  
  --activebackground:draw(0, 0)
  activebackground:draw( xOffset, 0 )  
  activebackground:draw( xOffset - imageWidth, 0 ) 
  
end
1 Like

Thanks! Sorry I missed these replies and thanks to Matt for the responses. I've been busy with the semester beginning and making sure my students are getting all set up correctly. ^_^;;

1 Like