Created
April 10, 2025 21:45
-
-
Save jafish/8caa45f5ae63e29220ab985335f5e684 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Name this file `main.lua`. Your game can use multiple source files if you wish | |
-- (use the `import "myFilename"` command), but the simplest games can be written | |
-- with just `main.lua`. | |
-- You'll want to import these in just about every project you'll work on. | |
import "CoreLibs/object" | |
import "CoreLibs/graphics" | |
import "CoreLibs/sprites" | |
import "CoreLibs/timer" | |
-- Declaring this "gfx" shorthand will make your life easier. Instead of having | |
-- to preface all graphics calls with "playdate.graphics", just use "gfx." | |
-- Performance will be slightly enhanced, too. | |
-- NOTE: Because it's local, you'll have to do it in every .lua source file. | |
local gfx <const> = playdate.graphics | |
-- Here's our player sprite declaration. We'll scope it to this file because | |
-- several functions need to access it. | |
local playerSprite = nil | |
-- A function to set up our game environment. | |
function myGameSetUp() | |
-- Create a dynamic player image | |
local playerSize = 20 -- Size in pixels | |
local playerImage = gfx.image.new(playerSize, playerSize) | |
-- Draw on the player image | |
gfx.pushContext(playerImage) | |
-- Fill with white | |
gfx.fillRect(0, 0, playerSize, playerSize) | |
-- Draw a simple face | |
gfx.setColor(gfx.kColorBlack) | |
-- Eyes | |
gfx.fillRect(5, 5, 2, 2) | |
gfx.fillRect(playerSize - 7, 5, 2, 2) | |
-- Mouth | |
gfx.drawLine(5, 15, playerSize - 5, 15) | |
gfx.popContext() | |
playerSprite = gfx.sprite.new(playerImage) | |
playerSprite:moveTo(200, 120) | |
playerSprite:add() | |
-- Create a dynamic background | |
local backgroundWidth, backgroundHeight = 400, 240 | |
local backgroundImage = gfx.image.new(backgroundWidth, backgroundHeight) | |
-- Draw on the background image | |
gfx.pushContext(backgroundImage) | |
-- Fill with a pattern | |
gfx.setPattern({0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55}) | |
gfx.fillRect(0, 0, backgroundWidth, backgroundHeight) | |
-- Draw some objects | |
gfx.setColor(gfx.kColorBlack) | |
-- A few random circles | |
for i = 1, 10 do | |
local x = math.random(backgroundWidth) | |
local y = math.random(backgroundHeight) | |
local size = math.random(10, 30) | |
gfx.drawCircleAtPoint(x, y, size) | |
end | |
-- Draw a ground line | |
gfx.setLineWidth(3) | |
gfx.drawLine(0, 200, backgroundWidth, 200) | |
gfx.popContext() | |
gfx.sprite.setBackgroundDrawingCallback( | |
function(x, y, width, height) | |
backgroundImage:draw(0, 0) | |
end | |
) | |
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 | |
-- Rotate the player sprite based on the crank position | |
local crankPosition = playdate.getCrankPosition() | |
print("Crank Position: " .. crankPosition); | |
playerSprite:setRotation(crankPosition); | |
-- 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment