Advertisement
Guest User

Untitled

a guest
Sep 29th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.84 KB | None | 0 0
  1. local function paddle(x, up, down)
  2.     local p = {
  3.     x = x,
  4.     y = 300,
  5.     w = 20,
  6.     h = 80,
  7.     speed = 500,
  8.     score = 0,
  9.     }
  10.    
  11.     function p:update(dt)
  12.         -- Handle Movement
  13.         if love.keyboard.isDown(up) then
  14.             self.y = self.y - self.speed * dt
  15.         elseif love.keyboard.isDown(down) then
  16.             self.y = self.y + self.speed * dt
  17.         end
  18.        
  19.         -- Stop the paddle from moving outside of the game window.
  20.        
  21.         self.y = math.min(math.max(self.y, self.h/2), 600-self.h/2)
  22.     end
  23.    
  24.     function p:draw()
  25.         love.graphics.rectangle("fill", self.x-self.w/2, self.y-self.h/2, self.w, self.h)
  26.     end
  27.    
  28.     function p:collide(ball)
  29.    
  30.     end
  31.    
  32.     return p
  33. end
  34.  
  35. local p1 = paddle(30, "w", "s")
  36. local p2 = paddle(770, "up", "down")
  37.  
  38. local ball = {
  39.     x = 400,
  40.     y = 300,
  41.     r = 10,
  42.     dx = -200,
  43.     dy = 0,
  44.     speed = 200,
  45. }
  46.  
  47. function ball:update(dt)
  48.     -- Set the ball's movement
  49.     self.x = self.x + self.dx * dt
  50.     self.y = self.y + self.dy * dt
  51.    
  52.     -- Handle collision with the bounds of the window
  53.     if self.y  <= self.r then
  54.         self.dy = math.abs(self.dy)
  55.     elseif self.dy >= 600-self.r then
  56.         self.dy = -math.abs(self.dy)
  57.     end
  58. end
  59.  
  60. function ball:draw()
  61.     -- single line comment
  62.    
  63.     --[[
  64.    
  65.     Multiline comment
  66.    
  67.     ]]
  68.    
  69.     -- Draw the circle to represent the ball.
  70.     love.graphics.circle("fill", self.x, self.y, self.r)
  71. end
  72.  
  73. -- Stores our game objects
  74. local objs = {ball, p1, p2}
  75.  
  76.  
  77.  
  78. function love.update(dt)
  79.     --[[p1:update(dt)
  80.     p2:update(dt)
  81.     ball:draw(dt)]]
  82.    
  83.     -- Loop through our table and call the update function on each object
  84.     for i, v in ipairs(objs) do
  85.         v:update(dt)
  86.     end
  87. end
  88.  
  89. function love.draw()
  90.     --[[p1:draw()
  91.     p2:draw()
  92.     ball:draw()]]
  93.    
  94.     -- Loop through our table and call the draw function on each object
  95.     for i, v in ipairs(objs) do
  96.         v:draw()
  97.     end
  98. end
  99.  
  100. function love.keypressed(key)
  101.     if key == "escape" then
  102.         return love.event.quit()
  103.     end
  104. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement