Maze-+Enemy+Class


 * Below is a sample class for creating an enemy from my formative maze game. It is here, where we find the RECIPE for the ghost!! This tells the computer what the ghost does (speed, bullet shooting, movements). It also tells the computer when the ghost will shoot bullets, as well as what to do if it hits the walls. The ghost is our cookie, and the the class is the recipe.**

Public Class Ghost

' Creating a Picturebox Public pbxGhost As New PictureBox ' Creating a Timer Private tmrGTimer As New Timer ' Declaring a Speed Private intSpeed As Integer ' Creating a counter Private intCount As Integer = 0 ' Cause ghost to start moving. Private xMove As Integer = 5 Private yMove As Integer = 5 'Creating a list of Bullets.

Public myRand As New Random Private lstBullets As New List(Of Bullet)

Public Sub New Try Dim myRand As New Random pbxGhost.Image = Image.FromFile("ghost.jpg") pbxGhost.SizeMode = PictureBoxSizeMode.StretchImage pbxGhost.Height = 20 pbxGhost.Width = 20

Catch ex As Exception MsgBox("CANNOT FIND IMAGE!!" & ex.Message) End Try

tmrGTimer.Interval = 100 tmrGTimer.Start AddHandler tmrGTimer.Tick, AddressOf timeGhost

End Sub

Private Sub timeGhost

' Executed when timer ticks. If (pbxGhost.Location.X + pbxGhost.Width) >= frmLevel3.Width - 20 Then xMove = -5 ElseIf pbxGhost.Location.X <= 0 Then xMove = 5 ElseIf (pbxGhost.Location.Y + pbxGhost.Height) >= frmLevel3.Height - 20 Then yMove = -5 ElseIf pbxGhost.Location.Y <= 0 Then yMove = 5 End If

pbxGhost.Location = New Point(pbxGhost.Location.X + xMove, pbxGhost.Location.Y + yMove)

If intLevelNumb = 2 Then If pbxGhost.Bounds.IntersectsWith(frmLevel2.pbxElmo2.Bounds) Then tmrGTimer.Stop pbxGhost.Location = New Point(-1000, 0) pbxGhost.Dispose pbxGhost = Nothing MsgBox("You were caught!!!") End If End If

If intLevelNumb = 3 Then If pbxGhost.Bounds.IntersectsWith(frmLevel3.pbxElmo2.Bounds) Then tmrGTimer.Stop pbxGhost.Location = New Point(-1000, 0) pbxGhost.Dispose pbxGhost = Nothing MsgBox("You were caught!!!") End If End If

If intLevelNumb >= 3 Then

intCount += 1 If intCount Mod 50 = 0 Then Dim b As New Bullet(20) b = New Bullet(intCount Mod 20) b.pbxBullet.Location = pbxGhost.Location lstBullets.Add(b) frmLevel3.Controls.Add(b.pbxBullet) End If End If

If intCount = 100000 Then MsgBox("You took too long to finish.") tmrGTimer.Stop pbxGhost.Dispose pbxGhost = Nothing End If End Sub

End Class