125 lines
3.2 KiB
Lua
125 lines
3.2 KiB
Lua
require("player")
|
|
require("platforms")
|
|
require("enemies")
|
|
require("collision")
|
|
function love.load()
|
|
lose=false
|
|
win=false
|
|
p:new(0, 200, 320, 30)
|
|
p:new(300, 300, 200, 30)
|
|
p:new(0, 300, 200, 30)
|
|
p:new(700, 300, 200, 30)
|
|
p:new(480, 200, 320, 30)
|
|
p:new(0, 400, 320, 30)
|
|
p:new(480, 400, 320, 30)
|
|
e:new(100, 170, 30, 30, 3)
|
|
e:new(300, 270, 30, 30, 3)
|
|
end
|
|
function love.draw()
|
|
if win then
|
|
love.graphics.print("YOU WIN", 0, 0, 0, 2, 2)
|
|
elseif lose then
|
|
love.graphics.print("YOU LOSE", 0, 0, 0, 2, 2)
|
|
else
|
|
love.graphics.setColor(255, 255, 255)
|
|
love.graphics.draw(player.img, player.x, player.y)
|
|
love.graphics.setColor(182,182,109)
|
|
for _,b in pairs(player.bullets) do
|
|
love.graphics.rectangle("fill", b.x, b.y, b.w, b.h)
|
|
end
|
|
love.graphics.setColor(109, 182, 182)
|
|
for _,pl in pairs(p.platforms) do
|
|
love.graphics.rectangle("fill", pl.x, pl.y, pl.w, pl.h)
|
|
end
|
|
for _,e in pairs(e.enemies) do
|
|
love.graphics.setColor(182-109, 182*2-109, 109)
|
|
love.graphics.rectangle("fill", e.x, e.y, e.w, e.h)
|
|
end
|
|
end
|
|
end
|
|
function love.update(dt)
|
|
if love.keyboard.isDown('d') then
|
|
player.x=player.x+player.speed
|
|
end
|
|
if love.keyboard.isDown('a') then
|
|
player.x=player.x-player.speed
|
|
end
|
|
if love.keyboard.isDown('lshift') then
|
|
player.speed=4
|
|
else
|
|
player.speed=3
|
|
end
|
|
if love.mouse.isDown(1) and player.cooldown==0 then
|
|
player:fire()
|
|
end
|
|
if player.jump then
|
|
if player.jumped==0 then
|
|
player.dy=-16
|
|
player.ddy=1
|
|
end
|
|
if player.dy==0 or player.onground then
|
|
player.jumped=0
|
|
player.jump=false
|
|
else
|
|
player.jumped=1
|
|
end
|
|
player.dy=player.dy+player.ddy
|
|
player.y=player.y+player.dy
|
|
end
|
|
for _,pl in pairs(p.platforms) do
|
|
if player.x+player.w>=pl.x and player.x<=pl.x+pl.w and player.y+player.h>=pl.y and player.y+player.h<=pl.y+pl.h then
|
|
player.onground=true
|
|
player.y=pl.y-player.h
|
|
end
|
|
end
|
|
if player.jump==false and player.onground==false then
|
|
player.dy=player.dy+1
|
|
player.y=player.y+player.dy
|
|
end
|
|
if love.keyboard.isDown('space') and player.onground then
|
|
player.jump=true
|
|
end
|
|
for q,bl in pairs(player.bullets) do
|
|
if bl.dirx>bl.diry then
|
|
bl.x=bl.x+20
|
|
bl.y=bl.y+(bl.diry/bl.dirx)*20
|
|
else
|
|
bl.y=bl.y+20
|
|
bl.x=bl.x+(bl.dirx/bl.diry)*20
|
|
end
|
|
for _,pl in pairs(p.platforms) do
|
|
if collision(bl, pl) then
|
|
table.remove(player.bullets, q)
|
|
end
|
|
end
|
|
for q1,en in pairs(e.enemies) do
|
|
if collision(en, bl) then
|
|
table.remove(player.bullets, q)
|
|
en.lifes=en.lifes-1
|
|
if en.lifes==0 then
|
|
table.remove(e.enemies, q1)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
for _,en in pairs(e.enemies) do
|
|
if collision(en, player) and not lose and not win then
|
|
lose=true
|
|
end
|
|
en.x=en.x+2*en.dir
|
|
for _,pl in pairs(p.platforms) do
|
|
if pl.x==en.x and pl.y==en.y+en.w then
|
|
en.dir=1
|
|
elseif pl.x+pl.w==en.x+en.w and pl.y==en.y+en.w then
|
|
en.dir=-1
|
|
end
|
|
end
|
|
end
|
|
if player.cooldown~=0 then
|
|
player.cooldown=player.cooldown-1
|
|
end
|
|
if #e.enemies==0 and not lose and not win then
|
|
win=true
|
|
end
|
|
player.onground=false
|
|
end |