'===========================================================================
' Subject: TWO PLAYER PONG                    Date: 03-01-97 (02:26)       
'  Author: Isaac Grover                       Code: QB, QBasic, PDS        
'  Origin: FidoNet QUIK_BAS Echo            Packet: GAMES.ABC
'===========================================================================
'Here's my version of Pong.  I was planning on releasing it as
'freeware, but since I never finished it, I'll go ahead and
'release the code as it exists into the public domain.  It was
'written using screen 13, and while it may not be the most
'efficient way to program Pong, it works and it appears to be
'fast.  That's all that matters anyway.  =)

DECLARE SUB SetScore (RightScore%, LeftScore%)
DECLARE SUB RightBar (KeyPress%, RightCol%, RightRow%, BarLength%, PlayStep%)
DECLARE SUB LeftBar (KeyPress%, LeftCol%, LeftRow%, BarLength%, PlayStep%)
DECLARE SUB BounceBall (BallCol%, BallRow%, ChangeInCol%, ChangeInRow%, RightScore%, LeftScore%)
DECLARE SUB InitGame (Delay#, BarLength%, BarWidth%)

DIM SHARED Bar(640)
DIM SHARED Ball(640)

LeftCol% = 48                           ' starting column of bar
LeftRow% = 48                           ' starting row of bar
RightCol% = 270                         ' starting column of bar
RightRow% = 48                          ' starting row of bar
BallCol% = 160                          ' starting column of ball
BallRow% = 100                          ' starting row of ball
ChangeInCol% = 1                        ' horiz. motion increment for ball
ChangeInRow% = 1                        ' vert. motion increment for ball
PlayStep% = 1                           ' motion increment for ball and bar
BarLength% = 30
BarWidth% = 2
RightScore% = 0
LeftScore% = 0

InitGame Delay#, BarLength%, BarWidth%

DO
  BounceBall BallCol%, BallRow%, ChangeInCol%, ChangeInRow%, RightScore%, LeftScore%
  IF KeyPress% = 1 THEN EXIT DO
  KeyPress% = INP(&H60)
  RightBar KeyPress%, RightCol%, RightRow%, BarLength%, PlayStep%
  WHILE INKEY$ <> "": WEND
  KeyPress% = INP(&H60)
  LeftBar KeyPress%, LeftCol%, LeftRow%, BarLength%, PlayStep%
  WHILE INKEY$ <> "": WEND
  FOR Loop1# = 1 TO Delay#: NEXT
LOOP

SUB BounceBall (BallCol%, BallRow%, ChangeInCol%, ChangeInRow%, RightScore%, LeftScore%)
  RANDOMIZE TIMER
  IF POINT(BallCol% + 6, BallRow% + 11) = 7 THEN ChangeInRow% = -ChangeInRow%
  IF POINT(BallCol% + 13, BallRow% + 5) = 7 THEN ChangeInCol% = -ChangeInCol%
  IF POINT(BallCol% + 6, BallRow% - 1) = 7 THEN ChangeInRow% = -ChangeInRow%
  IF POINT(BallCol% - 1, BallRow% + 5) = 7 THEN ChangeInCol% = -ChangeInCol%
  BallCol% = BallCol% + ChangeInCol%
  BallRow% = BallRow% + ChangeInRow%
  'LOCATE 12, 20: PRINT "Column:"; BallCol%
  'LOCATE 13, 20: PRINT "Row:"; BallRow%
  IF BallCol% >= 308 THEN RightScore% = (RightScore% + 1) / 3: SetScore RightScore%, LeftScore%
  IF BallCol% <= 0 THEN LeftScore% = (LeftScore% + 1) / 3: SetScore RightScore%, LeftScore%
  IF (BallCol% <= 307) AND (BallCol% > 0) THEN PUT (BallCol%, BallRow%), Ball, PSET
END SUB

SUB InitGame (Delay#, BarLength%, BarWidth%)

  StartTime# = TIMER
  FOR Loop1% = 1 TO 25: FOR Loop2% = 1 TO 32676: NEXT: NEXT
  StopTime# = TIMER: Delay# = (StopTime# - StartTime#) * 5000

  SCREEN 13

  LINE (50, 50)-(54 + BarWidth%, 50 + BarLength%), 7, BF
  GET (50, 49)-(54 + BarWidth%, 51 + BarLength%), Bar

  PSET (160, 100)
  DRAW "c7l5d2nu4rnu4fnu6fnu8rnu8rnu8rnu8rnu8enu6enu4rnu4"
  GET (154, 95)-(166, 105), Ball

  CLS

  FOR Loop1% = 1 TO 4
    LINE (Loop1%, 10)-(Loop1%, 80), 7
    LINE (Loop1%, 130)-(Loop1%, 200), 7
    LINE (320 - Loop1%, 10)-(320 - Loop1%, 80), 7
    LINE (320 - Loop1%, 130)-(320 - Loop1%, 200), 7
    LINE (Loop1%, Loop1% + 9)-(320 - Loop1%, Loop1% + 9), 7
    LINE (Loop1%, 200 - Loop1%)-(320 - Loop1%, 200 - Loop1%), 7
  NEXT

END SUB

SUB LeftBar (KeyPress%, LeftCol%, LeftRow%, BarLength%, PlayStep%)
  IF KeyPress% = 17 THEN LeftRow% = LeftRow% - PlayStep%
  IF KeyPress% = 31 THEN LeftRow% = LeftRow% + PlayStep%
  IF KeyPress% = 44 THEN LeftRow% = LeftRow% + PlayStep%
  IF KeyPress% = 45 THEN LeftRow% = LeftRow% + PlayStep%
  IF LeftRow% <= 14 THEN LeftRow% = 14
  IF LeftRow% >= 193 - BarLength% THEN LeftRow% = 193 - BarLength%
  PUT (LeftCol%, LeftRow%), Bar, PSET
END SUB

SUB RightBar (KeyPress%, RightCol%, RightRow%, BarLength%, PlayStep%)
  IF KeyPress% = 72 THEN RightRow% = RightRow% - PlayStep%
  IF KeyPress% = 80 THEN RightRow% = RightRow% + PlayStep%
  IF RightRow% <= 14 THEN RightRow% = 14
  IF RightRow% >= 193 - BarLength% THEN RightRow% = 193 - BarLength%
  PUT (RightCol%, RightRow%), Bar, PSET
END SUB

SUB SetScore (RightScore%, LeftScore%)
  LOCATE 1, 2: PRINT RightScore%
  LOCATE 1, 35: PRINT LeftScore%
END SUB
