home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Game Programming for Teens / VBGPFT.cdr / sources / chapter10 / AnimateSprite / Form1.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  2004-10-20  |  6.1 KB  |  203 lines

  1. VERSION 5.00
  2. Begin VB.Form Form1 
  3.    BorderStyle     =   1  'Fixed Single
  4.    Caption         =   "Form1"
  5.    ClientHeight    =   3045
  6.    ClientLeft      =   45
  7.    ClientTop       =   330
  8.    ClientWidth     =   4260
  9.    LinkTopic       =   "Form1"
  10.    MaxButton       =   0   'False
  11.    MinButton       =   0   'False
  12.    ScaleHeight     =   3045
  13.    ScaleWidth      =   4260
  14.    StartUpPosition =   2  'CenterScreen
  15. Attribute VB_Name = "Form1"
  16. Attribute VB_GlobalNameSpace = False
  17. Attribute VB_Creatable = False
  18. Attribute VB_PredeclaredId = True
  19. Attribute VB_Exposed = False
  20. '----------------------------------------------------------------------
  21. ' Visual Basic Game Programming With DirectX
  22. ' AnimateSprite Source Code File
  23. '----------------------------------------------------------------------
  24. Option Explicit
  25. Option Base 0
  26. 'Windows API functions and structures
  27. Private Declare Function GetTickCount Lib "kernel32" () As Long
  28. Const C_BLACK As Long = &H0
  29. Const KEY_ESC As Integer = 27
  30. Const KEY_LEFT As Integer = 37
  31. Const KEY_UP As Integer = 38
  32. Const KEY_RIGHT As Integer = 39
  33. Const KEY_DOWN As Integer = 40
  34. 'program constants
  35. Const SCREENWIDTH As Long = 640
  36. Const SCREENHEIGHT As Long = 480
  37. Const FULLSCREEN As Boolean = False
  38. Const DRAGONSPEED As Integer = 4
  39. Dim dragonSpr As TSPRITE
  40. Dim dragonImg As Direct3DTexture8
  41. Dim terrain As Direct3DSurface8
  42. Dim backbuffer As Direct3DSurface8
  43. Private Sub Form_Load()
  44.     'set up the main form
  45.     Form1.Caption = "AnimateSprite"
  46.     Form1.AutoRedraw = False
  47.     Form1.BorderStyle = 1
  48.     Form1.ClipControls = False
  49.     Form1.KeyPreview = True
  50.     Form1.ScaleMode = 3
  51.     Form1.width = Screen.TwipsPerPixelX * (SCREENWIDTH + 12)
  52.     Form1.height = Screen.TwipsPerPixelY * (SCREENHEIGHT + 30)
  53.     Form1.Show
  54.     'initialize Direct3D
  55.     InitDirect3D Me.hwnd, SCREENWIDTH, SCREENHEIGHT, FULLSCREEN
  56.     'get the back buffer
  57.     Set backbuffer = d3ddev.GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO)
  58.     'load the background image
  59.     Set terrain = LoadSurface(App.Path & "\terrain.bmp", 640, 480)
  60.     If terrain Is Nothing Then
  61.         MsgBox "Error loading bitmap"
  62.         Shutdown
  63.     End If
  64.     'load the dragon sprite
  65.     Set dragonImg = LoadTexture(d3ddev, App.Path & "\dragon.bmp")
  66.     'initialize the dragon sprite
  67.     InitSprite d3ddev, dragonSpr
  68.     With dragonSpr
  69.         .FramesPerRow = 8
  70.         .FrameCount = 8
  71.         .DirX = 0
  72.         .CurrentFrame = 0
  73.         .AnimDelay = 2
  74.         .width = 128
  75.         .height = 128
  76.         .ScaleFactor = 1
  77.         .x = 150
  78.         .y = 100
  79.     End With
  80.     Dim start As Long
  81.     start = GetTickCount()
  82.     'start main game loop
  83.     Do While True
  84.         If GetTickCount - start > 25 Then
  85.             
  86.             'draw the background
  87.             DrawSurface terrain, 0, 0
  88.             
  89.             'start rendering
  90.             d3ddev.BeginScene
  91.             
  92.             'move the dragon
  93.             MoveDragon
  94.             
  95.             'animate the dragon
  96.             AnimateDragon
  97.         
  98.             'stop rendering
  99.             d3ddev.EndScene
  100.         
  101.             'draw the back buffer to the screen
  102.             d3ddev.Present ByVal 0, ByVal 0, 0, ByVal 0
  103.             start = GetTickCount
  104.             DoEvents
  105.         End If
  106.     Loop
  107. End Sub
  108. Public Sub MoveDragon()
  109.     With dragonSpr
  110.         .x = .x + .SpeedX
  111.         If .x < 0 Then
  112.             .x = 0
  113.             .SpeedX = 0
  114.         End If
  115.         If .x > SCREENWIDTH - .width Then
  116.             .x = SCREENWIDTH - .width - 1
  117.             .SpeedX = 0
  118.         End If
  119.         
  120.         .y = .y + .SpeedY
  121.         If .y < 0 Then
  122.             .y = 0
  123.             .SpeedX = 0
  124.         End If
  125.         If .y > SCREENHEIGHT - .height Then
  126.             .y = SCREENHEIGHT - .height - 1
  127.             .SpeedX = 0
  128.         End If
  129.     End With
  130. End Sub
  131. Public Sub AnimateDragon()
  132.     With dragonSpr
  133.         'increment the animation counter
  134.         .AnimCount = .AnimCount + 1
  135.         
  136.         'has the animation counter waited long enough?
  137.         If .AnimCount > .AnimDelay Then
  138.             .AnimCount = 0
  139.             
  140.             'okay, go to the next frame
  141.             .CurrentFrame = .CurrentFrame + 1
  142.             
  143.             'loop through the frames
  144.             If .CurrentFrame > .StartFrame + 7 Then
  145.                 .CurrentFrame = .StartFrame
  146.             End If
  147.             
  148.         End If
  149.     End With
  150.     'draw the dragon sprite
  151.     DrawSprite dragonImg, dragonSpr, &HFFFFFFFF
  152. End Sub
  153. Public Sub DrawSurface(ByRef source As Direct3DSurface8, ByVal x As Long, ByVal y As Long)
  154.     Dim r As DxVBLibA.RECT
  155.     Dim point As DxVBLibA.point
  156.     Dim desc As D3DSURFACE_DESC
  157.     source.GetDesc desc
  158.     'set dimensions of the source image
  159.     r.Left = x
  160.     r.Top = y
  161.     r.Right = x + desc.width
  162.     r.bottom = y + desc.height
  163.         
  164.     'set the destination point
  165.     point.x = 0
  166.     point.y = 0
  167.     'draw the scroll window
  168.     d3ddev.CopyRects source, r, 1, backbuffer, point
  169. End Sub
  170. Private Sub Form_Unload(Cancel As Integer)
  171.     Shutdown
  172. End Sub
  173. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  174.     Select Case KeyCode
  175.         Case KEY_UP
  176.             dragonSpr.StartFrame = 0
  177.             dragonSpr.CurrentFrame = 0
  178.             dragonSpr.SpeedX = 0
  179.             dragonSpr.SpeedY = -DRAGONSPEED
  180.         
  181.         Case KEY_RIGHT
  182.             dragonSpr.StartFrame = 16
  183.             dragonSpr.CurrentFrame = 16
  184.             dragonSpr.SpeedX = DRAGONSPEED
  185.             dragonSpr.SpeedY = 0
  186.         
  187.         Case KEY_DOWN
  188.             dragonSpr.StartFrame = 32
  189.             dragonSpr.CurrentFrame = 32
  190.             dragonSpr.SpeedX = 0
  191.             dragonSpr.SpeedY = DRAGONSPEED
  192.         
  193.         Case KEY_LEFT
  194.             dragonSpr.StartFrame = 48
  195.             dragonSpr.CurrentFrame = 48
  196.             dragonSpr.SpeedX = -DRAGONSPEED
  197.             dragonSpr.SpeedY = 0
  198.         
  199.         Case KEY_ESC
  200.             Shutdown
  201.     End Select
  202. End Sub
  203.