Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function colliding(a,b)
- if a.x<=0 or a.y<=0 or b.x<=0 or b.y<=0 then return false end
- return a.x < b.x + b.s and
- a.x + a.s > b.x and
- a.y < b.y + b.s and
- a.y + a.s > b.y
- end
- function setColor(r,b,g)
- love.graphics.setColor(r/255,b/255,g/255)
- end
- function love.load()
- font = love.graphics.newFont(12)
- plr = {
- x = 300;
- y = 300;
- s = 30;
- speed = 4;
- facing = "right";
- }
- object = {
- x = 400;
- y = 400;
- s = 100;
- health = 100;
- color = {0,255,0}
- }
- bullets = {}
- delay = 10;
- end
- function love.update(dt)
- if love.keyboard.isDown("w") then
- plr.facing = "up"
- plr.y = plr.y - plr.speed;
- end
- if love.keyboard.isDown("s") then
- plr.facing = "down"
- plr.y = plr.y + plr.speed;
- end
- if love.keyboard.isDown("a") then
- plr.facing = "left"
- plr.x = plr.x - plr.speed;
- end
- if love.keyboard.isDown("d") then
- plr.facing = "right"
- plr.x = plr.x + plr.speed;
- end
- if love.keyboard.isDown("space") then
- if delay == 10 then
- delay = 0;
- xx,yy = 0,0;
- if plr.facing == "right" then
- xx = plr.x+plr.s+10;
- yy = plr.y+plr.s/2
- elseif plr.facing == "left" then
- xx = plr.x-10;
- yy = plr.y+plr.s/2
- elseif plr.facing == "up" then
- xx = plr.x+plr.s/2;
- yy = plr.y-10;
- else
- xx = plr.x+plr.s/2;
- yy = plr.y+plr.s+10;
- end
- local bullet = {
- dir = plr.facing;
- x = xx;
- y = yy;
- s = 5;
- travelspeed = 10;
- damage = 5;
- }
- table.insert(bullets,bullet);
- else
- delay = delay + 1;
- end
- end
- for i=1,#bullets,1 do
- local bullet = bullets[i]
- if bullet ~= nil then
- if bullet.x<=0 then
- table.remove(bullets,i)
- elseif colliding(bullet,object) then
- table.remove(bullets,i)
- object.color = {love.math.random(0,255),love.math.random(0,255),love.math.random(0,255)}
- object.health = object.health - bullet.damage;
- else
- if bullet.dir == "right" then
- bullet.x = bullet.x + bullet.travelspeed
- elseif bullet.dir == "left" then
- bullet.x = bullet.x - bullet.travelspeed
- elseif bullet.dir == "up" then
- bullet.y = bullet.y - bullet.travelspeed
- else
- bullet.y = bullet.y + bullet.travelspeed
- end
- end
- end
- end
- end
- function drawBarrel()
- if plr.facing == "right" then
- love.graphics.rectangle("line",plr.x+plr.s, plr.y+plr.s/2-7/2, 10,7)
- elseif plr.facing == "left" then
- love.graphics.rectangle("line",plr.x-10, plr.y+plr.s/2-7/2, 10,7)
- elseif plr.facing == "up" then
- love.graphics.rectangle("line",plr.x+plr.s/2-7/2, plr.y-10, 7,10)
- else
- love.graphics.rectangle("line",plr.x+plr.s/2-7/2, plr.y+plr.s, 7,10)
- end
- end
- function love.draw()
- setColor(255,255,255)
- love.graphics.rectangle("line",plr.x, plr.y, plr.s, plr.s)
- drawBarrel()
- setColor(object.color[1],object.color[2],object.color[3])
- love.graphics.rectangle("line",object.x,object.y,object.s,object.s);
- setColor(255,255,255);
- local w = font:getWidth("health: "..object.health)
- love.graphics.print("health: "..object.health,object.x+object.s/2-w/2,object.y+object.s+5);
- for i=1,#bullets,1 do
- local bullet = bullets[i]
- setColor(255,0,0)
- love.graphics.circle("line",bullet.x,bullet.y,bullet.s,50);
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement