-- Farben
white = Color.new(255, 255, 255);
red = Color.new(255,0,0)
unloadbuff()

Background=Image.load("playpsp/bg1.png")
screen:blit(0,0,Background)

-- paddle image
paddleimg = Image.load("playpsp/paddle.png")


-- ball image
ballimg = Image.load("playpsp/ball.png")


-- score
score1 = 0
score2 = 0

---Direction
direction = "none" -- X direction
direction2 = "none" -- Y direction
Bstartdir = math.random(1,2) -- Ball start direction

-- paddles
paddle = { } -- paddle array
paddle[1] = { x = 5, y = 100, img = paddleimg, 
width = 10, height = 60 } -- paddle 1
paddle[2] = { x = 465, y = 100, img = paddleimg, 
width = 10, height = 60 } -- paddle 2

-- ball
ball = { } -- ball array
ball[1] = { x = 240, y = 130, img = ballimg, 
width = 10, height = 10} -- the ball

-- screen
screenwidth = 480 - paddleimg:width() -- the screen height
screenheight = 272 - paddleimg:height() -- the screen height

-- speed
speed = 2 -- the speed

-- Collision Detection Function
function collision(x1, y1, w1, h1, x2, y2, w2, h2)
if (y2 >= y1 and y1 + h1 >= y2) or (y2 + h2 >= y1 
and y1 + h1 >= y2 + h2) or (y1 >=
y2 and y2 + h2 >= y1) or (y1 + h1 >= y2 and y2 + h2 >= y1 + h1) 
then
if x2 >= x1 and x1 + w1 >= x2 then
return true
elseif x2 + w2 >= x1 and x1 + w1 >= x2 + w2 then
return true
elseif x1 >= x2 and x2 + w2 >= x1 then
return true
elseif x1 + w1 >= x2 and x2 + w2 >= x1 + w1 then
return true
end
end

return false
end

function movePlayer1( )
if pad:up() and paddle[1].y > 0 then
paddle[1].y = paddle[1].y - speed
end
if pad:down() and paddle[1].y < screenheight then
paddle[1].y = paddle[1].y + speed
end
end

function movePlayer2( )

if pad:triangle() and paddle[2].y > 0 then
paddle[2].y = paddle[2].y - speed
end

if pad:cross() and paddle[2].y < screenheight then
paddle[2].y = paddle[2].y + speed
end
end

while true do
screen:clear()
-- store player's position at beginning of each loop
oldx = ball[1].x
oldy = ball[1].y
screen:clear()

screen:blit(0,0,Background)
screen:print(220, 15, ""..score1, white)
screen:print(255, 15, ""..score2, white)


screen:blit(paddle[1].x, paddle[1].y, paddleimg )
screen:blit(paddle[2].x, paddle[2].y, paddleimg )


screen:blit(ball[1].x, ball[1].y, ball[1].img )
pad = Controls.read()

movePlayer1( )
movePlayer2( )

if pad:start() then
dofile("main.lua")
end

---Direction
if direction == "left" then
ball[1].x = ball[1].x - speed
end

if direction == "right" then
ball[1].x = ball[1].x + speed
end

if direction2 == "up" then
ball[1].y = ball[1].y - speed
end

if direction2 == "down" then
ball[1].y = ball[1].y + speed
end
-- ball start richtung
if Bstartdir == 1 then
direction = "left"
end

if Bstartdir == 2 then
direction = "right"
end

-- Collision Detection for paddle1
Bstartdir = 0
if collision(ball[1].x, ball[1].y, ball[1].width,ball[1].height, paddle[1].x,
paddle[1].y, paddle[1].width, paddle[1].height) then
direction = "right"
end

if collision(ball[1].x, ball[1].y, ball[1].width,ball[1].height, paddle[1].x,
paddle[1].y, paddle[1].width, paddle[1].height) and pad:up() then
Bstartdir = 0
direction = "right"
direction2 = "up"
end

if collision(ball[1].x, ball[1].y, ball[1].width,ball[1].height, paddle[1].x,
paddle[1].y, paddle[1].width, paddle[1].height) and pad:down() then
Bstartdir = 0
direction = "right"
direction2 = "down"
end

--- Collision Detection for paddle2
if collision(ball[1].x, ball[1].y, ball[1].width,ball[1].height, paddle[2].x,
paddle[2].y, paddle[2].width, paddle[2].height) then
Bstartdir = 0
direction = "left"
end

if collision(ball[1].x, ball[1].y, ball[1].width,ball[1].height, paddle[2].x,
paddle[2].y, paddle[2].width, paddle[2].height) and pad:triangle()
then
Bstartdir = 0
direction = "left"
direction2 = "up"
end

if collision(ball[1].x, ball[1].y, ball[1].width,ball[1].height, paddle[2].x,
paddle[2].y, paddle[2].width, paddle[2].height) and pad:cross() then
Bstartdir = 0
direction = "left"
direction2 = "down"
end

-- Edges
if ball[1].x >= 478 then
score1 = score1 + 1
direction = "left"
end

if ball[1].x <= 2 then
score2 = score2 + 1
direction = "right"
end

if ball[1].y <= 0 then
direction2 = "down"
end

if ball[1].y >= 262 then
direction2 = "up"
end

screen.waitVblankStart()
screen.flip()
end
