game programming _ enemy copters

MrMk2gtx

Active member
Joined
Apr 1, 2011
Messages
29
Programming Experience
Beginner
Hey, guys

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
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,553
Location
Sydney, Australia
Programming Experience
10+
You should define a class that represents a single helicopter. That class would have properties for all the parameters that determine how a helicopter looks and behaves. You can then create multiple instances of that class and add them to an array or collection. For instance, you might have a Speed property and a Move method. The Move method would use the value of the Speed property to update the Location. You could then just loop through your list of helicopters and call Move on each one. Each Location property would update based on the Speed of that helicopter. Welcome to OOP.
 

MrMk2gtx

Active member
Joined
Apr 1, 2011
Messages
29
Programming Experience
Beginner
thanks :), please could you explain ina much simpler ways, it's just am new to visual basis. it would be great if you could do that thank you :)
 

MrMk2gtx

Active member
Joined
Apr 1, 2011
Messages
29
Programming Experience
Beginner
fault

[FONT=&quot]hey there,
i have almost finished making my game but theres a slight problem, the shooter fire the shot and the enemy gets hit and goes to a restart point but
when it's moving across the screen it dissapears when reaches he where the shooter is
it doesn't go past that point and the enemy just keeps going back to it's restart point.....HELP Pls
[/FONT]
VB.NET:
  [COLOR=blue][FONT=Consolas]Dim[/FONT][/COLOR][FONT=Consolas] SRight [COLOR=blue]AsBoolean
