I am starting a butterfly program and having an issue and really just barely have it started and would like to get some help\feedback on how to do this. The following is the instructions and I will make a second post on what code I have so far...

The butterfly is going to be moving from side to side in a slightly upward
assent when the animation starts. You need to build a set of controls that
allow the user to control whether the butterfly ascends (up) or descends
(down) and also allows the direction of the butterfly to change (left or
right). The best way to implement this feature is through the use of
keystrokes that when pressed act to control the direction of the butterfly.
Whether you implement this feature through keystrokes or controls, make
sure that the feature is communicated to the user.

Butterflies fly by opening and closing their wings. So it is expected that
the butterfly will alternate between opened and closed as it wings its way
across the form. If the butterfly is moving right, make certain the butterfly
graphic displayed shows the butterfly as moving toward the right. If the
butterfly is moving to the left, make certain the butterfly graphic displayed
shows the butterfly as moving toward the left.

If the butterfly reaches the top of the form in its assent, it should wrap
around to the bottom of the form. If the butterfly reaches the bottom of the
form in its descent, it should wrap around to the top of the form.
If


If the butterfly reaches the left edge of the form, it should begin flying
toward the right side of the form. If the butterfly reaches the right edge of
the form, it should begin flying toward the left side of the form. If the user
changes the direction of the butterfly, insure that the correct graphics are
used in relation to the direction of the butterfly and that the butterfly moves
in the indicated direction. Please note that when the butterfly reaches the
edge of the form, the butterfly must change direction regardless of any
user input.

Recommended Answers

All 8 Replies

h

h

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging
Imports System.Drawing.Text

Public Class Form1

    Dim x1, x2, y1, y2, move1x, move2x, move1y, move2y As Integer
    Const butterflysize As Integer = 25

    Private Sub quitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles quitButton.Click
        Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
        End
        Me.Cursor = System.Windows.Forms.Cursors.Default
    End Sub

    Private Sub startButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles startButton.Click
        Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
        If startButton.Text = "START" Then
            quitButton.Text = "STOP"
            ' pick a starting point for the object to animate
            x1 = Int(Rnd() * Me.Width)
            y1 = Int(Rnd() * Me.Height)
            x2 = Int(Rnd() * Me.Width)
            y2 = Int(Rnd() * Me.Height)
            move1x = CInt(Int((butterflysize - (-butterflysize) + 1) * Rnd() _
            + (-butterflysize)))
            move2x = CInt(Int((butterflysize - (-butterflysize) + 1) * Rnd() _
            + (-butterflysize)))
            move1y = CInt(Int((butterflysize - (-butterflysize) + 1) * Rnd() _
            + (-butterflysize)))
            move2y = CInt(Int((butterflysize - (-butterflysize) + 1) * Rnd() _
            + (-butterflysize)))
            Timer1.Interval = Int(Rnd() * 499) + 1
            Timer2.Interval = Int(Rnd() * 499) + 1
            Timer1.Enabled = True
            Timer2.Enabled = True
        Else
            startButton.Text = "START"
            Timer1.Enabled = False
            Timer2.Enabled = False
        End If
        Me.Cursor = System.Windows.Forms.Cursors.Default

    End Sub

End Class

I apologize for the incorrect code tags but after looking at this can someone please let me know also if I am on the right track or way off

can anyone take a look at this for me? I used my friends account because I forgot my password but I am stuck on this if anyone could offer any help?

Ok here is the best way I can explain what I am trying to do....

So I have been given four 4 designs so if I want to move a butterfly left, right, up, and down. I have the design set up but I guess my first question is I am just not sure how to take these 4 jpeg pictures and put them in one program. What I have done so far I believe is I have the butterfly set to random location but I want to at least start with how to move them so as long as someone can help me out with setting up with these 4 pictures into one I should be good. Please let me know if that sounds confusing...

Here is my code...

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging
Imports System.Drawing.Text

