More Code
SHOOTIN'
GALLERY
BASIC COLLISION BETWEEN TWO MOVING OBJECTS: BULLET AND TARGET

`Shootin' Gallery
`Author: Hop A Long
`June 13, 2004

`************************************************
`  Get Ready
`************************************************
   Autocam off
   Sync On
   Sync Rate 40
   Hide Mouse

`************************************************
`  Make a "Target" Object
`************************************************
   Make Object Sphere 1,20
   Position Object 1,105,-20,100
   Color Object 1, RGB(250,0,0)

`************************************************
`  Make Gun
`************************************************
   Make object cylinder 2,2
   Color Object 2, RGB(250,250,0)
   XRotate Object 2,90
`  Reset the object's axis to zero in it's present location
   Fix object pivot 2
   Scale object 2,100,100,500
   position object 2,0,39,10

`************************************************
`  Make bullet
`************************************************
   Make Object Sphere 3,2
   Color Object 3, RGB(0,250,0)
   Hide Object 3

`********************************************************************
`  START MAIN  LOOP
`********************************************************************
   Do

      `******************************************
      `  Tex Output
      `******************************************
         Center Text 320,20, "SHOOTIN' GALLERY"
         Center Text 320,50,"Mouse to aim and shoot"
         Center Text 320,70,"Space Bar to begin"

      `******************************************
      `  BULLETS
      `******************************************
      `  Left Click returns 1, Right Click returns 2
         If Mouseclick()=1 and BulletLife = 0
         `  Place the bullet in the gun
            Position object 3, 0,39,10
         `  The bullet faces the same direction as the gun,
            Set object to object orientation 3,2
            BulletLife =25
            Record_Score = 1
            Show object 3
         Endif

         If BulletLife > 0
            Dec BulletLife
            Move object 3,10
            If BulletLife = 0 then Hide object 3
         Endif

      `******************************************
      `  MOVEMENT CODE FOR TARGET
      `******************************************
      `  Get the Target's positions
         posx# = Object position x(1)
         posy# = Object position y(1)

         If Spacekey()=1
            `  Enter the initial height increment
            dy#=3.0
            `  Start varying the height values
            Begin=1
         Endif

         If Begin
            ` The Target will move left across the screen
            Dec posx#
            `  Begin again on the right
            If posx#<-100 then posx#=100
            `  Vary the amount of the steps
            dy#=dy#-0.1
            `  If the Target comes down, make it bounce up again
            If dy#<-3.0 then dy#=3.0
            `  Add the steps to the height values
            posy#=posy#+dy#
         Endif

      `  Use the three location values to position the Target
         position object 1,posx#,posy#,100

      `******************************************
      `  GUN MOVEMENT
      `******************************************

      `  Store the Gun angle y in cy# (change in y)
         cy# = Object Angle Y(2)
         cx# = Object Angle X(2)

      `  Rotate the Gun's direction but keep within bounds
         y_range# = wrapvalue(cy#+(mousemovex()*0.5))
         x_range# = wrapvalue(cx#+(mousemovey()*0.5))

      `  Declare test variables
         y_temp# = y_range#
         x_temp# = x_range#

      `  Allow for a continual range from left to right
         If y_temp# > 0.0 and y_temp# < 180.0
            y_temp# = y_temp# + 360.0
         Endif
         If x_scale# > 0.0 and x_scale# < 180.0
            x_scale# = x_scale# + 360.0
         Endif

      `  Set the left bound and an exception for start conditions
         If y_temp# < 315.0 and y_temp# <> 0.0 then y_range# = 315.0
         If x_scale# < 315.0 and x_scale# <> 0.0 then x_range# = 315.0

      `  Set the right bound
         If y_temp# > 404.0 then y_range# = 45.0
         If x_scale# > 404.0 then x_range# = 45.0

      `  Rotate the Gun
         Yrotate Object 2, y_range#
         Xrotate Object 2, x_range#

      `******************************************
      `  COLLISION DETECTION CODE
      `******************************************
      `  Detect collision with an Object and
         col_return = Object Collision(1,0)

      `  There is a hit, announce it and end the bullet
         If col_return<>0
            BulletLife = 0
            Hide object 3
            While Record_Score = 1
               Inc Hit
               Record_Score = 2
            Endwhile
         Endif

      `  The bullet is almost spent, consider it a miss
         If BulletLife = 1
            Inc Miss
         Endif

         Text 10,150,"MISSED :  "+str$(Miss)
         Text 500,150,"HIT :  "+str$(Hit)

      `******************************************
      ` CAMERA ROUTINES
      `******************************************
      `  Place the camera
         Position Camera 0,50,0
      `  Point the camera at the player object
         Point camera 0,25,100
      `  Refresh Screen
         Sync

   Loop
`**************************************************************************************************
`  END MAIN LOOP
`**************************************************************************************************