DECLARE SUB Beginning (r1%, num%)
DECLARE SUB eraseghost (gx%, gy%, gi%, gj%, pi!)
DECLARE SUB moveghost (gx%, gy%, gi%, gj%, pi%, pj%, board!())
DECLARE SUB drawghost (gx%, gy%, gi%, gj%, pi!)
DECLARE SUB Movepacman (x%, y%, key$, pi%, pj%, board(), counter%)
DECLARE SUB erasePacman (x%, y%)
DECLARE SUB DRAWPACMAN (x%, y%)
DECLARE SUB theboard (board!())
SCREEN 12
CALL Beginning(r1%, num%)
RANDOMIZE TIMER
      
DIM board(19, 22)

CALL theboard(board())

bx% = 0
by% = 0
FOR i% = 1 TO 19
bx% = 0
 FOR j% = 1 TO 22
    IF board(i%, j%) = 0 THEN
      LINE (bx%, by%)-STEP(29, 24), 15, BF
      LINE (bx%, by%)-STEP(29, 24), 0, B
      CIRCLE (bx% + 15, by% + 12), 3, 4
      PAINT (bx% + 15, by% + 12), 4, 4
    ELSEIF board(i%, j%) = 1 THEN
      LINE (bx%, by%)-STEP(29, 24), 1, BF
      LINE (bx%, by%)-STEP(29, 24), 0, B
    END IF
        bx% = bx% + 29
 NEXT j%
        by% = by% + 24
NEXT i%
x% = 43
y% = 36
pi% = 2
pj% = 2
gx% = 304
gy% = 204
gi% = 9
gj% = 11
pi! = 3.14
                                                 
DO
DO
        key$ = INKEY$
        CALL DRAWPACMAN(x%, y%)
        CALL drawghost(gx%, gy%, gi%, gj%, pi!)
        
        
        
        start! = TIMER
        DO
        LOOP UNTIL TIMER - start > .01
        CALL erasePacman(x%, y%)
        CALL Movepacman(x%, y%, key$, pi%, pj%, board(), counter%)
        CALL eraseghost(gx%, gy%, gi%, gj%, pi!)
        r% = INT(RND * num%) + 1
        IF r% = 1 THEN
        CALL moveghost(gx%, gy%, gi%, gj%, pi%, pj%, board())
        END IF
        LOOP UNTIL key$ <> " "
       
LOOP UNTIL key$ = "q" OR pi% = gi% AND pj% = gj%
IF counter% > 1700 THEN
PRINT " Congratulations.  You have beaten PAKman."
ELSE PRINT " You have been conquered by PAKman."
END IF
start! = TIMER
DO
LOOP UNTIL TIMER - start! > 4

END

SUB Beginning (r1%, num%)
FOR i% = 1 TO 40
r1% = INT(RND * 16) + 1
PAINT (0, 0), r1%
NEXT i%
LOCATE 20, 29
COLOR 4
PRINT "Welcome to PAKman...Craig Style !!!"
start! = TIMER
DO
LOOP UNTIL TIMER - start! > 2
PRINT " Choose Your Level"
INPUT " (1 - 8) Eight is easiest.  One is insanity", num%

END SUB

SUB drawghost (gx%, gy%, gi%, gj%, pi!)
REM ***Blue Ghost***
PSET (gx%, gy%), 9
CIRCLE STEP(0, 0), 9, 9, (pi! / 6), (5 * pi! / 6), 1
LINE STEP(-8, -4)-STEP(0, 12), 9
LINE STEP(16, -12)-STEP(0, 12), 9
CIRCLE STEP(-8, 5), 9, 9, (pi! / 6), (5 * pi! / 6), 1
PAINT STEP(1, -10), 9, 9
CIRCLE STEP(-5, -7), 1, 15
PAINT STEP(0, 0), 15, 15
CIRCLE STEP(8, 0), 1, 15
PAINT STEP(0, 0), 15, 15





END SUB

SUB DRAWPACMAN (x%, y%)
PSET (x%, y%), 14
CIRCLE STEP(0, 0), 10, 0, .57, 6
LINE STEP(0, 0)-STEP(10, -4), 0
LINE STEP(-10, 4)-STEP(10, 4), 0
PAINT STEP(-11, 0), 14, 0
END SUB

SUB eraseghost (gx%, gy%, gi%, gj%, pi!)
PSET (gx%, gy%), 15
CIRCLE STEP(0, 0), 9, 15, (pi! / 6), (5 * pi! / 6), 1
LINE STEP(-8, -4)-STEP(0, 12), 15
LINE STEP(16, -12)-STEP(0, 12), 15
CIRCLE STEP(-8, 5), 9, 15, (pi! / 6), (5 * pi! / 6), 1
PAINT STEP(1, -10), 15, 15
CIRCLE STEP(-5, -7), 1, 15
PAINT STEP(0, 0), 15, 15
CIRCLE STEP(8, 0), 1, 15
PAINT STEP(0, 0), 15, 15


END SUB

SUB erasePacman (x%, y%)
PSET (x%, y%), 16
CIRCLE STEP(0, 0), 10, 16, .57, 6
LINE STEP(0, 0)-STEP(10, -4), 16
LINE STEP(-10, 4)-STEP(10, 4), 16
PAINT STEP(-11, 0), 16, 16

END SUB

        SUB moveghost (gx%, gy%, gi%, gj%, pi%, pj%, board())
options% = 0
  IF board(gi% - 1, gj%) <> 1 THEN
    options% = options% + 1
  END IF
  IF board(gi% + 1, gj%) <> 1 THEN
    options% = options% + 2
  END IF
  IF board(gi%, gj% - 1) <> 1 THEN
    options% = options% + 4
  END IF
  IF board(gi%, gj% + 1) <> 1 THEN
    options% = options% + 8
  END IF

IF options% = 1 THEN
   IF pi% < gi% THEN
     gmove$ = "up"
   ELSE
    gmove$ = "no"
   END IF
ELSEIF options% = 2 THEN
   IF pi% > gi% THEN
     gmove$ = "down"
   ELSE
    gmove$ = "no"
   END IF
ELSEIF options% = 3 THEN
   IF pi% > gi% THEN
     gmove$ = "down"
   ELSEIF pi% < gi% THEN
     gmove$ = "up"
   ELSE
    gmove$ = "no"
   END IF
ELSEIF options% = 4 THEN
   IF pj% < gj% THEN
     gmove$ = "left"
   ELSE
    gmove$ = "no"
   END IF
ELSEIF options% = 5 THEN
   IF pi% < gi% THEN
      gmove$ = "up"
   ELSEIF pj% < gj% THEN
      gmove$ = "left"
   ELSE
   gmove$ = "no"
   END IF
ELSEIF options% = 6 THEN
   IF pi% > gi% THEN
      gmove$ = "down"
   ELSEIF pj% < gj% THEN
      gmove$ = "left"
   ELSE
   gmove$ = "no"
   END IF
ELSEIF options% = 7 THEN
   IF pi% < gi% THEN
      gmove$ = "up"
   ELSEIF pj% < gj% THEN
      gmove$ = "left"
   ELSEIF pi% > gi% THEN
   gmove$ = "down"
   ELSE
   gmove$ = "no"
   END IF
ELSEIF options% = 8 THEN
   IF pj% > gj% THEN
      gmove$ = "right"
   ELSE
   gmove$ = "no"
   END IF
ELSEIF options% = 9 THEN
   IF pi% < gi% THEN
      gmove$ = "up"
   ELSEIF pj% > gj% THEN
      gmove$ = "right"
   ELSE
   gmove$ = "no"
   END IF
ELSEIF options% = 10 THEN
   IF pi% > gi% THEN
      gmove$ = "down"
   ELSEIF pj% > gj% THEN
      gmove$ = "right"
   ELSE
   gmove$ = "no"
   END IF
ELSEIF options% = 11 THEN
   IF pi% < gi% THEN
      gmove$ = "up"
   ELSEIF pj% > gj% THEN
      gmove$ = "right"
   ELSEIF pi% > gi% THEN
   gmove$ = "down"
   ELSE
   gmove$ = "no"
   END IF
ELSEIF options% = 12 THEN
   IF pj% > gj% THEN
      gmove$ = "right"
   ELSEIF pj% < gj% THEN
      gmove$ = "left"
   ELSE
   gmove$ = "no"
   END IF
ELSEIF options% = 13 THEN
   IF pi% < gi% THEN
      gmove$ = "up"
   ELSEIF pj% < gj% THEN
      gmove$ = "left"
   ELSEIF pj% > gj% THEN
   gmove$ = "right"
   ELSE
   gmove$ = "no"
   END IF
ELSEIF options% = 14 THEN
   IF pi% > gi% THEN
      gmove$ = "down"
   ELSEIF pj% < gj% THEN
      gmove$ = "left"
   ELSEIF pj% > gj% THEN
   gmove$ = "right"
   ELSE
   gmove$ = "no"
   END IF
ELSEIF options% = 15 THEN
   IF pi% < gi% THEN
      gmove$ = "up"
   ELSEIF pj% < gj% THEN
      gmove$ = "left"
   ELSEIF pi% > gi% THEN
   gmove$ = "down"
   ELSEIF pj% > gj% THEN
   gmove$ = "right"
   ELSE
   gmove$ = "no"
   END IF

END IF


IF gmove$ = "up" THEN
   gi% = gi% - 1
   gy% = gy% - 24
ELSEIF gmove$ = "down" THEN
   gi% = gi% + 1
   gy% = gy% + 24
ELSEIF gmove$ = "left" THEN
   gj% = gj% - 1
   gx% = gx% - 29
ELSEIF gmove$ = "right" THEN
   gj% = gj% + 1
   gx% = gx% + 29
END IF


END SUB

