home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Game Programming for Teens / VBGPFT.cdr / sources / chapter12 / WalkAbout / Game.bas < prev    next >
Encoding:
BASIC Source File  |  2004-10-23  |  5.6 KB  |  209 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. Const HEROSPEED As Integer = 4
  15. Dim heroSpr As TSPRITE
  16. Dim heroImg As Direct3DTexture8
  17. Dim SuperHero As Boolean
  18.  
  19. Dim frm As New Form1
  20.  
  21. Public Sub Main()
  22.     
  23.     'set up the main form
  24.     frm.Caption = "WalkAbout"
  25.     frm.AutoRedraw = False
  26.     frm.BorderStyle = 1
  27.     frm.ClipControls = False
  28.     frm.ScaleMode = 3
  29.     frm.width = Screen.TwipsPerPixelX * (SCREENWIDTH + 12)
  30.     frm.height = Screen.TwipsPerPixelY * (SCREENHEIGHT + 30)
  31.     frm.Show
  32.     
  33.     'initialize Direct3D
  34.     InitDirect3D frm.hwnd
  35.     
  36.     InitDirectInput
  37.     InitKeyboard frm.hwnd
  38.     
  39.     'get reference to the back buffer
  40.     Set backbuffer = d3ddev.GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO)
  41.     
  42.     'load the bitmap file
  43.     Set tiles = LoadSurface(App.Path & "\ireland.bmp", 1024, 576)
  44.     
  45.     'load the map data from the Mappy export file
  46.     LoadBinaryMap App.Path & "\ireland.mar", MAPWIDTH, MAPHEIGHT
  47.     
  48.     'load the dragon sprite
  49.     Set heroImg = LoadTexture(d3ddev, App.Path & "\hero_sword_walk.bmp")
  50.     
  51.     'initialize the dragon sprite
  52.     InitSprite d3ddev, heroSpr
  53.     With heroSpr
  54.         .FramesPerRow = 9
  55.         .FrameCount = 9
  56.         .CurrentFrame = 0
  57.         .AnimDelay = 1
  58.         .width = 96
  59.         .height = 96
  60.         .ScaleFactor = 1
  61.         .x = (SCREENWIDTH - .width) / 2
  62.         .y = (SCREENHEIGHT - .height) / 2
  63.     End With
  64.     
  65.     'create the small scroll buffer surface
  66.     Set scrollbuffer = d3ddev.CreateImageSurface( _
  67.         SCROLLBUFFERWIDTH, _
  68.         SCROLLBUFFERHEIGHT, _
  69.         dispmode.Format)
  70.         
  71.     'start player in the city of Dubh Linn
  72.     ScrollX = 1342 * TILEWIDTH
  73.     ScrollY = 945 * TILEHEIGHT
  74.     
  75.     'this helps to keep a steady framerate
  76.     Dim start As Long
  77.     start = GetTickCount()
  78.  
  79.     'clear the screen to black
  80.     d3ddev.Clear 0, ByVal 0, D3DCLEAR_TARGET, C_BLACK, 1, 0
  81.  
  82.     'main loop
  83.     Do While (True)
  84.         'poll DirectInput for keyboard input
  85.         Check_Keyboard
  86.  
  87.         'update the scrolling window
  88.         UpdateScrollPosition
  89.         DrawTiles
  90.         DrawScrollWindow
  91.         Scroll 0, 0
  92.         ShowScrollData
  93.     
  94.         'reset scroll speed
  95.         SuperHero = False
  96.         
  97.         'set the screen refresh to about 50 fps
  98.         If GetTickCount - start > 20 Then
  99.         
  100.             'start rendering
  101.             d3ddev.BeginScene
  102.             
  103.             'animate the dragon
  104.             If heroSpr.Animating Then
  105.                 AnimateSprite heroSpr, heroImg
  106.             End If
  107.         
  108.             'draw the hero sprite
  109.             DrawSprite heroImg, heroSpr, &HFFFFFFFF
  110.  
  111.             'stop rendering
  112.             d3ddev.EndScene
  113.         
  114.             d3ddev.Present ByVal 0, ByVal 0, 0, ByVal 0
  115.             start = GetTickCount
  116.             DoEvents
  117.         End If
  118.     Loop
  119. End Sub
  120.  
  121. Public Sub ShowScrollData()
  122.     Static old As point
  123.     Dim player As point
  124.     Dim tile As point
  125.     Dim tilenum As Long
  126.     
  127.     player.x = ScrollX + SCREENWIDTH / 2
  128.     player.y = ScrollY + SCREENHEIGHT / 2
  129.     tile.x = player.x \ TILEWIDTH
  130.     tile.y = player.y \ TILEHEIGHT
  131.     
  132.     If (tile.x <> old.x) Or (tile.y <> old.y) Then
  133.         old = tile
  134.         tilenum = mapdata(tile.y * MAPWIDTH + tile.x)
  135.         frm.Caption = "WalkAbout - " & _
  136.             "Scroll=(" & player.x & "," & player.y & ") " & _
  137.             "Tile(" & tile.x & "," & tile.y & ")=" & tilenum
  138.     End If
  139. End Sub
  140.  
  141. 'This is called from DirectInput.bas on keypress events
  142. Public Sub KeyPressed(ByVal key As Long)
  143.     Select Case key
  144.         Case KEY_UP, KEY_NUMPAD8
  145.             heroSpr.AnimSeq = 0
  146.             heroSpr.Animating = True
  147.             Scroll 0, -HEROSPEED
  148.             
  149.         Case KEY_NUMPAD9
  150.             heroSpr.AnimSeq = 1
  151.             heroSpr.Animating = True
  152.             Scroll HEROSPEED, -HEROSPEED
  153.         
  154.         Case KEY_RIGHT, KEY_NUMPAD6
  155.             heroSpr.AnimSeq = 2
  156.             heroSpr.Animating = True
  157.             Scroll HEROSPEED, 0
  158.             
  159.         Case KEY_NUMPAD3
  160.             heroSpr.AnimSeq = 3
  161.             heroSpr.Animating = True
  162.             Scroll HEROSPEED, HEROSPEED
  163.         
  164.         Case KEY_DOWN, KEY_NUMPAD2
  165.             heroSpr.AnimSeq = 4
  166.             heroSpr.Animating = True
  167.             Scroll 0, HEROSPEED
  168.             
  169.         Case KEY_NUMPAD1
  170.             heroSpr.AnimSeq = 5
  171.             heroSpr.Animating = True
  172.             Scroll -HEROSPEED, HEROSPEED
  173.         
  174.         Case KEY_LEFT, KEY_NUMPAD4
  175.             heroSpr.AnimSeq = 6
  176.             heroSpr.Animating = True
  177.             Scroll -HEROSPEED, 0
  178.             
  179.         Case KEY_NUMPAD7
  180.             heroSpr.AnimSeq = 7
  181.             heroSpr.Animating = True
  182.             Scroll -HEROSPEED, -HEROSPEED
  183.             
  184.         Case KEY_LSHIFT, KEY_RSHIFT
  185.             SuperHero = True
  186.         
  187.         Case KEY_ESC
  188.             Shutdown
  189.     
  190.     End Select
  191.     
  192.     'uncomment this when you want to find new key codes
  193.     'Debug.Print "Key = " & key
  194.     
  195. End Sub
  196.  
  197.  
  198. Public Sub Scroll(ByVal horiz As Long, ByVal vert As Long)
  199.     SpeedX = horiz
  200.     SpeedY = vert
  201.     
  202.     If SuperHero Then
  203.         SpeedX = SpeedX * 4
  204.         SpeedY = SpeedY * 4
  205.     End If
  206. End Sub
  207.  
  208.  
  209.