
init 

 dim sc1=score
 dim sc2=score+1
 dim sc3=score+2


 dim m0xVelocity=a
 dim m0yVelocity=b

 dim m1xVelocity=c
 dim m1yVelocity=d

 dim p0balls=e
 dim p1balls=f

 dim pixelX=g
 dim pixelY=h

 dim pfX=i
 dim pfY=j

 dim collisionCounter=k
 dim growSide=l

 dim newXVelocity=m
 dim newYVelocity=n

 dim m0x=o
 dim m0y=p
 dim m1x=q
 dim m1y=r

 dim animationCounter=s
 dim deathColor=t

 data paddleXVelocity
 -2, -2, -1, -1, 1, 1, 2, 2
end

 data paddleYVelocity
 1, 1, 1, 2, 2, 1, 1, 1
end

 data pfreadMask
 128, 64, 32, 16, 8, 4, 2, 1
end

 const animationPeriod=2

 COLUPF = 24
 COLUP0 = 192
 COLUP1 = 192

 deathColor = COLUPF

 score = 0
 scorecolor = 14

 player1:
 %11111111
end
 
 player0:
 %11111111
end

 p0balls = 5
 p1balls = 5

 player0x = 90
 player0y = 3
 m0x = 90
 m0y = 22
 missile0height = 0
 m0xVelocity = 0
 m0yVelocity = 255
 AUDC0 = 0
 AUDF0 = 2
 AUDV0 = 0

 player1x = 90
 player1y = 88
 m1x = 90
 m1y = 60
 missile1height = 0
 m1xVelocity = 1
 m1yVelocity = 1
 AUDC1 = 0
 AUDF1 = 14
 AUDV1 = 0

 pfhline 0  5 31 on
 pfvline 0  0 10 on
 pfvline 31 0 10 on

 pfhline 0  4 31 on
 pfhline 0  6 31 on

 animationCounter = animationPeriod

mainloop

 rem checkWin
 if collision(playfield, player0) then goto player1Wins
 rem if p0balls = 0 then goto player1Wins
 if collision(playfield, player1) then goto player0Wins
 rem if p1balls = 0 then goto player0Wins

 rem checkCollisions
 if collision(playfield, missile0) then gosub handleM0PFCollision
 if collision(playfield, missile1) then gosub handleM1PFCollision
 if collision(player0, missile0) then gosub handleM0P0Collision
 if collision(player1, missile1) then gosub handleM1P1Collision

 rem updateCoords ***NOTE: FIX JOY1 PROBLEMS ****
 gosub updatePlayerCoords
 if animationCounter = 0 then gosub updateMissileCoords : animationCounter = animationPeriod
 animationCounter = animationCounter - 1

 if AUDV0 > 0 then AUDV0 = AUDV0 - 1
 if AUDV1 > 0 then AUDV1 = AUDV1 - 1

 drawscreen
 goto mainloop


changeColors
 COLUPF = deathColor
 COLUBK = deathColor ^ 255
 deathColor = deathColor + 1
 return


player0Wins
 pfhline 0 6  31 on
 pfhline 0 7  31 on
 pfhline 0 8  31 on
 pfhline 0 9  31 on
 pfhline 0 10 31 on

p0WinsLoop
 if animationCounter = 0 then gosub changeColors : animationCounter = animationPeriod
 animationCounter = animationCounter - 1
 drawscreen
 goto p0WinsLoop


player1Wins
 pfhline 0 0  31 on
 pfhline 0 1  31 on
 pfhline 0 2  31 on
 pfhline 0 3  31 on
 pfhline 0 4  31 on

p1WinsLoop
 if animationCounter = 0 then gosub changeColors : animationCounter = animationPeriod
 animationCounter = animationCounter - 1
 drawscreen
 goto p1WinsLoop


handleM0PFCollision
 pixelX = m0x
 pixelY = m0y

 gosub pixelToPF 

 if pfX = 0 || pfX = 31 then return
 if pfY < 5 then pfpixel pfX pfY off

 collisionCounter = 0
 z = pixelY - 1
 z = z & 7
 if z = 0 || z = 1 then collisionCounter = collisionCounter + 1 : newXVelocity = m0xVelocity : newYVelocity = 0 - m0yVelocity

 z = pixelX - 36
 z = z & 3
 if z = 0 || z = 3 then collisionCounter = collisionCounter + 1 : newXVelocity = 0 - m0xVelocity : newYVelocity = m0yVelocity

 if collisionCounter = 0 then newXVelocity = m0xVelocity : newYVelocity = 0 - m0yVelocity
 if collisionCounter = 2 then newXVelocity = 0 - m0xVelocity : newYVelocity = 0 - m0yVelocity

 rem sc3 = collisionCounter

 m0xVelocity = newXVelocity
 m0yVelocity = newYVelocity

 growSide = 1
 gosub growColumn
 
 return


handleM1PFCollision
 pixelX = m1x
 pixelY = m1y

 gosub pixelToPF 

 if pfX = 0 || pfX = 31 then return
 if pfY > 5 then pfpixel pfX pfY off

 collisionCounter = 0
 z = pixelY - 1
 z = z & 7
 if z = 0 || z = 1 then collisionCounter = collisionCounter + 1 : newXVelocity = m1xVelocity : newYVelocity = 0 - m1yVelocity

 z = pixelX - 36
 z = z & 3
 if z = 0 || z = 3 then collisionCounter = collisionCounter + 1 : newXVelocity = 0 - m1xVelocity : newYVelocity = m1yVelocity

 if collisionCounter = 0 then newXVelocity = m1xVelocity : newYVelocity = 0 - m1yVelocity
 if collisionCounter = 2 then newXVelocity = 0 - m1xVelocity : newYVelocity = 0 - m1yVelocity

 rem sc3 = collisionCounter

 m1xVelocity = newXVelocity
 m1yVelocity = newYVelocity

 growSide = 255
 gosub growColumn
 
 return

growColumn
 pfY = 5 + growSide
 sc3 = pfY
growLoop
 if pfread(pfX, pfY) then pfpixel pfX pfY on : goto growthDone
 pfY = pfY + growSide
 if pfY < 1 || pfY > 11 then goto growthDone else goto growLoop
growthDone
 return


handleM0P0Collision
 AUDV0 = 15
 z = m0x - player0x
 z = z - 1
 m0xVelocity = paddleXVelocity[z]
 m0yVelocity = paddleYVelocity[z]
 return

handleM1P1Collision
 AUDV1 = 15
 z = m1x - player1x
 z = z - 1
 m1xVelocity = paddleXVelocity[z]
 m1yVelocity = 0 - paddleYVelocity[z]
 return


pixelToPF
 pfX = pixelX - 32
 pfX = pfX / 4
 pfY = pixelY -  1
 pfY = pfY / 8
 return 

pfToPixel
 pixelX = pfX * 4 + 32
 pixelY = pfY * 8 +  1
 return


updatePlayerCoords

 if joy0left  then player0x = player0x - 1
 if joy0right then player0x = player0x + 1
 if joy1left  then player1x = player1x - 1
 if joy1right then player1x = player1x + 1

 if player0x < 35  then player0x = 35
 if player0x > 147 then player0x = 147

 if player1x < 35  then player1x = 35
 if player1x > 146 then player1x = 146

 return



resetMissile0
 m0x = 90
 m0y = 22
 missile0height = 0
 m0xVelocity = 0
 m0yVelocity = 255
 p0balls = p0balls - 1
 return

resetMissile1
 m1x = 90
 m1y = 60
 missile1height = 0
 m1xVelocity = 1
 m1yVelocity = 1
 p1balls = p1balls - 1
 return


updateMissileCoords

 m0x = m0x + m0xVelocity : m0y = m0y + m0yVelocity
 if m0x < 36  then m0xVelocity = 0 - m0xVelocity : m0x = 36
 if m0x > 154 then m0xVelocity = 0 - m0xVelocity : m0x = 154
 if m0y < 1   then gosub resetMissile0
 if m0y >= 40 then m0yVelocity = 255 : m0y = 40

 m1x = m1x + m1xVelocity : m1y = m1y + m1yVelocity
 if m1x < 36  then m1xVelocity = 0 - m1xVelocity : m1x = 36
 if m1x > 154 then m1xVelocity = 0 - m1xVelocity : m1x = 154
 if m1y <= 49 then m1yVelocity =   1 : m1y = 49
 if m1y > 88  then gosub resetMissile1

 if m0y = 40 then pixelX = m0x : pixelY = m0y : gosub pixelToPF : growSide = 1   : gosub growColumn
 rem if m1y = 49 then pixelX = m1x : pixelY = m1y : gosub pixelToPF : growSide = 255 : gosub growColumn

 missile0x = m0x : missile0y = m0y : missile1x = m1x : missile1y = m1y

 return