Public Class Form1

    'Global variables
    Dim x1, x2, y1, y2, move1x, move2x, move1y, move2y As Integer
    Const butterflysize As Integer = 25



    Private Sub quitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles quitButton.Click
        'close the program if quit is selected
        Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
        End
        Me.Cursor = System.Windows.Forms.Cursors.Default
    End Sub

    Private Sub startButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles startButton.Click
        Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
        ' Pick a starting point for the object to animate
        x1 = Int(Rnd() * Me.Width)
        y1 = Int(Rnd() * Me.Height)

        ' A starting point is being selected for the x and y coordinates. 
        ' A random X value is chosen between zero and the form‟s width. A random Y value is chosen between zero and the form‟s height
        move1x = CInt(Int((butterflysize - (-butterflysize) + 1) * Rnd() + (-butterflysize)))
        move1y = CInt(Int((butterflysize - (-butterflysize) + 1) * Rnd() + (-butterflysize)))
    End Sub

    Private Sub leftButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles leftButton.Click

    End Sub

    Private Sub rightButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rightButton.Click

    End Sub

    Private Sub upButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles upButton.Click

    End Sub

    Private Sub downButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles downButton.Click

    End Sub
End Class

this is what I got but still working on it...

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging
Imports System.Drawing.Text

Public Class Form1

    'Global variables
    Dim x1, x2, y1, y2, move1x, move2x, move1y, move2y As Integer
    Const butterflysize As Integer = 25

    Private Sub quitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles quitButton.Click
        'close the program if quit is selected
        Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
        End
        Me.Cursor = System.Windows.Forms.Cursors.Default
    End Sub

    Private Sub startButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles startButton.Click
        Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
        ' Pick a starting point for the object to animate
        x1 = Int(Rnd() * Me.Width)
        y1 = Int(Rnd() * Me.Height)

        ' A starting point is being selected for the x and y coordinates. 
        ' A random X value is chosen between zero and the form‟s width. A random Y value is chosen between zero and the form‟s height
        move1x = CInt(Int((butterflysize - (-butterflysize) + 1) * Rnd() + (-butterflysize)))
        move1y = CInt(Int((butterflysize - (-butterflysize) + 1) * Rnd() + (-butterflysize)))
    End Sub

    Private Sub leftButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles leftButton.Click
        PictureBox1.Image = Image.FromFile("E:\Programming Assignment 3\Programming Assignment 3\bfly1.jpg")
    End Sub

    Private Sub rightButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rightButton.Click
        PictureBox1.Image = Image.FromFile("E:\Programming Assignment 3\Programming Assignment 3\bfl2y.jpg")
    End Sub

    Private Sub upButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles upButton.Click
        PictureBox1.Image = Image.FromFile("E:\Programming Assignment 3\Programming Assignment 3\bfly3.jpg")
    End Sub

    Private Sub downButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles downButton.Click
        PictureBox1.Image = Image.FromFile("E:\Programming Assignment 3\Programming Assignment 3\bfly4.jpg")
    End Sub

End Class
Member Avatar for Unhnd_Exception

Change the images in the Load event.

Use the arrow keys to change direction.

Set the form's KeyPreview = True

Public Class Form1

    Private ButterFlyRect As Rectangle
    Private ButterFly(1) As Bitmap
    Private WithEvents Pilot As Timer
    Private Heading As FlightPath

    Private Enum FlightPath
        Hover = 0
        Up = 1
        Down = 2
        Left = 4
        Right = 8
        LeftAndUp = Left + Up
        LeftAndDown = Left + Down
        RightAndUp = Right + Up
        RightAndDown = Right + Down
    End Enum

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

        ButterFly(0) = New Bitmap(My.Resources.WingUp)
        ButterFly(1) = New Bitmap(My.Resources.WingDown)

        ButterFlyRect = New Rectangle(Me.DisplayRectangle.Left, _
                                      Me.DisplayRectangle.Bottom - ButterFly(0).Height, _
                                      ButterFly(0).Width, _
                                      ButterFly(0).Height)

        Heading = FlightPath.RightAndUp

        Pilot = New Timer
        Pilot.Interval = 200
        Pilot.Start()

    End Sub

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        'Draw the image at the current rectangle
        'Since theres only 2 images the 1 liner can flip the indexes
        'It will flip it from 0 to -1 so use math.abs.
        'Same technique works for flipping boolean values.

        Static Index As Integer

        e.Graphics.DrawImage(ButterFly(Math.Abs(Index)), ButterFlyRect)

        Index = Not Index

    End Sub

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        'Give the new direction to the wingman.

        Select Case e.KeyCode
            Case Keys.Left
                WingMan_ChangeHeading(FlightPath.Left)

            Case Keys.Right
                WingMan_ChangeHeading(FlightPath.Right)

            Case Keys.Up
                WingMan_ChangeHeading(FlightPath.Up)

            Case Keys.Down
                WingMan_ChangeHeading(FlightPath.Down)

        End Select

    End Sub

    Private Sub Pilot_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Pilot.Tick
        'Get the new coordinates from the wingman.
        'Invalidate the current rectangle so its erased.
        'Shift the rectangle.
        'Invalidate the new rectangle so its drawn.

        Dim Coordinate As Point = WingMan_PlotCoordinate()

        Me.Invalidate(ButterFlyRect)
        ButterFlyRect.Offset(Coordinate.X, Coordinate.Y)
        Me.Invalidate(ButterFlyRect)

    End Sub

    Private Function WingMan_PlotCoordinate() As Point

        Randomize()

        'Get a random shift (0 - 1) * (Width or Height)
        Dim Coordinate As New Point(Rnd() * ButterFlyRect.Width, _
                                    Rnd() * ButterFlyRect.Height)

        'If heading Left or Up make it negative so it goes backwards. 
        If Heading And FlightPath.Left Then Coordinate.X *= -1
        If Heading And FlightPath.Up Then Coordinate.Y *= -1

        'Check the Left and Right Bounds, change heading if needed.
        If ButterFlyRect.X + Coordinate.X < Me.DisplayRectangle.Left Then
            Coordinate.X = Me.DisplayRectangle.Left - ButterFlyRect.Left
            WingMan_ChangeHeading(FlightPath.Right)

        ElseIf ButterFlyRect.X + Coordinate.X + ButterFlyRect.Width > Me.DisplayRectangle.Right Then
            Coordinate.X = Me.DisplayRectangle.Right - ButterFlyRect.Right
            WingMan_ChangeHeading(FlightPath.Left)

        End If

        'Check the Top and Bottom Bounds, change heading if needed.
        If ButterFlyRect.Y + Coordinate.Y < Me.DisplayRectangle.Top Then
            Coordinate.Y = Me.DisplayRectangle.Top - ButterFlyRect.Top
            WingMan_ChangeHeading(FlightPath.Down)

        ElseIf ButterFlyRect.Y + Coordinate.Y + ButterFlyRect.Height > Me.DisplayRectangle.Bottom Then
            Coordinate.Y = Me.DisplayRectangle.Bottom - ButterFlyRect.Bottom
            WingMan_ChangeHeading(FlightPath.Up)

        End If

        Return Coordinate

    End Function

    Private Sub WingMan_ChangeHeading(ByVal NewFlightPath As FlightPath)

        'Change the current heading if we're not already
        'heading in that direction.

        'If the direction changes from Left or Right
        'then flip the images.

        Dim Flip As Boolean

        Select Case Heading

            Case FlightPath.LeftAndUp
                If NewFlightPath = FlightPath.Down Then
                    Heading = FlightPath.LeftAndDown
                ElseIf NewFlightPath = FlightPath.Right Then
                    Heading = FlightPath.RightAndUp
                    Flip = True
                End If

            Case FlightPath.LeftAndDown
                If NewFlightPath = FlightPath.Up Then
                    Heading = FlightPath.LeftAndUp
                ElseIf NewFlightPath = FlightPath.Right Then
                    Heading = FlightPath.RightAndDown
                    Flip = True
                End If

            Case FlightPath.RightAndUp
                If NewFlightPath = FlightPath.Down Then
                    Heading = FlightPath.RightAndDown
                ElseIf NewFlightPath = FlightPath.Left Then
                    Heading = FlightPath.LeftAndUp
                    Flip = True
                End If

            Case FlightPath.RightAndDown
                If NewFlightPath = FlightPath.Up Then
                    Heading = FlightPath.RightAndUp
                ElseIf NewFlightPath = FlightPath.Left Then
                    Heading = FlightPath.LeftAndDown
                    Flip = True
                End If

        End Select

        If Flip Then
            ButterFly(0).RotateFlip(RotateFlipType.RotateNoneFlipX)
            ButterFly(1).RotateFlip(RotateFlipType.RotateNoneFlipX)
        End If
    End Sub

End Class
commented: VERY HELPFUL!!!!!!!!! +3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.