📄 ctank.cls
字号:
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "cTank"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes"
Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
Option Explicit
'local variable(s) to hold property value(s)
Private mvarX As Long 'local copy
Private mvarY As Long 'local copy
Private mvarDirection As SpriteDirectionConstants 'local copy
Private mvarShield As Byte 'local copy
Private mvarSpeed As Byte
Private mvarBackBuffer As DirectDrawSurface7
Private mvarBulletBmp As DirectDrawSurface7
Private mvarScore As Long
Dim Sprite As cSprite
Dim Bullets As Collection
Public Property Get Score() As Long
Score = mvarScore
End Property
Public Property Let Score(ByVal vData As Long)
mvarScore = vData
End Property
Public Property Get Group() As Integer
Group = Sprite.CurrentGroup
End Property
Public Property Let Group(ByVal vData As Integer)
Sprite.CurrentGroup = vData
End Property
Public Property Get Speed() As Byte
Speed = mvarSpeed
End Property
Public Property Let Speed(ByVal vData As Byte)
mvarSpeed = vData
End Property
Public Property Let Shield(ByVal vData As Byte)
'used when assigning a value to the property, on the left side of an assignment.
'Syntax: X.Shield = 5
mvarShield = vData
End Property
Public Property Get Shield() As Byte
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.Shield
Shield = mvarShield
End Property
Public Sub moveTank()
Static i As Integer
If i < (11 - mvarSpeed) Then
i = i + 1
End If
If i = (11 - mvarSpeed) Then
Sprite.moveSprite 3
i = 0
End If
MoveBullets
End Sub
Public Sub drawTank()
Sprite.drawSprite
DrawBullets
End Sub
Public Property Let Direction(ByVal vData As SpriteDirectionConstants)
'used when assigning an Object to the property, on the left side of a Set statement.
'Syntax: Set x.Direction = Form1
mvarDirection = vData
Sprite.Direction = vData
End Property
Public Property Get Direction() As SpriteDirectionConstants
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.Direction
Direction = mvarDirection
End Property
Public Property Let Y(ByVal vData As Long)
'used when assigning a value to the property, on the left side of an assignment.
'Syntax: X.Y = 5
mvarY = vData
Sprite.Y = vData
End Property
Public Property Get Y() As Long
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.Y
Y = mvarY
End Property
Public Property Let x(ByVal vData As Long)
'used when assigning a value to the property, on the left side of an assignment.
'Syntax: X.X = 5
mvarX = vData
Sprite.x = vData
End Property
Public Property Get x() As Long
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.X
x = mvarX
End Property
Private Sub Class_Initialize()
Set Sprite = New cSprite
Set Bullets = New Collection
End Sub
Private Sub Class_Terminate()
Set Sprite = Nothing
Set Bullets = Nothing
End Sub
Sub initTank(ddsBitmap As DirectDrawSurface7, ddsBackBuffer As DirectDrawSurface7, ddsBullet As DirectDrawSurface7)
With Sprite
Set .ddsBackBuffer = ddsBackBuffer
Set .ddsBitmap = ddsBitmap
.LoadData App.Path & "\tank01.spr"
.Direction = dirRight
.CurrentGroup = 1
.CurrentFrame = 2
End With
Set mvarBulletBmp = ddsBullet
End Sub
Sub Fire()
If Bullets.Count < 11 Then
Dim aBullet As New cSprite
With aBullet
.LoadData App.Path & "\bullet01.spr"
Set .ddsBackBuffer = Sprite.ddsBackBuffer
Set .ddsBitmap = mvarBulletBmp
.CurrentGroup = 1
.CurrentFrame = Sprite.Direction
.Direction = Sprite.Direction
.x = Sprite.x
.Y = Sprite.Y
aBullet.moveSprite 20
End With
Bullets.Add aBullet
End If
End Sub
Public Sub MoveBullets()
Dim aBullet As cSprite
Dim i As Integer
For i = Bullets.Count To 1 Step -1
Set aBullet = Bullets(i)
With aBullet
.moveSprite 5
If .x < 100 + .SpriteWidth Or .Y < .SpriteHeight Or _
.x > 640 - .SpriteWidth Or .Y > 480 - .SpriteHeight Then _
Bullets.Remove i
End With
Next i
End Sub
Private Sub DrawBullets()
Dim aBullet As cSprite
For Each aBullet In Bullets
aBullet.drawSprite
Next
End Sub
Public Function Collsion(tank As cTank) As Boolean
Dim aBullet As cSprite
Dim colTmp As Collection
Dim i As Integer
Set colTmp = Bullets
For Each aBullet In colTmp
i = i + 1
If RectCollision(aBullet.RectOnScreen, tank.RectOnScreen) Then
Debug.Print "Removing star: " & i
Bullets.Remove i
Collsion = True
End If
Next
End Function
Public Property Get RectOnScreen() As RECT
RectOnScreen = Sprite.RectOnScreen
End Property
Public Sub ClearBullets()
Set Bullets = Nothing
Set Bullets = New Collection
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -