-
Oct 13th, 2017, 11:38 AM
#1
Thread Starter
New Member
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?
-
Oct 13th, 2017, 12:32 PM
#2
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
-
Oct 13th, 2017, 12:56 PM
#3
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
-
Oct 13th, 2017, 01:10 PM
#4
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 13th, 2017, 01:16 PM
#5
Re: How would I make my form bounce around the screen?
Originally Posted by igdegoo
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
-
Oct 13th, 2017, 01:27 PM
#6
Thread Starter
New Member
-
Oct 13th, 2017, 04:31 PM
#7
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 15th, 2017, 05:36 AM
#8
Thread Starter
New Member
Re: How would I make my form bounce around the screen?
Originally Posted by .paul.
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?
-
Oct 15th, 2017, 08:56 AM
#9
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...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 16th, 2017, 06:40 AM
#10
Thread Starter
New Member
Re: How would I make my form bounce around the screen?
Originally Posted by .paul.
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
-
Oct 16th, 2017, 07:16 AM
#11
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.
-
Oct 16th, 2017, 03:00 PM
#12
Re: How would I make my form bounce around the screen?
Originally Posted by igdegoo
I have a completely blank form with unchanged source code
In that case, add a button to your form, then paste the code...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 16th, 2017, 05:13 PM
#13
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.
-
Oct 16th, 2017, 05:56 PM
#14
Re: How would I make my form bounce around the screen?
Originally Posted by kpmc
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...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|