-
-
Save simme/fd7235517a733f3717f8de4d154d5565 to your computer and use it in GitHub Desktop.
This file contains 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
import "CoreLibs/object" | |
import "CoreLibs/graphics" | |
import "CoreLibs/sprites" | |
import "CoreLibs/timer" | |
local gfx <const> = playdate.graphics | |
local geometry <const> = playdate.geometry | |
local levelWidth = 360 | |
local levelHeight = 204 | |
-- Level | |
class('World').extends(gfx.sprite) | |
function World:init(x, y, w, h) | |
World.super.init(self) | |
self:moveTo(x, y) | |
self:setSize(w, h) | |
self:setCollideRect(2, 2, w - 4, h - 4) | |
end | |
function World:draw(x, y, w, h) | |
gfx.setColor(gfx.kColorWhite) | |
gfx.fillRect(x, y, w, h) | |
gfx.setColor(gfx.kColorBlack) | |
gfx.setStrokeLocation(gfx.kStrokeInside) | |
gfx.drawRoundRect(x, y, w, h, 2) | |
gfx.drawRect(x + 2, y + 2, w - 4, h - 4) | |
end | |
-- Ball | |
class('Ball').extends(gfx.sprite) | |
function Ball:init(x, y, vector) | |
Ball.super.init(self) | |
self.vector = vector | |
self:moveTo(x, y) | |
self:setSize(8, 8) | |
self:setCollideRect(1, 1, 6, 6) | |
end | |
function Ball:update() | |
self:moveBy(self.vector.dx, self.vector.dy) | |
if (self.y + self.height / 2 >= 4 + levelHeight - 4) then | |
self:flipDirection() | |
elseif (self.y - self.height / 2 <= 4) then | |
self:flipDirection() | |
end | |
end | |
function Ball:flipDirection() | |
self.vector = geometry.vector2D.new(self.vector.dx * -1, self.vector.dy * -1) | |
end | |
function Ball:draw(x, y, width, height) | |
playdate.graphics.fillCircleInRect(x, y, width, height) | |
end | |
local world = World(2 + levelWidth / 2, 2 + levelHeight / 2, levelWidth, levelHeight) | |
world:add() | |
local ball = Ball(100, 100, geometry.vector2D.new(0, 4)) | |
ball:add() | |
-- Level Setup | |
function setupLevel() | |
--[[ | |
for x=4,levelWidth,levelWidth/12 do | |
for y=4, levelHeight, levelHeight/12 do | |
gfx.drawRect(x, y, levelWidth/12, levelHeight/12) | |
end | |
end | |
--]] | |
end | |
setupLevel() | |
function playdate.update() | |
gfx.sprite.update() | |
playdate.drawFPS(384, 2) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment