857 lines
30 KiB
Lua
857 lines
30 KiB
Lua
-- Ensure that every play is different by changing the random number seed
|
|
math.randomseed( os.time() )
|
|
|
|
-- Variables
|
|
playerDistance = 40
|
|
playAreaHeight = love.graphics:getHeight() - 50
|
|
gameOver = false
|
|
endMenuSelect = 1
|
|
menuKeyTimeout = 0
|
|
pauseMenuSelect = 1
|
|
paused = false
|
|
keyCounter = 0
|
|
|
|
-- Player coordinates and scores
|
|
player1 = {
|
|
x = playerDistance + 20,
|
|
y = playAreaHeight - ( 120 + playerDistance ),
|
|
score = 0,
|
|
xImg = nil,
|
|
yImg = nil
|
|
}
|
|
player2 = {
|
|
x = love.graphics:getWidth() - ( 120 + playerDistance ),
|
|
y = playerDistance + 20,
|
|
score = 0,
|
|
xImg = nil,
|
|
yImg = nil,
|
|
xVelocity = nil,
|
|
yVelocity = nil,
|
|
counter = 0
|
|
}
|
|
|
|
-- Blocks - 10 for each side, positive if the block is there, zero if it is not
|
|
leftBlocks = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
|
|
bottomBlocks = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
|
|
rightBlocks = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
|
|
topBlocks = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
|
|
|
|
-- Ball
|
|
ball = {
|
|
x = ( love.graphics:getWidth() / 2 ) - 20,
|
|
y = ( playAreaHeight / 2 ) - 20,
|
|
velocity = 120,
|
|
acceleration = 5,
|
|
direction = math.random( 1, 360 ),
|
|
img = nil,
|
|
counter = 0,
|
|
color = { 1, 1, 1 }
|
|
}
|
|
|
|
-- Graphics
|
|
backgroundImg = nil -- background
|
|
|
|
-- Function to reset the game after every point
|
|
function reset()
|
|
-- Reset the random number seed
|
|
math.randomseed( os.time() )
|
|
|
|
-- Reset player 1
|
|
player1.x = playerDistance + 20
|
|
player1.y = playAreaHeight - ( 120 + playerDistance )
|
|
|
|
-- Reset player 2
|
|
player2.x = love.graphics:getWidth() - ( 120 + playerDistance )
|
|
player2.y = playerDistance + 20
|
|
player2.counter = 0
|
|
player2.xVelocity = nil
|
|
player2.yVelocity = nil
|
|
|
|
-- Reset the ball
|
|
ball.x = ( love.graphics:getWidth() / 2 ) - 20
|
|
ball.y = ( playAreaHeight / 2 ) - 20
|
|
ball.velocity = 120 + ( player1.score * ball.acceleration ) +
|
|
( player2.score * ball.acceleration )
|
|
ball.direction = math.random( 1, 360 )
|
|
ball.counter = 0
|
|
ball.color = { 1, 1, 1 }
|
|
end
|
|
|
|
-- Function to restart the game
|
|
function restart()
|
|
-- Reset
|
|
reset()
|
|
|
|
-- Variables
|
|
playerDistance = 40
|
|
playAreaHeight = love.graphics:getHeight() - 50
|
|
gameOver = false
|
|
endMenuSelect = 1
|
|
menuKeyTimeout = 0
|
|
pauseMenuSelect = 1
|
|
paused = false
|
|
keyCounter = 0
|
|
|
|
-- Player coordinates and scores
|
|
player1 = {
|
|
x = playerDistance + 20,
|
|
y = playAreaHeight - ( 120 + playerDistance ),
|
|
score = 0,
|
|
xImg = nil,
|
|
yImg = nil
|
|
}
|
|
player2 = {
|
|
x = love.graphics:getWidth() - ( 120 + playerDistance ),
|
|
y = playerDistance + 20,
|
|
score = 0,
|
|
xImg = nil,
|
|
yImg = nil,
|
|
xVelocity = nil,
|
|
yVelocity = nil,
|
|
counter = 0
|
|
}
|
|
|
|
-- Blocks - 10 for each side, positive if the block is there, zero if it is not
|
|
leftBlocks = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
|
|
bottomBlocks = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
|
|
rightBlocks = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
|
|
topBlocks = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
|
|
|
|
-- Ball
|
|
ball = {
|
|
x = ( love.graphics:getWidth() / 2 ) - 20,
|
|
y = ( playAreaHeight / 2 ) - 20,
|
|
velocity = 120,
|
|
acceleration = 5,
|
|
direction = math.random( 1, 360 ),
|
|
img = nil,
|
|
counter = 0,
|
|
color = { 1, 1, 1 }
|
|
}
|
|
|
|
-- Load graphics
|
|
love.load()
|
|
end
|
|
|
|
function love.load()
|
|
-- Background
|
|
backgroundImg = love.graphics.newImage( "assets/background.png" )
|
|
|
|
-- Player 1 paddles
|
|
player1.xImg = love.graphics.newImage( "assets/red-x.png" )
|
|
player1.yImg = love.graphics.newImage( "assets/red-y.png" )
|
|
|
|
-- Player 2 paddles
|
|
player2.xImg = love.graphics.newImage( "assets/blue-x.png" )
|
|
player2.yImg = love.graphics.newImage( "assets/blue-y.png" )
|
|
|
|
-- Ball
|
|
ball.img = love.graphics.newImage( "assets/ball.png" )
|
|
end
|
|
|
|
function endUpdate()
|
|
gameOver = true
|
|
if love.keyboard.isDown( "w" ) and menuKeyTimeout > 10 then
|
|
if endMenuSelect == 1 then
|
|
endMenuSelect = 2
|
|
menuKeyTimeout = 0
|
|
else
|
|
endMenuSelect = 1
|
|
menuKeyTimeout = 0
|
|
end
|
|
elseif love.keyboard.isDown( "s" ) and menuKeyTimeout > 10 then
|
|
if endMenuSelect == 1 then
|
|
endMenuSelect = 2
|
|
menuKeyTimeout = 0
|
|
else
|
|
endMenuSelect = 1
|
|
menuKeyTimeout = 0
|
|
end
|
|
end
|
|
if love.keyboard.isDown( "q" ) then
|
|
love.event.push( "quit" )
|
|
end
|
|
if love.keyboard.isDown( "r" ) and menuKeyTimeout > 10 then
|
|
restart()
|
|
end
|
|
if love.keyboard.isDown( "return" ) and menuKeyTimeout > 10 then
|
|
if endMenuSelect == 1 then
|
|
restart()
|
|
else
|
|
love.event.push( "quit" )
|
|
end
|
|
end
|
|
menuKeyTimeout = menuKeyTimeout + 1
|
|
return
|
|
end
|
|
|
|
function love.update( dt )
|
|
-- End the game if one player's score is 10 or if the sum of the two players'
|
|
-- scores is negative
|
|
if player1.score >= 10 or player2.score >= 10 then
|
|
endUpdate()
|
|
end
|
|
if player1.score + player2.score < 0 then
|
|
endUpdate()
|
|
end
|
|
if paused then
|
|
if love.keyboard.isDown( "w" ) and menuKeyTimeout > 10 then
|
|
if pauseMenuSelect == 1 then
|
|
pauseMenuSelect = 2
|
|
menuKeyTimeout = 0
|
|
else
|
|
pauseMenuSelect = 1
|
|
menuKeyTimeout = 0
|
|
end
|
|
elseif love.keyboard.isDown( "s" ) and menuKeyTimeout > 10 then
|
|
if pauseMenuSelect == 1 then
|
|
pauseMenuSelect = 2
|
|
menuKeyTimeout = 0
|
|
else
|
|
pauseMenuSelect = 1
|
|
menuKeyTimeout = 0
|
|
end
|
|
end
|
|
if love.keyboard.isDown( "q" ) then
|
|
love.event.push( "quit" )
|
|
end
|
|
if love.keyboard.isDown( "r" ) and menuKeyTimeout > 10 then
|
|
paused = false
|
|
menuKeyTimeout = 0
|
|
end
|
|
if love.keyboard.isDown( "return" ) and menuKeyTimeout > 10 then
|
|
if pauseMenuSelect == 1 then
|
|
paused = false
|
|
menuKeyTimeout = 0
|
|
else
|
|
love.event.push( "quit" )
|
|
end
|
|
end
|
|
menuKeyTimeout = menuKeyTimeout + 1
|
|
return
|
|
end
|
|
|
|
if love.keyboard.isDown( "p" ) and keyCounter > 10 then
|
|
paused = true
|
|
keyCounter = 0
|
|
return
|
|
end
|
|
|
|
-- Left and right movement for player 1
|
|
if love.keyboard.isDown( "a" ) then
|
|
player1.x = player1.x - ( ( ( 3 * ball.velocity ) / 2 ) * dt )
|
|
if player1.x < playerDistance + 20 then
|
|
player1.x = playerDistance + 20
|
|
end
|
|
elseif love.keyboard.isDown( "d" ) then
|
|
player1.x = player1.x + ( ( ( 3 * ball.velocity ) / 2 ) * dt )
|
|
if player1.x > love.graphics:getWidth() - ( 120 + playerDistance ) then
|
|
player1.x = love.graphics:getWidth() - ( 120 + playerDistance )
|
|
end
|
|
end
|
|
|
|
-- Up and down movement for player 1
|
|
if love.keyboard.isDown( "w" ) then
|
|
player1.y = player1.y - ( ( ( 3 * ball.velocity ) / 2 ) * dt )
|
|
if player1.y < playerDistance + 20 then
|
|
player1.y = playerDistance + 20
|
|
end
|
|
elseif love.keyboard.isDown( "s" ) then
|
|
player1.y = player1.y + ( ( ( 3 * ball.velocity ) / 2 ) * dt )
|
|
if player1.y > playAreaHeight - ( 120 + playerDistance ) then
|
|
player1.y = playAreaHeight - ( 120 + playerDistance )
|
|
end
|
|
end
|
|
|
|
-- Left and right movement for player 2
|
|
if player2.xVelocity == nil then
|
|
if player2.x - 20 > ball.x then
|
|
player2.x = player2.x - ( ( ( 3 * ball.velocity ) / 2 ) * dt )
|
|
player2.xVelocity = "left"
|
|
if player2.x < playerDistance + 20 then
|
|
player2.x = playerDistance + 20
|
|
end
|
|
elseif player2.x + 80 < ball.x then
|
|
player2.x = player2.x + ( ( ( 3 * ball.velocity ) / 2 ) * dt )
|
|
player2.xVelocity = "right"
|
|
if player2.x > love.graphics:getWidth() - ( 120 + playerDistance )
|
|
then
|
|
player2.x = love.graphics:getWidth() - ( 120 + playerDistance )
|
|
end
|
|
end
|
|
else
|
|
if player2.xVelocity == "left" then
|
|
player2.x = player2.x - ( ( ( 3 * ball.velocity ) / 2 ) * dt )
|
|
if player2.x < playerDistance + 20 then
|
|
player2.x = playerDistance + 20
|
|
end
|
|
elseif player2.xVelocity == "right" then
|
|
player2.x = player2.x + ( ( ( 3 * ball.velocity ) / 2 ) * dt )
|
|
if player2.x > love.graphics:getWidth() - ( 120 + playerDistance )
|
|
then
|
|
player2.x = love.graphics:getWidth() - ( 120 + playerDistance )
|
|
end
|
|
end
|
|
end
|
|
-- Up and down movement for player 2
|
|
if player2.yVelocity == nil then
|
|
if player2.y - 20 > ball.y then
|
|
player2.y = player2.y - ( ( ( 3 * ball.velocity ) / 2 ) * dt )
|
|
player2.yVelocity = "up"
|
|
if player2.y < playerDistance + 20 then
|
|
player2.y = playerDistance + 20
|
|
end
|
|
elseif player2.y + 80 < ball.y then
|
|
player2.y = player2.y + ( ( ( 3 * ball.velocity ) / 2 ) * dt )
|
|
player2.yVelocity = "down"
|
|
if player2.y > playAreaHeight - ( 120 + playerDistance ) then
|
|
player2.y = playAreaHeight - ( 120 + playerDistance )
|
|
end
|
|
end
|
|
else
|
|
if player2.yVelocity == "up" then
|
|
player2.y = player2.y - ( ( ( 3 * ball.velocity ) / 2 ) * dt )
|
|
if player2.y < playerDistance + 20 then
|
|
player2.y = playerDistance + 20
|
|
end
|
|
elseif player2.yVelocity == "down" then
|
|
player2.y = player2.y + ( ( ( 3 * ball.velocity ) / 2 ) * dt )
|
|
if player2.y > playAreaHeight - ( 120 + playerDistance ) then
|
|
player2.y = playAreaHeight - ( 120 + playerDistance )
|
|
end
|
|
end
|
|
end
|
|
-- Reset the counter and allow player2 to move twice a second
|
|
if player2.counter > love.timer.getFPS() / 2 then
|
|
player2.counter = 0
|
|
player2.xVelocity = nil
|
|
player2.yVelocity = nil
|
|
end
|
|
|
|
if ball.counter > 60 then
|
|
-- Move the ball
|
|
ball.x = ball.x + ( dt * ball.velocity * math.cos( ( ball.direction *
|
|
math.pi ) / 180 ) )
|
|
ball.y = ball.y + ( dt * ball.velocity * math.sin( ( ball.direction *
|
|
math.pi ) / 180 ) )
|
|
-- Accelerate the ball
|
|
ball.velocity = ball.velocity + ( ball.acceleration * dt )
|
|
else
|
|
ball.counter = ball.counter + 1
|
|
end
|
|
-- Check collision with the left blocks
|
|
if ball.x < 15 then
|
|
local blockLength = ( playAreaHeight - ( 2 * math.sqrt( 2 ) *
|
|
playerDistance ) ) / 10
|
|
for i = 1,10,1
|
|
do
|
|
if leftBlocks[i] > 0 then
|
|
if ball.y > ( math.sqrt( 2 ) * playerDistance ) + ( blockLength * ( i - 1 ) ) - 10 and
|
|
ball.y < ( math.sqrt( 2 ) * playerDistance ) + ( blockLength * i ) - 10 then
|
|
ball.direction = 180 - ball.direction
|
|
ball.x = 15
|
|
leftBlocks[i] = leftBlocks[i] - 1
|
|
player2.score = player2.score + 1
|
|
break
|
|
end
|
|
end
|
|
end
|
|
end
|
|
-- Check collision with the right blocks
|
|
if ball.x > love.graphics:getWidth() - 35 then
|
|
local blockLength = ( playAreaHeight - ( 2 * math.sqrt( 2 ) *
|
|
playerDistance ) ) / 10
|
|
for i = 1,10,1
|
|
do
|
|
if rightBlocks[i] > 0 then
|
|
if ball.y > ( math.sqrt( 2 ) * playerDistance ) + ( blockLength * ( i - 1 ) ) - 10 and
|
|
ball.y < ( math.sqrt( 2 ) * playerDistance ) + ( blockLength * i ) - 10 then
|
|
ball.direction = 180 - ball.direction
|
|
ball.x = love.graphics:getWidth() - 35
|
|
rightBlocks[i] = rightBlocks[i] - 1
|
|
player1.score = player1.score + 1
|
|
break
|
|
end
|
|
end
|
|
end
|
|
end
|
|
-- Check collision with the top blocks
|
|
if ball.y < 15 then
|
|
local blockLength = ( love.graphics:getWidth() - ( 2 * math.sqrt( 2 ) *
|
|
playerDistance ) ) / 10
|
|
for i = 1,10,1
|
|
do
|
|
if topBlocks[i] > 0 then
|
|
if ball.x > ( math.sqrt( 2 ) * playerDistance ) + ( blockLength * ( i - 1 ) ) - 10 and
|
|
ball.x < ( math.sqrt( 2 ) * playerDistance ) + ( blockLength * i ) - 10 then
|
|
ball.direction = 360 - ball.direction
|
|
ball.y = 15
|
|
topBlocks[i] = topBlocks[i] -1
|
|
player1.score = player1.score + 1
|
|
break
|
|
end
|
|
end
|
|
end
|
|
end
|
|
-- Check collision with the bottom blocks
|
|
if ball.y > playAreaHeight - 35 then
|
|
local blockLength = ( love.graphics:getWidth() - ( 2 * math.sqrt( 2 ) *
|
|
playerDistance ) ) / 10
|
|
for i = 1,10,1
|
|
do
|
|
if bottomBlocks[i] > 0 then
|
|
if ball.x > ( math.sqrt( 2 ) * playerDistance ) + ( blockLength * ( i - 1 ) ) - 10 and
|
|
ball.x < ( math.sqrt( 2 ) * playerDistance ) + ( blockLength * i ) - 10 then
|
|
ball.direction = 360 - ball.direction
|
|
ball.y = playAreaHeight - 35
|
|
bottomBlocks[i] = bottomBlocks[i] -1
|
|
player2.score = player2.score + 1
|
|
break
|
|
end
|
|
end
|
|
end
|
|
end
|
|
-- Check collision with the edge
|
|
if ( ball.x > love.graphics:getWidth() - 20 or ball.x < 20 ) and
|
|
( ball.y > playAreaHeight - 20 or ball.y < 20 ) then
|
|
ball.direction = ball.direction + 180
|
|
if ball.x > love.graphics:getWidth() - 20 then
|
|
ball.x = love.graphics:getWidth() - 20
|
|
else
|
|
ball.x = 20
|
|
end
|
|
if ball.y > playAreaHeight - 20 then
|
|
ball.y = playAreaHeight - 20
|
|
else
|
|
ball.y = 20
|
|
end
|
|
elseif ball.x > love.graphics:getWidth() - 20 or ball.x < 0 then
|
|
ball.direction = 180 - ball.direction
|
|
-- Right edge
|
|
if ball.x > love.graphics:getWidth() - 20 then
|
|
if ball.y > math.sqrt( 2 ) * playerDistance and
|
|
ball.y < playAreaHeight - ( math.sqrt( 2 ) * playerDistance )
|
|
then
|
|
player2.score = player2.score - 1
|
|
reset()
|
|
else
|
|
ball.color = { 1, 0, 1 }
|
|
ball.x = love.graphics:getWidth() - 20
|
|
end
|
|
else -- Left edge
|
|
if ball.y > math.sqrt( 2 ) * playerDistance and
|
|
ball.y < playAreaHeight - ( math.sqrt( 2 ) * playerDistance )
|
|
then
|
|
player1.score = player1.score - 1
|
|
reset()
|
|
else
|
|
ball.color = { 1, 0, 1 }
|
|
ball.x = 0
|
|
end
|
|
end
|
|
elseif ball.y > playAreaHeight - 20 or ball.y < 0 then
|
|
ball.direction = 360 - ball.direction
|
|
-- Bottom edge
|
|
if ball.y > playAreaHeight - 20 then
|
|
if ball.x > math.sqrt( 2 ) * playerDistance and
|
|
ball.x < love.graphics:getWidth() -
|
|
( math.sqrt( 2 ) * playerDistance ) then
|
|
player1.score = player1.score - 1
|
|
reset()
|
|
else
|
|
ball.color = { 1, 0, 1 }
|
|
ball.y = playAreaHeight - 20
|
|
end
|
|
else -- Top edge
|
|
if ball.x > math.sqrt( 2 ) * playerDistance and
|
|
ball.x < love.graphics:getWidth() -
|
|
( math.sqrt( 2 ) * playerDistance ) then
|
|
player2.score = player2.score - 1
|
|
reset()
|
|
else
|
|
ball.color = { 1, 0, 1 }
|
|
ball.y = 0
|
|
end
|
|
end
|
|
end
|
|
-- Check collision with player 1's x paddle
|
|
if ball.x > ( player1.x - 20 ) and ball.x < ( player1.x + 100 ) and
|
|
ball.y > playAreaHeight - ( 40 + playerDistance ) and
|
|
ball.y < playAreaHeight - playerDistance then
|
|
ball.color = { 1, 0.4, 0.4 }
|
|
if ball.x < ( player1.x - 15 ) or ball.x > ( player1.x + 95 ) then
|
|
ball.direction = 180 - ball.direction
|
|
if ball.x < ( player1.x - 15 ) then
|
|
ball.x = player1.x - 20
|
|
else
|
|
ball.x = player1.x + 100
|
|
end
|
|
elseif ball.y > playAreaHeight - ( 40 + playerDistance ) and
|
|
ball.y < playAreaHeight - playerDistance then
|
|
ball.direction = 360 - ball.direction
|
|
if ball.y < playAreaHeight - ( 20 + playerDistance ) then
|
|
ball.y = playAreaHeight - ( 40 + playerDistance )
|
|
else
|
|
ball.y = playAreaHeight - playerDistance
|
|
end
|
|
end
|
|
end
|
|
-- Check collision with player 1's y paddle
|
|
if ball.y > ( player1.y - 20 ) and ball.y < ( player1.y + 100 ) and
|
|
ball.x > playerDistance - 20 and ball.x < playerDistance + 20 then
|
|
ball.color = { 1, 0.6, 0.6 }
|
|
if ball.y < ( player1.y - 15 ) or ball.y > ( player1.y + 95 ) then
|
|
ball.direction = 360 - ball.direction
|
|
if ball.y < ( player1.y - 15 ) then
|
|
ball.y = player1.y - 20
|
|
else
|
|
ball.y = player1.y + 100
|
|
end
|
|
elseif ball.x > playerDistance - 20 and
|
|
ball.x < playerDistance + 20 then
|
|
ball.direction = 180 - ball.direction
|
|
if ball.x < playerDistance then
|
|
ball.x = playerDistance - 20
|
|
else
|
|
ball.x = playerDistance + 20
|
|
end
|
|
end
|
|
end
|
|
-- Check collision with player 2's x paddle
|
|
if ball.x > ( player2.x - 20 ) and ball.x < ( player2.x + 100 ) and
|
|
ball.y > playerDistance - 20 and ball.y < playerDistance + 20 then
|
|
ball.color = { 0.6, 0.6, 1 }
|
|
if ball.x < ( player2.x - 15 ) or ball.x > ( player2.x + 95 ) then
|
|
ball.direction = 180 - ball.direction
|
|
if ball.x < ( player2.x - 15 ) then
|
|
ball.x = player2.x - 20
|
|
else
|
|
ball.x = player2.x + 100
|
|
end
|
|
elseif ball.y > playerDistance - 20 and
|
|
ball.y < playerDistance + 20 then
|
|
ball.direction = 360 - ball.direction
|
|
if ball.y < playerDistance then
|
|
ball.y = playerDistance - 20
|
|
else
|
|
ball.y = playerDistance + 20
|
|
end
|
|
end
|
|
end
|
|
-- Check collision with player 2's y paddle
|
|
if ball.y > ( player2.y - 20 ) and ball.y < ( player2.y + 100 ) and
|
|
ball.x > love.graphics:getWidth() - ( 40 + playerDistance ) and
|
|
ball.x < love.graphics:getWidth() - playerDistance then
|
|
ball.color = { 0.4, 0.4, 1 }
|
|
if ball.y < ( player2.y - 15 ) or ball.y > ( player2.y + 95 ) then
|
|
ball.direction = 360 - ball.direction
|
|
if ball.y < ( player2.y - 15 ) then
|
|
ball.y = player2.y - 20
|
|
else
|
|
ball.y = player2.y + 100
|
|
end
|
|
elseif ball.x > love.graphics:getWidth() - ( 40 + playerDistance ) and
|
|
ball.x < love.graphics:getWidth() - playerDistance then
|
|
ball.direction = 180 - ball.direction
|
|
if ball.x < love.graphics:getWidth() - ( 20 + playerDistance ) then
|
|
ball.x = love.graphics:getWidth() - ( 40 + playerDistance )
|
|
else
|
|
ball.x = love.graphics:getWidth() - playerDistance
|
|
end
|
|
end
|
|
end
|
|
|
|
-- Increase player 2's counter
|
|
player2.counter = player2.counter + 1
|
|
|
|
-- Key counter
|
|
keyCounter = keyCounter + 1
|
|
|
|
-- Reset playAreaHeight ( In case of window resize )
|
|
playAreaHeight = love.graphics:getHeight() - 50
|
|
end
|
|
|
|
function love.draw( dt )
|
|
-- Game over
|
|
if gameOver then
|
|
drawCorners()
|
|
local winner = nil
|
|
if player1.score > player2.score then
|
|
love.graphics.setColor( 1, 0, 0 )
|
|
winner = "red"
|
|
else
|
|
love.graphics.setColor( 0, 0, 1 )
|
|
winner = "blue"
|
|
end
|
|
love.graphics.rectangle( "fill", 0, playAreaHeight, love.graphics:getWidth(), 50 )
|
|
love.graphics.setColor( 1, 1, 1 )
|
|
scoreFont = love.graphics.newFont( 40 )
|
|
love.graphics.setFont( scoreFont )
|
|
love.graphics.printf( "Winner: " .. winner, 10, playAreaHeight,
|
|
love.graphics:getWidth() - 20, "center" )
|
|
love.graphics.printf( "Game over!", 10, 160, love.graphics:getWidth() - 20, "center" )
|
|
if endMenuSelect == 1 then
|
|
scoreFont = love.graphics.newFont( 35 )
|
|
love.graphics.setFont( scoreFont )
|
|
love.graphics.setColor( 1, 1, 1 )
|
|
else
|
|
scoreFont = love.graphics.newFont( 30 )
|
|
love.graphics.setFont( scoreFont )
|
|
love.graphics.setColor( 0.5, 0.5, 0.5 )
|
|
end
|
|
love.graphics.printf( "> (R)estart", 10, 240, love.graphics:getWidth() - 20, "center" )
|
|
if endMenuSelect == 2 then
|
|
scoreFont = love.graphics.newFont( 35 )
|
|
love.graphics.setFont( scoreFont )
|
|
love.graphics.setColor( 1, 1, 1 )
|
|
else
|
|
scoreFont = love.graphics.newFont( 30 )
|
|
love.graphics.setFont( scoreFont )
|
|
love.graphics.setColor( 0.5, 0.5, 0.5 )
|
|
end
|
|
love.graphics.printf( "> (Q)uit", 10, 320, love.graphics:getWidth() - 20, "center" )
|
|
return
|
|
end
|
|
|
|
-- Paused
|
|
if paused then
|
|
drawCorners()
|
|
drawScore()
|
|
love.graphics.setColor( 1, 1, 1 )
|
|
scoreFont = love.graphics.newFont( 40 )
|
|
love.graphics.setFont( scoreFont )
|
|
love.graphics.printf( "Paused", 10, 160, love.graphics.getWidth() - 20, "center" )
|
|
if pauseMenuSelect == 1 then
|
|
scoreFont = love.graphics.newFont( 35 )
|
|
love.graphics.setFont( scoreFont )
|
|
love.graphics.setColor( 1, 1, 1 )
|
|
else
|
|
scoreFont = love.graphics.newFont( 30 )
|
|
love.graphics.setFont( scoreFont )
|
|
love.graphics.setColor( 0.5, 0.5, 0.5 )
|
|
end
|
|
love.graphics.printf( "> (R)esume", 10, 240, love.graphics:getWidth() - 20, "center" )
|
|
if pauseMenuSelect == 2 then
|
|
scoreFont = love.graphics.newFont( 35 )
|
|
love.graphics.setFont( scoreFont )
|
|
love.graphics.setColor( 1, 1, 1 )
|
|
else
|
|
scoreFont = love.graphics.newFont( 30 )
|
|
love.graphics.setFont( scoreFont )
|
|
love.graphics.setColor( 0.5, 0.5, 0.5 )
|
|
end
|
|
love.graphics.printf( "> (Q)uit", 10, 320, love.graphics:getWidth() - 20, "center" )
|
|
return
|
|
end
|
|
|
|
drawBackground()
|
|
|
|
-- Ball
|
|
love.graphics.setColor( ball.color )
|
|
love.graphics.draw( ball.img, ball.x, ball.y )
|
|
love.graphics.setColor( 1, 1, 1 )
|
|
|
|
drawCorners()
|
|
|
|
drawScore()
|
|
|
|
-- Player 1 paddles
|
|
love.graphics.draw( player1.xImg, player1.x,
|
|
playAreaHeight - ( 20 + playerDistance ) )
|
|
love.graphics.draw( player1.yImg, playerDistance, player1.y )
|
|
|
|
-- Player 2 paddles
|
|
love.graphics.draw( player2.xImg, player2.x, playerDistance )
|
|
love.graphics.draw( player2.yImg,
|
|
love.graphics:getWidth() - ( 20 + playerDistance ),
|
|
player2.y )
|
|
|
|
-- Left blocks
|
|
local blockLength = ( playAreaHeight - ( 2 * math.sqrt( 2 ) *
|
|
playerDistance ) ) / 10
|
|
for i = 1,10,1
|
|
do
|
|
if leftBlocks[i] > 0 then
|
|
love.graphics.setColor( ball.color )
|
|
love.graphics.rectangle( "fill", 0, ( ( i - 1 ) * blockLength ) + ( math.sqrt( 2 ) * playerDistance ),
|
|
15, blockLength )
|
|
love.graphics.setColor( 0, 0, 0 )
|
|
love.graphics.rectangle( "line", 0, ( ( i - 1 ) * blockLength ) + ( math.sqrt( 2 ) * playerDistance ),
|
|
15, blockLength )
|
|
end
|
|
end
|
|
|
|
-- Right blocks
|
|
for i = 1,10,1
|
|
do
|
|
if rightBlocks[i] > 0 then
|
|
love.graphics.setColor( ball.color )
|
|
love.graphics.rectangle( "fill", love.graphics:getWidth() - 15,
|
|
( ( i - 1 ) * blockLength ) + ( math.sqrt( 2 ) * playerDistance ),
|
|
15, blockLength )
|
|
love.graphics.setColor( 0, 0, 0 )
|
|
love.graphics.rectangle( "line", love.graphics:getWidth() - 15,
|
|
( ( i - 1 ) * blockLength ) + ( math.sqrt( 2 ) * playerDistance ),
|
|
15, blockLength )
|
|
end
|
|
end
|
|
|
|
-- Top blocks
|
|
local blockLength = ( love.graphics:getWidth() - ( 2 * math.sqrt( 2 ) * playerDistance ) ) / 10
|
|
for i = 1,10,1
|
|
do
|
|
if topBlocks[i] > 0 then
|
|
love.graphics.setColor( ball.color )
|
|
love.graphics.rectangle( "fill", ( ( i - 1 ) * blockLength ) + ( math.sqrt( 2 ) * playerDistance ),
|
|
0, blockLength, 15 )
|
|
love.graphics.setColor( 0, 0, 0 )
|
|
love.graphics.rectangle( "line", ( ( i - 1 ) * blockLength ) + ( math.sqrt( 2 ) * playerDistance ),
|
|
0, blockLength, 15 )
|
|
end
|
|
end
|
|
|
|
-- Bottom blocks
|
|
for i = 1,10,1
|
|
do
|
|
if bottomBlocks[i] > 0 then
|
|
love.graphics.setColor( ball.color )
|
|
love.graphics.rectangle( "fill", ( ( i - 1 ) * blockLength ) + ( math.sqrt( 2 ) * playerDistance ),
|
|
playAreaHeight - 15, blockLength, 15 )
|
|
love.graphics.setColor( 0, 0, 0 )
|
|
love.graphics.rectangle( "line", ( ( i - 1 ) * blockLength ) + ( math.sqrt( 2 ) * playerDistance ),
|
|
playAreaHeight - 15, blockLength, 15 )
|
|
end
|
|
end
|
|
end
|
|
|
|
-- Draw the background
|
|
function drawBackground()
|
|
-- Background
|
|
love.graphics.setColor( 1, 0, 1 )
|
|
local linePoints = {
|
|
playerDistance / math.sqrt( 2 ),
|
|
playerDistance / math.sqrt( 2 ),
|
|
love.graphics:getWidth() - ( playerDistance / math.sqrt( 2 ) ),
|
|
playAreaHeight - ( playerDistance / math.sqrt( 2 ) ),
|
|
}
|
|
love.graphics.line( linePoints )
|
|
love.graphics.setColor( 1, 1, 1 )
|
|
end
|
|
|
|
-- Draw the corners
|
|
function drawCorners()
|
|
-- Corners
|
|
love.graphics.setColor( 1, 0, 0, 0.75 )
|
|
local trianglePoints = {
|
|
0,
|
|
playAreaHeight - ( math.sqrt( 2 ) * playerDistance ),
|
|
( math.sqrt( 2 ) * playerDistance ),
|
|
playAreaHeight,
|
|
0,
|
|
playAreaHeight
|
|
}
|
|
love.graphics.polygon( "fill", trianglePoints )
|
|
trianglePoints = {
|
|
0,
|
|
0,
|
|
playerDistance / math.sqrt( 2 ),
|
|
playerDistance / math.sqrt( 2 ),
|
|
0,
|
|
math.sqrt( 2 ) * playerDistance
|
|
}
|
|
love.graphics.polygon( "fill", trianglePoints )
|
|
trianglePoints = {
|
|
love.graphics:getWidth() - ( playerDistance / math.sqrt( 2 ) ),
|
|
playAreaHeight - ( playerDistance / math.sqrt( 2 ) ),
|
|
love.graphics:getWidth(),
|
|
playAreaHeight,
|
|
love.graphics:getWidth() - ( math.sqrt( 2 ) * playerDistance ),
|
|
playAreaHeight
|
|
}
|
|
love.graphics.polygon( "fill", trianglePoints )
|
|
love.graphics.setColor( 0, 0, 1, 0.75 )
|
|
trianglePoints = {
|
|
love.graphics:getWidth() - ( math.sqrt( 2 ) * playerDistance ),
|
|
0,
|
|
love.graphics:getWidth(),
|
|
0,
|
|
love.graphics:getWidth(),
|
|
math.sqrt( 2 ) * playerDistance
|
|
}
|
|
love.graphics.polygon( "fill", trianglePoints )
|
|
trianglePoints = {
|
|
0,
|
|
0,
|
|
playerDistance / math.sqrt( 2 ),
|
|
playerDistance / math.sqrt( 2 ),
|
|
math.sqrt( 2 ) * playerDistance,
|
|
0
|
|
}
|
|
love.graphics.polygon( "fill", trianglePoints )
|
|
trianglePoints = {
|
|
love.graphics:getWidth() - ( playerDistance / math.sqrt( 2 ) ),
|
|
playAreaHeight - ( playerDistance / math.sqrt( 2 ) ),
|
|
love.graphics:getWidth(),
|
|
playAreaHeight,
|
|
love.graphics:getWidth(),
|
|
playAreaHeight - ( math.sqrt( 2 ) * playerDistance )
|
|
}
|
|
love.graphics.polygon( "fill", trianglePoints )
|
|
love.graphics.setColor( 1, 0, 1 )
|
|
linePoints = {
|
|
0,
|
|
0,
|
|
playerDistance / math.sqrt( 2 ),
|
|
playerDistance / math.sqrt( 2 )
|
|
}
|
|
love.graphics.line( linePoints )
|
|
linePoints = {
|
|
love.graphics:getWidth() - ( playerDistance / math.sqrt( 2 ) ),
|
|
playAreaHeight - ( playerDistance / math.sqrt( 2 ) ),
|
|
love.graphics:getWidth(),
|
|
playAreaHeight
|
|
}
|
|
love.graphics.line( linePoints )
|
|
love.graphics.setColor( 1, 1, 1 )
|
|
end
|
|
|
|
function drawScore()
|
|
-- Scoring background
|
|
love.graphics.setColor( 1, 0, 0, 0.5 )
|
|
local rectanglePoints = {
|
|
0,
|
|
playAreaHeight,
|
|
( love.graphics:getWidth() / 2 ) - 25,
|
|
playAreaHeight,
|
|
( love.graphics:getWidth() / 2 ) + 25,
|
|
love.graphics:getHeight(),
|
|
0,
|
|
love.graphics:getHeight()
|
|
}
|
|
love.graphics.polygon( "fill", rectanglePoints )
|
|
love.graphics.setColor( 0, 0, 1, 0.5 )
|
|
rectanglePoints = {
|
|
( love.graphics:getWidth() / 2 ) - 25,
|
|
playAreaHeight,
|
|
love.graphics:getWidth(),
|
|
playAreaHeight,
|
|
love.graphics:getWidth(),
|
|
love.graphics:getHeight(),
|
|
( love.graphics:getWidth() / 2 ) + 25,
|
|
love.graphics:getHeight()
|
|
}
|
|
love.graphics.polygon( "fill", rectanglePoints )
|
|
love.graphics.setColor( 1, 1, 1 )
|
|
|
|
-- Player 1 score
|
|
scoreFont = love.graphics.newFont( 40 )
|
|
love.graphics.setFont( scoreFont )
|
|
love.graphics.printf( player1.score, 10, playAreaHeight,
|
|
( love.graphics:getWidth() / 2 ) - 60, "left" )
|
|
|
|
-- Player 2 score
|
|
love.graphics.printf( player2.score, ( love.graphics:getWidth() / 2 ) + 50,
|
|
playAreaHeight, ( love.graphics:getWidth() / 2 ) - 60,
|
|
"right" )
|
|
end
|