Source

Image-Breakout / lua / Block.lua

Full commit

Block = class("Block")

function Block:initialize(x, y, color)
	self.alive = true
	self.width = 32
	self.height = 12
	self.color = color
	self.x = x
	self.y = y
end

function Block:update(dt, ball)
	if ball:collide_with_rect(self) and not ball.collided then
		ball.collided = true
		ball.velocity_y = -ball.velocity_y
		self:explode()
	end
end

function Block:explode()
	self.alive = false
end

function Block:draw()
	love.graphics.setColor(0,0,0)
	love.graphics.rectangle('fill', self.x, self.y, self.width, self.height)
	love.graphics.setColor(unpack(self.color))
	love.graphics.rectangle('fill', self.x-1, self.y-1, self.width-2, self.height-2)
	love.graphics.setColor(255,255,255)
end