home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Game Programming for Teens / VBGPFT.cdr / sources / chapter18 / CelticCrusader3 / Game.bas < prev    next >
Encoding:
BASIC Source File  |  2004-10-25  |  7.7 KB  |  299 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. Dim wood As Direct3DSurface8
  15.  
  16. Dim badtiles() As Integer
  17.  
  18. Public heroSprWalk As TSPRITE
  19. Public heroImgWalk As Direct3DTexture8
  20. Public heroSprAttack As TSPRITE
  21. Public heroImgAttack As Direct3DTexture8
  22. Public heroSprRun As TSPRITE
  23. Public heroImgRun As Direct3DTexture8
  24.  
  25. Dim SuperHero As Boolean
  26.  
  27. Dim frm As New Form1
  28.  
  29.  
  30. Public Function Random(ByVal lMax As Long)
  31.     Random = Int(Rnd * lMax)
  32. End Function
  33.  
  34.  
  35. Public Sub Main()
  36.     
  37.     'set random number seed
  38.     Randomize GetTickCount
  39.     
  40.     'set up the main form
  41.     frm.Caption = "Celtic Crusader"
  42.     frm.AutoRedraw = False
  43.     frm.BorderStyle = 1
  44.     frm.ClipControls = False
  45.     frm.ScaleMode = 3
  46.     frm.width = Screen.TwipsPerPixelX * (SCREENWIDTH + 12)
  47.     frm.height = Screen.TwipsPerPixelY * (SCREENHEIGHT + 30)
  48.     frm.Show
  49.     
  50.     'initialize Direct3D
  51.     InitDirect3D frm.hwnd
  52.     
  53.     'initialize DirectInput
  54.     InitDirectInput
  55.     InitKeyboard frm.hwnd
  56.     
  57.     'get reference to the back buffer
  58.     Set backbuffer = d3ddev.GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO)
  59.     
  60.     'create the font
  61.     Set fontImg = LoadTexture(d3ddev, App.Path & "\font.bmp")
  62.     InitSprite d3ddev, fontSpr
  63.     fontSpr.FramesPerRow = 20
  64.     fontSpr.width = 8
  65.     fontSpr.height = 12
  66.     fontSpr.ScaleFactor = 1
  67.     
  68.     'clear the screen to black
  69.     d3ddev.Clear 0, ByVal 0, D3DCLEAR_TARGET, C_BLACK, 1, 0
  70.  
  71.     'display a startup message
  72.     d3ddev.BeginScene
  73.     PrintText fontImg, fontSpr, 10, 10, C_GREEN, "Celtic Crusader"
  74.     PrintText fontImg, fontSpr, 10, 40, C_GREEN, "PLEASE WAIT, LOADING..."
  75.     d3ddev.EndScene
  76.     d3ddev.Present ByVal 0, ByVal 0, 0, ByVal 0
  77.     
  78.     'load the bitmap file
  79.     Set tiles = LoadSurface(App.Path & "\ireland.bmp", 1024, 576)
  80.     
  81.     'fill the badtiles array
  82.     BuildTileCollisionList
  83.     
  84.     'load the bottom toolbar image
  85.     Set wood = LoadSurface(App.Path & "\bottom.bmp", 644, 32)
  86.     
  87.     'load the map data from the Mappy export file
  88.     LoadBinaryMap App.Path & "\ireland.mar", MAPWIDTH, MAPHEIGHT
  89.     
  90.     'create the small scroll buffer surface
  91.     Set scrollbuffer = d3ddev.CreateImageSurface( _
  92.         SCROLLBUFFERWIDTH, _
  93.         SCROLLBUFFERHEIGHT, _
  94.         dispmode.Format)
  95.         
  96.     'initialize NPCs
  97.     InitCharacters
  98.     
  99.     'initialize the hero
  100.     InitHero
  101.     
  102.     'start player in the city of Dubh Linn
  103.     ScrollX = PLAYERSTARTX * TILEWIDTH
  104.     ScrollY = PLAYERSTARTY * TILEHEIGHT
  105.     
  106.     'this helps to keep a steady framerate
  107.     Dim start As Long
  108.     start = GetTickCount()
  109.  
  110.     'main loop
  111.     Do While (True)
  112.         'erase the bottom toolbar
  113.         DrawSurface wood, 0, 0, 639, 30, backbuffer, 0, 449
  114.     
  115.         'poll DirectInput for keyboard input
  116.         Check_Keyboard
  117.  
  118.         'update the scrolling window
  119.         UpdateScrollPosition
  120.         CheckTileCollisions
  121.         DrawTiles
  122.         DrawScrollWindow
  123.         Scroll 0, 0
  124.         
  125.         'update NPCs
  126.         MoveNPCs
  127.         CheckNPCCollisions
  128.     
  129.         'set the screen refresh to about 50 fps
  130.         If GetTickCount - start > 20 Then
  131.         
  132.             'start rendering
  133.             d3ddev.BeginScene
  134.             
  135.             DrawNPCs
  136.             
  137.             'update the hero
  138.             UpdateHero
  139.  
  140.             ShowScrollData
  141.  
  142.             'stop rendering
  143.             d3ddev.EndScene
  144.         
  145.             d3ddev.Present ByVal 0, ByVal 0, 0, ByVal 0
  146.             start = GetTickCount
  147.             DoEvents
  148.         End If
  149.         
  150.     Loop
  151. End Sub
  152.  
  153.  
  154. Public Sub BuildTileCollisionList()
  155.     ReDim badtiles(5)
  156.     badtiles(0) = 2
  157.     badtiles(1) = 34
  158.     badtiles(2) = 44
  159.     badtiles(3) = 54
  160.     badtiles(4) = 79
  161.    
  162. End Sub
  163.  
  164. Public Function IsBadTile(ByVal tilenum As Long) As Boolean
  165.     Dim n As Long
  166.     
  167.     For n = 0 To 4
  168.         If badtiles(n) - 1 = tilenum Then
  169.             IsBadTile = True
  170.             Exit Function
  171.         End If
  172.     Next n
  173.     
  174.     IsBadTile = False
  175.  
  176. End Function
  177.  
  178. Public Function TileAt(ByVal x As Long, ByVal y As Long) As Long
  179.     Dim tile As point
  180.     tile.x = x \ TILEWIDTH
  181.     tile.y = y \ TILEHEIGHT
  182.     TileAt = mapdata(tile.y * MAPWIDTH + tile.x)
  183. End Function
  184.  
  185. Public Function CurrentTile() As Long
  186.     CurrentTile = TileAt(PlayerPos.x, PlayerPos.y)
  187. End Function
  188.  
  189. Public Sub ShowScrollData()
  190.     Dim tile As point
  191.     
  192.     tile.x = PlayerPos.x \ TILEWIDTH
  193.     tile.y = PlayerPos.y \ TILEHEIGHT
  194.     
  195.     PrintText fontImg, fontSpr, 5, 452, C_WHITE, "Scroll=(" & PlayerPos.x & "," & PlayerPos.y & ") "
  196.     PrintText fontImg, fontSpr, 5, 466, C_WHITE, "Tile(" & tile.x & "," & tile.y & ")=" & CurrentTile()
  197. End Sub
  198.  
  199. 'modified in chapter 18
  200. 'This is called from DirectInput.bas on keypress events
  201. Public Sub KeyPressed(ByVal key As Long)
  202.     
  203.     Select Case key
  204.     
  205.         Case KEY_UP, KEY_NUMPAD8
  206.             heroSprWalk.AnimSeq = 0
  207.             heroSprWalk.Animating = True
  208.             Scroll 0, -WALKSPEED
  209.             PlayerData.state = HERO_WALKING
  210.             
  211.         Case KEY_NUMPAD9
  212.             heroSprWalk.AnimSeq = 1
  213.             heroSprWalk.Animating = True
  214.             Scroll WALKSPEED, -WALKSPEED
  215.             PlayerData.state = HERO_WALKING
  216.         
  217.         Case KEY_RIGHT, KEY_NUMPAD6
  218.             heroSprWalk.AnimSeq = 2
  219.             heroSprWalk.Animating = True
  220.             Scroll WALKSPEED, 0
  221.             PlayerData.state = HERO_WALKING
  222.             
  223.         Case KEY_NUMPAD3
  224.             heroSprWalk.AnimSeq = 3
  225.             heroSprWalk.Animating = True
  226.             Scroll WALKSPEED, WALKSPEED
  227.             PlayerData.state = HERO_WALKING
  228.         
  229.         Case KEY_DOWN, KEY_NUMPAD2
  230.             heroSprWalk.AnimSeq = 4
  231.             heroSprWalk.Animating = True
  232.             Scroll 0, WALKSPEED
  233.             PlayerData.state = HERO_WALKING
  234.             
  235.         Case KEY_NUMPAD1
  236.             heroSprWalk.AnimSeq = 5
  237.             heroSprWalk.Animating = True
  238.             Scroll -WALKSPEED, WALKSPEED
  239.             PlayerData.state = HERO_WALKING
  240.         
  241.         Case KEY_LEFT, KEY_NUMPAD4
  242.             heroSprWalk.AnimSeq = 6
  243.             heroSprWalk.Animating = True
  244.             Scroll -WALKSPEED, 0
  245.             PlayerData.state = HERO_WALKING
  246.             
  247.         Case KEY_NUMPAD7
  248.             heroSprWalk.AnimSeq = 7
  249.             heroSprWalk.Animating = True
  250.             Scroll -WALKSPEED, -WALKSPEED
  251.             PlayerData.state = HERO_WALKING
  252.             
  253.         Case KEY_LCTRL
  254.             'point attack anim in same direction as walk
  255.             heroSprAttack.AnimSeq = heroSprWalk.AnimSeq
  256.             heroSprAttack.Animating = True
  257.             PlayerData.state = HERO_ATTACKING
  258.         
  259.         Case KEY_ESC
  260.             Shutdown
  261.     
  262.     End Select
  263.     
  264.     'uncomment this when you want to find new key codes
  265.     Debug.Print "Key = " & key
  266.     
  267. End Sub
  268.  
  269.  
  270. Public Sub Scroll(ByVal horiz As Long, ByVal vert As Long)
  271.     SpeedX = horiz
  272.     SpeedY = vert
  273. End Sub
  274.  
  275. Public Sub Shutdown()
  276.     Dim n As Long
  277.     
  278.     Set wood = Nothing
  279.     Set heroImgWalk = Nothing
  280.     Set heroImgAttack = Nothing
  281.     Set heroImgRun = Nothing
  282.     
  283.     For n = 0 To NUMCHARS - 1
  284.         Set charWalk(n) = Nothing
  285.     Next n
  286.     
  287.     Set scrollbuffer = Nothing
  288.     Set tiles = Nothing
  289.     
  290.     Set d3ddev = Nothing
  291.     Set d3d = Nothing
  292.     Set d3dx = Nothing
  293.     Set dx = Nothing
  294.     
  295.     End
  296. End Sub
  297.  
  298.  
  299.