Advertisement
bkuhns

Ball lua

Jan 2nd, 2013
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.89 KB | None | 0 0
  1. Ball = {}
  2. Ball.__index = Ball
  3.  
  4.  
  5. function Ball.create(x, y, width, height)
  6.     -- Set up default arguments.
  7.     x = x or 0
  8.     y = y or 0
  9.     width = width or 20
  10.     height = height or 20
  11.  
  12.     local ball = {}
  13.     setmetatable(ball, Ball)
  14.    
  15.     -- Set data members from params.
  16.     ball.x = x
  17.     ball.y = y
  18.     ball.width = width
  19.     ball.height = height
  20.    
  21.     -- Set default data members not in constructor params.
  22.     ball.speed = 0
  23.     ball.heading = {}
  24.     ball.heading.x = 0
  25.     ball.heading.y = 0
  26.    
  27.     return ball
  28. end
  29.  
  30.  
  31. function Ball:setSpeed(speed)
  32.     self.speed = speed
  33. end
  34.  
  35.  
  36. function Ball:setHeading(x, y)
  37.     self.heading.x = x
  38.     self.heading.y = y
  39. end
  40.  
  41.  
  42. function Ball:tick(dt)
  43.     self.x = self.x + math.cos(self.heading.x / (self.speed * dt) )
  44.     self.y = self.y + math.cos(self.heading.y / (self.speed * dt) )
  45. end
  46.  
  47.  
  48. function Ball:render()
  49.     love.graphics.rectangle("fill", self.x, self.y, self.width, self.height)
  50. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement