Results 1 to 14 of 14

Thread: How would I make my form bounce around the screen?

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2017
    Posts
    4

    Question How would I make my form bounce around the screen?

    I originally posted this in the SharpDevelop forums, but it never got any answers.
    SharpDevelop is just Visual Studio except a lot less bloated


    I'm making a little prank program in VB.NET and I want to make the form bounce around the edges of the screen.

    But none of the tutorials are helping me!

    Can you tell me how to do this?

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,541

    Re: How would I make my form bounce around the screen?

    You'll need a timer... each time the timer fires off, you would need to know what direction the form is traveling, calculate the new point, then set the location of the form to that new point. Then you need to see if your form has hit the edge of the screen and set something so the next time around you can change direction.

    and of course, have a button to stop the madness.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,044

    Re: How would I make my form bounce around the screen?

    Rather than a button to stop the madness, use a keypress. That'll make it all the more maddening.

    I had an application that ran full screen with a single button on it. Whenever the mouse approached the button, the button would scoot away. The user would chase the button all over the screen trying to click it. Because it was full screen, there wasn't even the taskbar showing. Of course, it was just a regular program, so Ctl+Alt+Del would take care of it, and there was a key combo to exit, as well, but it was fun.
    My usual boring signature: Nothing

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: How would I make my form bounce around the screen?

    Similar to the app. that Shaggy was talking about...

    http://www.scproject.biz/Using%20For...seventhexample

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,044

    Re: How would I make my form bounce around the screen?

    Quote Originally Posted by igdegoo View Post
    I originally posted this in the SharpDevelop forums, but it never got any answers.
    SharpDevelop is just Visual Studio except a lot less bloated
    Guess their forum isn't as bloated, either.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2017
    Posts
    4

    Re: How would I make my form bounce around the screen?

    I just want something I can copy and paste into the code to make it bounce around.
    Yes, I'm very lazy
    Attached Images Attached Images  

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: How would I make my form bounce around the screen?

    Simples...

    Code:
    Public Class Form1
    
        Private WithEvents tmr As New Timer With {.Interval = 50}
        Dim angle As Integer = 135
        Dim currentScreen As Screen
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.DoubleBuffered = True
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            currentScreen = Screen.AllScreens.First(Function(s) s.Bounds.Contains(Me.Location))
            tmr.Start()
        End Sub
    
        Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmr.Tick
            Dim angleRadians As Single = CSng(Math.PI * (angle Mod 360) / 180)
            'Calculate X2 and Y2
            Dim pointX2 As Integer = CInt(Me.Location.X - Math.Sin(angleRadians) * 10)
            Dim pointY2 As Integer = CInt(Me.Location.Y + Math.Cos(angleRadians) * 10)
            Me.Location = New Point(pointX2, pointY2)
            Dim p1 As Point = New Point(Me.Location.X - currentScreen.Bounds.Left, Me.Location.Y - currentScreen.Bounds.Top)
            Dim p2 As Point = New Point(Me.Right - currentScreen.Bounds.Left, Me.Bottom - currentScreen.Bounds.Top)
            If (p1.X < 0 Or p1.Y < 0 _
                Or p2.Y > currentScreen.Bounds.Height Or p2.X > currentScreen.Bounds.Width) Then
                angle += 90
            End If
        End Sub
        
    End Class

  8. #8

    Thread Starter
    New Member
    Join Date
    Oct 2017
    Posts
    4

    Re: How would I make my form bounce around the screen?

    Quote Originally Posted by .paul. View Post
    Simples...

    Code:
    Public Class Form1
    
        Private WithEvents tmr As New Timer With {.Interval = 50}
        Dim angle As Integer = 135
        Dim currentScreen As Screen
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.DoubleBuffered = True
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            currentScreen = Screen.AllScreens.First(Function(s) s.Bounds.Contains(Me.Location))
            tmr.Start()
        End Sub
    
        Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmr.Tick
            Dim angleRadians As Single = CSng(Math.PI * (angle Mod 360) / 180)
            'Calculate X2 and Y2
            Dim pointX2 As Integer = CInt(Me.Location.X - Math.Sin(angleRadians) * 10)
            Dim pointY2 As Integer = CInt(Me.Location.Y + Math.Cos(angleRadians) * 10)
            Me.Location = New Point(pointX2, pointY2)
            Dim p1 As Point = New Point(Me.Location.X - currentScreen.Bounds.Left, Me.Location.Y - currentScreen.Bounds.Top)
            Dim p2 As Point = New Point(Me.Right - currentScreen.Bounds.Left, Me.Bottom - currentScreen.Bounds.Top)
            If (p1.X < 0 Or p1.Y < 0 _
                Or p2.Y > currentScreen.Bounds.Height Or p2.X > currentScreen.Bounds.Width) Then
                angle += 90
            End If
        End Sub
        
    End Class
    So what do I need to do before inserting this?

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: How would I make my form bounce around the screen?

    Really???

    Show me your code, and i'll show you how to integrate my bouncing form code.
    What i posted is everything except a timer stop method to stop the form bouncing around...

  10. #10

    Thread Starter
    New Member
    Join Date
    Oct 2017
    Posts
    4

    Re: How would I make my form bounce around the screen?

    Quote Originally Posted by .paul. View Post
    Really???

    Show me your code, and i'll show you how to integrate my bouncing form code.
    What i posted is everything except a timer stop method to stop the form bouncing around...
    I have a completely blank form with unchanged source code

  11. #11
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: How would I make my form bounce around the screen?

    Hi, Paul. When you're done making his program, if you wouldnt mind, I've got a backlog you can start on right away.

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: How would I make my form bounce around the screen?

    Quote Originally Posted by igdegoo View Post
    I have a completely blank form with unchanged source code
    In that case, add a button to your form, then paste the code...

  13. #13
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: How would I make my form bounce around the screen?

    My new policy is I answer questions when someone gripes about questions being answered. There's some janky bits in this code that I didn't care to iron out. Handouts aren't as good-quality as tutorials, it is what it is.

    Code:
    Public Class Form1
    
        ' Mess with the interval and velocities to get the speed you want.
        Private ReadOnly TimerInterval As TimeSpan = TimeSpan.FromMilliseconds(75)
        Private ReadOnly MinVelocity As Integer = 50
        Private ReadOnly MaxVelocity As Integer = 80
    
        Private ReadOnly RNG As New Random()
    
        Private _currentDirection As DirectionInfo
        Private _velocity As Point
    
        Protected Overrides Sub OnShown(e As EventArgs)
            MyBase.OnShown(e)
    
            _currentDirection = New DirectionInfo()
            UpdateVelocity(_currentDirection)
    
            Timer1.Interval = TimerInterval.TotalMilliseconds
            Timer1.Start()
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Dim nextDirection = GetNextDirection()
            UpdateVelocity(nextDirection)
            _currentDirection = nextDirection
    
            Dim currentLocation = Me.Location
            currentLocation.Offset(_velocity)
            Location = currentLocation
        End Sub
    
        Public Function GetNextDirection() As DirectionInfo
            Dim bounds As Rectangle = Me.DesktopBounds
    
            ' Beware of multiple monitors.
            Dim currentScreen = Screen.FromControl(Me)
            Dim screenBounds As Rectangle = currentScreen.WorkingArea
    
            Dim newDirection = New DirectionInfo() With {
                .IsUp = _currentDirection.IsUp,
                .IsRight = _currentDirection.IsRight
            }
    
            Dim yBound = If(_currentDirection.IsUp, screenBounds.Top, screenBounds.Bottom)
            Dim nextBound =
                IIf(_currentDirection.IsUp, bounds.Top, bounds.Bottom) + _velocity.Y
            If _currentDirection.IsUp AndAlso nextBound <= yBound Then
                newDirection.IsUp = False
            ElseIf Not _currentDirection.IsUp AndAlso yBound <= nextBound Then
                newDirection.IsUp = True
            End If
    
            Dim xBound = If(_currentDirection.IsRight, screenBounds.Right, screenBounds.Left)
            nextBound =
                IIf(_currentDirection.IsRight, bounds.Right, bounds.Left) + _velocity.X
            If _currentDirection.IsRight AndAlso xBound <= nextBound Then
                newDirection.IsRight = False
            ElseIf Not _currentDirection.IsRight AndAlso nextBound <= xBound Then
                newDirection.IsRight = True
            End If
    
            Return newDirection
        End Function
    
        Private Sub UpdateVelocity(ByVal directionInfo As DirectionInfo)
            ' Only change velocity when a direction changes.
            If directionInfo.IsUp = _currentDirection.IsUp AndAlso
                    directionInfo.IsRight = _currentDirection.IsRight AndAlso
                    _velocity <> Point.Empty Then
                Return
            End If
    
            Dim newXVelocity = RNG.Next(MinVelocity, MaxVelocity)
            Dim newYVelocity = RNG.Next(MinVelocity, MaxVelocity)
    
            ' Fun trick to avoid StackOverflowException! This case matters because
            ' if Vx and Vy are both 0, the form stops moving.
            If newXVelocity = newYVelocity AndAlso newXVelocity = 0 Then
                Me.Invoke(Sub() UpdateVelocity(directionInfo))
                Return
            End If
    
            If Not directionInfo.IsRight Then
                newXVelocity *= -1
            End If
    
            If directionInfo.IsUp Then
                newYVelocity *= -1
            End If
    
            _velocity = New Point(newXVelocity, newYVelocity)
        End Sub
    End Class
    
    Public Class DirectionInfo
        Public IsRight As Boolean
        Public IsUp As Boolean
    End Class
    Mess with the numbers at the top to make it go faster or slower. If you want it to be "smooth", tough cookies: this is Windows Forms and you're usually lucky to get 15FPS.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: How would I make my form bounce around the screen?

    Quote Originally Posted by kpmc View Post
    Hi, Paul. When you're done making his program, if you wouldnt mind, I've got a backlog you can start on right away.
    Sure. £500 per hour for projects i don't choose because they're amusing.
    Let me know when you want me to start...��

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width