home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Game Programming for Teens / VBGPFT.cdr / sources / chapter19 / CelticCrusader4 / Game.bas < prev    next >
Encoding:
BASIC Source File  |  2004-10-27  |  11.5 KB  |  404 lines

  1. Attribute VB_Name = "Game"
  2. '---------------------------------------------------------------
  3. ' Visual Basic Game Programming for Teens
  4. ' Chapter 12 - WalkAbout program
  5. '
  6. ' Requires the following files:
  7. '   Direct3D.bas, DirectInput.bas, Globals.bas, TileScroller.bas,
  8. '   Sprite.bas, and an empty Form1.
  9. '---------------------------------------------------------------
  10.  
  11. Option Explicit
  12. Option Base 0
  13.  
  14. Public wood As Direct3DSurface8
  15.  
  16. Public badtiles() As Integer
  17.  
  18. Public Const SCENERY_IMAGES As Long = 4
  19. Public sceneImage As Direct3DTexture8
  20.  
  21. Public Const SCENERY_SPRITES As Long = 100
  22. Public sceneSprites(SCENERY_SPRITES) As TSPRITE
  23.  
  24. Public heroSprWalk As TSPRITE
  25. Public heroImgWalk As Direct3DTexture8
  26. Public heroSprAttack As TSPRITE
  27. Public heroImgAttack As Direct3DTexture8
  28. Public heroSprRun As TSPRITE
  29. Public heroImgRun As Direct3DTexture8
  30.  
  31. Dim frm As New Form1
  32.  
  33.  
  34. Public Function Random(ByVal lMax As Long)
  35.     Random = Int(Rnd * lMax)
  36. End Function
  37.  
  38.  
  39. Public Sub Main()
  40.     
  41.     'set random number seed
  42.     Randomize GetTickCount
  43.     
  44.     'set up the main form
  45.     frm.Caption = "Celtic Crusader"
  46.     frm.AutoRedraw = False
  47.     frm.BorderStyle = 1
  48.     frm.ClipControls = False
  49.     frm.ScaleMode = 3
  50.     frm.width = Screen.TwipsPerPixelX * (SCREENWIDTH + 12)
  51.     frm.height = Screen.TwipsPerPixelY * (SCREENHEIGHT + 30)
  52.     frm.Show
  53.     
  54.     'initialize Direct3D
  55.     InitDirect3D frm.hwnd
  56.     
  57.     'initialize DirectInput
  58.     InitDirectInput
  59.     InitKeyboard frm.hwnd
  60.     
  61.     'get reference to the back buffer
  62.     Set backbuffer = d3ddev.GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO)
  63.     
  64.     'load the scenery sprites
  65.     LoadScenery
  66.     
  67.     'create the font
  68.     Set fontImg = LoadTexture(d3ddev, App.Path & "\font.bmp")
  69.     InitSprite d3ddev, fontSpr
  70.     fontSpr.FramesPerRow = 20
  71.     fontSpr.width = 8
  72.     fontSpr.height = 12
  73.     fontSpr.ScaleFactor = 1
  74.     
  75.     'clear the screen to black
  76.     d3ddev.Clear 0, ByVal 0, D3DCLEAR_TARGET, C_BLACK, 1, 0
  77.  
  78.     'display a startup message
  79.     d3ddev.BeginScene
  80.     PrintText fontImg, fontSpr, 10, 10, C_GREEN, "Celtic Crusader"
  81.     PrintText fontImg, fontSpr, 10, 40, C_GREEN, "PLEASE WAIT, LOADING..."
  82.     d3ddev.EndScene
  83.     d3ddev.Present ByVal 0, ByVal 0, 0, ByVal 0
  84.     
  85.     'load the bitmap file
  86.     Set tiles = LoadSurface(App.Path & "\ireland.bmp", 1024, 576)
  87.     
  88.     'fill the badtiles array
  89.     BuildTileCollisionList
  90.     
  91.     'load the bottom toolbar image
  92.     Set wood = LoadSurface(App.Path & "\bottom.bmp", 644, 32)
  93.     
  94.     'load the map data from the Mappy export file
  95.     LoadBinaryMap App.Path & "\ireland.mar", MAPWIDTH, MAPHEIGHT
  96.     
  97.     'create the small scroll buffer surface
  98.     Set scrollbuffer = d3ddev.CreateImageSurface( _
  99.         SCROLLBUFFERWIDTH, _
  100.         SCROLLBUFFERHEIGHT, _
  101.         dispmode.Format)
  102.         
  103.     'initialize NPCs
  104.     InitCharacters
  105.     
  106.     'initialize the hero
  107.     InitHero
  108.     
  109.     'start player in the city of Dubh Linn
  110.     ScrollX = PLAYERSTARTX * TILEWIDTH
  111.     ScrollY = PLAYERSTARTY * TILEHEIGHT
  112.     
  113.     'this helps to keep a steady framerate
  114.     Dim start As Long
  115.     start = GetTickCount()
  116.  
  117.     'main loop
  118.     Do While (True)
  119.         'erase the bottom toolbar
  120.         DrawSurface wood, 0, 0, 639, 30, backbuffer, 0, 449
  121.     
  122.         'poll DirectInput for keyboard input
  123.         Check_Keyboard
  124.  
  125.         'call various update routines
  126.         UpdateScrollPosition
  127.         CheckTileCollisions
  128.         CheckSceneryCollisions
  129.         DrawTiles
  130.         DrawScrollWindow
  131.         Scroll 0, 0
  132.         MoveNPCs
  133.         CheckNPCCollisions
  134.     
  135.         'set the screen refresh to about 50 fps
  136.         If GetTickCount - start > 20 Then
  137.         
  138.             'start rendering
  139.             d3ddev.BeginScene
  140.             
  141.             'call various drawing routines
  142.             DrawScenery
  143.             DrawNPCs
  144.             UpdateHero
  145.             ShowPlayerInfo
  146.  
  147.             'stop rendering
  148.             d3ddev.EndScene
  149.         
  150.             d3ddev.Present ByVal 0, ByVal 0, 0, ByVal 0
  151.             start = GetTickCount
  152.             DoEvents
  153.         End If
  154.         
  155.     Loop
  156. End Sub
  157.  
  158. Public Sub CheckSceneryCollisions()
  159.     Dim n As Long
  160.     Dim p As point
  161.     Static spr As TSPRITE
  162.     
  163.     'copy hero sprite location and size into a temp variable
  164.     spr.x = ScrollX + SCREENWIDTH / 2 - heroSprWalk.width / 2
  165.     spr.y = ScrollY + SCREENHEIGHT / 2 - heroSprWalk.height / 2
  166.     spr.width = heroSprWalk.width
  167.     spr.height = heroSprWalk.height
  168.     
  169.     'look at all the scenery
  170.     For n = 0 To SCENERY_SPRITES - 1
  171.     
  172.         'let's deal with just one at a time
  173.         With sceneSprites(n)
  174.             
  175.             'is the global sprite is within the scrolling viewport?
  176.             If .x > ScrollX - 1 And .x + .width < ScrollX + SCREENWIDTH + 1 And _
  177.                .y > ScrollY - 1 And .y + .height < ScrollY + SCREENHEIGHT + 1 Then
  178.                
  179.                 'next test: collision with the player?
  180.                 If Collision(spr, sceneSprites(n)) Then
  181.                     StopHero
  182.                     Exit For
  183.                 End If
  184.                 
  185.             End If
  186.         End With
  187.     Next n
  188.  
  189. End Sub
  190.  
  191. Public Sub DrawScenery()
  192.     Dim n As Long
  193.     Dim p As point
  194.     
  195.     For n = 0 To SCENERY_SPRITES - 1
  196.     
  197.         With sceneSprites(n)
  198.             
  199.             'is the global sprite is within the scrolling viewport?
  200.             If .x > ScrollX - 1 And .x + .width < ScrollX + SCREENWIDTH + 1 And _
  201.                .y > ScrollY - 1 And .y + .height < ScrollY + SCREENHEIGHT + 1 Then
  202.             
  203.                 'save global x,y position
  204.                 p.x = .x
  205.                 p.y = .y
  206.                 
  207.                 'set sprite to viewport position
  208.                 .x = .x - ScrollX
  209.                 .y = .y - ScrollY
  210.                 
  211.                 'draw the sprite
  212.                 DrawSprite sceneImage, sceneSprites(n), C_WHITE
  213.                 
  214.                 'restore the global position
  215.                 .x = p.x
  216.                 .y = p.y
  217.             End If
  218.         End With
  219.     Next n
  220. End Sub
  221.  
  222. Public Sub LoadScenery()
  223.     Dim n As Long
  224.     Dim w As Long
  225.     Dim h As Long
  226.     
  227.     'load scenery image (containing all the trees)
  228.     Set sceneImage = LoadTexture(d3ddev, App.Path & "\trees.bmp")
  229.     
  230.     'initialize scenery sprites (trees)
  231.     'assumes all are 128x128...change as needed
  232.     For n = 0 To SCENERY_SPRITES - 1
  233.         InitSprite d3ddev, sceneSprites(n)
  234.         sceneSprites(n).width = 128
  235.         sceneSprites(n).height = 128
  236.         sceneSprites(n).FramesPerRow = 4
  237.         sceneSprites(n).CurrentFrame = Random(4)
  238.     Next n
  239.     
  240.     'randomly place the scenery sprites
  241.     For n = 0 To SCENERY_SPRITES - 1
  242.         'normally you will spread scenery items all over the map
  243.         'but i'm focusing in on just around the starting position
  244.         w = PLAYERSTARTX * TILEWIDTH
  245.         h = PLAYERSTARTY * TILEHEIGHT
  246.         sceneSprites(n).x = w + Random(4000) - 2000
  247.         sceneSprites(n).y = h + Random(4000) - 2000
  248.     Next n
  249. End Sub
  250.  
  251. Public Sub BuildTileCollisionList()
  252.     ReDim badtiles(5)
  253.     badtiles(0) = 2
  254.     badtiles(1) = 34
  255.     badtiles(2) = 44
  256.     badtiles(3) = 54
  257.     badtiles(4) = 79
  258.    
  259. End Sub
  260.  
  261. Public Function IsBadTile(ByVal tilenum As Long) As Boolean
  262.     Dim n As Long
  263.     
  264.     For n = 0 To 4
  265.         If badtiles(n) - 1 = tilenum Then
  266.             IsBadTile = True
  267.             Exit Function
  268.         End If
  269.     Next n
  270.     
  271.     IsBadTile = False
  272.  
  273. End Function
  274.  
  275. Public Function TileAt(ByVal x As Long, ByVal y As Long) As Long
  276.     Dim tile As point
  277.     tile.x = x \ TILEWIDTH
  278.     tile.y = y \ TILEHEIGHT
  279.     TileAt = mapdata(tile.y * MAPWIDTH + tile.x)
  280. End Function
  281.  
  282. Public Function CurrentTile() As Long
  283.     CurrentTile = TileAt(PlayerPos.x, PlayerPos.y)
  284. End Function
  285.  
  286. Public Sub ShowPlayerInfo()
  287.     Dim tile As point
  288.     
  289.     PrintText fontImg, fontSpr, 5, 452, C_WHITE, Player.name
  290.     PrintText fontImg, fontSpr, 110, 452, C_WHITE, "LVL:" & Player.level
  291.     PrintText fontImg, fontSpr, 160, 452, C_WHITE, "EXP:" & Player.experience
  292.     PrintText fontImg, fontSpr, 5, 466, C_WHITE, "STR:" & Player.strength
  293.     PrintText fontImg, fontSpr, 60, 466, C_WHITE, "DEX:" & Player.dexterity
  294.     PrintText fontImg, fontSpr, 110, 466, C_WHITE, "INT:" & Player.intellect
  295.     PrintText fontImg, fontSpr, 160, 466, C_WHITE, "CHA:" & Player.charisma
  296.     PrintText fontImg, fontSpr, 210, 466, C_WHITE, "STA:" & Player.stamina
  297.     
  298.     tile.x = PlayerPos.x \ TILEWIDTH
  299.     tile.y = PlayerPos.y \ TILEHEIGHT
  300.     PrintText fontImg, fontSpr, 5, 5, C_WHITE, "Scroll=(" & PlayerPos.x & "," & PlayerPos.y & ") "
  301.     PrintText fontImg, fontSpr, 5, 18, C_WHITE, "Tile(" & tile.x & "," & tile.y & ")=" & CurrentTile()
  302. End Sub
  303.  
  304. 'modified in chapter 18
  305. 'This is called from DirectInput.bas on keypress events
  306. Public Sub KeyPressed(ByVal key As Long)
  307.     
  308.     Select Case key
  309.     
  310.         Case KEY_UP, KEY_NUMPAD8
  311.             heroSprWalk.AnimSeq = 0
  312.             heroSprWalk.Animating = True
  313.             Scroll 0, -WALKSPEED
  314.             PlayerData.state = HERO_WALKING
  315.             
  316.         Case KEY_NUMPAD9
  317.             heroSprWalk.AnimSeq = 1
  318.             heroSprWalk.Animating = True
  319.             Scroll WALKSPEED, -WALKSPEED
  320.             PlayerData.state = HERO_WALKING
  321.         
  322.         Case KEY_RIGHT, KEY_NUMPAD6
  323.             heroSprWalk.AnimSeq = 2
  324.             heroSprWalk.Animating = True
  325.             Scroll WALKSPEED, 0
  326.             PlayerData.state = HERO_WALKING
  327.             
  328.         Case KEY_NUMPAD3
  329.             heroSprWalk.AnimSeq = 3
  330.             heroSprWalk.Animating = True
  331.             Scroll WALKSPEED, WALKSPEED
  332.             PlayerData.state = HERO_WALKING
  333.         
  334.         Case KEY_DOWN, KEY_NUMPAD2
  335.             heroSprWalk.AnimSeq = 4
  336.             heroSprWalk.Animating = True
  337.             Scroll 0, WALKSPEED
  338.             PlayerData.state = HERO_WALKING
  339.             
  340.         Case KEY_NUMPAD1
  341.             heroSprWalk.AnimSeq = 5
  342.             heroSprWalk.Animating = True
  343.             Scroll -WALKSPEED, WALKSPEED
  344.             PlayerData.state = HERO_WALKING
  345.         
  346.         Case KEY_LEFT, KEY_NUMPAD4
  347.             heroSprWalk.AnimSeq = 6
  348.             heroSprWalk.Animating = True
  349.             Scroll -WALKSPEED, 0
  350.             PlayerData.state = HERO_WALKING
  351.             
  352.         Case KEY_NUMPAD7
  353.             heroSprWalk.AnimSeq = 7
  354.             heroSprWalk.Animating = True
  355.             Scroll -WALKSPEED, -WALKSPEED
  356.             PlayerData.state = HERO_WALKING
  357.             
  358.         Case KEY_LCTRL
  359.             'point attack anim in same direction as walk
  360.             heroSprAttack.AnimSeq = heroSprWalk.AnimSeq
  361.             heroSprAttack.Animating = True
  362.             PlayerData.state = HERO_ATTACKING
  363.         
  364.         Case KEY_ESC
  365.             Shutdown
  366.     
  367.     End Select
  368.     
  369.     'uncomment this when you want to find new key codes
  370.     Debug.Print "Key = " & key
  371.     
  372. End Sub
  373.  
  374.  
  375. Public Sub Scroll(ByVal horiz As Long, ByVal vert As Long)
  376.     SpeedX = horiz
  377.     SpeedY = vert
  378. End Sub
  379.  
  380. Public Sub Shutdown()
  381.     Dim n As Long
  382.     
  383.     Set wood = Nothing
  384.     Set heroImgWalk = Nothing
  385.     Set heroImgAttack = Nothing
  386.     Set heroImgRun = Nothing
  387.     
  388.     For n = 0 To NUMCHARS - 1
  389.         Set charWalk(n) = Nothing
  390.     Next n
  391.     
  392.     Set scrollbuffer = Nothing
  393.     Set tiles = Nothing
  394.     
  395.     Set d3ddev = Nothing
  396.     Set d3d = Nothing
  397.     Set d3dx = Nothing
  398.     Set dx = Nothing
  399.     
  400.     End
  401. End Sub
  402.  
  403.  
  404.