home *** CD-ROM | disk | FTP | other *** search
/ 3D Game Programming for Teens (2nd Edition) / 3DGPFT2E.iso / Source / Chapter14 / shootinggallery.bb < prev    next >
Encoding:
Text File  |  2009-01-21  |  7.1 KB  |  235 lines

  1. ;Shooting Gallery
  2. ;________________
  3.  
  4. ;Key constants
  5. Const ENTER_KEY = 28
  6. Const R_KEY = 19
  7. Const V_KEY = 47
  8. Const P_KEY = 25
  9. Const LEFT_KEY = 203
  10. Const RIGHT_KEY = 205
  11. Const UP_KEY = 200
  12. Const DOWN_KEY = 208
  13. Const SPACE_BAR = 57
  14. Const ESC_KEY = 1
  15.  
  16.  
  17.      welcome()
  18.      Function welcome()
  19.           Graphics 1024,768 
  20.           screen=LoadImage ("welcome.bmp")
  21.           DrawImage screen,0,0
  22.           While Not KeyDown(ESC_KEY)
  23.                If KeyDown(ENTER_KEY) Then 
  24.                     Return
  25.                EndIf 
  26.           Wend
  27.      End Function
  28. ; Loading the sounds
  29.      phaser=LoadSound("phaser.wav")
  30.      explosion=LoadSound("explode.wav")
  31. ;Creating a bookmark
  32.      .start
  33. ;Setting the graphics for the program
  34.      Graphics3D 1024,768
  35.      SetBuffer BackBuffer()
  36. ;Setting the type values
  37.      type_player=1
  38.      type_scenery=2
  39.      type_gallery=3
  40.      type_bullet=4; Creating the camera
  41.      camera=CreateCamera()
  42.      CameraClsColor camera ,0,125,255
  43.      EntityType camera, type_player
  44.      EntityRadius camera, 5
  45. ; Creating a light
  46.      light=CreateLight()
  47. ; Creating a the water plane
  48.      water=CreatePlane() 
  49.      PositionEntity water, 0,-15,0
  50.      watertexture=LoadTexture ("water.jpg")
  51.      EntityTexture water, watertexture
  52.      ScaleTexture watertexture,15,15
  53.      EntityType water, type_scenery
  54.  
  55. ; Loading the heightmap
  56.      terrain=LoadTerrain ( "ground.jpg" )
  57.      ScaleEntity terrain,5,50,5
  58.      PositionEntity terrain,-500,-20,-500
  59.      tex=LoadTexture( "greenery.jpg" )
  60.      ScaleTexture tex, 50,50
  61.      EntityTexture terrain,tex
  62.      EntityType terrain, type_scenery
  63. ;Creating the gallery items
  64.      Dim ducks(5)
  65.           For x = 1 To 5
  66.                ducks(x)= LoadMesh ("duck.3ds")
  67.                PositionEntity ducks(x), x*15,0,90
  68.                EntityColor ducks(x), 255,255,0
  69.                ScaleEntity ducks(x), 0.2,0.2,0.2
  70.                EntityType ducks(x), type_gallery
  71.                EntityRadius ducks(x), 3
  72.           Next
  73.      Dim seahorse(5)
  74.           For x = 1 To 5
  75.                seahorse(x)= LoadMesh ("seahorse.3ds")
  76.                PositionEntity seahorse(x), x*6,10,45
  77.                EntityColor seahorse(x), 255,102,0
  78.                ScaleEntity seahorse(x), 0.03,0.03,0.03
  79.                EntityType seahorse(x), type_gallery
  80.                EntityRadius seahorse(x), 3
  81.           Next
  82.  
  83.      Dim donkey(5)
  84.           For x = 1 To 5
  85.                donkey(x)= LoadMesh ("donkey.3ds")
  86.                PositionEntity donkey(x), x*5,35,85
  87.                EntityColor donkey(x), 204,204,204
  88.                ScaleEntity donkey(x), 0.3,0.3,0.3
  89.                EntityType donkey(x), type_gallery
  90.                EntityRadius donkey(x), 3
  91.           Next
  92.      Dim flamingo(5)
  93.           For y = 1 To 5
  94.                flamingo(y)= LoadMesh ("flamingo.3ds")
  95.                PositionEntity flamingo(y), y*6,8,25
  96.                EntityColor flamingo(y), 102,51,51
  97.                ScaleEntity flamingo(y), 0.003,0.003,0.003
  98.                EntityType flamingo(y), type_gallery
  99.                EntityRadius flamingo(y), 3
  100.           Next
  101.  
  102.      Dim dolphin(5)
  103.           For x = 1 To 5
  104.                dolphin(x)= LoadMesh ("dolphin.3ds")
  105.                PositionEntity dolphin(x), x*6,45,0
  106.                EntityColor dolphin(x), 102,153,255
  107.                ScaleEntity dolphin(x), 0.3,0.3,0.3
  108.                EntityType dolphin(x), type_gallery
  109.                EntityRadius dolphin(x), 3
  110.           Next
  111.  
  112.      Dim snake(5)
  113.           For x = 1 To 5
  114.                snake(x)= LoadMesh ("snake.3ds")
  115.                PositionEntity snake(x), x*16,10,10
  116.                EntityColor snake(x), 153,255,153
  117.                ScaleEntity snake(x), 0.06,0.06,0.06
  118.                EntityType snake(x), type_gallery
  119.                EntityRadius snake(x), 5
  120.           Next
  121.  
  122.      
  123. ;Creating the gun
  124.      gun=CreateCylinder(12)
  125.      ScaleEntity gun,0.2,0.6,0.4     RotateEntity gun,45,0,0
  126.      PositionEntity gun, EntityX(camera),EntityY(camera)-2, EntityZ(camera)+3
  127.      EntityOrder gun, -1
  128.      EntityParent gun,camera
  129.      EntityColor gun, 100,100,100 
  130.  
  131. ;Creating the bullets
  132.      maxbull = 100
  133.      Dim bullet(maxbull)
  134.           For i=0 To maxbull
  135.            bullet(i)=LoadMesh ("bullet.3ds")
  136.                EntityColor bullet(i), 100,100,100
  137.                EntityType bullet(i), type_bullet
  138.           Next
  139. ; Defining the Collisions
  140.      Collisions type_gallery,type_scenery,2,2
  141.      Collisions type_bullet,type_gallery,2,1
  142.      Collisions type_player,type_scenery,2,2
  143.      Collisions type_scenery, type_gallery, 2,2
  144.  
  145.  
  146. ;Pausing the game
  147.      If KeyDown(P_KEY) Then
  148.           Cls
  149.           pause=LoadImage ("pause.bmp")
  150.           DrawImage pause,0,0
  151.           Flip
  152.           WaitMouse()
  153.      EndIf
  154. ;Defining the starting time of the game
  155.      starttime=MilliSecs()
  156.  
  157.  
  158. ; The following code makes our program run
  159.  
  160.     While Not KeyDown(ESC_KEY)
  161.  
  162.         If KeyDown(RIGHT_KEY)=True Then TurnEntity camera,0,-1,0
  163.           If KeyDown(LEFT_KEY)=True Then TurnEntity camera,0,1,0
  164.           If KeyDown(DOWN_KEY)=True Then MoveEntity camera,0,0,-1
  165.           If KeyDown(UP_KEY)=True Then MoveEntity camera,0,0,1
  166.           If KeyDown(V_KEY)=True Then TranslateEntity camera,0,3,0          
  167.         MoveEntity camera, 0,-0.8,0
  168. ; Moving the targets
  169.      For a = 1 To 5
  170.           MoveEntity ducks(a), 0.1,-.02,0
  171.           MoveEntity seahorse(a), 0,-.02,0
  172.           MoveEntity donkey(a), 0,-.02,0
  173.           MoveEntity flamingo(a), 0.1,-0.2,0
  174.           MoveEntity snake(a), -0.2,-0.2,0
  175.           MoveEntity dolphin(a), 0,-0.2,-0.3
  176.      Next
  177.  
  178. ;Firing bullets
  179.     If KeyHit (SPACE_BAR) And reload = 0
  180.           PlaySound phaser 
  181.          PositionEntity bullet(t) ,EntityX(gun,1),EntityY(gun,1),EntityZ(gun,1)
  182.          RotateEntity bullet(t),EntityPitch#(gun,1)-35,EntityYaw#(gun,1),EntityRoll#(gun,1)
  183.          EntityColor bullet(t),0,0,255
  184.     t=t+1
  185.     EndIf 
  186.     For q = 0 To maxbull
  187.          MoveEntity bullet(q), 0,0.8,3
  188.     If CountCollisions (bullet(q))
  189.             crash=CollisionEntity (bullet(q),1)
  190.             HideEntity crash
  191.         score#=score#+1
  192.         PlaySound explosion
  193.       EndIf
  194.     Next
  195.     bulletcount=100-t
  196.         If t=100 Then
  197.          reload=1
  198.         EndIf 
  199.         If KeyDown (R_KEY) = True Then
  200.          t=0
  201.          reload=0
  202.     EndIf
  203.  
  204. ;Changing the position of gallery items
  205.      For q = 0 To maxbull
  206.           If CountCollisions (terrain)
  207.                smash=CollisionEntity (terrain,1)
  208.                PositionEntity smash, 0,Rand(10,100),Rand(-100,100)
  209.           EndIf
  210.     Next 
  211.  
  212. ;Ending the game
  213.      If score# = 35 Or timeleft < 0 Then 
  214.      Cls
  215.      End=LoadImage ("end.bmp")
  216.      DrawImage End,0,0
  217.      Flip
  218.      WaitKey() 
  219.      If KeyHit(ESC_KEY) Then End
  220.      If KeyHit(SPACE_BAR) Then Goto start
  221.      EndIf
  222.  
  223.     UpdateWorld 
  224.  
  225.       RenderWorld
  226.     CurrentTime = MilliSecs() 
  227.     timeleft= 180000 - ((CurrentTime - starttime))
  228.     Text 100,10,"Score: " +score#,True,False
  229.     Text 400,10,"Bullets Remaining: "+bulletcount
  230.     Text 800,10,"Time Remaining: "+TimeLeft /1000
  231.     If reload=1 Then Text GraphicsWidth()/2, GraphicsHeight()/2,"Press R to Reload",1,1
  232.  
  233.      Flip
  234. Wend
  235. End