fatboychummy

dvdlogomeme.lua

Jul 27th, 2019
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.33 KB | None | 0 0
  1. local image, x, y, fx, fy, offsetX, offsetY, width, height
  2.  
  3. --[[
  4.   calculates the four edges of the image, and returns them as a table:
  5.   {
  6.     x1,
  7.     y1,
  8.     x2,
  9.     y2
  10.   }
  11. ]]
  12. local function calculateEdges(image, x, y)
  13.   local edges = {}
  14.   local closeInOffset = 30
  15.   edges[1] = x - offsetX + closeInOffset
  16.   edges[2] = y - offsetY + closeInOffset
  17.   edges[3] = x + offsetX - closeInOffset
  18.   edges[4] = y + offsetY - closeInOffset
  19.   return edges
  20. end
  21.  
  22. --[[
  23.   The default load function
  24. ]]
  25. function love.load()
  26.   image = love.graphics.newImage("images/DVD_LOGO.PNG")
  27.   offsetX = image:getWidth() / 2
  28.   offsetY = image:getHeight() / 2
  29.   fx = true
  30.   fy = true
  31.   love.window.setMode(1091, 699)
  32.   width, height = love.graphics.getDimensions()
  33.   x = math.random(50, width - 49)
  34.   y = math.random(50, height - 49)
  35. end
  36.  
  37. function love.update(dt)
  38.   local function inc(t, n)
  39.     if t then
  40.       return n + 1
  41.     else
  42.       return n - 1
  43.     end
  44.   end
  45.  
  46.   x = inc(fx, x)
  47.   y = inc(fy, y)
  48.  
  49.   local edges = calculateEdges(image, x, y)
  50.  
  51.   if edges[1] < 1 then
  52.     fx = true
  53.   elseif edges[3] > width - 1 then
  54.     fx = false
  55.   end
  56.  
  57.   if edges[2] < 1 then
  58.     fy = true
  59.   elseif edges[4] > height - 1 then
  60.     fy = false
  61.   end
  62.  
  63. end
  64.  
  65. function love.draw()
  66.   love.graphics.draw(image, x, y, 0, 1, 1, offsetX, offsetY)
  67. end
Advertisement
Add Comment
Please, Sign In to add comment