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

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "TriangleSprite"
  6. Attribute VB_Creatable = False
  7. Attribute VB_Exposed = False
  8. ' ************************************************
  9. ' Moving triangle sprite.
  10. ' ************************************************
  11. Option Explicit
  12.  
  13. ' The three corners are stored as the distance
  14. ' PtR(i) and angle PtT(i) from the center of the
  15. ' triangle (Cx, Cy) to the corner. This makes it
  16. ' easier to rotate the triangle. When rotated by
  17. ' angle Theta, the coordinates of corner i are:
  18. '
  19. '   x = Cx + PtR(i) * Cos(PtT(i) + Theta)
  20. '   y = Cy + PtR(i) * Sin(PtT(i) + Theta)
  21. '
  22. Dim PtR(1 To 3) As Integer
  23. Dim PtT(1 To 3) As Single
  24. Dim Cx As Integer       ' Position of center.
  25. Dim Cy As Integer
  26. Dim Theta As Single     ' Orientation.
  27. Dim Vx As Integer       ' Velocity.
  28. Dim Vy As Integer
  29. Dim Vtheta As Single    ' Angular velocity.
  30. Dim Clr As Long         ' Color.
  31. ' ************************************************
  32. ' Draw the triangle on the indicated picture box.
  33. ' ************************************************
  34. Public Sub DrawSprite(pic As PictureBox)
  35. Dim i As Integer
  36. Dim pts(1 To 3) As POINTAPI
  37. Dim status As Long
  38. Dim newpen As Long
  39. Dim oldpen As Long
  40. Dim newbrush As Long
  41. Dim oldbrush As Long
  42.  
  43.     ' Compute the current corner locations.
  44.     For i = 1 To 3
  45.         pts(i).x = Cx + PtR(i) * Cos(PtT(i) + Theta)
  46.         pts(i).y = Cy + PtR(i) * Sin(PtT(i) + Theta)
  47.     Next i
  48.  
  49.     newpen = CreatePen(PS_SOLID, 0, Clr)
  50.     oldpen = SelectObject(pic.hdc, newpen)
  51.     newbrush = CreateSolidBrush(Clr)
  52.     oldbrush = SelectObject(pic.hdc, newbrush)
  53.     
  54.     status = Polygon(pic.hdc, pts(1), 3)
  55.  
  56.     newpen = SelectObject(pic.hdc, oldpen)
  57.     status = DeleteObject(newpen)
  58.     newbrush = SelectObject(pic.hdc, oldbrush)
  59.     status = DeleteObject(newbrush)
  60. End Sub
  61.  
  62. ' ************************************************
  63. ' Initialize the rectangle.
  64. ' ************************************************
  65. Public Sub InitializeSprite(x As Integer, y As Integer, dx As Integer, dy As Integer, r1 As Integer, t1 As Integer, r2 As Integer, t2 As Integer, r3 As Integer, t3 As Integer, dangle As Single, c As Long)
  66.     PtR(1) = r1
  67.     PtT(1) = t1
  68.     
  69.     PtR(2) = r2
  70.     PtT(2) = t2
  71.     
  72.     PtR(3) = r3
  73.     PtT(3) = t3
  74.     
  75.     Cx = x
  76.     Cy = y
  77.     Theta = 0
  78.     Vx = dx
  79.     Vy = dy
  80.     Vtheta = dangle
  81.     Clr = c
  82. End Sub
  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.