Advertisement
Ham62

Text Adventure.bs2

Oct 18th, 2017
777
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
QBasic 3.31 KB | None | 0 0
  1. ' {$STAMP BS2}
  2. ' {$PBASIC 2.5}
  3.  
  4. Speaker      PIN 4
  5. MAX_INPUT    CON 10
  6.  
  7. sInputBuff   VAR Byte(MAX_INPUT)  'input buffer
  8. byKeyIn      VAR Byte             'Key read from terminal
  9. byCurPos     VAR Byte             'cursor position/input length
  10. byLinePos    VAR Byte             'position in line we're printing
  11. byCharIn     VAR byKeyIn          'Reuse KeyIn for reading strings to output
  12. wStrPtr      VAR Word             'pointer to string
  13.  
  14. bOutType     VAR Bit              'what we'll print, 0 = room description, 1 = other
  15. nDescription VAR Nib              'Description to type
  16. nCommand     VAR nDescription     'comand to compare against
  17. nRoom        VAR Nib              'Stores room we're in
  18.  
  19. '---- Room Descriptions ----'
  20. Room1 DATA "You find yourself in a dimly lit dungeon, exits are NORTH, SOUTH and WEST.", 0
  21. Room2 DATA "North room"
  22. '---- Other Descriptions ----'
  23. UnknownCommand DATA "I don't know what you're saying...", 0
  24. DSC_NOT_COMMAND CON 0
  25.  
  26. DO
  27.   byCurPos = 0 'reset input length to 0
  28.   GOSUB ReadInput
  29.  
  30.   GOSUB CheckInput
  31. LOOP
  32. END
  33.  
  34. 'Command strings
  35. CmdLook DATA "LOOK", 0
  36. 'and lookup constants
  37. CMD_LOOK CON 0
  38. CMD_NONE CON 1   'always must be last
  39.  
  40. bFoundMatch VAR Bit
  41. CheckInput:
  42.   nCommand = CMD_LOOK
  43.   GOSUB CompareStrings
  44.  
  45.   SELECT nCommand
  46.   CASE CMD_LOOK
  47.     bOutType = 0 'print room
  48.   CASE CMD_NONE
  49.     nDescription = DSC_NOT_COMMAND
  50.     bOutType = 1
  51.   ENDSELECT
  52.   GOSUB TypeDescription
  53. RETURN
  54.  
  55. CompareStrings:
  56.   DO
  57.     bFoundMatch = 1 'all are match until otherwise proven
  58.     LOOKUP nCommand, [CmdLook], wStrPtr
  59.     FOR byLinePos = 0 TO byCurPos - 1
  60.       READ wStrPtr + byLinePos, byCharIn
  61.       IF byCharIn <> sInputBuff(byLinePos + 1) THEN
  62.         bFoundMatch = 0           'not a match
  63.         nCommand = nCommand + 1   'move to next one
  64.         EXIT
  65.       ENDIF
  66.     NEXT
  67.     IF bFoundMatch THEN RETURN
  68.    LOOP UNTIL nCommand = CMD_NONE
  69. RETURN
  70.  
  71. TypeDescription:
  72.   byLinePos = 0 'start at begin of line
  73.  
  74.   IF bOutType = 0 THEN  'Room description
  75.     LOOKUP (nRoom), [Room1, Room2], wStrPtr
  76.   ELSE                  'Describe something else
  77.     SELECT nDescription
  78.     CASE DSC_NOT_COMMAND
  79.       wStrPtr = UnknownCommand
  80.     ENDSELECT
  81.   ENDIF
  82.  
  83.   DO
  84.     READ wStrPtr, byCharIn
  85.     IF byCharIn <> 0 THEN
  86.       DEBUG byCharIn
  87.     ENDIF
  88.     wStrPtr = wStrPtr + 1
  89.   LOOP UNTIL byCharIn = 0
  90.  
  91.   DEBUG 13, ">"
  92. RETURN
  93.  
  94.  
  95. ReadInput:
  96.   DEBUGIN byKeyIn      'read key in
  97.  
  98.   IF byKeyIn = 8 THEN      'backspace
  99.     sInputBuff(byCurPos) = 0
  100.     IF byCurPos > 0 THEN
  101.       byCurPos = byCurPos - 1
  102.     ELSE
  103.       DEBUG ">"
  104.     ENDIF
  105.     GOTO ReadInput
  106.   ELSEIF byKeyIn = 13 THEN 'return
  107.     RETURN
  108.   ENDIF
  109.  
  110.   'make sure input doesn't overflow
  111.   IF byCurPos < MAX_INPUT THEN
  112.     byCurPos = byCurPos + 1
  113.   ELSE                          'hit end of input buffer!
  114.     DEBUG 8                     'backspace
  115.     FREQOUT Speaker, 125, 3000  'beep
  116.     GOTO ReadInput              'Go back to input
  117.   ENDIF
  118.  
  119.   'Do stuff based on char read
  120.   SELECT byKeyIn
  121.   CASE "a" TO "z"
  122.     sInputBuff(byCurPos) = (byKeyIn - 32) 'convert to upper case
  123.  
  124.   CASE "A" TO "Z"
  125.     sInputBuff(byCurPos) = byKeyIn
  126.  
  127.   CASE ELSE
  128.     DEBUG 8                     'don't accept non-text characters
  129.     FREQOUT Speaker, 125, 3000  'beep
  130.   ENDSELECT
  131.  
  132. GOTO ReadInput 'loop until return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement