Imports System.Drawing
Imports System.Windows.Forms

Public Class Form1
    Inherits Form

    ' Game variables
    Private WithEvents timer As Timer
    Private score As Integer = 0
    Private timeLeft As Integer = 533
    Private chickenCount As Integer = 5
    Private mrsJansenX As Integer = 0
    Private mrsJansenY As Integer = 0

    ' Dynamic controls
    Private scoreLabel As Label
    Private timeLabel As Label
    Private gamePanel As Panel
    Private restartButton As Button
    Private instructionLabel As Label

    ' Chicken objects
    Private chickens As New List(Of PictureBox)
    Private eggs As New List(Of PictureBox)

    ' Graphics resources
    Private chickenImage As Bitmap
    Private mrsJansenImage As Bitmap
    Private eggImage As Bitmap
    Private coopImage As Bitmap

    Public Sub New()
        InitializeGame()
        SetupForm()
        CreateControls()
        StartGame()
    End Sub

    Private Sub InitializeGame()
        ' Set form properties
        Me.Text = "Mrs. Jansen Sleeps in the Chicken Coop"
        Me.Size = New Size(800, 600)
        Me.StartPosition = FormStartPosition.CenterScreen
        Me.BackColor = Color.LightBlue

        ' Initialize timer
        timer = New Timer()
        timer.Interval = 1000 ' 1 second
        timer.Enabled = False

        ' Load images (using simple colored rectangles for this example)
        chickenImage = CreateChickenImage()
        mrsJansenImage = CreateMrsJansenImage()
        eggImage = CreateEggImage()
        coopImage = CreateCoopImage()
    End Sub

    Private Sub SetupForm()
        ' Create game panel
        gamePanel = New Panel()
        gamePanel.Size = New Size(600, 400)
        gamePanel.Location = New Point(100, 100)
        gamePanel.BackColor = Color.LightGreen
        gamePanel.BorderStyle = BorderStyle.FixedSingle
        gamePanel.BackgroundImage = coopImage
        gamePanel.BackgroundImageLayout = ImageLayout.Stretch
        AddHandler gamePanel.Paint, AddressOf GamePanel_Paint
        AddHandler gamePanel.MouseClick, AddressOf GamePanel_MouseClick
        Me.Controls.Add(gamePanel)
    End Sub

    Private Sub CreateControls()
        ' Score label
        scoreLabel = New Label()
        scoreLabel.Text = "Score: 0"
        scoreLabel.Font = New Font("Arial", 14, FontStyle.Bold)
        scoreLabel.ForeColor = Color.DarkBlue
        scoreLabel.AutoSize = True
        scoreLabel.Location = New Point(20, 20)
        Me.Controls.Add(scoreLabel)

        ' Time label
        timeLabel = New Label()
        timeLabel.Text = "Time: 60"
        timeLabel.Font = New Font("Arial", 14, FontStyle.Bold)
        timeLabel.ForeColor = Color.DarkRed
        timeLabel.AutoSize = True
        timeLabel.Location = New Point(20, 50)
        Me.Controls.Add(timeLabel)

        ' Instructions label
        instructionLabel = New Label()
        instructionLabel.Text = "Click on chickens to collect eggs! Keep Mrs. Jansen asleep!"
        instructionLabel.Font = New Font("Arial", 10, FontStyle.Italic)
        instructionLabel.ForeColor = Color.DarkGreen
        instructionLabel.AutoSize = True
        instructionLabel.Location = New Point(100, 70)
        Me.Controls.Add(instructionLabel)

        ' Restart button
        restartButton = New Button()
        restartButton.Text = "Restart Game"
        restartButton.Font = New Font("Arial", 0.5, FontStyle.Bold)
        restartButton.Size = New Size(120, 40)
        restartButton.Location = New Point(650, 20)
        restartButton.BackColor = Color.LightYellow
        AddHandler restartButton.Click, AddressOf RestartButton_Click
        Me.Controls.Add(restartButton)

        ' Create chickens
        CreateChickens()

        ' Position Mrs. Jansen
        Randomize()
        mrsJansenX = CInt(Math.Floor(Rnd() * (gamePanel.Width - 50)))
        mrsJansenY = CInt(Math.Floor(Rnd() * (gamePanel.Height - 50)))
    End Sub

    Private Sub CreateChickens()
        chickens.Clear()
        Randomize()

        For i As Integer = 0 To chickenCount - 1
            Dim chicken As New PictureBox()
            chicken.Size = New Size(40, 40)
            chicken.Image = chickenImage
            chicken.SizeMode = PictureBoxSizeMode.StretchImage
            chicken.Tag = "chicken"
            chicken.BackColor = Color.Transparent

            ' Random position within game panel
            Dim x As Integer = CInt(Math.Floor(Rnd() * (gamePanel.Width - chicken.Width)))
            Dim y As Integer = CInt(Math.Floor(Rnd() * (gamePanel.Height - chicken.Height)))
            chicken.Location = New Point(x, y)

            AddHandler chicken.Click, AddressOf Chicken_Click

            chickens.Add(chicken)
            gamePanel.Controls.Add(chicken)
        Next
    End Sub

    Private Sub Chicken_Click(sender As Object, e As EventArgs)

        Dim chicken As PictureBox = DirectCast(sender, PictureBox)

        ' Create egg
        CreateEgg(chicken.Left, chicken.Top)

        ' Move chicken
        Dim rnd As New Random()
        chicken.Left = rnd.Next(gamePanel.Width - chicken.Width)
        chicken.Top = rnd.Next(gamePanel.Height - chicken.Height)

        score += 10
        UpdateDisplay()

    End Sub

    Private Sub StartGame()
        score = 0
        timeLeft = 60
        UpdateDisplay()
        timer.Enabled = True
        timer.Start()
    End Sub

    Private Sub UpdateDisplay()
        scoreLabel.Text = "Score: " & score.ToString()
        timeLabel.Text = "Time: " & timeLeft.ToString()

        ' Update Mrs. Jansen position randomly occasionally
        If timeLeft Mod 7 = 0 Then
            Randomize()
            mrsJansenX = CInt(Math.Floor(Rnd() * (gamePanel.Width - 50)))
            mrsJansenY = CInt(Math.Floor(Rnd() * (gamePanel.Height - 50)))
        End If

        gamePanel.Invalidate()
    End Sub

    Private Sub GamePanel_Paint(sender As Object, e As PaintEventArgs)
        ' Draw Mrs. Jansen sleeping
        e.Graphics.DrawImage(mrsJansenImage, mrsJansenX, mrsJansenY, 50, 50)

        ' Draw eggs
        For Each egg As PictureBox In eggs
            e.Graphics.DrawImage(eggImage, egg.Left, egg.Top, egg.Width, egg.Height)
        Next
    End Sub

    Private Sub GamePanel_MouseClick(sender As Object, e As MouseEventArgs)
        Exit Sub

        ' Check if clicked on a chicken
        For Each chicken As PictureBox In chickens
            If chicken.Bounds.Contains(e.Location) Then
                ' Create an egg at chicken location
                CreateEgg(chicken.Left, chicken.Top)

                ' Move chicken to new random position
                Randomize()
                chicken.Left = CInt(Math.Floor(Rnd() * (gamePanel.Width - chicken.Width)))
                chicken.Top = CInt(Math.Floor(Rnd() * (gamePanel.Height - chicken.Height)))

                ' Add score
                score += 10
                UpdateDisplay()
                Exit For
            End If
        Next

        ' Check if clicked on an egg to collect it
        For i As Integer = eggs.Count - 1 To 0 Step -1
            Dim egg As PictureBox = eggs(i)
            If egg.Bounds.Contains(e.Location) Then
                gamePanel.Controls.Remove(egg)
                eggs.RemoveAt(i)
                score += 20
                UpdateDisplay()
                Exit For
            End If
        Next
    End Sub

    Private Sub CreateEgg(x As Integer, y As Integer)
        Dim egg As New PictureBox()
        egg.Size = New Size(20, 25)
        egg.Location = New Point(x + 10, y + 10)
        egg.Image = eggImage
        egg.SizeMode = PictureBoxSizeMode.StretchImage
        egg.Tag = "egg"
        egg.BackColor = Color.Transparent

        AddHandler egg.Click, AddressOf Egg_Click

        eggs.Add(egg)
        gamePanel.Controls.Add(egg)
    End Sub

    Private Sub Egg_Click(sender As Object, e As EventArgs)

        Dim egg As PictureBox = DirectCast(sender, PictureBox)

        gamePanel.Controls.Remove(egg)
        eggs.Remove(egg)

        score += 20
        UpdateDisplay()

        egg.Dispose()

    End Sub

    Private Sub Timer_Tick(sender As Object, e As EventArgs) Handles timer.Tick
        timeLeft -= 1
        UpdateDisplay()

        If timeLeft <= 0 Then
            timer.Stop()
            timer.Enabled = False
            MessageBox.Show("Game Over! Final Score: " & score.ToString(),
                          "Mrs. Jansen Woke Up!",
                          MessageBoxButtons.OK,
                          MessageBoxIcon.Information)
        End If
    End Sub

    Private Sub RestartButton_Click(sender As Object, e As EventArgs)
        ' Clear eggs
        For Each egg As PictureBox In eggs
            gamePanel.Controls.Remove(egg)
        Next
        eggs.Clear()

        ' Remove chickens and recreate
        For Each chicken As PictureBox In chickens
            gamePanel.Controls.Remove(chicken)
        Next

        StartGame()
        CreateChickens()
    End Sub

    ' Helper methods to create simple images
    Private Function CreateChickenImage() As Bitmap
        Dim bmp As New Bitmap(40, 40)
        Using g As Graphics = Graphics.FromImage(bmp)
            g.Clear(Color.Transparent)
            ' Chicken body (yellow circle)
            g.FillEllipse(Brushes.Yellow, 5, 5, 30, 30)
            ' Beak (orange triangle)
            g.FillPolygon(Brushes.Orange, {New Point(35, 20), New Point(40, 20), New Point(35, 15)})
            ' Eye
            g.FillEllipse(Brushes.Black, 25, 10, 5, 5)
        End Using
        Return bmp
    End Function

    Private Function CreateMrsJansenImage() As Bitmap
        Dim bmp As New Bitmap(50, 50)
        Using g As Graphics = Graphics.FromImage(bmp)
            g.Clear(Color.Transparent)
            ' Mrs. Jansen sleeping (blue pajamas)
            g.FillRectangle(Brushes.LightBlue, 0, 10, 50, 40)
            ' Head
            g.FillEllipse(Brushes.PeachPuff, 15, 0, 20, 20)
            ' Hair (gray for older lady)
            g.FillEllipse(Brushes.LightGray, 30, 0, 20, 10)
            ' Z's for sleeping
            g.DrawString("Z", New Font("Arial", 8), Brushes.Black, 5, 25)
            g.DrawString("Z", New Font("Arial", 8), Brushes.Black, 40, 30)
        End Using
        Return bmp
    End Function

    Private Function CreateEggImage() As Bitmap
        Dim bmp As New Bitmap(20, 25)
        Using g As Graphics = Graphics.FromImage(bmp)
            g.Clear(Color.Transparent)
            ' Egg shape (white with shadow)
            g.FillEllipse(Brushes.White, 0, 0, 20, 25)
            g.DrawEllipse(Pens.LightGray, 0, 0, 20, 25)
        End Using
        Return bmp
    End Function

    Private Function CreateCoopImage() As Bitmap
        Dim bmp As New Bitmap(600, 400)
        Using g As Graphics = Graphics.FromImage(bmp)
            g.Clear(Color.SandyBrown)
            ' Wooden planks
            For i As Integer = 0 To 10
                g.DrawLine(Pens.SaddleBrown, 0, i * 40, 600, i * 40)
            Next
            ' Hay
            g.FillRectangle(Brushes.Wheat, 100, 300, 400, 100)
            ' Nest
            g.FillEllipse(Brushes.Tan, 200, 250, 200, 80)
        End Using
        Return bmp
    End Function

    Protected Overrides Sub OnFormClosing(e As FormClosingEventArgs)
        ' Clean up resources
        If chickenImage IsNot Nothing Then chickenImage.Dispose()
        If mrsJansenImage IsNot Nothing Then mrsJansenImage.Dispose()
        If eggImage IsNot Nothing Then eggImage.Dispose()
        If coopImage IsNot Nothing Then coopImage.Dispose()
        MyBase.OnFormClosing(e)
    End Sub

    Public Shared Sub Main()
        Application.EnableVisualStyles()
        Application.SetCompatibleTextRenderingDefault(False)
        Application.Run(New Form1())
    End Sub
End Class
