You are on page 1of 4

EZ Invaders

Public Class Form1


Dim SRight As Boolean
Dim SLeft As Boolean
Dim ShooterSpeed As Integer
Dim ShotSpeed As Integer
Dim InvaderSpeed As Integer
Dim InvaderDrop As Integer
Const NumOfInvaders As Integer = 10
Dim IRight(NumOfInvaders) As Boolean
Dim Invaders(NumOfInvaders) As PictureBox
Dim x As Integer
Dim ShotDown As Integer
Dim Paused As Boolean
'Tristan Zarin'

Private Sub TimerMain_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles TimerMain.Tick
MoveShooter()
FireShot()
MoveInvader()
CheckHit()
CheckGameOver()
End Sub

Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As


System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
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.Space And Shot.Visible = False Then
Shot.Top = Shooter.Top
Shot.Left = Shooter.Left + (Shooter.Width / 2) - (Shot.Width / 2)
Shot.Visible = True
End If
End Sub
Private Sub MoveShooter()
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 Form1_KeyUp(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
If e.KeyValue = Keys.Right Then
SRight = False
End If
If e.KeyValue = Keys.Left Then
SLeft = False
End If
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles MyBase.Load
LoadInvaders()
LoadSettings()

End Sub

Private Sub LoadSettings()


Paused = False
ShotSpeed = 10
ShooterSpeed = 3
Shot.Visible = False
For Me.x = 1 To NumOfInvaders
IRight(x) = True
Invaders(x).Left = (-50 * x) - (x * 5)
Invaders(x).Top = 0
Invaders(x).Visible = True
Next
InvaderSpeed = 3
InvaderDrop = 50
ShotDown = 0
SRight = False
SLeft = False
TimerMain.Enabled = True
End Sub

Private Sub FireShot()


If Shot.Visible = True Then
Shot.Top -= ShotSpeed
End If
If Shot.Top + Shot.Height < Me.ClientRectangle.Top Then
Shot.Visible = False
End If
End Sub

Private Sub MoveInvader()


For Me.X = 1 To NumOfInvaders
If IRight(x) = True Then
Invaders(x).Left += InvaderSpeed
Else
Invaders(x).Left -= InvaderSpeed
End If

If Invaders(x).Left + Invaders(x).Width > Me.ClientRectangle.Width And IRight(x) = True


Then
IRight(x) = False
Invaders(x).Top += InvaderDrop
End If
If Invaders(x).Left < Me.ClientRectangle.Left And IRight(x) = False Then
IRight(x) = True
Invaders(x).Top += InvaderDrop
End If
Next
End Sub

Private Sub CheckGameOver()


For Me.x = 1 To NumOfInvaders
If Invaders(x).Top + Invaders(x).Height >= Shooter.Top And Invaders(x).Visible = True
Then
TimerMain.Enabled = False
Me.x = NumOfInvaders
MsgBox("GAME OVER - EARTH HAS BEEN INVADED")
PlayAgain()
End If

Next
If ShotDown = NumOfInvaders Then
TimerMain.Enabled = False
MsgBox("EARTH IS SAVED!")
PlayAgain()
End If
End Sub

Private Sub CheckHit()


For Me.x = 1 To NumOfInvaders
If (Shot.Top + Shot.Height >= Invaders(x).Top) And (Shot.Top <= Invaders(x).Top +
Invaders(x).Height) And (Shot.Left + Shot.Width >= Invaders(x).Left) And (Shot.Left <=
Invaders(x).Left + Invaders(x).Width) And Shot.Visible = True And Invaders(x).Visible = True Then
Invaders(x).Visible = False

Shot.Visible = False
ShotDown += 1
End If
Next

End Sub

Private Sub LoadInvaders()


For Me.x = 1 To NumOfInvaders
Invaders(x) = New PictureBox
Invaders(x).Image = My.Resources.Aliens
Invaders(x).Width = 50
Invaders(x).Height = 50
Invaders(x).BackColor = Color.Transparent

Controls.Add(Invaders(x))
Next
End Sub

Private Sub Form1_KeyPress(ByVal sender As System.Object, ByVal e As


System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
If e.KeyChar = "P" Or e.KeyChar = "p" Then
If Paused = True Then
TimerMain.Enabled = True
Paused = False
Else
TimerMain.Enabled = False
Paused = True
End If
End If
End Sub

Private Sub PlayAgain()


Dim Result = MsgBox("Play Again?", MsgBoxStyle.YesNo)

If Result = MsgBoxResult.Yes Then


LoadSettings()
Else
Me.Close()
End If
End Sub
End Class

You might also like