Skip to content

Instantly share code, notes, and snippets.

@omaraboumrad omaraboumrad/main.lua
Last active Aug 29, 2015

Embed
What would you like to do?
Pixel clone of Flappy bird using Love2d
function love.load()
reset()
topscore = 0
local font = love.graphics.newFont(24)
love.graphics.setFont(font)
end
function reset()
x = 60
y = love.window.getHeight()/2
vely = 0
gravity = 0.5
time = 0
elapsed = 0
enemies = {}
end
function wall_top(walltop)
return {walltop[1], 0, 20, walltop[2]}
end
function wall_bot(wallbot)
return {wallbot[1], wallbot[2], 20, love.window.getHeight()}
end
function collides(a, b)
local t1 = {a[1], a[1] + a[3], a[2], a[2] + a[4]}
local t2 = {b[1], b[1] + b[3], b[2], b[2] + b[4]}
return not (t2[1] > t1[2] or
t2[2] < t1[1] or
t2[3] > t1[4] or
t2[4] < t1[3])
end
function love.draw()
love.graphics.setColor(0,100,0,255)
for i, enemy in pairs(enemies) do
love.graphics.rectangle("fill", unpack(enemy))
end
love.graphics.setColor(255,0,0,255)
love.graphics.rectangle("fill", x, y, 20, 20)
love.graphics.setColor(255, 255, 255, 255)
love.graphics.print("S: " .. math.floor(time), 10, 10)
love.graphics.print("H: " .. topscore, 10, 40)
end
function love.update(dt)
time = time + dt
elapsed = elapsed + dt
vely = vely + gravity
y = y + vely
if y > love.window.getHeight()-20 then
y = love.window.getHeight() -20
elseif y < 0 then
y = 0
end
-- enemies
if elapsed > 1 then
local top = math.random(love.window.getHeight()/2)
local bot = top+150
table.insert(enemies, wall_top({love.window.getWidth(), top}))
table.insert(enemies, wall_bot({love.window.getWidth(), bot}))
elapsed = 0
end
for i, enemy in pairs(enemies) do
enemy[1] = enemy[1] - 2
if collides({x,y,20,20}, enemy) then
topscore = math.max(topscore, math.floor(time))
reset()
end
end
end
function love.keypressed(key)
if key == '-' then
love.window.setFullscreen(
not love.window.getFullscreen())
elseif key == 'escape' then
love.quit()
elseif key == ' ' then
vely = -9
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.