SHARE
TWEET

Untitled

a guest May 22nd, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function colliding(a,b)
  2.     if a.x<=0 or a.y<=0 or b.x<=0 or b.y<=0 then return false end
  3.     return a.x < b.x + b.s and
  4.     a.x + a.s > b.x and
  5.     a.y < b.y + b.s and
  6.     a.y + a.s > b.y
  7. end
  8.  
  9. function setColor(r,b,g)
  10.     love.graphics.setColor(r/255,b/255,g/255)
  11. end
  12.  
  13.  
  14. function love.load()
  15.     font = love.graphics.newFont(12)
  16.  
  17.     plr = {
  18.         x = 300;
  19.         y = 300;
  20.         s = 30;
  21.         speed = 4;
  22.         facing = "right";
  23.     }
  24.  
  25.     object = {
  26.         x = 400;
  27.         y = 400;
  28.         s = 100;
  29.         health = 100;
  30.         color = {0,255,0}
  31.     }
  32.  
  33.     bullets = {}
  34.  
  35.     delay = 10;
  36. end
  37.  
  38. function love.update(dt)
  39.     if love.keyboard.isDown("w") then
  40.         plr.facing = "up"
  41.         plr.y = plr.y - plr.speed;
  42.     end
  43.     if love.keyboard.isDown("s") then
  44.         plr.facing = "down"
  45.         plr.y = plr.y + plr.speed;
  46.     end
  47.     if love.keyboard.isDown("a") then
  48.         plr.facing = "left"
  49.         plr.x = plr.x - plr.speed;
  50.     end
  51.     if love.keyboard.isDown("d") then
  52.         plr.facing = "right"
  53.         plr.x = plr.x + plr.speed;
  54.     end
  55.  
  56.     if love.keyboard.isDown("space") then
  57.         if delay == 10 then
  58.             delay = 0;
  59.             xx,yy = 0,0;
  60.             if plr.facing == "right" then
  61.                 xx = plr.x+plr.s+10;
  62.                 yy = plr.y+plr.s/2
  63.             elseif plr.facing == "left" then
  64.                 xx = plr.x-10;
  65.                 yy = plr.y+plr.s/2
  66.             elseif plr.facing == "up" then
  67.                 xx = plr.x+plr.s/2;
  68.                 yy = plr.y-10;
  69.             else
  70.                 xx = plr.x+plr.s/2;
  71.                 yy = plr.y+plr.s+10;
  72.             end
  73.             local bullet = {
  74.                 dir = plr.facing;
  75.                 x = xx;
  76.                 y = yy;
  77.                 s = 5;
  78.                 travelspeed = 10;
  79.                 damage = 5;
  80.             }
  81.             table.insert(bullets,bullet);
  82.         else
  83.             delay = delay + 1;
  84.         end
  85.     end
  86.  
  87.     for i=1,#bullets,1 do
  88.         local bullet = bullets[i]
  89.         if bullet ~= nil then
  90.             if bullet.x<=0 then
  91.                 table.remove(bullets,i)
  92.             elseif colliding(bullet,object) then
  93.                 table.remove(bullets,i)
  94.                 object.color = {love.math.random(0,255),love.math.random(0,255),love.math.random(0,255)}
  95.                 object.health = object.health - bullet.damage;
  96.             else
  97.                 if bullet.dir == "right" then
  98.                     bullet.x = bullet.x + bullet.travelspeed
  99.                 elseif bullet.dir == "left" then
  100.                     bullet.x = bullet.x - bullet.travelspeed
  101.                 elseif bullet.dir == "up" then
  102.                     bullet.y = bullet.y - bullet.travelspeed
  103.                 else
  104.                     bullet.y = bullet.y + bullet.travelspeed
  105.                 end
  106.             end
  107.         end
  108.     end
  109. end
  110.  
  111. function drawBarrel()
  112.     if plr.facing == "right" then
  113.         love.graphics.rectangle("line",plr.x+plr.s, plr.y+plr.s/2-7/2, 10,7)
  114.     elseif plr.facing == "left" then
  115.         love.graphics.rectangle("line",plr.x-10, plr.y+plr.s/2-7/2, 10,7)
  116.     elseif plr.facing == "up" then
  117.         love.graphics.rectangle("line",plr.x+plr.s/2-7/2, plr.y-10, 7,10)
  118.     else
  119.         love.graphics.rectangle("line",plr.x+plr.s/2-7/2, plr.y+plr.s, 7,10)
  120.     end
  121. end
  122.  
  123. function love.draw()
  124.     setColor(255,255,255)
  125.     love.graphics.rectangle("line",plr.x, plr.y, plr.s, plr.s)
  126.     drawBarrel()
  127.     setColor(object.color[1],object.color[2],object.color[3])
  128.     love.graphics.rectangle("line",object.x,object.y,object.s,object.s);
  129.     setColor(255,255,255);
  130.     local w = font:getWidth("health: "..object.health)
  131.     love.graphics.print("health: "..object.health,object.x+object.s/2-w/2,object.y+object.s+5);
  132.  
  133.     for i=1,#bullets,1 do
  134.         local bullet = bullets[i]
  135.         setColor(255,0,0)
  136.         love.graphics.circle("line",bullet.x,bullet.y,bullet.s,50);
  137.     end
  138. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top