[Solved] Move a picturebox randomly around screen

Post

Posted
Rating:
#1 (In Topic #1413)
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
Hi All,

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)
Online now: No Back to the top

Post

Posted
Rating:
#2
Guru
BruceSteers is in the usergroup ‘Guru’
So what's your question?

maybe this topic will help.
Gambas One - Gambas ONE
Online now: No Back to the top

Post

Posted
Rating:
#3
Guru
BruceSteers is in the usergroup ‘Guru’

Code (gambas)

  1. ' Gambas class file
  2.  
  3. Private $hATimer As New Timer As "ATimer"
  4.  
  5. Private iX As Integer ' logo X pos
  6. Private iY As Integer  ' logo Y pos
  7. Private bXRev As Boolean ' logo X direction (reverse toggle)
  8. Private bYRev As Boolean ' logo Y direction
  9. Private iMoveAmount As Integer = 2
  10.  
  11. Public Sub Form_Open()
  12.  
  13.   $hImg = Picture.Load("icon:/128/gambas")  ' load logo
  14.  
  15.   $hATimer.Delay = 20  ' (about 50fps)
  16.   $hATimer.Start
  17.  
  18.  
  19. Public Sub Form_Close()
  20.  
  21.   $hATimer.Stop
  22.  
  23.  
  24. Public Sub ATimer_Timer()
  25.  
  26.   ' increase or decrease X and Y positions depending on direction
  27.   ix += If(bXRev, -iMoveAmount, iMoveAmount)
  28.   iy += If(bYRev, -iMoveAmount, iMoveAmount)
  29.  
  30.  ' change X direction if on an edge
  31.   If ix < iMoveAmount Then bXRev = False
  32.   If ix > DrawingArea1.W - $hImg.W - iMoveAmount Then bXRev = True
  33.  
  34.  ' change Y direction if on an edge
  35.   If iY < iMoveAmount Then bYRev = False
  36.   If iY > DrawingArea1.H - $hImg.H - iMoveAmount Then bYRev = True
  37.  
  38.   DrawingArea1.Refresh  ' trigger redraw
  39.  
  40.  
  41. Public Sub DrawingArea1_Draw()
  42.  
  43.   ' nice gradient background
  44.   Paint.Brush = Paint.LinearGradient(0, 0, 0, Last.H, [Color.LightGray, Color.Gray, Color.DarkGray], [0, 0.5, 1])
  45.   Paint.Rectangle(0, 0, Last.W, Last.H)
  46.   Paint.Fill
  47.  
  48.   ' nice gradient logo background
  49.   Paint.Brush = Paint.LinearGradient(iX, iY, iX, iY + $hImg.H, [Color.Yellow, Color.Green], [0, 1])
  50.   Paint.Rectangle(iX, iY, $hImg.W, $hImg.H, 30)
  51.   Paint.Fill
  52.  
  53.   Paint.DrawPicture($hImg, iX, iY, $hImg.W, $hImg.H)
  54.  
  55.  
  56. Public Sub SpinBox1_Change()
  57.  
  58.   iMoveAmount = Last.Value
  59.  
  60.  
  61.  

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.

Attachment
Online now: No Back to the top

Post

Posted
Rating:
#4
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’

BruceSteers said

Code (gambas)

  1. ' Gambas class file
  2.  
  3. Private $hATimer As New Timer As "ATimer"
  4.  
  5. Private iX As Integer ' logo X pos
  6. Private iY As Integer  ' logo Y pos
  7. Private bXRev As Boolean ' logo X direction (reverse toggle)
  8. Private bYRev As Boolean ' logo Y direction
  9. Private iMoveAmount As Integer = 2
  10.  
  11. Public Sub Form_Open()
  12.  
  13.   $hImg = Picture.Load("icon:/128/gambas")  ' load logo
  14.  
  15.   $hATimer.Delay = 20  ' (about 50fps)
  16.   $hATimer.Start
  17.  
  18.  
  19. Public Sub Form_Close()
  20.  
  21.   $hATimer.Stop
  22.  
  23.  
  24. Public Sub ATimer_Timer()
  25.  
  26.   ' increase or decrease X and Y positions depending on direction
  27.   ix += If(bXRev, -iMoveAmount, iMoveAmount)
  28.   iy += If(bYRev, -iMoveAmount, iMoveAmount)
  29.  
  30.  ' change X direction if on an edge
  31.   If ix < iMoveAmount Then bXRev = False
  32.   If ix > DrawingArea1.W - $hImg.W - iMoveAmount Then bXRev = True
  33.  
  34.  ' change Y direction if on an edge
  35.   If iY < iMoveAmount Then bYRev = False
  36.   If iY > DrawingArea1.H - $hImg.H - iMoveAmount Then bYRev = True
  37.  
  38.   DrawingArea1.Refresh  ' trigger redraw
  39.  
  40.  
  41. Public Sub DrawingArea1_Draw()
  42.  
  43.   ' nice gradient background
  44.   Paint.Brush = Paint.LinearGradient(0, 0, 0, Last.H, [Color.LightGray, Color.Gray, Color.DarkGray], [0, 0.5, 1])
  45.   Paint.Rectangle(0, 0, Last.W, Last.H)
  46.   Paint.Fill
  47.  
  48.   ' nice gradient logo background
  49.   Paint.Brush = Paint.LinearGradient(iX, iY, iX, iY + $hImg.H, [Color.Yellow, Color.Green], [0, 1])
  50.   Paint.Rectangle(iX, iY, $hImg.W, $hImg.H, 30)
  51.   Paint.Fill
  52.  
  53.   Paint.DrawPicture($hImg, iX, iY, $hImg.W, $hImg.H)
  54.  
  55.  
  56. Public Sub SpinBox1_Change()
  57.  
  58.   iMoveAmount = Last.Value
  59.  
  60.  
  61.  

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 BruceSteers that was ideal for what I wanted and I would not have worked that out in 2 years  so thank you once again
Online now: No Back to the top
1 guest and 0 members have just viewed this.