crong/main.lua

97 lines
2.2 KiB
Lua
Raw Normal View History

2023-09-27 01:15:36 +00:00
import "CoreLibs/object"
import "CoreLibs/graphics"
import "CoreLibs/sprites"
import "CoreLibs/timer"
import "CoreLibs/crank"
import "pixie"
import "ball"
2023-09-27 01:15:36 +00:00
local gfx <const> = playdate.graphics
local paddle = nil
local boundary = nil
local ball = nil
2023-09-27 01:15:36 +00:00
local ballVector = {}
local ballSpeed = 3
local gameOver = false
local gameOverText <const> = "Game Over!\nPress 'A' to play again."
function setup()
levelTimer()
paddle = Pixie(6, 80, gfx.kColorWhite, 5, 120)
paddle:add()
boundary = Pixie(2, 240, gfx.kColorClear)
boundary:add()
ball = Ball(6, 6, gfx.kColorWhite, 200, 120)
ball:add()
2023-09-27 01:15:36 +00:00
local backgroundImage = gfx.image.new(400, 240, gfx.kColorBlack)
gfx.sprite.setBackgroundDrawingCallback(
function(x, y, width, height)
backgroundImage:draw(0, 0)
if gameOver then
gfx.setColor(gfx.kColorWhite)
gfx.fillRect(0, 80, 400, 80)
local level = ball.speed - 2
2023-09-27 01:15:36 +00:00
playdate.graphics.drawTextAligned("Game Over!\nYou reached level " ..
level ..
"\nPress 'A' to play again.", 200, 90, kTextAlignment.center)
end
end
)
end
function levelTimer()
playdate.timer.new(10000,
function()
ball:levelUp()
2023-09-27 01:15:36 +00:00
levelTimer()
end
)
end
2023-09-27 01:15:36 +00:00
function playdate.update()
gfx.clear()
2023-09-27 01:15:36 +00:00
if gameOver then
if playdate.buttonJustPressed(playdate.kButtonA) then
paddle:remove()
boundary:remove()
ball:remove()
2023-09-27 01:15:36 +00:00
playdate.file.run("main.pdz")
end
else
-- Check collisions
local collisions = ball:overlappingSprites()
2023-09-27 01:15:36 +00:00
for i = 1, #collisions do
local collision = collisions[i]
if collision == paddle then
2023-09-27 01:15:36 +00:00
redirectBall()
end
if collision == boundary then
2023-09-27 01:15:36 +00:00
gameOver = true
end
end
-- Handle movement
movePaddle(playdate.getCrankTicks(80))
ball:move()
2023-09-27 01:15:36 +00:00
playdate.timer.updateTimers()
end
gfx.sprite.update()
end
function redirectBall()
ball:setDirection(1)
2023-09-27 01:15:36 +00:00
end
function movePaddle(ticks)
if ticks == 0 then return end
paddle:moveBy(0, ticks*-5)
2023-09-27 01:15:36 +00:00
end
setup()