Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function love.update(dt)
- if ball:collides(player1) then -- collides() is stored in Ball.lua
- -- deflect the ball to the right
- -- change velocity from -100, to 100
- ball.dx = -ball.dx
- end
- if ball:collides(player2) then
- -- deflect ball to the left
- ball.dx = -ball.dx
- end
- if ball.y <= 0 then -- the top of the screen
- --deflect the ball down
- ball.dy = -ball.dy
- -- reset the ball in the case of it hitting the top of the screen
- ball.y = 0
- end
- if ball.y >= VIRTUAL_HEIGHT - 4 then -- the ball is 4 pixels tall so offset collision by 4 pixels
- ball.dy = -ball.dy
- ball.y = VIRTUAL_HEIGHT - 4
- end
- --[[
- if we reach the left or right edge of the screen
- go back to start and update the score
- ]]
- if ball.x < 0 then
- player2Score = player2Score + 1
- serveringPlayer = 1
- ball:reset() -- randomizes the balls dx. we want to set that based on who scored
- if player2Score >= 3 then
- gameState = 'victory'
- winningPlayer = 2
- else
- gameState = 'serve'
- end
- end
- if ball.x > VIRTUAL_WIDTH then
- player1Score = player1Score + 1 -- serving player not working... always says serving player 2
- servingPlayer = 2
- ball:reset()
- --ball.dx = -100
- --gameState = 'serve'
- if player1Score >= 3 then
- gameState = 'victory'
- winningPlayer = 1
- else
- gameState = 'serve'
- end
- end
- -- player 1 movement
- if love.keyboard.isDown('w') then
- player1.dy = -PADDLE_SPEED -- negative is up
- elseif love.keyboard.isDown('s') then
- player1.dy = PADDLE_SPEED -- replaces math.min(VIRTUAL_HEIGHT - 2-,PLAYER1Y + PADDLE_SPEED * dt)
- else
- player1.dy = 0 -- if neither is true, the paddle speed is 0
- end
- -- player 2 movement
- if love.keyboard.isDown('up') then
- player2.dy = -PADDLE_SPEED
- elseif love.keyboard.isDown('down') then
- player2.dy = PADDLE_SPEED
- end
- else
- player2.dy = 0
- end
- if gameState == 'play' then
- ball:update(dt)
- --ballX = ballX + ballDX * dt these will instead be done with update
- --ballY = ballY + ballDY * dt
- end
- player1:update(dt) -- replaces player1Y = math.min(VIRTUAL_HEIGHT - 20, player1Y + PADDLE_SPEED * dt)... now stored in the paddle class and replace player1Y with self.y
- player2:update(dt)
- ball:render()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement