Tutorial 022 : Letter Boxes


This program shows you how to print on the screen at a click of your mouse. (This technique will be used in tutorial 023, which is an all-mouse Hangman program).


      REM: Alphaboxes
      REM: by Richard Weston
      REM: 24th April 2003
      MODE8:OFF
      VDU5: REM print at graphics cursor
      DIM bx(26)
      by=500
      bw=30
      bh=30
      FOR i=1 TO 26
        bx(i)=100+(35*i)
        PROCbox(bx(i),by,bw,bh)
        PLOT0,8,32
        PRINTCHR$(96+i)
      NEXT i
      :
      message$ = "Click the letters (slowly) or outside for a SPACE"
      caption$ = "What to do"
      SYS "MessageBox", @hwnd%, message$, caption$, 64
      :
      VDU4
      PRINT
      :
      REPEAT
        clicked=FALSE
        boxclicked=FALSE
        :
        REPEAT
          PROCmouse
        UNTIL boxclicked
      UNTIL FALSE
      :
      END
      :
      DEF PROCbox(x,y,a,b)
      RECTANGLE x,y,a,b
      ENDPROC
      :
      DEF PROCmouse
      MOUSE x,y,btn
      FOR i=1 TO 26
        IF btn=4 THEN
          clicked=TRUE
          IF x>bx(i) THEN
            IF x<bx(i)+bw THEN
              IF y>by THEN
                IF y<by+bh THEN
                  boxclicked=TRUE
                  PROCprintletter
                ENDIF
              ENDIF
            ENDIF
          ENDIF
        ENDIF
      NEXT i
      IF clicked=TRUE AND boxclicked=FALSE THEN
        PRINT" ";
        delay=INKEY(100)
        clicked=FALSE
      ENDIF
      ENDPROC
      :
      DEF PROCprintletter
      PRINT CHR$(96+i);
      delay=INKEY(50)
      ENDPROC 
   

Annotated Listing

      REM: Alphaboxes
      REM: by Richard Weston
      REM: 24th April 2003
      MODE8:OFF
      VDU5: REM print at graphics cursor
      DIM bx(26) *** x position of each letter box's left edge ***
      by=500
      bw=30 *** box width ***
      bh=30 *** box height ***
      FOR i=1 TO 26
        bx(i)=100+(35*i) *** spaces the boxes appropriately ***
        PROCbox(bx(i),by,bw,bh)
        PLOT0,8,32 *** MOVES the graphics cursor relative to the last point to print inside the box ***
        PRINTCHR$(96+i) *** the i-th letter of the alphabet ***
      NEXT i
      :
      message$ = "Click the letters (slowly) or outside for a SPACE" *** slowly or double letters may not appear! ***
      caption$ = "What to do"
      SYS "MessageBox", @hwnd%, message$, caption$, 64 *** parameter 64 signifies an information symbol ***
      :
      VDU4 *** separates graphics and text cursors again (to the default)***
      PRINT *** neater to start printing one line down ***
      :
      REPEAT
        clicked=FALSE
        boxclicked=FALSE
        :
        REPEAT *** keeps scanning what the mouse is up to ***
          PROCmouse
        UNTIL boxclicked
      UNTIL FALSE
      :
      END
      :
      DEF PROCbox(x,y,a,b)
      RECTANGLE x,y,a,b
      ENDPROC
      :
      DEF PROCmouse
      MOUSE x,y,btn
      FOR i=1 TO 26 ***scans all the boxes until you click on one ***
        IF btn=4 THEN
          clicked=TRUE *** left mouse button has been detected ***
          IF x>bx(i) THEN *** detects when one of the  letter boxes has been clicked ***
            IF x<bx(i)+bw THEN
              IF y>by THEN
                IF y<by+bh THEN
                  boxclicked=TRUE *** jackpot! ***
                  PROCprintletter
                ENDIF
              ENDIF
            ENDIF
          ENDIF
        ENDIF
      NEXT i
      IF clicked=TRUE AND boxclicked=FALSE THEN
        PRINT" ";
        delay=INKEY(100) *** needed to avoid repetitions ***
        clicked=FALSE
      ENDIF
      ENDPROC
      :
      DEF PROCprintletter
      PRINT CHR$(96+i); *** prints the letter whose box you clicked ***
      delay=INKEY(50) *** needed to avoid repetitions ***
      ENDPROC


Next Tutorial

Richard Weston's Homepage