SUB Movepacman (x%, y%, key$, pi%, pj%, board(), counter%)
IF key$ = "4" THEN
  IF pi% = 10 AND pj% = 1 THEN
     pj% = 22
     x% = 29 * (21) + 15

  ELSEIF board(pi%, pj% - 1) = 0 OR board(pi%, pj% - 1) = 2 THEN
     x% = x% - 29
     pj% = pj% - 1
     IF board(pi%, pj%) = 0 THEN
       board(pi%, pj%) = 2
      bx% = 29 * (pj% - 1)
      by% = 24 * (pi% - 1)
      LINE (bx%, by%)-STEP(29, 24), 15, BF
      LINE (bx%, by%)-STEP(29, 24), 0, B

       counter% = counter% + 10
       LOCATE 1, 1
       PRINT counter%
       END IF
  END IF

ELSEIF key$ = "8" THEN
  IF board(pi% - 1, pj%) = 0 OR board(pi% - 1, pj%) = 2 THEN
     y% = y% - 24
     pi% = pi% - 1
     IF board(pi%, pj%) = 0 THEN
       board(pi%, pj%) = 2
      bx% = 29 * (pj% - 1)
      by% = 24 * (pi% - 1)
      LINE (bx%, by%)-STEP(29, 24), 15, BF
      LINE (bx%, by%)-STEP(29, 24), 0, B

       counter% = counter% + 10
       LOCATE 1, 1
       PRINT counter%

     END IF
  END IF



ELSEIF key$ = "6" THEN
  IF pi% = 10 AND pj% = 22 THEN
     pj% = 1
     x% = 29 * (0) + 15
   
 ELSEIF board(pi%, pj% + 1) = 0 OR board(pi%, pj% + 1) = 2 THEN

        x% = x% + 29
        pj% = pj% + 1
     IF board(pi%, pj%) = 0 THEN
       board(pi%, pj%) = 2
      bx% = 29 * (pj% - 1)
      by% = 24 * (pi% - 1)
      LINE (bx%, by%)-STEP(29, 24), 15, BF
      LINE (bx%, by%)-STEP(29, 24), 0, B

       counter% = counter% + 10
       LOCATE 1, 1
       PRINT counter%

    END IF
  END IF


ELSEIF key$ = "2" THEN
  IF board(pi% + 1, pj%) = 0 OR board(pi% + 1, pj%) = 2 THEN
    y% = y% + 24
    pi% = pi% + 1
     IF board(pi%, pj%) = 0 THEN
       board(pi%, pj%) = 2
      bx% = 29 * (pj% - 1)
      by% = 24 * (pi% - 1)
      LINE (bx%, by%)-STEP(29, 24), 15, BF
      LINE (bx%, by%)-STEP(29, 24), 0, B

       counter% = counter% + 10
       LOCATE 1, 1
       PRINT counter%
      END IF
    END IF

END IF
END SUB

SUB theboard (board())
FOR i% = 1 TO 9
board(i%, 1) = 1
NEXT i%
FOR i% = 11 TO 19
board(i%, 1) = 1
NEXT i%
FOR i% = 1 TO 22
board(1, i%) = 1
NEXT i%
FOR i% = 1 TO 22
board(19, i%) = 1
NEXT i%
FOR i% = 1 TO 9
board(i%, 22) = 1
NEXT i%
FOR i% = 11 TO 19
board(i%, 22) = 1
NEXT i%
FOR i% = 3 TO 9
board(i%, 3) = 1
NEXT i%
board(3, 4) = 1
FOR i% = 8 TO 11
board(i%, 9) = 1
NEXT i%
FOR i% = 9 TO 13
board(11, i%) = 1
NEXT i%
FOR i% = 8 TO 11
board(i%, 13) = 1
NEXT i%
board(8, 12) = 1
board(8, 10) = 1
FOR i% = 11 TO 17
board(i%, 3) = 1
NEXT i%
board(17, 4) = 1
FOR i% = 3 TO 9
board(i%, 20) = 1
NEXT i%
board(3, 19) = 1
FOR i% = 6 TO 17
board(3, i%) = 1
board(17, i%) = 1
NEXT i%
FOR i% = 11 TO 17
board(i%, 20) = 1
NEXT i%
board(17, 19) = 1
FOR i% = 5 TO 15
board(i%, 5) = 1
board(i%, 18) = 1
NEXT i%
FOR i% = 4 TO 6
board(i%, 11) = 1
NEXT i%
FOR i% = 13 TO 16
board(i%, 11) = 1
NEXT i%
FOR i% = 5 TO 15
board(i%, 7) = 1
board(i%, 16) = 1
board(10, 16) = 0
NEXT i%
board(10, 7) = 0
FOR i% = 5 TO 6
board(i%, 9) = 1
NEXT i%
FOR i% = 5 TO 6
board(i%, 13) = 1
NEXT i%
board(6, 14) = 1
board(14, 9) = 1
board(14, 14) = 1
board(14, 13) = 1


END SUB