Dim[/COLOR] SLeft [COLOR=blue]AsBoolean
Dim[/COLOR] ShooterSpeed [COLOR=blue]AsInteger
Dim[/COLOR] shotspeed [COLOR=blue]AsInteger
PrivateSub[/COLOR] TimerMain_Tick([COLOR=blue]ByVal[/COLOR] sender [COLOR=blue]As[/COLOR] System.[COLOR=#2B91AF]Object[/COLOR], [COLOR=blue]ByVal[/COLOR] e [COLOR=blue]As[/COLOR] System.[COLOR=#2B91AF]EventArgs[/COLOR]) [COLOR=blue]Handles[/COLOR] TimerMain.Tick
MoveShooter()
FireShot()
CheckHit()
[COLOR=blue]EndSub
PrivateSub[/COLOR] Form1_KeyDown([COLOR=blue]ByVal[/COLOR] sender [COLOR=blue]As[/COLOR] System.[COLOR=#2B91AF]Object[/COLOR], [COLOR=blue]ByVal[/COLOR] e [COLOR=blue]As[/COLOR] System.Windows.Forms.[COLOR=#2B91AF]KeyEventArgs[/COLOR]) [COLOR=blue]HandlesMyBase[/COLOR].KeyDown
[COLOR=blue]If[/COLOR] e.KeyValue = [COLOR=#2B91AF]Keys[/COLOR].Right [COLOR=blue]Then
[/COLOR]SRight = [COLOR=blue]True
[/COLOR]SLeft = [COLOR=blue]False
EndIf
If[/COLOR] e.KeyValue = [COLOR=#2B91AF]Keys[/COLOR].Left [COLOR=blue]Then
[/COLOR]SLeft = [COLOR=blue]True
[/COLOR]SRight = [COLOR=blue]False
EndIf
If[/COLOR] e.KeyValue = [COLOR=#2B91AF]Keys[/COLOR].Space [COLOR=blue]And[/COLOR] Shot.Visible = [COLOR=blue]FalseThen
[/COLOR]Shot.Top = Shooter.Top
Shot.Left = Shooter.Left + (Shooter.Width / 2) - (Shot.Width / 2)
Shot.Visible = [COLOR=blue]True
EndIf
EndSub
PrivateSub[/COLOR] MoveShooter()
[COLOR=blue]If[/COLOR] SRight = [COLOR=blue]TrueAnd[/COLOR] Shooter.Left + Shooter.Width < [COLOR=blue]Me[/COLOR].ClientRectangle.Width [COLOR=blue]Then
[/COLOR]Shooter.Left += ShooterSpeed
[COLOR=blue]EndIf
If[/COLOR] SLeft = [COLOR=blue]TrueAnd[/COLOR] Shooter.Left > [COLOR=blue]Me[/COLOR].ClientRectangle.Left [COLOR=blue]Then
[/COLOR]Shooter.Left -= ShooterSpeed
[COLOR=blue]EndIf
EndSub
PrivateSub[/COLOR] Form1_KeyUp([COLOR=blue]ByVal[/COLOR] sender [COLOR=blue]As[/COLOR] System.[COLOR=#2B91AF]Object[/COLOR], [COLOR=blue]ByVal[/COLOR] e [COLOR=blue]As[/COLOR] System.Windows.Forms.[COLOR=#2B91AF]KeyEventArgs[/COLOR]) [COLOR=blue]HandlesMyBase[/COLOR].KeyUp
[COLOR=blue]If[/COLOR] e.KeyValue = [COLOR=#2B91AF]Keys[/COLOR].Right [COLOR=blue]Then
[/COLOR]SRight = [COLOR=blue]False
EndIf
If[/COLOR] e.KeyValue = [COLOR=#2B91AF]Keys[/COLOR].Left [COLOR=blue]Then
[/COLOR]SLeft = [COLOR=blue]False
EndIf
EndSub
PrivateSub[/COLOR] Form1_Load([COLOR=blue]ByVal[/COLOR] sender [COLOR=blue]As[/COLOR] System.[COLOR=#2B91AF]Object[/COLOR], [COLOR=blue]ByVal[/COLOR] e [COLOR=blue]As[/COLOR] System.[COLOR=#2B91AF]EventArgs[/COLOR]) [COLOR=blue]HandlesMyBase[/COLOR].Load
loadsettings()
[COLOR=blue]EndSub
PrivateSub[/COLOR] loadsettings()
shotspeed = 6
ShooterSpeed = 3
Shot.Visible = [COLOR=blue]False
EndSub
PrivateSub[/COLOR] FireShot()
[COLOR=blue]If[/COLOR] Shot.Visible = [COLOR=blue]TrueThen
[/COLOR]Shot.Top -= shotspeed
[COLOR=blue]EndIf
If[/COLOR] Shot.Top + Shot.Height < [COLOR=blue]Me[/COLOR].ClientRectangle.Top [COLOR=blue]Then
[/COLOR]Shot.Visible = [COLOR=blue]False
EndIf
EndSub
PrivateSub[/COLOR] Timer1_Tick([COLOR=blue]ByVal[/COLOR] sender [COLOR=blue]As[/COLOR] System.[COLOR=#2B91AF]Object[/COLOR], [COLOR=blue]ByVal[/COLOR] e [COLOR=blue]As[/COLOR] System.[COLOR=#2B91AF]EventArgs[/COLOR]) [COLOR=blue]Handles[/COLOR] Timer1.Tick
[COLOR=blue]If[/COLOR] Timer1.Enabled = [COLOR=blue]TrueThen
[/COLOR]Enemy.Left = Enemy.Left + 15
[COLOR=blue]EndIf
If[/COLOR] Enemy.Left >= [COLOR=blue]Me[/COLOR].ClientRectangle.Width [COLOR=blue]Then
[/COLOR]Enemy.Left = -50
[COLOR=blue]EndIf
EndSub
PrivateSub[/COLOR] Timer2_Tick([COLOR=blue]ByVal[/COLOR] sender [COLOR=blue]As[/COLOR] System.[COLOR=#2B91AF]Object[/COLOR], [COLOR=blue]ByVal[/COLOR] e [COLOR=blue]As[/COLOR] System.[COLOR=#2B91AF]EventArgs[/COLOR]) [COLOR=blue]Handles[/COLOR] Timer2.Tick
[COLOR=blue]If[/COLOR] Timer1.Enabled = [COLOR=blue]TrueThen
[/COLOR]Enemy1.Left = Enemy1.Left + 20
[COLOR=blue]EndIf
If[/COLOR] Enemy1.Left >= [COLOR=blue]Me[/COLOR].ClientRectangle.Width [COLOR=blue]Then
[/COLOR]Enemy1.Left = -50
[COLOR=blue]EndIf
EndSub
PrivateSub[/COLOR] Timer3_Tick([COLOR=blue]ByVal[/COLOR] sender [COLOR=blue]As[/COLOR] System.[COLOR=#2B91AF]Object[/COLOR], [COLOR=blue]ByVal[/COLOR] e [COLOR=blue]As[/COLOR] System.[COLOR=#2B91AF]EventArgs[/COLOR]) [COLOR=blue]Handles[/COLOR] Timer3.Tick
[COLOR=blue]If[/COLOR] Timer1.Enabled = [COLOR=blue]TrueThen
[/COLOR]Enemy2.Left = Enemy2.Left + 20
[COLOR=blue]EndIf
If[/COLOR] Enemy2.Left >= [COLOR=blue]Me[/COLOR].ClientRectangle.Width [COLOR=blue]Then
[/COLOR]Enemy2.Left = -50
[COLOR=blue]EndIf
EndSub
PrivateSub[/COLOR] Timer4_Tick([COLOR=blue]ByVal[/COLOR] sender [COLOR=blue]As[/COLOR] System.[COLOR=#2B91AF]Object[/COLOR], [COLOR=blue]ByVal[/COLOR] e [COLOR=blue]As[/COLOR] System.[COLOR=#2B91AF]EventArgs[/COLOR]) [COLOR=blue]Handles[/COLOR] Timer4.Tick
[COLOR=blue]If[/COLOR] Timer1.Enabled = [COLOR=blue]TrueThen
[/COLOR]Enemy3.Left = Enemy3.Left + 15
[COLOR=blue]EndIf
If[/COLOR] Enemy3.Left >= [COLOR=blue]Me[/COLOR].ClientRectangle.Width [COLOR=blue]Then
[/COLOR]Enemy3.Left = -50
[COLOR=blue]EndIf
EndSub
PrivateSub[/COLOR] Timer5_Tick([COLOR=blue]ByVal[/COLOR] sender [COLOR=blue]As[/COLOR] System.[COLOR=#2B91AF]Object[/COLOR], [COLOR=blue]ByVal[/COLOR] e [COLOR=blue]As[/COLOR] System.[COLOR=#2B91AF]EventArgs[/COLOR]) [COLOR=blue]Handles[/COLOR] Timer5.Tick
[COLOR=blue]If[/COLOR] Timer1.Enabled = [COLOR=blue]TrueThen
[/COLOR]Enemy4.Left = Enemy4.Left + 20
[COLOR=blue]EndIf
If[/COLOR] Enemy4.Left >= [COLOR=blue]Me[/COLOR].ClientRectangle.Width [COLOR=blue]Then
[/COLOR]Enemy4.Left = -50
[COLOR=blue]EndIf
EndSub
PrivateSub[/COLOR] CheckHit()
[COLOR=green]'Collision Code enemy
[/COLOR][COLOR=blue]If[/COLOR] Shot.Top < Enemy.Top + Enemy.Height [COLOR=blue]And[/COLOR] Shot.Left > Enemy.Left [COLOR=blue]And[/COLOR] Shot.Left < Enemy.Left + Enemy.Width [COLOR=blue]Then
[/COLOR]Enemy.Visible = [COLOR=blue]False
[/COLOR]Enemy.Left = -100
Enemy.Visible = [COLOR=blue]True
EndIf
[/COLOR][COLOR=green]'Collision Code enemy1
[/COLOR][COLOR=blue]If[/COLOR] Shot.Top < Enemy1.Top + Enemy1.Height [COLOR=blue]And[/COLOR] Shot.Left > Enemy1.Left [COLOR=blue]And[/COLOR] Shot.Left < Enemy1.Left + Enemy1.Width [COLOR=blue]Then
[/COLOR]Enemy1.Visible = [COLOR=blue]False
[/COLOR]Enemy1.Left = -100
Enemy1.Visible = [COLOR=blue]True
EndIf
[/COLOR][COLOR=green]'Collision Code enemy2
[/COLOR][COLOR=blue]If[/COLOR] Shot.Top < Enemy2.Top + Enemy2.Height [COLOR=blue]And[/COLOR] Shot.Left > Enemy2.Left [COLOR=blue]And[/COLOR] Shot.Left < Enemy2.Left + Enemy2.Width [COLOR=blue]Then
[/COLOR]Enemy2.Visible = [COLOR=blue]False
[/COLOR]Enemy2.Left = -100
Enemy2.Visible = [COLOR=blue]True
EndIf
[/COLOR][COLOR=green]'Collision Code enemy3
[/COLOR][COLOR=blue]If[/COLOR] Shot.Top < Enemy3.Top + Enemy3.Height [COLOR=blue]And[/COLOR] Shot.Left > Enemy3.Left [COLOR=blue]And[/COLOR] Shot.Left < Enemy3.Left + Enemy3.Width [COLOR=blue]Then
[/COLOR]Enemy3.Visible = [COLOR=blue]False
[/COLOR]Enemy3.Left = -100
Enemy3.Visible = [COLOR=blue]True
EndIf
[/COLOR][COLOR=green]'Collision Code enemy
[/COLOR][COLOR=blue]If[/COLOR] Shot.Top < Enemy4.Top + Enemy4.Height [COLOR=blue]And[/COLOR] Shot.Left > Enemy4.Left [COLOR=blue]And[/COLOR] Shot.Left < Enemy4.Left + Enemy4.Width [COLOR=blue]Then
[/COLOR]Enemy4.Visible = [COLOR=blue]False
[/COLOR]Enemy4.Left = -100
Enemy4.Visible = [COLOR=blue]True
EndIf
EndSub
EndClass[/COLOR][/FONT]
[FONT=&quot]
[/FONT]
 
Top Bottom