Question Game Help

samcp16

Member
Joined
Jan 27, 2012
Messages
8
Programming Experience
1-3
Hello, im currently coding a simple space invaders type game and was wondering how i can use a constant to set a number of "lives" for a picture box so that if a "bullet" that is a picture box collides with an "enemy" picture box 3 times, the enemy picture box will be hidden.
thanks
 
That sounds like the simplest usage of a variable, not a constant. Remember, Variables can vary (change) whereas constants cannot.
I can't offer any more help without further information about what you're doing.
 
I think you want a counter variable declared as Static. A Static variable stays in memory even after the sub ends. For example, here is some pseudocode:

Static count As Integer
If bullet collides with enemy Then count += 1
If count = 3 Then hide picture box

Reset the count to 0 when done, or it will continue to increment.
 
Heres something that might help you out:


VB.NET:
Public Class Form1

    Dim Enemy1HP As Integer = 3 ' Here it can be accessed from other subs.

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        If pbBullet.Bounds.IntersectsWith(pbEnemy1.Bounds) Then
            Enemy1HP -= 1
        End If

        If Enemy1HP = 0 Then
            Me.Controls.Remove(pbEnemy1)
        End If

    End Sub

End Class

Personally, I wouldn't do a game like this, but from pure Control Scanning through the Form (rather panel, for only intented controls(PictureBoxes in this case)) and using their Tag property.
 
Back
Top