Question VB.NET Random events & Progress Bars ...

PRo-Beaniie

Well-known member
Joined
Mar 17, 2011
Messages
55
Location
Hertford, Hertfordshire, United Kingdom
Programming Experience
3-5
Hey guys,

Im fairly new to VB but what i have learnt i picked up fast, so heres my problem :confused:

Im currently in college and have been assigned to create a VB game, i have come quite far and have managed to create a neat and tidy little rocket game in 2d. However i would like to incorperate into the game a Reloading bar, a health bar and i would like to have the enemy fire at my shooter but randomly .... I cant quite figure it out hope you can help and here is my recent code .... :confused:
VB.NET:
Public Class FormMain
    'DIM controls ...
    Dim sright As Boolean
    Dim sleft As Boolean
    Dim shooterspeed As Integer
    Dim missilespeed As Integer
    Dim EnemySpeed As Integer
    Dim ERight As Boolean
    Dim ELeft As Boolean

    Private Sub FormMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        'Arrow Key Contorls ...
        If e.KeyValue = Keys.Right Then
            sright = True
            sleft = False
        End If
        If e.KeyValue = Keys.Left Then
            sleft = True
            sright = False
        End If
        If e.KeyValue = Keys.Down Then
            sleft = False
            sright = False
        End If

        If e.KeyValue = Keys.Space And Missile.Visible = False Then
            Missile.Top = Shooter.Top
            Missile.Left = Shooter.Left + (Shooter.Width / 2) - (Missile.Width / 2)
            Missile.Visible = True And (Missile.Top >= Shooter.Height + Shooter.Width) And Me.ClientRectangle.Width
        End If
    End Sub
    Private Sub FormMain_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        'Arrow Key Controls...
        If e.KeyValue = Keys.Right Then
            sright = False
        End If
        If e.KeyValue = Keys.Left Then
            sleft = False
        End If
        If e.KeyValue = Keys.Space Then
            sleft = False
            sright = False
        End If
    End Sub
    Private Sub FormMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Event On Form Load...
        LoadSettings()
        'Splash Screen Load 
        TriggerhappyLoad.ShowDialog()
    End Sub
    Private Sub LoadSettings()
        'Load Settings Speed Controls ...
        missilespeed = 10
        shooterspeed = 3
        EnemySpeed = 5
        ERight = True
        Missile.Visible = False
    End Sub
    Private Sub FormMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        'Do You Want To Quit?
        Dim result As MsgBoxResult
        result = MessageBox.Show("Do you really want to quit?", "Quit?", MessageBoxButtons.YesNo, MessageBoxIcon.Question)

        If result = MsgBoxResult.No Then
            e.Cancel = True
        End If
    End Sub

    Private Sub TimerMain_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerMain.Tick
        'Timer Main Variables...
        moveshooter()
        Firemissile()
        CheckHit()
        MoveEnemy()
    End Sub
    Private Sub moveshooter()
        'Shooter Timer Variables ...
        If sright = True And Shooter.Left + Shooter.Width < Me.ClientRectangle.Width Then
            Shooter.Left += shooterspeed
        End If
        If sleft = True And Shooter.Left > Me.ClientRectangle.Left Then
            Shooter.Left -= shooterspeed
        End If
    End Sub
    Private Sub Firemissile()
        'Missile Timer Variables ...
        If Missile.Visible = True Then
            Missile.Top -= missilespeed
        End If
        If Missile.Top + Missile.Height < Me.ClientRectangle.Top Then
            Missile.Visible = False
        End If
    End Sub
    Private Sub CheckHit()
        'Timer Main Collision Code ...
        If (Missile.Top + Missile.Height >= Enemy.Top) And (Missile.Top <= Enemy.Top + Enemy.Height) And (Missile.Left + Missile.Width >= Enemy.Left) And (Missile.Left <= Enemy.Left + Enemy.Width) And Enemy.Visible = True Then
            Enemy.Visible = False
            Missile.Visible = False
        End If
    End Sub
    Private Sub MoveEnemy()
        'Enemy Timer Variables ...
        If ERight = True Then
            Enemy.Left += EnemySpeed
        End If
        If Enemy.Left >= 900 Then
            Enemy.Left = -100
        End If
    End Sub
End Class
 
Last edited by a moderator:
For the bars you would use a ProgressBar. Set the Minimum to zero, the Maximum to the appropriate value (maybe 100 or maybe something else) and then set the Value to indicate how much of the bar should be filled.

Whenever you want to do anything random, you use the Random class. Create a single instance and call its Next method whenever you need a random number. You then use that number in whatever way is appropriate for your app. Often it will be as an index into an array or collection but, in your case, it would be for the Interval of a Timer to control how long until the next shot. You may have to Stop and Start the Timer, although I'm not sure.
 

Latest posts

Back
Top