BBC BASIC for Windows
Programming >> Graphics and Games >> My first space ship with bullet/collision detect
http://bb4w.conforums.com/index.cgi?board=graphics&action=display&num=1470010868

My first space ship with bullet/collision detect
Post by michael on Aug 1st, 2016, 12:21am

I had to imagine how a game like space invaders worked. Well I think I have it figured out. So using this model, technically I could make many bullets that fire in their own directions and kill any object I choose.

Directions.. use mouse to move ship. and press LEFT mouse button to shoot.

CHECK IT OUT.
Code:
      REM       width;height;charwidth,charheight,number of colors,character set
      VDU 23,22,1024;600;8,15,16,1 :REM max width is 1920 and 1440 height
      REM Turn off the text cursor _
      REM OFF

      mstatu$="loaded"
      MOUSE OFF
      VDU 5 : REM Treat text as graphics (transparent background)
                * REFRESH OFF
      REPEAT
        REM 1 right 2 middle 4 left
        MOUSE moux,mouy,but
        PROCcolor("b","0")
        CLG
        PROCcolor("f","2")
        CIRCLE FILL 500,500,30
      PROCpset(200,500,"green")
   PROCmisslecheck
        PROCship(moux,10,"white")
        * REFRESH
  
        WAIT 5
      UNTIL but=2
      END
      DEF PROCmisslecheck
      PRIVATE sh%,sy%
      IF but=4 THEN IF mstatu$="loaded" THEN sh%=moux:sy%=10:mstatu$="enroute"
      IF mstatu$="enroute" THEN
        PROCcolor("f","yellow")
        LINE sh%,sy%,sh%,sy%+10
        sy%=sy%+20
      ENDIF
      IF sy% > 800 THEN mstatu$="loaded"
      IF POINT(sh%+2,sy%)=2 THEN
        SOUND 0,-15,10,1
        mstatu$="loaded"
      ENDIF
      ENDPROC
      DEF PROCship(x%,y%,c$)
      PROCcolor("f",c$)
      LINE x%-10,y%+10,x%+10,y%+10
      LINE x%-10,y%+10,x%,y%+20
      LINE x%+10,y%+10,x%,y%+20
      MOVE 0,0
      ENDPROC
      REM c$ can be colors like blue or 1 or a R,G,B color
      DEF PROCpset(x%,y%,c$)
      LOCAL h%
      PROCcolor("f",c$)
      FOR h%=0 TO 20
        LINE x%+h%,y%,x%+h%,y%+20
      NEXT
      ENDPROC
      DEF PROCsbox(x%,y%,w%,h%)
      LOCAL ry%,sx%,sy%
      sx%=x%:sy%=y%
      IF x%>w% THEN x%=w%:w%=sx%
      IF y%>h% THEN y%=h%:h%=sy%
      ry%=y%
      PROCcolor("f","255,255,255")
      REPEAT
        LINE x%,y%,w%,y%
        y%=y%+1
      UNTIL y%=h%
      y%=ry%
      PROCcolor("f","000,000,000")
      LINE x%+2,y%+2,w%-2,y%+2
      LINE w%-2,y%+2,w%-2,h%-4
      LINE w%-2,h%-4,x%+2,h%-4
      LINE x%+2,h%-4,x%+2,y%+2
      ENDPROC
      REM color "F"or"B", "color name number or R G B"
      REM this was created to manage special colors and to hold onto the original first 2 pallets (as I use them all the time)
      DEF PROCcolor(fb$,rgb$)
      IF rgb$="0" OR rgb$="black" THEN rgb$="000,000,000"
      IF rgb$="1" OR rgb$="red" THEN rgb$="200,000,000"
      IF rgb$="2" OR rgb$="green" THEN rgb$="000,200,000"
      IF rgb$="3" OR rgb$="yellow" THEN rgb$="200,200,000"
      IF rgb$="4" OR rgb$="blue" THEN rgb$="000,000,200"
      IF rgb$="5" OR rgb$="magneta" OR rgb$="purple" THEN rgb$="200,000,200"
      IF rgb$="6" OR rgb$="cyan" THEN rgb$="000,200,200"
      IF rgb$="7" OR rgb$="white" THEN rgb$="200,200,200"
      IF rgb$="8" OR rgb$="grey" THEN rgb$="056,056,056"
      IF rgb$="9" OR rgb$="light red" THEN rgb$="248,056,056"
      IF rgb$="10" OR rgb$="light green" THEN rgb$="056,248,056"
      IF rgb$="11" OR rgb$="light yellow" THEN rgb$="248,248,056"
      IF rgb$="12" OR rgb$="light blue" THEN rgb$="056,056,248"
      IF rgb$="13" OR rgb$="light magnetta" OR rgb$="light purple" THEN rgb$="248,056,248"
      IF rgb$="14" OR rgb$="light cyan" THEN rgb$="056,248,248"
      IF rgb$="15" OR rgb$="light white" THEN rgb$="248,248,248"
      PRIVATE assemble$,br%,bg%,bb%
      assemble$=rgb$
      br%=VAL(MID$(assemble$,1,3)):bg%=VAL(MID$(assemble$,5,3)):bb%=VAL(MID$(assemble$,9,3))
      IF fb$="f" OR fb$="F" THEN COLOUR 0,br%,bg%,bb% : GCOL 0
      IF fb$="b" OR fb$="B" THEN COLOUR 1,br%,bg%,bb% : GCOL 128+1
      ENDPROC
      REM X,Y,text color,message,r,g,b
      REM ************************************************************************
      DEF PROCpr(X,Y,msg$,r,g,b)
      LOCAL initialx%,fi%,reduction%,tx,ty
      initialx%=LEN(msg$)
      COLOUR 0,r,g,b
      GCOL 0
      LET tx= X+initialx%+25
      LET ty= Y:reduction%=0
      reduction%=initialx%/2
      reduction%=reduction%*6
      IF initialx%<20 THEN reduction%=reduction%/2
      initialx%=initialx%*22-reduction%
      FOR fi%=12 TO 48
        LINE X-3,Y+20-fi%,X+initialx%+8,Y+20-fi%
      NEXT
      COLOUR 0,0,0,0
      GCOL 0
      MOVE tx,ty
      PRINT msg$
      MOVE 0,0 REM hide that thing
      ENDPROC

 

Re: My first space ship with bullet/collision dete
Post by DDRM on Aug 1st, 2016, 6:09pm

Hi Michael,

You could certainly do it like that. If you want to be able to have more than one missile in flight at once, you could set up an array to hold the position and course of each.

Your method of working out when you hit an object is fine, though it gets more complicated if you wanted to have targets of different colours. You could choose to use even-numbered colours for non targets (0 for space/sky, 2 for the launcher, 4 for clouds, 6 for background scenery etc), and then use colours 1,3,5 etc for potential targets - then you could use POINT(X,Y) AND 1 to test whether it is a target. You would still have to have a way of working out what target had been hit, and removing it.

An alternative would be to store the positions (and size - you need the bounding box) of the targets, and then check the missile position against them. In practice, for modest numbers of targets, this is likely to be quick enough, and be easier in the long run, since you immediately know what the target is, can allocate an appropriate score, and remove it.

In practice, a lot of the early space invader games used "text art" - the aliens were redefined text characters (see VDU23), and all "graphic" objects were plotted as characters. This means you can store everything in a text (byte or string) array. Having one string for each line of the screen is quite a nice way of doing it. Then you just check which character position your missile is in, and if it has an alien in, you zap it by changing it to a space! To make the aliens move down the screen you just print more blank lines before printing them.

Your use of the mouse makes it easy to move the missile launcher back and forth very quickly - often one of the challenges of the game was to get it where you needed it in time. You could consider using the character keys to move it left and right, or you could limit the rate at which it could move (so it would move a bit as if it were on elastic) - but that tends to feel rather counterintuitive with a mouse.

Best wishes,

D