Tutorial 021 Click a Box!


The following program shows you how to make something happen when you right-click on a particular area of the screen.

"Click that Box!" game

      REM: Click that Box!
      REM: by Richard Weston
      REM: 24/04/2003
      MODE8:OFF
      COLOUR9
      FOR i=1 TO 5
        bx=20+RND(1000)
        by=20+RND(800)
        boxw=30+RND(70)
        boxh=30+RND(70)
        :
        PROCbox(bx,by,boxw,boxh)
      NEXT i
      :
      message$ = "Click on the new boxes which appear"
      caption$ = "How to play"
      SYS "MessageBox", @hwnd%, message$, caption$, 64
      :
      score=0
      n=0
      REPEAT:n+=1
        bx=20+RND(1000)
        by=20+RND(800)
        boxw=30+RND(70)
        boxh=30+RND(70)
        :
        PROCbox(bx,by,boxw,boxh)
        :
        boxclicked=FALSE
        clicked=FALSE
        :
        REPEAT
          PROCmouse
        UNTIL boxclicked OR clicked
        IF boxclicked THEN
          PRINTTAB(1,1)"Score = "STR$(score);" / ";n
          delay=INKEY(50)
          PRINTTAB(1,1)SPC(24)
        ELSE
          IF clicked THEN
            PRINTTAB(1,1)"Its not here"
            delay=INKEY(50)
            PRINTTAB(1,1)SPC(24)
          ENDIF
        ENDIF
      UNTIL FALSE
      END
      :
      DEF PROCbox(x,y,a,b)
      RECTANGLE x,y,a,b
      ENDPROC
      :
      DEF PROCmouse
      MOUSE x,y,btn
      IF btn=4 THEN
        clicked=TRUE
        IF x>bx THEN
          IF x<bx+boxw THEN
            IF y>by THEN
              IF y<by+boxh THEN
                boxclicked=TRUE
                score=score+1
              ENDIF
            ENDIF
          ENDIF
        ENDIF
      ENDIF
      ENDPROC


Annotated listing :    

      REM: Click that Box!
      REM: by Richard Weston
      REM: 24/04/2003
      MODE8:OFF
      COLOUR9
      FOR i=1 TO 5 *** Puts a few random boxes on the screen as distractors, otherwise its too easy! ***
        bx=20+RND(1000)
        by=20+RND(800)
        boxw=30+RND(70)
        boxh=30+RND(70)
        :
        PROCbox(bx,by,boxw,boxh)
      NEXT i
      :
      message$ = "Click on the new boxes which appear" *** Instructions ***
      caption$ = "How to play"
      SYS "MessageBox", @hwnd%, message$, caption$, 64
      :
      score=0
      n=0
      REPEAT: n+=1 *** Start of  main loop ***
        bx=20+RND(1000)
        by=20+RND(800)
        boxw=30+RND(70)
        boxh=30+RND(70)
        :
        PROCbox(bx,by,boxw,boxh)  *** The new box to look out for ***
        :
        boxclicked=FALSE
        clicked=FALSE
        :
        REPEAT
          PROCmouse *** keeps looking at the state of the mouse....... ***
        UNTIL boxclicked OR clicked
        IF boxclicked THEN *** Good News - you got the right box! ***
          PRINTTAB(1,1)"Score = "STR$(score);" / ";n
          delay=INKEY(50) *** needed to prevent the program running amuck - try REMing it out!
          PRINTTAB(1,1)SPC(24) *** wipes the score away ***
        ELSE *** you missed the new box..... ***
          IF clicked THEN *** Give them the bad news! ***
            PRINTTAB(1,1)"Its not here"
            delay=INKEY(50)
            PRINTTAB(1,1)SPC(24)
          ENDIF
        ENDIF
      UNTIL FALSE
      END
      :
      DEF PROCbox(x,y,a,b) *** hardly worth a procedure this, but ....see next tutorial ***
      RECTANGLE x,y,a,b
      ENDPROC
      :
      DEF PROCmouse    *** This is the clever bit which sees if you are clicking within the new box ***
      MOUSE x,y,btn
      IF btn=4 THEN    *** detects the left mouse button has been clicked ***
        clicked=TRUE
        IF x>bx THEN
          IF x<bx+boxw THEN
            IF y>by THEN
              IF y<by+boxh THEN
                boxclicked=TRUE *** all these IFs have to be true to have clicked within the box! ***
                score=score+1 *** "Give 'em the money, Mabel!" (Dates me) ***
              ENDIF
            ENDIF
          ENDIF
        ENDIF
      ENDIF
      ENDPROC

     
Next Tutorial

Richard Weston's Homepage