Question Random Event ... Timer Trigger?

PRo-Beaniie

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

As you may have read in my other posts i am creating a game in VB.NET for my First Diploma college course;) ... Im just putting the finishing touches on the game now. However Thought to myslef why not make te game that little bit more interesting... i have a genral idea of what i want to happen but have no knowledge of Random events...:confused:

Basically what i have is three enemys (helicopters) going across the screen to the Right. when fired appon they recycle back to the starting position. THey do the same if they reach the end of the form also, What i am hoping for is to learn some code that could make the enemy projectile fall from the Enemys Randomly.

Like i said i have no knowledge of random events and i am hoping you guys could enlighten me. I think i have the code for the Enemyprojectile to fall from the Enemy as shown below...

VB.NET:
    Private Sub DropMissilePosition()
        For Me.x = 1 To NumOfEnemy
            If Enemy(x).Left = Me.ClientRectangle.Left + 250 And EnemyMissile.Visible = False Then
                EnemyMissile.Top = Enemy(x).Top
                EnemyMissile.Left = Enemy(x).Left + (Enemy(x).Width / 2) - (EnemyMissile.Width / 2)
                EnemyMissile.Visible = True And (EnemyMissile.Top >= Enemy(x).Height + Enemy(x).Width) And Me.ClientRectangle.Width
            End If
        Next
    End Sub
    Private Sub DropMissile()
        'EnemyMissile Timer Variables ...
        If EnemyMissile.Visible = True Then
            EnemyMissile.Top += enemymissilespeed
        End If
        If EnemyMissile.Top + EnemyMissile.Height < Me.ClientRectangle.Top Then
            EnemyMissile.Visible = False
        End If
    End Sub

If you could help it would be much appreciated. ;)

Thanks,
 
Simply for the purposes of choosing when to launch.
You need to generate a random number, either using a Random object or using the Rnd function. You could

1) Create a random number (per enemy) at some point the helicopter will reach (if not shot down) and tell the move function to dropMissile() if that point is reached. Similarly you could have a timer with it's tick time set to a random value between 0 and the time taken for a helicopter to cross the screen. Change the random number every time the helicopter is shot down or passes the end. This would only really work if each helicopter has one shot.

2) Create a random number every time it moves. If this is within a range you decide on, it fires a missile. For example
VB.NET:
...
if rnd() < 0.01 then
launchEnemyMissile()
end if 
...
would have a 1% chance of dropping a missile every time you move the helicopter. You can tweak the actual value depending on how frequently you want them to fire. Of course you'd want to check that the helicopter in question didn't already have a shot on the screen.

3) be a real brat to the player and actually have the helicopters aim to hit you.
 
That would depend on what strategy you wanted to use.
You could create a second timer if you wanted to use the timer technique. This has the advantage of not checking either the random variable or the target distance code every time your helicopter moves.

If you went for the target position technique you'd need to declare a new integer variable to hold the target position.

If you went for the repeated checking code I suggested there aren't any additional things to declare.

In this case I'd probably go for one of the first two, but each strategy is suitable for different situations.
 
Then I suggest that you start by googling "VB.net Random" (no quotes). That will help you to generate a random whole number between two boundaries.

Generate such a number for each ship when it starts (or restarts after being shot or reaching the end) flying. Store these as integers in an array.

Tell your helicopter to drop a missile when it reaches that number, because it will be a random number, it will drop from a different place for each ship and each time, and look like the computer is deciding when to fire.
 
Hey

I am creating a game in VB.NET as well for my sixthform course and i ve just started adding helicopters to the top of form. i have added one enemy copter which is working very well, but when i have no idea how to add 1. more enemy copters, make them move in the same direction, and add the collision code.

Basically what i have is 5 enemy copters 1 going across the screen to the Right and the other 4 enemy copters just staying there. what am hoping to do is to learn some code that could make the all enemy copters move to the right at different speeds.

Dim SRight As Boolean
Dim SLeft As Boolean
Dim Shooterspeed As Integer
Dim shotspeed As Integer
Dim Enemycopterspeed As Integer
Dim ERight As Boolean
Dim ELeft As Boolean
Private Sub TimerMain_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerMain.Tick
Moveshooter()
fireshot()
CheckHit()
MoveEnemycopter()
End Sub
Private Sub LoadSettings()
shotspeed = 10
Shooterspeed = 6
Enemycopterspeed = 5
ERight = True
Shot.Visible = False
End Sub
Private Sub CheckHit()
If (Shot.Top + Shot.Height >= Enemycopter.Top) And (Shot.Top <= Enemycopter.Top + Enemycopter.Height) And (Shot.Left + Shot.Width >= Enemycopter.Left) And (Shot.Left <= Enemycopter.Left + Enemycopter.Width) And Enemycopter.Visible = True Then
Enemycopter.Visible = False
Shot.Visible = False
End If
End Sub
Private Sub MoveEnemycopter()
If ERight = True Then
Enemycopter.Left += EnemycopterSpeed
End If
If Enemycopter.Left >= 900 Then
Enemycopter.Left = -100
End If
End Sub
End Class
 
Arrays & Timers

I have applied the method you asked about in that game previously, however it was impractical. Im not saying it cant be done, it can be done but its alot of code and its ample amounts of copy & Pasting so ill explain what you would need to achieve your goal... (I will upload my previous code for this soon but im not by my home PC .)

(This is my original method and this is the only way i knew how to do this... )
You will need a timer for each Enemy, Within each timer you need to set the enemy at a different speed. Copy this into each timer and replace the (X) with the number of the enemy (Enemy1,Enemy2,Enemy3, Enemy4)
for example

VB.NET:
'Enemy speed and reset start.
If timer1.enabled = true then 
enemy(X).left = enemy(X).left + 10 
End if 
If enemy (X).left >= me.clientrectangle.width then 
Enemy(X).left = - 50
This is the collision code this code will be applied to each of your enemys (Copied 4 times )...
for example ...
Enemy 1
Enemy 2
Enemy 3
Enemy 4
Replace the (x) with the number...

VB.NET:
' Collision Code ...
            If (Shot.Top + Shot.Height >= Enemy(x).Top) And (Shot.Top <= Enemy(x).Top + Enemy(x).Height) And (Shot.Left + Shot.Width >= Enemy(x).Left) And (Shot.Left <= Enemy(x).Left + Enemy(x).Width) And Enemy(x).Visible = True Then
                
                Enemy(x).Visible = False
                Enemy(x).Top = -100
Im not very good at explaining but i hope you understand if you have any questions just ask :)
P.s When ever referrreing to code on the forum please insert code Tags ... Thanks :)

(CODE)
(/CODE)
[] use <<< These Brackets.
 
Last edited:
Arrays & Timers

I have applied the method you asked about in that game previously, however it was impractical. Im not saying it cant be done, it can be done but its alot of code and its ample amounts of copy & Pasting so ill explain what you would need to achieve your goal...

(This is my original method and this is the only way i knew how to do this... )
You will need a timer for each Enemy, Within each timer you need to set the enemy at a different speed.
for example

VB.NET:
'Enemy speed and reset start.
If timer1.enabled = true then 
enemy1.left = enemy1.left + 10 
End if 
If enemy 1.left >= me.clientrectangle.width then 
Enemy1.left = - 50

This is the collision code this code will be applied to each of your enemys (Copied 4 times )...
for example ...
Enemy 1
Enemy 2
Enemy 3
Enemy 4
Replace the (x) with the number...

VB.NET:
' Collision Code ...
            If (Shot.Top + Shot.Height >= Enemy(x).Top) And (Shot.Top <= Enemy(x).Top + Enemy(x).Height) And (Shot.Left + Shot.Width >= Enemy(x).Left) And (Shot.Left <= Enemy(x).Left + Enemy(x).Width) And Enemy(x).Visible = True Then
                
                Enemy(x).Visible = False
                Enemy(x).Top = -100

Im not vary good at explaining but i hope you understand if you have any questions just ask :)
 
pls where do you put the collision code for the enemy2,3,4
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Timer1.Enabled = True Then
Enemy1.Left = Enemy1.Left + 10
End If
If Enemy1.Left >= Me.ClientRectangle.Width Then
Enemy1.Left = -50
End If
End Sub
Private Sub CheckHit()
If (Shot.Top + Shot.Height >= enemy1.Top) And (Shot.Top <= enemy1.Top + enemy1.Height) And (Shot.Left + Shot.Width >= enemy1.Left) And (Shot.Left <= enemy1.Left + enemy1.Width) And enemy1.Visible = True Then
enemy1.Visible = False
enemy1.Top = -100
End If
End Sub
End Class

i have done this and its comming up with an error
Error 1 'Private Sub CheckHit()' has multiple definitions with identical signatures. E:\MATT DAVIS\College\WindowsApplication1\WindowsApplication1\Form1.vb 90 17 WindowsApplication1


 
you do not need to create any other private subs you simply past it within the check hit. here is my original code from my previous project. In this project i have three rows of helicopters scrolling across the screen from left to right. This is broken into three timers...

like this

VB.NET:
'timer 1
 Private Sub Row1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TMRRow1.Tick
        'Top Helicopter Row
        heli1.Left = heli1.Left + 40
        heli4.Left = heli4.Left + 40

        If heli1.Left > 1400 Then
            heli1.Left = -100
        End If

        If heli4.Left > 1400 Then
            heli4.Left = -100
        End If
    End Sub
'Timer 2
    Private Sub Row2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TMRRow2.Tick
        'Middle Helicopter Row
        heli2.Left = heli2.Left + 25
        heli5.Left = heli5.Left + 25
        heli6.Left = heli6.Left + 25

        If heli2.Left > 1400 Then
            heli2.Left = -100
        End If

        If heli5.Left > 1400 Then
            heli5.Left = -100
        End If

        If heli6.Left > 1400 Then
            heli6.Left = -100
        End If
    End Sub
'timer 3
    Private Sub Row3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TMRRow3.Tick
        'Bottom Helicopter Row
        heli3.Left = heli3.Left + 30
        heli7.Left = heli7.Left + 30

        If heli3.Left > 1400 Then
            heli3.Left = -100
        End If

        If heli7.Left = heli7.Left > 1400 Then
            heli7.Left = heli7.Left < -100
        End If
    End Sub
The collision code is compsed as such... you would enter this code under your private Sub "Check Hit()" < your error is that you are declaring this sub multiple times which makes VB confused ....

VB.NET:
        'Collision Code heli 1
        If Missile.Top < heli1.Top + heli1.Height And Missile.Left > heli1.Left And Missile.Left < heli1.Left + heli1.Width Then
            heli1.Visible = False
        End If
        'Collision Code heli 2
        If Missile.Top < heli2.Top + heli2.Height And Missile.Left > heli2.Left And Missile.Left < heli2.Left + heli2.Width Then
            heli2.Visible = False
        End If
        'Collision Code heli 3
        If Missile.Top < heli3.Top + heli3.Height And Missile.Left > heli3.Left And Missile.Left < heli3.Left + heli3.Width Then

            heli3.Visible = False
        End If
        'Collision Code heli 4
        If Missile.Top < heli4.Top + heli4.Height And Missile.Left > heli4.Left And Missile.Left < heli4.Left + heli4.Width Then
            heli4.Visible = False
        End If
        'Collision Code heli 5
        If Missile.Top < heli5.Top + heli5.Height And Missile.Left > heli5.Left And Missile.Left < heli5.Left + heli5.Width Then
            heli5.Visible = False
        End If
        'Collision Code heli 6
        If Missile.Top < heli6.Top + heli6.Height And Missile.Left > heli6.Left And Missile.Left < heli6.Left + heli6.Width Then
            heli6.Visible = False
        End If
        'Collision Code heli 7
        If Missile.Top < heli7.Top + heli7.Height And Missile.Left > heli7.Left And Missile.Left < heli7.Left + heli7.Width Then
            heli7.Visible = False
        End If
        
    End Sub
Try that and everything should be dandy :)
 
now i ve just gotta make the copter restart once they are shot down by the missle
i have tried using the code you posted earlier
i wrote it under this

PrivateSub Row1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TMRRow1.Tick
Enemy1.Left = Enemy1.Left + 40
Enemy4.Left = Enemy4.Left + 40
If Enemy1.Left > 1400 Then
Enemy1.Left = -100
EndIf
If Enemy4.Left > 1400 Then
Enemy4.Left = -100
EndIf
EndSub


but nothing happpend all the copters got sshot but didn't restart they just dissapeared
Helppppppp :confused:
 
Back
Top