Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function paddle(x, up, down)
- local p = {
- x = x,
- y = 300,
- w = 20,
- h = 80,
- speed = 500,
- score = 0,
- }
- function p:update(dt)
- -- Handle Movement
- if love.keyboard.isDown(up) then
- self.y = self.y - self.speed * dt
- elseif love.keyboard.isDown(down) then
- self.y = self.y + self.speed * dt
- end
- -- Stop the paddle from moving outside of the game window.
- self.y = math.min(math.max(self.y, self.h/2), 600-self.h/2)
- end
- function p:draw()
- love.graphics.rectangle("fill", self.x-self.w/2, self.y-self.h/2, self.w, self.h)
- end
- function p:collide(ball)
- end
- return p
- end
- local p1 = paddle(30, "w", "s")
- local p2 = paddle(770, "up", "down")
- local ball = {
- x = 400,
- y = 300,
- r = 10,
- dx = -200,
- dy = 0,
- speed = 200,
- }
- function ball:update(dt)
- -- Set the ball's movement
- self.x = self.x + self.dx * dt
- self.y = self.y + self.dy * dt
- -- Handle collision with the bounds of the window
- if self.y <= self.r then
- self.dy = math.abs(self.dy)
- elseif self.dy >= 600-self.r then
- self.dy = -math.abs(self.dy)
- end
- end
- function ball:draw()
- -- single line comment
- --[[
- Multiline comment
- ]]
- -- Draw the circle to represent the ball.
- love.graphics.circle("fill", self.x, self.y, self.r)
- end
- -- Stores our game objects
- local objs = {ball, p1, p2}
- function love.update(dt)
- --[[p1:update(dt)
- p2:update(dt)
- ball:draw(dt)]]
- -- Loop through our table and call the update function on each object
- for i, v in ipairs(objs) do
- v:update(dt)
- end
- end
- function love.draw()
- --[[p1:draw()
- p2:draw()
- ball:draw()]]
- -- Loop through our table and call the draw function on each object
- for i, v in ipairs(objs) do
- v:draw()
- end
- end
- function love.keypressed(key)
- if key == "escape" then
- return love.event.quit()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement