This commit is contained in:
Leszek Winnicki 2017-04-22 20:27:46 +02:00
parent b8af1cd861
commit 1a041ce6bb
8 changed files with 288 additions and 89 deletions

Binary file not shown.

View File

@ -7,6 +7,13 @@ function e:new(x,y,w,h,l)
enemy.w=w
enemy.h=h
enemy.dir=1
enemy.onground=false
enemy.dy=0
enemy.lifes=l
table.insert(e.enemies, enemy)
end
function e:clear()
for k in pairs (e.enemies) do
e.enemies[k] = nil
end
end

299
main.lua
View File

@ -2,124 +2,245 @@ require("player")
require("platforms")
require("enemies")
require("collision")
require("menu")
require("maploader")
points=0
mp={
{0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,"e",0},
{0,0,0,0,1,1,1,1,1},
{0,0,0,0,0,0,0,0,0},
{1,1,1,1,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0}
}
function love.load()
lose=false
win=false
moveleft=true
moveright=true
player.x=0
player.y=0
loadmap(mp)
--[[
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(600, 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(100, 0, 30, 30, 3)
e:new(300, 270, 30, 30, 3)
]]--
font=love.graphics.newFont("MerriweatherSans-ExtraBold.ttf", 20)
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)
if startmenu then
love.graphics.setColor(play.color[1], play.color[2], play.color[3])
love.graphics.rectangle("fill", play.x, play.y, play.w, play.h)
love.graphics.setColor(255-play.color[1], 255-play.color[2], 255-play.color[3])
love.graphics.translate((play.x+play.w/2)-45, (play.y+play.h/2)-13)
love.graphics.setFont(font)
love.graphics.print("Play", 0, 0, 0, 1, 1)
love.graphics.translate(-((play.x+play.w/2)-45), -((play.y+play.h/2)-13))
love.graphics.setColor(quit.color[1], quit.color[2], quit.color[3])
love.graphics.rectangle("fill", quit.x, quit.y, quit.w, quit.h)
love.graphics.setColor(255-quit.color[1], 255-quit.color[2], 255-quit.color[3])
love.graphics.translate((quit.x+quit.w/2)-45, (quit.y+quit.h/2)-13)
love.graphics.setFont(font)
love.graphics.print("Quit", 0, 0, 0, 1, 1)
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)
if pausemenu then
love.graphics.setColor(play.color[1], play.color[2], play.color[3])
love.graphics.rectangle("fill", play.x, play.y, play.w, play.h)
love.graphics.setColor(255-play.color[1], 255-play.color[2], 255-play.color[3])
love.graphics.translate((play.x+play.w/2)-90, (play.y+play.h/2)-13)
love.graphics.setFont(font)
love.graphics.print("Back To Game", 0, 0, 0, 1, 1)
love.graphics.translate(-((play.x+play.w/2)-90), -((play.y+play.h/2)-13))
love.graphics.setColor(quit.color[1], quit.color[2], quit.color[3])
love.graphics.rectangle("fill", quit.x, quit.y, quit.w, quit.h)
love.graphics.setColor(255-quit.color[1], 255-quit.color[2], 255-quit.color[3])
love.graphics.translate((quit.x+quit.w/2)-90, (quit.y+quit.h/2)-13)
love.graphics.setFont(font)
love.graphics.print("Quit To Menu", 0, 0, 0, 1, 1)
love.graphics.translate(-((quit.x+quit.w/2)-90), -((quit.y+quit.h/2)-13))
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)
if win then
startmenu=true
win=false
e:clear()
t:clear()
love.load()
elseif lose then
startmenu=true
lose=false
e:clear()
t:clear()
love.load()
else
love.graphics.setColor(239, 121, 89)
love.graphics.setFont(font)
love.graphics.print("Points: "..points, 0, 0, 0, 1, 1)
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 _,ti in pairs(t.tiles) do
love.graphics.draw(ti.type, ti.x, ti.y)
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
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
function love.keyreleased(key)
if key=='escape' and not startmenu then
if pausemenu then
pausemenu=false
else
pausemenu=true
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
if love.mouse.isDown(1) then
if startmenu and quit.cooldown==0 then
if play.mouse() then
startmenu=false
end
if quit.mouse() then
love.event.quit()
end
elseif pausemenu then
if play.mouse() then
pausemenu=false
end
if quit.mouse() then
pausemenu=false
startmenu=true
quit.cooldown=20
e:clear()
p:clear()
love.load()
end
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
if player.cooldown==0 then
player:fire()
end
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
if not startmenu and not pausemenu then
if love.keyboard.isDown('d') and moveright then
player.x=player.x+player.speed
end
if love.keyboard.isDown('a') and moveleft then
player.x=player.x-player.speed
end
if love.keyboard.isDown('lshift') then
player.speed=4
else
bl.y=bl.y+20
bl.x=bl.x+(bl.dirx/bl.diry)*20
player.speed=3
end
for _,pl in pairs(p.platforms) do
if collision(bl, pl) then
table.remove(player.bullets, q)
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 _,ti in pairs(t.tiles) do
if player.x+player.w>=ti.x and player.x<=ti.x+32 and player.y+player.h>=ti.y and player.y+player.h<=ti.y+32 then
player.onground=true
player.y=ti.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
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)
for _,ti in pairs(t.tiles) do
if collision(bl, ti) 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
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
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 _,ti in pairs(t.tiles) do
if en.y+en.h>=ti.y and en.y+en.h<=ti.y+ti.h then
if ti.x==en.x and ti.y==en.y+en.h then
en.dir=1
elseif ti.x+ti.w==en.x+en.w then
en.dir=-1
end
en.onground=true
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
for _,ene in pairs(e.enemies) do
if not ene.onground then
ene.dy=ene.dy+1
ene.y=ene.y+ene.dy
end
ene.onground=false
end
player.onground=false
if player.x==0 then
moveleft=false
else
moveleft=true
end
if player.x==love.graphics.getWidth()-player.w then
moveright=false
else
moveright=true
end
else
if quit.cooldown~=0 then
quit.cooldown=quit.cooldown-1
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

33
maploader.lua Normal file
View File

@ -0,0 +1,33 @@
t={}
t.tiles={}
t.type={}
t.type[1]=love.graphics.newImage("tile1.png")
t.type[2]=love.graphics.newImage("tile2.png")
function t:new(typ, x, y)
tile={}
tile.x=x
tile.y=y
tile.w=32
tile.h=32
tile.type=t.type[typ]
table.insert(t.tiles, tile)
end
function t:clear()
for k in pairs (t.tiles) do
t.tiles[k] = nil
end
end
function loadmap(map)
t:clear()
e:clear()
for q,y in pairs(map) do
for c,x in pairs(y) do
if x~=0 and type(x)=="number" then
t:new(x, (c*32)-32, (q*32)-32)
elseif x=="e" then
e:new(0,0,(c*32)-32,(q*32)-32,3)
print(tostring(q..c))
end
end
end
end

33
menu.lua Normal file
View File

@ -0,0 +1,33 @@
startmenu=true
play={}
play.w=love.graphics.getWidth()/1.25
play.h=love.graphics.getHeight()/20
play.x=love.graphics.getWidth()/2-play.w/2
play.y=love.graphics.getHeight()/4-play.h/2
play.color={211, 215, 123}
play.mouse=function()
if love.mouse.getX()>play.x and
love.mouse.getX()<play.x+play.w and
love.mouse.getY()>play.y and
love.mouse.getY()<play.y+play.h then
return true
end
return false
end
quit={}
quit.w=love.graphics.getWidth()/1.25
quit.h=love.graphics.getHeight()/20
quit.x=love.graphics.getWidth()/2-quit.w/2
quit.y=play.y+love.graphics.getHeight()/2
quit.color={211, 215, 123}
quit.cooldown=0
quit.mouse=function()
if love.mouse.getX()>quit.x and
love.mouse.getX()<quit.x+quit.w and
love.mouse.getY()>quit.y and
love.mouse.getY()<quit.y+quit.h then
return true
end
return false
end
pausemenu=false

View File

@ -7,4 +7,9 @@ function p:new(x,y,w,h)
platform.w=w
platform.h=h
table.insert(p.platforms, platform)
end
function p:clear()
for k in pairs (p.platforms) do
p.platforms[k] = nil
end
end

BIN
tile1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 363 B

BIN
tile2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 373 B