---------------
-- Bat Class --
---------------
Bat = class()
function Bat:init()
self.pos = vec2(math.floor(WIDTH/2), math.floor(WIDTH/30)+1)
self.size = vec2(math.floor(WIDTH/7), math.floor(WIDTH/30))
end
function Bat:draw()
fill(167, 170, 186, 255)
noStroke()
rectMode(CENTER)
ellipse(self.pos.x - self.size.x / 2 + 5, self.pos.y, math.floor(WIDTH/30))
ellipse(self.pos.x + self.size.x / 2 - 5, self.pos.y, math.floor(WIDTH/30))
rect(self.pos.x, self.pos.y, self.size.x, self.size.y)
end
function Bat:collide(ball)
if ball:left() <= self:right() and
ball:right() >= self:left() and
ball:top() >= self:bottom() and
ball:bottom() <= self:top() then
sound(SOUND_JUMP)
ball.vel.y = -ball.vel.y
-- change the x velocity depending on where the ball hit the bat
ball.pos.y = self:top() + ball.radius
pos = ball.pos.x - self.pos.x
ball.vel.x = pos / 10
return true
end
return false
end
function Bat:left()
return self.pos.x - self.size.x / 2
end
function Bat:right()
return self.pos.x + self.size.x / 2
end
function Bat:top()
return self.pos.y + self.size.y / 2
end
function Bat:bottom()
return self.pos.y - self.size.y / 2
end
-----------------
-- Block Class --
-----------------
Block = class()
function Block:init(x, y, col)
self.pos = vec2(x, y)
self.size = vec2(math.floor(WIDTH/12),math.floor(WIDTH/24))
self.color = col
end
function Block:draw()
fill(self.color)
noStroke()
rectMode(CENTER)
rect(self.pos.x, self.pos.y, self.size.x, self.size.y)
end
function Block:collide(ball)
if ball:left() <= self:right() and
ball:right() >= self:left() and
ball:top() >= self:bottom() and
ball:bottom() <= self:top() then
sound(SOUND_BLIT)
if ball.pos.y <= self:top() and ball.pos.y >= self:bottom() then
ball.vel.x = -ball.vel.x
else
ball.vel.y = -ball.vel.y
end
return true
end
return false
end
function Block:left()
return self.pos.x - self.size.x / 2
end
function Block:right()
return self.pos.x + self.size.x / 2
end
function Block:top()
return self.pos.y + self.size.y / 2
end
function Block:bottom()
return self.pos.y - self.size.y / 2
end
------------------
-- Levels array --
------------------
levels = {
{{0,0,1,1,1,1,1,1,0,0},{0,1,1,0,0,0,0,1,1,0},{0,0,1,1,1,1,1,1,0,0},
{0,0,1,1,1,1,1,1,0,0},{0,1,1,0,0,0,0,1,1,0},{0,0,1,1,1,1,1,1,0,0}},
{{1,1,1,1,1,1,1,1,1,1},{1,0,0,1,0,0,1,0,0,1},{0,1,1,0,1,1,0,1,1,0},
{0,1,1,0,1,1,0,1,1,0},{1,0,1,1,0,0,1,0,0,1},{1,1,1,1,1,1,1,1,1,1}},
{{0,0,0,1,1,1,1,0,0,0},{0,0,1,1,1,1,1,1,0,0},{1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1},{0,1,1,1,1,1,1,1,1,0},{0,0,1,2,1,1,1,1,0,0}},
{{1,0,1,1,1,1,1,1,0,1},{1,1,1,0,1,1,0,1,1,1},{1,0,1,1,0,0,1,1,0,1},
{1,0,1,1,0,0,1,1,0,1},{1,1,1,0,1,1,0,1,1,1},{1,0,1,1,1,1,1,1,0,1}},
{{1,1,1,1,1,1,1,1,1,1},{1,0,1,1,0,0,1,1,0,1},{1,1,0,0,1,1,0,0,1,1},
{1,1,0,0,1,1,0,0,1,1},{1,0,1,1,0,0,1,1,0,1},{1,1,1,1,1,1,1,1,1,1}}
}
I am thinking maybe I should speed up the ball as the levels increase… What do you think?