Swat the Skeeter!

by Welopez

I'm generally not much interested in computer games, but I do write one on occasion, because they are excellent practice using various commands and techniques. This program will start by drawing the image of a mosquito in memory, so we are not required to include an image file with the code. There is no trapclose in the GOSUB to draw the skeeter, since the user never sees that window and has no place to click. Instead, we force the window to close using code, after the image has been drawn.

We'll open a window for graphics and display the purpose of the game as text, using a FOR/NEXT loop, a TIMER to slow down the display, SELECT CASE sets the TIMER delay during play, based upon the number of attempts made, and DATA statements for the text to be displayed.

      'Swat the Skeeter
      'by Welopez, 11/03/2006

      GOSUB [drawSkeeter]  'Draw the image in memory

      NOMAINWIN
      WindowWidth=600
      WindowHeight=700
      UpperLeftX=INT((DisplayWidth-WindowWidth)/2)
      UpperLeftY=INT((DisplayHeight-WindowHeight)/2)

      GRAPHICBOX #g.gbx, 10, 10, 570, 580
      BUTTON #g.btn1, "Start", [start], LL, 160, 10, 60, 25
      BUTTON #g.btn2, "Close", quit, LL, 400, 10, 60, 25

      OPEN "Pesky Skeeter" FOR WINDOW AS #g
      PRINT #g, "trapclose quit"
      PRINT #g.gbx, "down"
      PRINT #g.gbx, "FILL 180 254 180"

      [how2]
      PRINT #g.gbx, "FONT arial 14"
      PRINT #g.gbx, "PLACE 25 175"
      PRINT #g.gbx, "BACKCOLOR 180 254 180"
      PRINT #g.gbx, "COLOR black"
      FOR k=1 to 10
      READ msg$
      PRINT #g.gbx, msg$
      CALL pause, 750, x, y, hits    'Pause while printing each line of text
      NEXT k

      PRINT #g.gbx, "PLACE 250 400"
      PRINT #g.gbx, "DRAWBMP skeeter"
      PRINT #g.gbx, "FLUSH"
      PRINT #g.gbx, "WHEN leftButtonDown [start]"  'A click anywhere will start
      WAIT

      [start]
      PRINT #g.gbx, "FILL 180 254 180"
      DO
      shots=shots+1
      SELECT CASE shots
      CASE shots>20 : delay=600
      CASE shots>15 : delay=700
      CASE shots>10 : delay=800
      CASE shots>5  : delay=900
      CASE ELSE : delay=1000
      END SELECT

      x=INT(RND(0)*500)+25
      y=INT(RND(0)*500)+25
      PRINT #g.gbx, "PLACE "; x; " "; y
      PRINT #g.gbx, "DRAWBMP skeeter"
      CALL pause, delay, x, y, hits
      PRINT #g.gbx, "COLOR 180 254 180"           'pen color for boxfilled border
      PRINT #g.gbx, "BACKCOLOR 180 254 180"       'color for boxfilled (same as fill)
      PRINT #g.gbx, "PLACE "; x; " "; y           'UL corner of boxfilled
      PRINT #g.gbx, "BOXFILLED "; x+55;" "; y+40  'LR corner to erase skeeter
      IF shots=25 THEN
          NOTICE "Your score..."+CHR$(13)+_
          "You scored "; hits; " for "; shots; "."
          CALL quit handle$
      END IF
      LOOP WHILE shots<25

      WAIT
      SUB quit handle$
          TIMER 0
          UNLOADBMP "skeeter"
          CLOSE #g
          END
      END SUB

      SUB pause delay, x, y, byref hits
          TIMER delay, [cont]
          SCAN
          PRINT #g.gbx, "WHEN leftButtonDown [cont]"
          WAIT
          [cont]
          IF ((MouseX>(x+10)) AND (MouseX<(x+40))) AND _
              ((MouseY>(y+10)) AND (MouseY<(y+40))) THEN
              PLAYWAVE "tada.wav", asynch
              hits=hits+1
          END IF
          TIMER 0
      END SUB

      [drawSkeeter]
      WindowWidth=300
      WindowHeight=300
      OPEN "skeeter.bmp" FOR GRAPHICS_nsb AS #g
      PRINT #g, "FILL 180 254 180"
      PRINT #g, "DOWN"
      PRINT #g, "PLACE 100 150"
      PRINT #g, "COLOR 235 235 235"
      PRINT #g, "ELLIPSEFILLED 25 10"             'left wing
      PRINT #g, "PLACE 125 150"
      PRINT #g, "ELLIPSEFILLED 25 10"             'right wing
      PRINT #g, "BACKCOLOR black"
      PRINT #g, "PLACE 112 145"
      PRINT #g, "ELLIPSEFILLED 10 20"             'thorax
      PRINT #g, "PLACE 112 155"
      PRINT #g, "ELLIPSEFILLED 10 20"             'abdomen
      PRINT #g, "GETBMP skeeter 85 130 55 40"
      CLOSE #g
      RETURN

      DATA "\While camping at the lake, you've just crawled into your tent."
      DATA "\A pesky mosquito has gotten inside your tent!"
      DATA "\Grab your boot (not the shotgun!) and swat the skeeter!"
      DATA "\You have 25 chances."
      DATA "\The skeeter will move faster as the game progresses."
      DATA "\"
      DATA "\Your score will be displayed when the game is over."
      DATA "\Good luck!"
      DATA "\"
      DATA "\Press the SKEETER to begin."

Basically, we have a shooting game. After the image of the skeeter is drawn, it is displayed at random locations on the window. The user has a limited time to squash the skeeter before the image is erased and printed to a new location.

Rather than write a new TIMER routine to display the text, I used the same TIMER which is used for displaying the skeeter. When the user clicks on the skeeter in the instructions, there is no checking for location, a click anywhere in the window will start the game.

The image for our pesky skeeter is 55 by 40, and will be displayed with the upper left corner printed at a random x y location. To allow the user to "hit" a zone near the center of the skeeter, the IF/THEN in the TIMER routine checks for MouseX>x+10 but less than x+40, and MouseY>y+10 and less than y+40. This provides the user with a hot-zone 30 by 30. You can expand these to make the game easier, or tighten them up to increase difficulty.

I used a media file (tada.wav) if the user makes a hit. If the user is not running Win XP, there will be no sound; but neither will there be an error.

A simple NOTICE advises the player of the score, but you could print the score to the GRAPHICBOX, even specifying novice, sharpshooter, or expert, if you choose.

Hey, this is only a demo to illustrate some simple techniques. You can modify it to create your own super-dooper game, maybe hunting that elusive Thanksgiving gobbler or shooting down Santa's sleigh? ROFL