Skip to content

Instantly share code, notes, and snippets.

@halldorel
Created September 7, 2014 12:52
Show Gist options
  • Save halldorel/f0a1164554f722ddeb53 to your computer and use it in GitHub Desktop.
Save halldorel/f0a1164554f722ddeb53 to your computer and use it in GitHub Desktop.
Smá
DEBUG = true
ball = {
x = love.window.getWidth() / 2,
y = love.window.getHeight() / 2,
r = 10,
s = 32,
aX = 4,
aY = 3,
update = function (self, dt)
if(DEBUG) then
self.x = love.mouse.getX()
self.y = love.mouse.getY()
return
end
nextX = self.x + self.aX
nextY = self.y + self.aY
if not self:isOutsideX(self.x)
and self:isOutsideX(nextX) then
self.aX = self.aX * (-1)
end
if not self:isOutsideY(self.y)
and self:isOutsideY(nextY) then
self.aY = self.aY * (-1)
end
if not (paddle:collidesWith(self.x, self.y, self.r))
and (paddle:collidesWith(nextX, nextY, self.r))
then
self.aY = self.aY * (-1)
score:increase()
nextX = self.x + self.aX
nextY = self.y + self.aY
end
self.x = nextX
self.y = nextY
end,
isOutsideX = function(self, x)
if (x + self.r >= love.window.getWidth()) or (x - self.r <= 0) then
return true
end
return false
end,
isOutsideY = function(self, y)
if (y + self.r >= love.window.getHeight()) or (y - self.r <= 0) then
return true
end
return false
end
}
paddle = {
x = love.window.getWidth() / 2,
y = love.window.getHeight() - 20,
w = 75,
h = 8,
--rightPoint = leftPoint + w,
update = function (self, dt)
self.x = love.mouse.getX()
end,
collidesWith = function (self, x, y, r)
if (x >= self.x and x <= self.x + self.w and y + r >= self.y) then
return true
end
return false
end
}
score = {
num = 0,
increase = function (self)
self.num = self.num + 1
end ,
decrease = function (self)
self.num = self.num - 1
end
-- score:increase() will increase the score by one
}
field = {
rows = 4,
cols = 6,
blockW = 70,
blockH = 25,
p = 6, -- padding
bricks = {},
fieldW = 0,
fieldH = 0,
x = 0,
y = 100,
setup = function (self, rows, cols)
self.rows = rows
self.cols = cols
for i = 1, rows, 1 do
self.bricks[i] = {}
for j = 1, cols, 1 do
self.bricks[i][j] = true
end
end
-- Reiknum þetta út einu sinni hér, því þetta eru fastar
self.fieldW = ((self.cols * self.blockW) + (self.p * (self.cols - 1)))
self.fieldH = self.rows * self.blockH + (self.p * (self.rows - 1))
self.x = (love.window.getWidth() - self.fieldW) / 2
end,
killBrickAt = function (self, row, col)
bricks[row][col] = false
end,
-- Tekur inn hnit punkts (x, y) og skilar hnitum á múrstein sem punkturinn er innan
-- Skilar false ef punkturinn er ekki innan lifandi múrsteins
collidesWithBallAt = function (self, x, y)
-- Athugum fyrst hvort við séum fyrir innan múrsteina-klasann
if ( x > self.x and x < self.x + self.fieldW
and y < self.y + self.fieldH and y > self.y) then
---- ÞETTA ER VITLAUST OG ÉG VEIT EKKI AF HVERJU : - (
brickCol = math.floor((x - self.x - self.p * (self.cols - 1)) / self.blockW) + 1 -- grr
brickRow = math.floor((y - self.y - self.p * (self.rows - 1)) / self.blockH) + 1 -- grrrrr...
print("Bolti er á múrstein: " .. brickCol .. ", " .. brickRow)
end
return false
end,
update = function (self)
brickIndex = self:collidesWithBallAt(ball.x, ball.y, ball.r)
end,
draw = function (self)
love.graphics.setColor(120,120,150)
-- Notum rows og cols til að reikna út hvar í griddinu
-- múrsteinarnir eiga að vera (í pixlum)
for rows = 1, self.rows, 1 do
for cols = 1, self.cols, 1 do
-- Þurfum að gera (cols - 1) og (rows - 1) út af
-- 1-based indexnum í Lua -.-
-- Fyrsti múrsteinn á að vera á (self.x, self.y) og því þarf
-- (cols - 1) og (rows - 1) að vera bæði 0
-- þ.e. cols == 1 og rows == 1
-- Ef brick er false þá returnum við, þ.e. teiknum *ekki*
if not self.bricks[rows][cols] then return end
x = self.x + (cols - 1) * (self.blockW + self.p)
y = self.y + (rows - 1) * (self.blockH + self.p)
love.graphics.rectangle("fill", x, y, self.blockW, self.blockH)
end
end
end
}
function love.load()
field:setup(4, 7)
end
function love.update(dt)
ball:update(dt)
paddle:update(dt)
field:update()
end
function love.draw()
field:draw()
love.graphics.setColor(255,0,0)
love.graphics.setBackgroundColor(200,220,220)
love.graphics.circle("fill", ball.x, ball.y, ball.r, ball.s)
love.graphics.rectangle("fill", paddle.x, paddle.y, paddle.w, paddle.h)
love.graphics.print(score.num, 200, 50)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment