﻿Public Class frmPacMan
    'Mike Guyadeen
    Public Enum MovementDirection
        Up
        Down
        Left
        Right
    End Enum
    Public Enum PacMove
        Up
        Down
        Left
        Right
        None
    End Enum
    Dim GhostCollection As New List(Of PictureBox)
    Dim WallCollection As New List(Of PictureBox)
    Dim FoodCollection As New List(Of PictureBox)
    Dim BigFoodCollection As New List(Of PictureBox)
    Dim FruitCollection As New List(Of PictureBox)
    Dim Direction As MovementDirection
    Dim PacDirection As PacMove
    Dim PacLastDirection As Integer
    Dim SpriteLocation As Point = Nothing
    Dim CurrentLocation As Point
    Dim GhostCurrentLocation As Point
    Dim CurrentSprite As Integer = 0
    Dim Random As New Random
    Dim ShoeCollected As Boolean
    Dim PotionCollected As Boolean
    Dim EdibleGhost As Boolean
    Dim ReleaseGhost As Boolean
    Dim CrazyTime As Integer
    Dim CrazyTimeLeft As Integer
    Dim CrazyTimeCountdown As Integer
    Dim CrazyTimeCountdownLeft As Integer
    Dim FastTime As Integer
    Dim FastTimeLeft As Integer
    Dim FastTimeCountdown As Integer
    Dim FastTimeCountdownLeft As Integer
    Dim SpawnFruitTimeCountdown As Integer
    Dim SpawnFruitTimeCountdownLeft As Integer
    Dim EdibleGhostTime As Integer
    Dim EdibleGhostTimeLeft As Integer
    Dim GhostActivity As Boolean
    Dim GAMEOVER As Boolean
    Dim ReleaseFruit As Boolean
    Dim FruitEaten As Boolean
    Dim FoodEaten As Boolean
    Dim PacManLives As Integer
    Dim SpawnFruitTime As Integer
    Dim SpawnFruitTimeLeft As Integer
    Dim Xaxis As Integer
    Dim Yaxis As Integer
    Dim SpawnFastTime As Boolean
    Dim SpawnCrazyTime As Boolean
    Dim FoodCounter As Integer
    Private Sub frmPacMan_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown

        Call CheckPacmanDirection()

        If e.KeyCode = Keys.Escape Then
            Application.Exit()
        End If

        If e.KeyCode = Keys.W Then
            PacDirection = PacMove.Up
            picPacman.Image = My.Resources.Up2
        ElseIf e.KeyCode = Keys.S Then
            PacDirection = PacMove.Down
            picPacman.Image = My.Resources.Down2
        ElseIf e.KeyCode = Keys.A Then
            PacDirection = PacMove.Left
            picPacman.Image = My.Resources.Left2
        ElseIf e.KeyCode = Keys.D Then
            PacDirection = PacMove.Right
            picPacman.Image = My.Resources.Right2
        End If
    End Sub
    Private Sub frmPacMan_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        picPacman.Focus()
    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Me.DoubleBuffered = True
    End Sub
    Private Sub tmrghostdir_Tick(sender As Object, e As EventArgs) Handles tmrGhostDir.Tick
        Me.DoubleBuffered = True
        Call CheckDirection()
    End Sub
    Private Sub tmrPacmanDir_Tick(sender As Object, e As EventArgs) Handles tmrPacmanDir.Tick
        Me.DoubleBuffered = True
        Call CheckPacmanDirection()
    End Sub
    Private Sub tmrFastTime_Tick(sender As Object, e As EventArgs) Handles tmrFastTime.Tick
        FastTime = lblFastTime.Text
        FastTimeLeft = 1
        lblFastTime.Text = FastTime - FastTimeLeft
        If lblFastTime.Text = "0" Then
            tmrFastTime.Stop()
            ShoeCollected = False
            SpawnFastTime = False
        End If

    End Sub

    Private Sub tmrFastTimeCountdown_Tick(sender As Object, e As EventArgs) Handles tmrFastTimeCountdown.Tick
        FastTimeCountdown = lblCountdownToFastTime.Text
        FastTimeCountdownLeft = 1
        lblCountdownToFastTime.Text = FastTimeCountdown - FastTimeCountdownLeft
        If lblCountdownToFastTime.Text = "0" Then
            tmrFastTimeCountdown.Stop()
            SpawnFastTime = True
        End If
    End Sub
    Private Sub tmrCrazyTime_Tick(sender As Object, e As EventArgs) Handles tmrCrazyTime.Tick
        CrazyTime = lblCrazyTime.Text
        CrazyTimeLeft = 1
        lblCrazyTime.Text = CrazyTime - CrazyTimeLeft
        If lblCrazyTime.Text = "0" Then
            tmrFastTime.Stop()
            PotionCollected = False
            SpawnCrazyTime = False
        End If
    End Sub

    Private Sub tmrCrazyTimeCountdown_Tick(sender As Object, e As EventArgs) Handles tmrCrazyTimeCountdown.Tick
        CrazyTimeCountdown = lblCountdownToCrazyTime.Text
        CrazyTimeCountdownLeft = 1
        lblCountdownToCrazyTime.Text = CrazyTime - CrazyTimeLeft
        If lblCountdownToCrazyTime.Text = "0" Then
            tmrCrazyTimeCountdown.Stop()
            SpawnCrazyTime = True
        End If
    End Sub
    Private Sub tmrSpawnFruitTime_Tick(sender As Object, e As EventArgs) Handles tmrSpawnFruitTime.Tick
        SpawnFruitTime = lblSpawnFruitTime.Text
        SpawnFruitTimeLeft = 1
        lblSpawnFruitTime.Text = SpawnFruitTime - SpawnFruitTimeLeft
        If lblSpawnFruitTime.Text = "0" Then
            Call ReleaseAllFruit()
            tmrSpawnFruitTime.Stop()
            tmrSpawnFruitTimeCountdown.Start()
        End If

    End Sub
    Private Sub tmrSpawnFruitTimeCountdown_Tick(sender As Object, e As EventArgs) Handles tmrSpawnFruitTimeCountdown.Tick
        SpawnFruitTimeCountdown = lblSpawnFruitTimeCountdown.Text
        SpawnFruitTimeCountdownLeft = 1
        lblSpawnFruitTimeCountdown.Text = SpawnFruitTimeCountdown - SpawnFruitTimeCountdownLeft
        If lblSpawnFruitTimeCountdown.Text = "0" Then
            tmrSpawnFruitTimeCountdown.Stop()
        End If
    End Sub
    Sub CheckDirection()
        Call DoGhosts()
        If EdibleGhost = False Then
            If Direction = MovementDirection.Down Then
                GhostRed.Image = My.Resources.RedDown
                GhostRed.Top += 2
            ElseIf Direction = MovementDirection.Up Then
                GhostRed.Image = My.Resources.RedUp
                GhostRed.Top -= 2
            ElseIf Direction = MovementDirection.Left Then
                GhostRed.Image = My.Resources.RedLeft
                GhostRed.Left -= 2
            ElseIf MovementDirection.Right Then
                GhostRed.Image = My.Resources.RedRight
                GhostRed.Left += 2
            End If
            DetectCollection(GhostRed)
        End If
        If EdibleGhost = True Then
            If Direction = MovementDirection.Down Then
                GhostActivity = True
                GhostRed.Image = My.Resources.Edible
                GhostRed.Top += 2
            ElseIf Direction = MovementDirection.Up Then
                GhostActivity = True
                GhostRed.Image = My.Resources.Edible
                GhostRed.Top -= 2
            ElseIf Direction = MovementDirection.Left Then
                GhostActivity = True
                GhostRed.Image = My.Resources.Edible
                GhostRed.Left -= 2
            ElseIf MovementDirection.Right Then
                GhostActivity = True
                GhostRed.Image = My.Resources.Edible
                GhostRed.Left += 2
            End If
            DetectCollection(GhostRed)
        End If
    End Sub
    Sub CheckPacmanDirection()
        Call CheckWall()
        Call HideFood()
        Call HideBigFood()
        Call GoFaster()
        Call GoCrazy()
        Call DoFruit()
        Call DoGhosts()
        If ShoeCollected = False Or PotionCollected = False Then
            If PacDirection = PacMove.Up Then
                picPacman.Top -= 3
            ElseIf PacDirection = PacMove.Down Then
                picPacman.Top += 3
            ElseIf PacDirection = PacMove.Left Then
                picPacman.Left -= 3
                If picPacman.Location.X < 253 Then
                    picPacman.Location = New Point(1025, 325)
                    PacDirection = PacMove.Left
                End If
            ElseIf PacDirection = PacMove.Right Then
                picPacman.Left += 3
                If picPacman.Location.X > 1025 Then
                    picPacman.Location = New Point(253, 325)
                    PacDirection = PacMove.Right
                End If
            End If
        End If
        If ShoeCollected = True Then
            If PacDirection = PacMove.Up Then
                picPacman.Top -= 5
            ElseIf PacDirection = PacMove.Down Then
                picPacman.Top += 5
            ElseIf PacDirection = PacMove.Left Then
                picPacman.Left -= 5
                If picPacman.Location.X < 253 Then
                    picPacman.Location = New Point(1025, 325)
                    PacDirection = PacMove.Left
                End If
            ElseIf PacDirection = PacMove.Right Then
                picPacman.Left += 5
                If picPacman.Location.X > 1025 Then
                    picPacman.Location = New Point(253, 325)
                    PacDirection = PacMove.Right
                End If
            End If
        End If
        If PotionCollected = True Then
            If PacDirection = PacMove.Up Then
                picPacman.Image = My.Resources.Down2
                picPacman.Top += 4
            ElseIf PacDirection = PacMove.Down Then
                picPacman.Image = My.Resources.Up2
                picPacman.Top -= 4
            ElseIf PacDirection = PacMove.Left Then
                picPacman.Image = My.Resources.Right2
                picPacman.Left += 4
                If picPacman.Location.X < 253 Then
                    picPacman.Location = New Point(1025, 325)
                    PacDirection = PacMove.Left
                End If
            ElseIf PacDirection = PacMove.Right Then
                picPacman.Image = My.Resources.Left2
                picPacman.Left -= 4
                If picPacman.Location.X > 1025 Then
                    picPacman.Location = New Point(253, 325)
                    PacDirection = PacMove.Right
                End If
            End If
        End If
    End Sub
    Sub CheckWall()
        For Each wall As PictureBox In WallCollection
            If picPacman.Bounds.IntersectsWith(wall.Bounds) Then
                picPacman.Location = New Point(CurrentLocation.X, CurrentLocation.Y)
            End If
        Next
        CurrentLocation = picPacman.Location
    End Sub
    Sub HideFood()
        Me.DoubleBuffered = True
        For Each food As PictureBox In FoodCollection
            If picPacman.Bounds.IntersectsWith(food.Bounds) Then
                food.Left += 1000000
                FoodEaten = True
            End If
        Next
    End Sub
    Sub HideBigFood()
        For Each bigfood As PictureBox In BigFoodCollection
            If picPacman.Bounds.IntersectsWith(bigfood.Bounds) Then
                bigfood.Left += 1000000
                EdibleGhost = True
                tmrEdibleGhostTimer.Start()
                tmrMakeGhostBlink.Start()
                FoodEaten = True
            End If
        Next
    End Sub
    Sub DoFruit()

        For Each fruit As PictureBox In FruitCollection
            If picPacman.Bounds.IntersectsWith(fruit.Bounds) Then
                fruit.Visible = False
                FruitEaten = True
                PacManLives += 1
            End If
        Next
    End Sub
    Sub GoFaster()
        If SpawnFastTime = True Then
            picShoe.Size = New Size(28, 28)
            picShoe.Location = New Point(462, 502)
        End If
        If picPacman.Bounds.IntersectsWith(picShoe.Bounds) Then
            ShoeCollected = True
            picShoe.Hide()
            tmrFastTime.Start()
        End If
    End Sub
    Sub GoCrazy()
        If SpawnCrazyTime = True Then
            picPotion.Size = New Size(28, 28)
            picPotion.Location = New Point(392, 124)
        End If
        If picPacman.Bounds.IntersectsWith(picPotion.Bounds) Then
            PotionCollected = True
            picPotion.Hide()
            tmrCrazyTime.Start()
        End If
    End Sub
    Sub DoGhosts()
        For Each ghost In GhostCollection
            Call HideBigFood()
            If EdibleGhost = False Then
                If ghost.Bounds.IntersectsWith(picPacman.Bounds) Then
                    PacManLives -= 1
                    Console.Beep()
                    System.Threading.Thread.Sleep(1000)
                    picPacman.Location = New Point(647, 392)
                    PacDirection = PacMove.Right
                End If
            End If
            If EdibleGhost = True Then
                If ghost.Bounds.IntersectsWith(picPacman.Bounds) Then
                    GhostRed.Location = New Point(647, 260)
                End If
            End If
        Next
    End Sub
    Sub ReleaseAllFruit()
        If lblSpawnFruitTime.Text = "0" Then
            ReleaseFruit = True
            If ReleaseFruit = True Then
                picFruit1.Size = New Size(28, 28)
                picFruit1.Location = New Point(764, 35)
            End If
        End If
    End Sub
    Private Function DetectCollection(ByVal ghost As PictureBox) As Boolean
        For Each Wall As PictureBox In WallCollection
            If ghost.Bounds.IntersectsWith(Wall.Bounds) Or ghost.Bounds.IntersectsWith(picBlockLeftPathway.Bounds) Or ghost.Bounds.IntersectsWith(picBlockRightPathway.Bounds) Then
                ghost.Location = GhostCurrentLocation
                Direction = Rnd() * 4 + 1
            End If
        Next
        GhostCurrentLocation = ghost.Location
    End Function
    Private Sub frmPacMan_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.DoubleBuffered = True

        Randomize()
        FoodCounter = 1
        PacDirection = PacMove.None
        PacManLives = 1
        ShoeCollected = False
        PotionCollected = False

        For Each myControl As Control In Me.Controls
            If TypeOf myControl Is PictureBox Then
                If myControl.Name.StartsWith("Ghost") Then
                    GhostCollection.Add(myControl)
                ElseIf myControl.Name.StartsWith("picWall") Then
                    WallCollection.Add(myControl)
                ElseIf myControl.Name.StartsWith("picFood") Then
                    FoodCollection.Add(myControl)
                ElseIf myControl.Name.StartsWith("picBig") Then
                    BigFoodCollection.Add(myControl)
                ElseIf myControl.Name.StartsWith("picFruit") Then
                    FruitCollection.Add(myControl)
                End If
            End If
        Next

            btnBottomLeft.Enabled = False
            btnBottomLeftA.Enabled = False
            btnBottomLeftSideWall.Enabled = False
            btnBottomLeftSideWallA.Enabled = False
            btnBottomLeftWall.Enabled = False
            btnBottomLeftWallA.Enabled = False
            btnBottomRight.Enabled = False
            btnBottomRightA.Enabled = False
            btnBottomRightSideWall.Enabled = False
            btnBottomRightSideWallA.Enabled = False
            btnBottomRightWall.Enabled = False
            btnBottomRightWallA.Enabled = False
            btnBottomWall.Enabled = False
            btnCageBottom.Enabled = False
            btnCageLeftSide.Enabled = False
            btnCageRightSide.Enabled = False
            btnCageTopLeft.Enabled = False
            btnCageTopRight.Enabled = False
            btnFood.Enabled = False
            btnLeftPathwayBottomWall.Enabled = False
            btnLeftPathwayBottomWall2.Enabled = False
            btnLeftPathwayBottomWallA.Enabled = False
            btnLeftPathwayTopWall.Enabled = False
            btnLeftPathwayTopWall2.Enabled = False
            btnLeftPathwayTopWallA.Enabled = False
            btnLives.Enabled = False
            btnMidBottomLeftWall.Enabled = False
            btnMidBottomLeftWall2.Enabled = False
            btnMidBottomRightWall.Enabled = False
            btnMidBottomRightWall2.Enabled = False
            btnMidBottomWall.Enabled = False
            btnMidBottomWall2.Enabled = False
            btnMidBottomWall2A.Enabled = False
            btnMidBottomWallA.Enabled = False
            btnMidTopLeftWall.Enabled = False
            btnMidTopLeftWallA.Enabled = False
            btnMidTopRightWall.Enabled = False
            btnMidTopRightWallA.Enabled = False
            btnMidTopWall.Enabled = False
            btnMidTopWallA.Enabled = False
            btnRightPathwayBottomWall.Enabled = False
            btnRightPathwayBottomWall2.Enabled = False
            btnRightPathwayBottomWallA.Enabled = False
            btnRightPathwayTopWall.Enabled = False
            btnRightPathwayTopWall2.Enabled = False
            btnRightPathwayTopWallA.Enabled = False
            btnSpecial.Enabled = False
            btnTopLeftSideWall.Enabled = False
            btnTopLeftWall.Enabled = False
            btnTopLeftWall2.Enabled = False
            btnTopLeftWallA.Enabled = False
            btnTopRightSideWall.Enabled = False
            btnTopRightWall.Enabled = False
            btnTopRightWall2.Enabled = False
            btnTopRightWallA.Enabled = False
            btnTopWall.Enabled = False
            btnTopWallA.Enabled = False
            btnUI.Enabled = False
    End Sub
    Private Sub tmrEdibleGhostTimer_Tick(sender As Object, e As EventArgs) Handles tmrEdibleGhostTimer.Tick
        Call CheckDirection()
        EdibleGhostTime = lblEdibleGhostTime.Text
        EdibleGhostTimeLeft = 1
        lblEdibleGhostTime.Text = EdibleGhostTime - EdibleGhostTimeLeft
        If lblEdibleGhostTime.Text = "0" Then
            EdibleGhost = False
            tmrEdibleGhostTimer.Stop()
            lblEdibleGhostTime.Text = "15"
        End If
    End Sub

    Private Sub tmrMakeGhostBlink_Tick(sender As Object, e As EventArgs) Handles tmrMakeGhostBlink.Tick
        If EdibleGhost = True Then
            If GhostRed.Visible = True Then
                GhostRed.Visible = False
            ElseIf GhostRed.Visible = False Then
                GhostRed.Visible = True
            End If
        End If
        If EdibleGhost = False Then
            GhostRed.Visible = True
        End If
    End Sub
End Class


