Optimize text boxes, tune difficulty, add brick marching.

This commit is contained in:
Anna Rose 2023-09-28 16:52:30 -04:00
parent ffc44093f4
commit e3cf1f7b1b
3 changed files with 55 additions and 19 deletions

View File

@ -19,7 +19,7 @@ function Ball:init(width, height, color, x, y)
end end
function Ball:levelUpTimer() function Ball:levelUpTimer()
playdate.timer.performAfterDelay(5000, playdate.timer.performAfterDelay(25000,
function(ball) function(ball)
if ball.direction.x > 0 then if ball.direction.x > 0 then
ball.direction.x += 1 ball.direction.x += 1

View File

@ -35,11 +35,11 @@ function setup()
addWallColliders() addWallColliders()
setupBricks() setupBricks()
timeWidget = TextBox(100, 20, 350, 10, 5, kTextAlignment.center) timeWidget = TextBox("", 100, 20, 350, 10, 5, kTextAlignment.center)
updateTimeWidget() updateTimeWidget()
timeWidget:add() timeWidget:add()
speedWidget = TextBox(100, 20, 50, 10, 5, kTextAlignment.center) speedWidget = TextBox("", 100, 20, 50, 10, 5, kTextAlignment.center)
updateSpeedWidget() updateSpeedWidget()
speedWidget:add() speedWidget:add()
@ -73,9 +73,11 @@ end
function setupBricks() function setupBricks()
for i=1,12 do for i=1,12 do
addBrickRow()
advanceBricks() advanceBricks()
addBrickRow()
end end
addBrickTimer()
end end
function addBrickRow() function addBrickRow()
@ -150,15 +152,22 @@ function moveBall()
end end
end end
function addBrickTimer()
playdate.timer.performAfterDelay(20000,
function()
advanceBricks()
addBrickRow()
addBrickTimer()
end
)
end
function endGame() function endGame()
endTime = playdate.getElapsedTime() endTime = playdate.getElapsedTime()
local textBox = TextBox(275, 40, 200, 120, 5, kTextAlignment.center) local t = string.format("Game Over!\nYou survived for %.2f seconds.\nPress 'A' to play again.", endTime)
textBox:setText( local textBox = TextBox(t, 275, 40, 200, 120,
"Game Over!\nYou survived for " .. 5, kTextAlignment.center)
string.format("%.2f", endTime) ..
" seconds.\n Press 'A' to play again."
)
textBox:add() textBox:add()
end end

View File

@ -8,25 +8,38 @@ local defaultFont = gfx.font.new("fonts/Mini Mono")
class("TextBox").extends(gfx.sprite) class("TextBox").extends(gfx.sprite)
function TextBox:init(w, h, x, y, vert, align, font) -- text - the initial text to display
-- w, h - width and height of the text box
-- x, y - initial position for the text box (center)
-- vert - vertical offset for the text (from the top of the box)
-- align - text alignment
-- font - font (defaults to Mini Mono)
function TextBox:init(text, w, h, x, y, vert, align, font)
TextBox.super.init(self) TextBox.super.init(self)
self.text = text
self:setSize(w, h) self:setSize(w, h)
self:moveTo(x, y) self:moveTo(x, y)
self:setZIndex(999) self.vert = vert or 0
self.align = align or kTextAlignment.left self.align = align or kTextAlignment.left
self.font = font or defaultFont self.font = font or defaultFont
self.vert = vert
self.text = "" self:setZIndex(999)
self:drawStatic()
end end
function TextBox:setText(text) function TextBox:setText(text)
self.text = text self.text = text
self:drawStatic()
end end
function TextBox:draw(x, y, w, h) function TextBox:drawStatic()
gfx.pushContext() local img = gfx.image.new(self:getSize())
local x = 0
local y = 0
local w = self.width
local h = self.height
gfx.pushContext(img)
gfx.setColor(gfx.kColorWhite) gfx.setColor(gfx.kColorWhite)
gfx.fillRect(x, y, w, h) gfx.fillRect(x, y, w, h)
gfx.setColor(gfx.kColorBlack) gfx.setColor(gfx.kColorBlack)
@ -37,7 +50,21 @@ function TextBox:draw(x, y, w, h)
self.text, self.text,
x, y+self.vert, w, h, nil, nil, self.align x, y+self.vert, w, h, nil, nil, self.align
) )
gfx.popContext() gfx.popContext()
self:setImage(img)
end end
-- draw the textbox. This is in a sub-function so we can change the context
-- depending on static-ness
function TextBox:subDraw(x, y, w, h)
gfx.setColor(gfx.kColorWhite)
gfx.fillRect(x, y, w, h)
gfx.setColor(gfx.kColorBlack)
gfx.drawRoundRect(x, y, w, h, 2)
gfx.setFont(self.font)
gfx.drawTextInRect(
self.text,
x, y+self.vert, w, h, nil, nil, self.align
)
end