Advertisement
ezak

Touches

Mar 12th, 2012
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.09 KB | None | 0 0
  1.  
  2. --# Main
  3.  
  4. function setup()
  5.     rects = {}
  6.     nbRects = 5
  7.     for i=1, nbRects do
  8.         local x = math.random()*WIDTH
  9.         local y = math.random()*HEIGHT
  10.         table.insert(rects, Rectangle(x, y, 100, 100))  
  11.     end
  12.    
  13. end
  14.  
  15. function draw()
  16.     background(40, 40, 50)
  17.     for i=1, nbRects do
  18.         rects[i]:draw()
  19.     end
  20.    
  21. end
  22.  
  23. function touched(touch)
  24.     for i=1, nbRects do
  25.         rects[i]:touched(touch)
  26.     end
  27. end
  28. --# Rectangle
  29. Rectangle = class()
  30.  
  31. function Rectangle:init(x, y, w, h)
  32.     self.x = x
  33.     self.y = y
  34.     self.w = w
  35.     self.h = h
  36.     local r = math.random()*255
  37.     local g = math.random()*255
  38.     local b = math.random()*255
  39.     self.col = color(r, g, b, 255)
  40.    
  41. end
  42.  
  43. function Rectangle:draw()
  44.     fill(self.col)
  45.     rect(self.x, self.y, self.w, self.h)
  46. end
  47.  
  48. function Rectangle:touched(touch)
  49.     local x = self.x
  50.     local y = self.y
  51.     local w = self.w
  52.     local h = self.h
  53.     if touch.x > x and touch.y > y and touch.x < x+w and touch.y < y+h then
  54.         self.x = touch.x-w/2
  55.         self.y = touch.y-h/2
  56.     end
  57. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement