[Solved] Move a picturebox randomly around screen
Posted
#1
(In Topic #1413)
Enthusiast

I am sure I have asked this before but i can not seem to find the posting.
What I would like to do is show a form with my Logo on it (I can do this with no issues)
<IMG src="https://support.algpos.co.uk/images_help/Screenshot_logo.png">
</IMG>I would like to bounce the picturebox around with in the 1024x768 screen size (AND NOT have the image go off the screen)
Posted
Guru

Posted
Guru

Code (gambas)
- ' Gambas class file
- $hATimer.Delay = 20 ' (about 50fps)
- $hATimer.Start
- $hATimer.Stop
- ' increase or decrease X and Y positions depending on direction
- ' change X direction if on an edge
- ' change Y direction if on an edge
- DrawingArea1.Refresh ' trigger redraw
- ' nice gradient background
- Paint.Brush = Paint.LinearGradient(0, 0, 0, Last.H, [Color.LightGray, Color.Gray, Color.DarkGray], [0, 0.5, 1])
- Paint.Fill
- ' nice gradient logo background
- Paint.Brush = Paint.LinearGradient(iX, iY, iX, iY + $hImg.H, [Color.Yellow, Color.Green], [0, 1])
- Paint.Rectangle(iX, iY, $hImg.W, $hImg.H, 30)
- Paint.Fill
- Paint.DrawPicture($hImg, iX, iY, $hImg.W, $hImg.H)
In summary…
I create a timer with a delay of 20 and start it
In the timer event I increase or decrease the X and Y logo positions the distance of "iMoveAmount" (2) depending on their direction.
If X or Y have gone to a far edge then the direction is reversed.
"Bounce" is thus achieved.
The attached example program lets you change iMoveAmount to increase or decrease speed of movement.
Posted
Enthusiast

BruceSteers said
Code (gambas)
' Gambas class file $hATimer.Delay = 20 ' (about 50fps) $hATimer.Start $hATimer.Stop ' increase or decrease X and Y positions depending on direction ' change X direction if on an edge ' change Y direction if on an edge DrawingArea1.Refresh ' trigger redraw ' nice gradient background Paint.Brush = Paint.LinearGradient(0, 0, 0, Last.H, [Color.LightGray, Color.Gray, Color.DarkGray], [0, 0.5, 1]) Paint.Fill ' nice gradient logo background Paint.Brush = Paint.LinearGradient(iX, iY, iX, iY + $hImg.H, [Color.Yellow, Color.Green], [0, 1]) Paint.Rectangle(iX, iY, $hImg.W, $hImg.H, 30) Paint.Fill Paint.DrawPicture($hImg, iX, iY, $hImg.W, $hImg.H)
In summary…
I create a timer with a delay of 20 and start it
In the timer event I increase or decrease the X and Y logo positions the distance of "iMoveAmount" (2) depending on their direction.
If X or Y have gone to a far edge then the direction is reversed.
"Bounce" is thus achieved.
The attached example program lets you change iMoveAmount to increase or decrease speed of movement.
Thank you so much
1 guest and 0 members have just viewed this.



