home *** CD-ROM | disk | FTP | other *** search
- Attribute VB_Name = "Game"
- '---------------------------------------------------------------
- ' Visual Basic Game Programming for Teens
- ' Chapter 12 - WalkAbout program
- '
- ' Requires the following files:
- ' Direct3D.bas, DirectInput.bas, Globals.bas, TileScroller.bas,
- ' Sprite.bas, and an empty Form1.
- '---------------------------------------------------------------
-
- Option Explicit
- Option Base 0
-
- Const HEROSPEED As Integer = 4
- Dim heroSpr As TSPRITE
- Dim heroImg As Direct3DTexture8
- Dim SuperHero As Boolean
-
- Dim fontImg As Direct3DTexture8
- Dim fontSpr As TSPRITE
-
- Dim wood As Direct3DSurface8
-
- Dim badtiles() As Integer
-
-
- Const C_PURPLE As Long = &HFFFF00FF
- Const C_RED As Long = &HFFFF0000
- Const C_GREEN As Long = &HFF00FF00
- Const C_BLUE As Long = &HFF0000FF
- Const C_WHITE As Long = &HFFFFFFFF
- Const C_BLACK As Long = &H0
- Const C_GRAY As Long = &HFFAAAAAA
-
-
- Dim frm As New Form1
-
- Public Sub Main()
-
- 'set up the main form
- frm.Caption = "CollisionTest"
- frm.AutoRedraw = False
- frm.BorderStyle = 1
- frm.ClipControls = False
- frm.ScaleMode = 3
- frm.width = Screen.TwipsPerPixelX * (SCREENWIDTH + 12)
- frm.height = Screen.TwipsPerPixelY * (SCREENHEIGHT + 30)
- frm.Show
-
- 'initialize Direct3D
- InitDirect3D frm.hwnd
-
- InitDirectInput
- InitKeyboard frm.hwnd
-
- 'get reference to the back buffer
- Set backbuffer = d3ddev.GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO)
-
- 'create the font
- Set fontImg = LoadTexture(d3ddev, App.Path & "\font.bmp")
- InitSprite d3ddev, fontSpr
- fontSpr.FramesPerRow = 20
- fontSpr.width = 8
- fontSpr.height = 12
- fontSpr.ScaleFactor = 1
-
- 'clear the screen to black
- d3ddev.Clear 0, ByVal 0, D3DCLEAR_TARGET, C_BLACK, 1, 0
-
- 'display a startup message
- d3ddev.BeginScene
- PrintText fontImg, fontSpr, 10, 10, C_GREEN, "CollisionTest Program"
- PrintText fontImg, fontSpr, 10, 40, C_GREEN, "PLEASE WAIT, LOADING MAP..."
- d3ddev.EndScene
- d3ddev.Present ByVal 0, ByVal 0, 0, ByVal 0
-
-
- 'load the bitmap file
- Set tiles = LoadSurface(App.Path & "\ireland.bmp", 1024, 576)
-
- BuildTileCollisionList
-
- Set wood = LoadSurface(App.Path & "\bottom.bmp", 644, 32)
-
- 'load the map data from the Mappy export file
- LoadBinaryMap App.Path & "\ireland.mar", MAPWIDTH, MAPHEIGHT
-
- 'load the dragon sprite
- Set heroImg = LoadTexture(d3ddev, App.Path & "\hero_sword_walk.bmp")
-
- 'initialize the dragon sprite
- InitSprite d3ddev, heroSpr
- With heroSpr
- .FramesPerRow = 9
- .FrameCount = 9
- .CurrentFrame = 0
- .AnimDelay = 1
- .width = 96
- .height = 96
- .ScaleFactor = 1
- .x = (SCREENWIDTH - .width) / 2
- .y = (SCREENHEIGHT - .height) / 2
- End With
-
- 'create the small scroll buffer surface
- Set scrollbuffer = d3ddev.CreateImageSurface( _
- SCROLLBUFFERWIDTH, _
- SCROLLBUFFERHEIGHT, _
- dispmode.Format)
-
- 'start player in the city of Dubh Linn
- ScrollX = 1445 * TILEWIDTH
- ScrollY = 1207 * TILEHEIGHT
-
- 'this helps to keep a steady framerate
- Dim start As Long
- start = GetTickCount()
-
- 'main loop
- Do While (True)
- 'poll DirectInput for keyboard input
- Check_Keyboard
-
- 'update the scrolling window
- UpdateScrollPosition
- CheckTileCollisions
-
- DrawTiles
- DrawScrollWindow
- Scroll 0, 0
-
- 'reset scroll speed
- SuperHero = False
-
- 'set the screen refresh to about 50 fps
- If GetTickCount - start > 20 Then
-
- 'start rendering
- d3ddev.BeginScene
-
- 'animate the dragon
- If heroSpr.Animating Then
- AnimateSprite heroSpr, heroImg
- End If
-
- 'draw the hero sprite
- DrawSprite heroImg, heroSpr, &HFFFFFFFF
-
- ShowScrollData
-
- 'stop rendering
- d3ddev.EndScene
-
- d3ddev.Present ByVal 0, ByVal 0, 0, ByVal 0
- start = GetTickCount
- DoEvents
- End If
- Loop
- End Sub
-
- Public Sub BuildTileCollisionList()
- ReDim badtiles(5)
- badtiles(0) = 2
- badtiles(1) = 34
- badtiles(2) = 44
- badtiles(3) = 54
- badtiles(4) = 79
-
- End Sub
-
- Public Function IsBadTile(ByVal tilenum As Long) As Boolean
- Dim n As Long
-
- For n = 0 To 4
- If badtiles(n) - 1 = tilenum Then
- IsBadTile = True
- Exit Function
- End If
- Next n
-
- IsBadTile = False
-
- End Function
-
- Public Sub CheckTileCollisions()
- Dim tilenum As Long
-
- tilenum = CurrentTile()
- If IsBadTile(tilenum) Then
- Scroll 0, 0
-
- Select Case heroSpr.AnimSeq
- Case 0
- ScrollY = ScrollY + HEROSPEED
- Case 1
- ScrollY = ScrollY + HEROSPEED
- ScrollX = ScrollX - HEROSPEED
- Case 2
- ScrollX = ScrollX - HEROSPEED
- Case 3
- ScrollX = ScrollX - HEROSPEED
- ScrollY = ScrollY - HEROSPEED
- Case 4
- ScrollY = ScrollY - HEROSPEED
- Case 5
- ScrollX = ScrollX + HEROSPEED
- ScrollY = ScrollY - HEROSPEED
- Case 6
- ScrollX = ScrollX + HEROSPEED
- Case 7
- ScrollX = ScrollX + HEROSPEED
- ScrollY = ScrollY + HEROSPEED
- End Select
-
- End If
-
- End Sub
-
- Public Function TileAt(ByVal x As Long, ByVal y As Long) As Long
- Dim tile As point
- tile.x = x \ TILEWIDTH
- tile.y = y \ TILEHEIGHT
- TileAt = mapdata(tile.y * MAPWIDTH + tile.x)
- End Function
-
- Public Function CurrentTile() As Long
- CurrentTile = TileAt(PlayerPos.x, PlayerPos.y)
- End Function
-
- Public Function PlayerPos() As point
- 'get tile pos at center of screen
- PlayerPos.x = ScrollX + SCREENWIDTH / 2
- PlayerPos.y = ScrollY + SCREENHEIGHT / 2
- End Function
-
- Public Sub ShowScrollData()
- Static old As point
- Dim tile As point
-
- tile.x = PlayerPos.x \ TILEWIDTH
- tile.y = PlayerPos.y \ TILEHEIGHT
-
- If (tile.x <> old.x) Or (tile.y <> old.y) Then
-
- 'erase the background
- DrawSurface wood, 0, 0, 639, 30, backbuffer, 0, 449
-
- old = tile
-
- PrintText fontImg, fontSpr, 5, 452, C_WHITE, "Scroll=(" & PlayerPos.x & "," & PlayerPos.y & ") "
- PrintText fontImg, fontSpr, 5, 466, C_WHITE, "Tile(" & tile.x & "," & tile.y & ")=" & CurrentTile()
- End If
- End Sub
-
- 'This is called from DirectInput.bas on keypress events
- Public Sub KeyPressed(ByVal key As Long)
- Select Case key
- Case KEY_UP, KEY_NUMPAD8
- heroSpr.AnimSeq = 0
- heroSpr.Animating = True
- Scroll 0, -HEROSPEED
-
- Case KEY_NUMPAD9
- heroSpr.AnimSeq = 1
- heroSpr.Animating = True
- Scroll HEROSPEED, -HEROSPEED
-
- Case KEY_RIGHT, KEY_NUMPAD6
- heroSpr.AnimSeq = 2
- heroSpr.Animating = True
- Scroll HEROSPEED, 0
-
- Case KEY_NUMPAD3
- heroSpr.AnimSeq = 3
- heroSpr.Animating = True
- Scroll HEROSPEED, HEROSPEED
-
- Case KEY_DOWN, KEY_NUMPAD2
- heroSpr.AnimSeq = 4
- heroSpr.Animating = True
- Scroll 0, HEROSPEED
-
- Case KEY_NUMPAD1
- heroSpr.AnimSeq = 5
- heroSpr.Animating = True
- Scroll -HEROSPEED, HEROSPEED
-
- Case KEY_LEFT, KEY_NUMPAD4
- heroSpr.AnimSeq = 6
- heroSpr.Animating = True
- Scroll -HEROSPEED, 0
-
- Case KEY_NUMPAD7
- heroSpr.AnimSeq = 7
- heroSpr.Animating = True
- Scroll -HEROSPEED, -HEROSPEED
-
- Case KEY_LSHIFT, KEY_RSHIFT
- SuperHero = True
-
- Case KEY_ESC
- Shutdown
-
- End Select
-
- 'uncomment this when you want to find new key codes
- 'Debug.Print "Key = " & key
-
- End Sub
-
-
- Public Sub Scroll(ByVal horiz As Long, ByVal vert As Long)
- SpeedX = horiz
- SpeedY = vert
-
- If SuperHero Then
- SpeedX = SpeedX * 4
- SpeedY = SpeedY * 4
- End If
- End Sub
-
-
-