Last active
August 29, 2015 14:08
-
-
Save omaraboumrad/d2fc894bccb4064b39de to your computer and use it in GitHub Desktop.
Pixel clone of Flappy bird using Love2d
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
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