first commit

This commit is contained in:
Leszek Winnicki 2017-04-18 17:48:10 +02:00
commit b8af1cd861
6 changed files with 180 additions and 0 deletions

6
collision.lua Normal file
View File

@ -0,0 +1,6 @@
function collision(a,b)
if a.x < b.x + b.w and a.x + a.w > b.x and a.y < b.y + b.h and a.h + a.y > b.y then
return true
end
return false
end

12
enemies.lua Normal file
View File

@ -0,0 +1,12 @@
e={}
e.enemies={}
function e:new(x,y,w,h,l)
enemy={}
enemy.x=x
enemy.y=y
enemy.w=w
enemy.h=h
enemy.dir=1
enemy.lifes=l
table.insert(e.enemies, enemy)
end

125
main.lua Normal file
View File

@ -0,0 +1,125 @@
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

10
platforms.lua Normal file
View File

@ -0,0 +1,10 @@
p={}
p.platforms={}
function p:new(x,y,w,h)
platform={}
platform.x=x
platform.y=y
platform.w=w
platform.h=h
table.insert(p.platforms, platform)
end

27
player.lua Normal file
View File

@ -0,0 +1,27 @@
player={}
player.x=0
player.y=0
player.dy=0
player.ddy=0
player.jumped=0
player.dir=1
player.cooldown=20
player.bullets={}
player.speed=3
player.onground=false
player.img=love.graphics.newImage("player.png")
player.w=player.img:getWidth()
player.h=player.img:getHeight()
player.fire=function()
bullet={}
bullet.x=player.x+player.w/2
bullet.y=player.y+player.h/2
bullet.w=2
bullet.h=2
bullet.dirx=love.mouse.getX()-bullet.x
bullet.diry=love.mouse.getY()-bullet.y
bullet.speed=3
table.insert(player.bullets, bullet)
player.cooldown=20
end
player.jump=false

BIN
player.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 176 B