Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --# Main
- function setup()
- rects = {}
- nbRects = 5
- for i=1, nbRects do
- local x = math.random()*WIDTH
- local y = math.random()*HEIGHT
- table.insert(rects, Rectangle(x, y, 100, 100))
- end
- end
- function draw()
- background(40, 40, 50)
- for i=1, nbRects do
- rects[i]:draw()
- end
- end
- function touched(touch)
- for i=1, nbRects do
- rects[i]:touched(touch)
- end
- end
- --# Rectangle
- Rectangle = class()
- function Rectangle:init(x, y, w, h)
- self.x = x
- self.y = y
- self.w = w
- self.h = h
- local r = math.random()*255
- local g = math.random()*255
- local b = math.random()*255
- self.col = color(r, g, b, 255)
- end
- function Rectangle:draw()
- fill(self.col)
- rect(self.x, self.y, self.w, self.h)
- end
- function Rectangle:touched(touch)
- local x = self.x
- local y = self.y
- local w = self.w
- local h = self.h
- if touch.x > x and touch.y > y and touch.x < x+w and touch.y < y+h then
- self.x = touch.x-w/2
- self.y = touch.y-h/2
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement