From b8af1cd8616a897cb14d26a76be632a4d7269c86 Mon Sep 17 00:00:00 2001 From: Leszek Winnicki Date: Tue, 18 Apr 2017 17:48:10 +0200 Subject: [PATCH] first commit --- collision.lua | 6 +++ enemies.lua | 12 ++++++ main.lua | 125 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ platforms.lua | 10 +++++ player.lua | 27 +++++++++++++ player.png | Bin 0 -> 176 bytes 6 files changed, 180 insertions(+) create mode 100644 collision.lua create mode 100644 enemies.lua create mode 100644 main.lua create mode 100644 platforms.lua create mode 100644 player.lua create mode 100644 player.png diff --git a/collision.lua b/collision.lua new file mode 100644 index 0000000..5a0ce47 --- /dev/null +++ b/collision.lua @@ -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 \ No newline at end of file diff --git a/enemies.lua b/enemies.lua new file mode 100644 index 0000000..6923f21 --- /dev/null +++ b/enemies.lua @@ -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 \ No newline at end of file diff --git a/main.lua b/main.lua new file mode 100644 index 0000000..b5ec28f --- /dev/null +++ b/main.lua @@ -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 \ No newline at end of file diff --git a/platforms.lua b/platforms.lua new file mode 100644 index 0000000..c2c4ef5 --- /dev/null +++ b/platforms.lua @@ -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 \ No newline at end of file diff --git a/player.lua b/player.lua new file mode 100644 index 0000000..8ed7c3f --- /dev/null +++ b/player.lua @@ -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 \ No newline at end of file diff --git a/player.png b/player.png new file mode 100644 index 0000000000000000000000000000000000000000..c4b5e68d71a99b757e541276c0a384d33a8a2872 GIT binary patch literal 176 zcmeAS@N?(olHy`uVBq!ia0vp^av;pX1SGcvS$+jloCO|{#S9GG!XV7ZFl&wkP>{XE z)7O>#Aq$@nzqWy>;!~iIWQl7;iF1B#Zfaf$gL6@8Vo7R>LV0FMhJw4NZ$Nk>pEyvF zmZytjh{y5dnholGjb;s?&t;uc GLK6V99x-_U literal 0 HcmV?d00001