home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH5 / SRC / SPRRECT.CLS < prev    next >
Encoding:
Text File  |  1996-02-26  |  2.9 KB  |  111 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "RectangleSprite"
  6. Attribute VB_Creatable = False
  7. Attribute VB_Exposed = False
  8. ' ************************************************
  9. ' Moving rectangle sprite.
  10. ' ************************************************
  11. Option Explicit
  12.  
  13. Dim Wid As Integer      ' Width.
  14. Dim Hgt As Integer      ' Height.
  15. Dim Cx As Integer       ' Position of center.
  16. Dim Cy As Integer
  17. Dim Theta As Single     ' Orientation.
  18. Dim Vx As Integer       ' Velocity.
  19. Dim Vy As Integer
  20. Dim Vtheta As Single    ' Angular velocity.
  21. Dim Clr As Long         ' Color.
  22. ' ************************************************
  23. ' Draw the rectangle on the indicated picture box.
  24. ' ************************************************
  25. Public Sub DrawSprite(pic As PictureBox)
  26. Const PI = 3.14159
  27. Const PI_OVER_2 = PI / 2
  28.  
  29. Dim wx As Single
  30. Dim wy As Single
  31. Dim hx As Single
  32. Dim hy As Single
  33. Dim pts(1 To 4) As POINTAPI
  34. Dim status As Long
  35. Dim newpen As Long
  36. Dim oldpen As Long
  37. Dim newbrush As Long
  38. Dim oldbrush As Long
  39.  
  40.     ' Compute vectors parallel to the axes.
  41.     wx = Wid * Cos(Theta)
  42.     wy = Wid * Sin(Theta)
  43.     hx = Hgt * Cos(Theta + PI_OVER_2)
  44.     hy = Hgt * Sin(Theta + PI_OVER_2)
  45.     
  46.     pts(1).x = Cx + (wx + hx) / 2
  47.     pts(1).y = Cy + (wy + hy) / 2
  48.     pts(2).x = pts(1).x - hx
  49.     pts(2).y = pts(1).y - hy
  50.     pts(3).x = pts(2).x - wx
  51.     pts(3).y = pts(2).y - wy
  52.     pts(4).x = pts(3).x + hx
  53.     pts(4).y = pts(3).y + hy
  54.     
  55.     newpen = CreatePen(PS_SOLID, 0, Clr)
  56.     oldpen = SelectObject(pic.hdc, newpen)
  57.     newbrush = CreateSolidBrush(Clr)
  58.     oldbrush = SelectObject(pic.hdc, newbrush)
  59.     
  60.     status = Polygon(pic.hdc, pts(1), 4)
  61.     
  62.     newpen = SelectObject(pic.hdc, oldpen)
  63.     status = DeleteObject(newpen)
  64.     newbrush = SelectObject(pic.hdc, oldbrush)
  65.     status = DeleteObject(newbrush)
  66. End Sub
  67.  
  68. ' ************************************************
  69. ' Initialize the rectangle.
  70. ' ************************************************
  71. Public Sub InitializeSprite(w As Integer, h As Integer, x As Integer, y As Integer, angle As Single, dx As Integer, dy As Integer, dangle As Single, c As Long)
  72.     Wid = w
  73.     Hgt = h
  74.     Cx = x
  75.     Cy = y
  76.     Theta = angle
  77.     Vx = dx
  78.     Vy = dy
  79.     Vtheta = dangle
  80.     Clr = c
  81. End Sub
  82.  
  83.  
  84. ' ************************************************
  85. ' Add the velocity components to the sprite's
  86. ' position components.
  87. ' ************************************************
  88. Public Sub MoveSprite(xmax As Integer, ymax As Integer)
  89.     Cx = Cx + Vx
  90.     Cy = Cy + Vy
  91.     
  92.     If Cx < 0 Then
  93.         Cx = -Cx
  94.         Vx = -Vx
  95.     ElseIf Cx > xmax Then
  96.         Cx = 2 * xmax - Cx
  97.         Vx = -Vx
  98.     End If
  99.     If Cy < 0 Then
  100.         Cy = -Cy
  101.         Vy = -Vy
  102.     ElseIf Cy > ymax Then
  103.         Cy = 2 * ymax - Cy
  104.         Vy = -Vy
  105.     End If
  106.     
  107.     Theta = Theta + Vtheta
  108. End Sub
  109.  
  110.  
  111.