diff --git a/src/ball.lua b/src/ball.lua index 2f9bb74..32852e9 100644 --- a/src/ball.lua +++ b/src/ball.lua @@ -19,7 +19,7 @@ function Ball:init(width, height, color, x, y) end function Ball:levelUpTimer() - playdate.timer.performAfterDelay(5000, + playdate.timer.performAfterDelay(25000, function(ball) if ball.direction.x > 0 then ball.direction.x += 1 diff --git a/src/main.lua b/src/main.lua index ca66920..d4a971b 100644 --- a/src/main.lua +++ b/src/main.lua @@ -35,11 +35,11 @@ function setup() addWallColliders() setupBricks() - timeWidget = TextBox(100, 20, 350, 10, 5, kTextAlignment.center) + timeWidget = TextBox("", 100, 20, 350, 10, 5, kTextAlignment.center) updateTimeWidget() timeWidget:add() - speedWidget = TextBox(100, 20, 50, 10, 5, kTextAlignment.center) + speedWidget = TextBox("", 100, 20, 50, 10, 5, kTextAlignment.center) updateSpeedWidget() speedWidget:add() @@ -73,9 +73,11 @@ end function setupBricks() for i=1,12 do - addBrickRow() advanceBricks() + addBrickRow() end + + addBrickTimer() end function addBrickRow() @@ -150,15 +152,22 @@ function moveBall() end end +function addBrickTimer() + playdate.timer.performAfterDelay(20000, + function() + advanceBricks() + addBrickRow() + addBrickTimer() + end + ) +end + function endGame() endTime = playdate.getElapsedTime() - local textBox = TextBox(275, 40, 200, 120, 5, kTextAlignment.center) - textBox:setText( - "Game Over!\nYou survived for " .. - string.format("%.2f", endTime) .. - " seconds.\n Press 'A' to play again." - ) + local t = string.format("Game Over!\nYou survived for %.2f seconds.\nPress 'A' to play again.", endTime) + local textBox = TextBox(t, 275, 40, 200, 120, + 5, kTextAlignment.center) textBox:add() end diff --git a/src/textbox.lua b/src/textbox.lua index caed2e3..3d32995 100644 --- a/src/textbox.lua +++ b/src/textbox.lua @@ -8,25 +8,55 @@ local defaultFont = gfx.font.new("fonts/Mini Mono") 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) + self.text = text self:setSize(w, h) self:moveTo(x, y) - self:setZIndex(999) + self.vert = vert or 0 self.align = align or kTextAlignment.left self.font = font or defaultFont - self.vert = vert - self.text = "" + + self:setZIndex(999) + self:drawStatic() end function TextBox:setText(text) self.text = text + self:drawStatic() end -function TextBox:draw(x, y, w, h) - gfx.pushContext() +function TextBox:drawStatic() + 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.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 + ) + gfx.popContext() + self:setImage(img) +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) @@ -37,7 +67,4 @@ function TextBox:draw(x, y, w, h) self.text, x, y+self.vert, w, h, nil, nil, self.align ) - - gfx.popContext() end -