Commit fa477dab by anshve

Upload New File

parent a2192ffb
Showing with 146 additions and 0 deletions
PlayState = Class{__includes = BaseState}
function PlayState:enter(params)
paddle1 = params.paddle1
paddle2 = params.paddle2
ball = params.ball
self.background = params.background
self.mode = params.mode
self.maxScore = params.maxScore
end
function PlayState:update(dt)
paddle1:update(dt)
paddle2:update(dt)
ball:update(dt)
-- paddle1 or paddle2 overlap the ball, bounce the ball
if ball:collides(paddle1) then
self:paddleOverlap(1)
gSounds['paddle_hit']:play()
elseif ball:collides(paddle2) then
self:paddleOverlap(2)
gSounds['paddle_hit']:play()
end
-- player1 control
if love.keyboard.isDown('w') then
paddle1.dy = -200
elseif love.keyboard.isDown('s') then
paddle1.dy = 200
else
paddle1.dy = 0
end
if self.mode == 0 then -- computer control
if ball.dx > 0 and ball.y < paddle2.y then
paddle2.dy = -PADDLE_BOT_SPEED
elseif ball.dx > 0 and ball.y > paddle2.y then
paddle2.dy = PADDLE_BOT_SPEED
else
paddle2.dy = 0
end
else -- player2 control
if love.keyboard.isDown('up') then
paddle2.dy = -PADDLE_SPEED
elseif love.keyboard.isDown('down') then
paddle2.dy = PADDLE_SPEED
else
paddle2.dy = 0
end
end
if ball.x >= VIRTUAL_WIDTH - 4 or ball.x <= 0 then
if ball.x >= VIRTUAL_WIDTH - 4 then
paddle1:changeScore(paddle1:getScore() + 1)
ball.dx = -100
end
if ball.x <= 0 then
paddle2:changeScore(paddle2:getScore() + 1)
ball.dx = 100
end
ball:reset()
gSounds['point_scored']:play()
local winner = self:isVictory()
if winner == 1 or winner == 2 then
gStateMachine:change('victory', {
background = self.background,
paddle1 = paddle1,
paddle2 = paddle2,
winner = winner,
})
else
gStateMachine:change('serve', {
mode = self.mode,
background = self.background,
maxScore = self.maxScore,
paddle1 = paddle1,
paddle2 = paddle2,
ball = ball
})
end
end
end
function PlayState:render()
if self.background == 0 then
love.graphics.clear(40 / 255, 45 / 255, 52 / 255, 1)
else
local backgroundName = 'background-' .. self.background
local backgroundWidth = gTextures[backgroundName]:getWidth()
local backgroundHeight = gTextures[backgroundName]:getHeight()
love.graphics.draw(gTextures[backgroundName], 0, 0, 0, VIRTUAL_WIDTH / (backgroundWidth - 1), VIRTUAL_HEIGHT / (backgroundHeight - 1))
end
love.graphics.setFont(gFonts['large'])
self:drawTextWithShadow(paddle1:getScore(), -100, 200)
self:drawTextWithShadow(paddle2:getScore(), 100, 200)
paddle1:render()
paddle2:render()
ball:render()
end
function PlayState:drawTextWithShadow(text, x, y)
love.graphics.setColor(34/255, 32/255, 52/255, 255/255)
love.graphics.printf(text, x + 2, y + 1, VIRTUAL_WIDTH, 'center')
love.graphics.printf(text, x + 1, y + 1, VIRTUAL_WIDTH, 'center')
love.graphics.printf(text, x + 0, y + 1, VIRTUAL_WIDTH, 'center')
love.graphics.printf(text, x + 1, y + 2, VIRTUAL_WIDTH, 'center')
love.graphics.setColor(1, 1, 1, 1)
love.graphics.printf(text, x, y, VIRTUAL_WIDTH, 'center')
end
function PlayState:paddleOverlap(player)
ball.dx = -ball.dx * BALL_ACCELERATION
if player == 1 then
ball.x = paddle1.x + 5
elseif player == 2 then
ball.x = paddle2.x - 4
end
if ball.dy < 0 then
ball.dy = -math.random(10, 80)
else
ball.dy = math.random(10, 80)
end
end
function PlayState:isVictory()
if self.maxScore <= paddle1:getScore() then
return 1
elseif self.maxScore <= paddle2:getScore() then
return 2
else
return 0
end
end
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment