Commit fb6b7dce by anshve

Upload New File

parent fa477dab
Showing with 81 additions and 0 deletions
ServeState = Class{__includes = BaseState}
function ServeState:enter(params)
paddle1 = params.paddle1
paddle2 = params.paddle2
-- decide which players will serve
servingPlayer = math.random(2) == 1 and 1 or 2
if params.ball == nil then
ball = Ball(VIRTUAL_WIDTH / 2 - 2, VIRTUAL_HEIGHT / 2 - 2, 4, 4)
if servingPlayer == 1 then
ball.dx = -100
else
ball.dx = 100
end
else
ball = params.ball
if ball.dx == -100 then
servingPlayer = 1
else
servingPlayer = 2
end
end
self.background = params.background
self.mode = params.mode
self.maxScore = params.maxScore
end
function ServeState:update(dt)
if love.keyboard.wasPressed('enter') or love.keyboard.wasPressed('return') then
gStateMachine:change('play', {
mode = self.mode,
background = self.background,
maxScore = self.maxScore,
paddle1 = paddle1,
paddle2 = paddle2,
ball = ball
})
end
end
function ServeState: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)
love.graphics.setFont(gFonts['small'])
self:drawTextWithShadow("Player " .. tostring(servingPlayer) .. "'s turn!", 0, 20)
self:drawTextWithShadow("Press Enter to Serve!", 0, 30)
paddle1:render()
paddle2:render()
ball:render()
end
function ServeState: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
\ 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