Skip to content

Instantly share code, notes, and snippets.

@orta
Created April 23, 2022 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orta/206b2864b182224851d4cc2c2600b191 to your computer and use it in GitHub Desktop.
Save orta/206b2864b182224851d4cc2c2600b191 to your computer and use it in GitHub Desktop.
A trivial version of pong for the playdate
import "CoreLibs/sprites"
import "CoreLibs/graphics"
playdate.display.setRefreshRate(20)
local gfx = playdate.graphics
local spritelib = gfx.sprite
local screenWidth = playdate.display.getWidth()
local screenHeight = playdate.display.getHeight()
local playerOneY = screenHeight/2
local playerTwoY = screenHeight/2
local ballX = screenWidth/2
local ballY = screenHeight/2
local ballGoingLeft = true
local ballGoingUp = true
local function startGame()
end
function playdate.update()
if ballGoingLeft then
ballX += 2
else
ballX -= 2
end
if ballGoingUp then
ballY -= 2
else
ballY += 2
end
-- collision player 1
if ballX > 20 and ballX < 20 + 8 and ballY > playerOneY - 20 and ballY < playerOneY + 40 then
ballGoingLeft = true
end
-- collision player 2
if ballX > screenWidth - 20 - 8 - 4 and ballX < screenWidth - 20 + 8 and ballY > playerTwoY - 20 and ballY < playerTwoY + 40 then
ballGoingLeft = false
end
-- score checking
if ballX > screenWidth then
playdate.graphics.drawText("Congrats p1", 20, 20)
return
end
if ballX < 0 then
playdate.graphics.drawText("Congrats p2", 20, 20)
return
end
if ballY < 0 then
ballGoingUp = false
end
if ballY > screenHeight then
ballGoingUp = true
end
-- clear the screen
gfx.setBackgroundColor(gfx.kColorWhite)
gfx.setColor(gfx.kColorWhite)
gfx.fillRect(0, 0, screenWidth , screenHeight )
-- draw ball
gfx.setBackgroundColor(gfx.kColorBlack)
gfx.setColor(gfx.kColorBlack)
gfx.fillRect(ballX, ballY, 4 , 4 )
-- paddle 1
gfx.setBackgroundColor(gfx.kColorBlack)
gfx.setColor(gfx.kColorBlack)
gfx.fillRect(20, playerOneY - 40/2, 8 , 40 )
-- paddle 2
gfx.setBackgroundColor(gfx.kColorBlack)
gfx.setColor(gfx.kColorBlack)
gfx.fillRect(screenWidth - 8 - 20, playerTwoY - 40/2, 8 , 40 )
end
function playdate.leftButtonDown()
end
function playdate.rightButtonDown()
end
function playdate.downButtonDown()
playerOneY += 5
end
function playdate.upButtonDown()
playerOneY -= 5
end
function playdate.AButtonDown()
playerTwoY += 5
end
function playdate.BButtonDown()
playerTwoY -= 5
end
function playdate.AButtonUp()
end
function playdate.BButtonUp()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment