[Solved] Move a picturebox randomly around screen

Post your Gambas programming questions here.
Post Reply
AndyGable
Posts: 406
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

[Solved] Move a picturebox randomly around screen

Post by AndyGable »

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)
Image
I would like to bounce the picturebox around with in the 1024x768 screen size (AND NOT have the image go off the screen)
Last edited by AndyGable on Tuesday 4th February 2025 10:10pm, edited 1 time in total.
User avatar
BruceSteers
Posts: 2000
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Move a picturebox randomly around screen

Post by BruceSteers »

So what's your question?

maybe this topic will help.
https://forum.gambas.one/viewtopic.php?p=8503#p8503
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 2000
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Move a picturebox randomly around screen

Post by BruceSteers »

' Gambas class file

Private $hImg As Picture
Private $hATimer As New Timer As "ATimer"

Private iX As Integer ' logo X pos
Private iY As Integer  ' logo Y pos
Private bXRev As Boolean ' logo X direction (reverse toggle)
Private bYRev As Boolean ' logo Y direction
Private iMoveAmount As Integer = 2

Public Sub Form_Open()

  $hImg = Picture.Load("icon:/128/gambas")  ' load logo

  $hATimer.Delay = 20  ' (about 50fps)
  $hATimer.Start

End

Public Sub Form_Close()

  $hATimer.Stop

End

Public Sub ATimer_Timer()

  ' increase or decrease X and Y positions depending on direction
  ix += If(bXRev, -iMoveAmount, iMoveAmount)
  iy += If(bYRev, -iMoveAmount, iMoveAmount)

 ' change X direction if on an edge
  If ix < iMoveAmount Then bXRev = False
  If ix > DrawingArea1.W - $hImg.W - iMoveAmount Then bXRev = True

 ' change Y direction if on an edge
  If iY < iMoveAmount Then bYRev = False
  If iY > DrawingArea1.H - $hImg.H - iMoveAmount Then bYRev = True

  DrawingArea1.Refresh  ' trigger redraw

End

Public Sub DrawingArea1_Draw()

  ' nice gradient background
  Paint.Brush = Paint.LinearGradient(0, 0, 0, Last.H, [Color.LightGray, Color.Gray, Color.DarkGray], [0, 0.5, 1])
  Paint.Rectangle(0, 0, Last.W, Last.H)
  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)

End

Public Sub SpinBox1_Change()

  iMoveAmount = Last.Value

End



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.
Attachments
_aa (3)-0.0.tar.gz
(8.46 KiB) Downloaded 132 times
If at first you don't succeed , try doing something differently.
BruceS
AndyGable
Posts: 406
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

Re: Move a picturebox randomly around screen

Post by AndyGable »

BruceSteers wrote: Tuesday 4th February 2025 2:31pm
' Gambas class file

Private $hImg As Picture
Private $hATimer As New Timer As "ATimer"

Private iX As Integer ' logo X pos
Private iY As Integer  ' logo Y pos
Private bXRev As Boolean ' logo X direction (reverse toggle)
Private bYRev As Boolean ' logo Y direction
Private iMoveAmount As Integer = 2

Public Sub Form_Open()

  $hImg = Picture.Load("icon:/128/gambas")  ' load logo

  $hATimer.Delay = 20  ' (about 50fps)
  $hATimer.Start

End

Public Sub Form_Close()

  $hATimer.Stop

End

Public Sub ATimer_Timer()

  ' increase or decrease X and Y positions depending on direction
  ix += If(bXRev, -iMoveAmount, iMoveAmount)
  iy += If(bYRev, -iMoveAmount, iMoveAmount)

 ' change X direction if on an edge
  If ix < iMoveAmount Then bXRev = False
  If ix > DrawingArea1.W - $hImg.W - iMoveAmount Then bXRev = True

 ' change Y direction if on an edge
  If iY < iMoveAmount Then bYRev = False
  If iY > DrawingArea1.H - $hImg.H - iMoveAmount Then bYRev = True

  DrawingArea1.Refresh  ' trigger redraw

End

Public Sub DrawingArea1_Draw()

  ' nice gradient background
  Paint.Brush = Paint.LinearGradient(0, 0, 0, Last.H, [Color.LightGray, Color.Gray, Color.DarkGray], [0, 0.5, 1])
  Paint.Rectangle(0, 0, Last.W, Last.H)
  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)

End

Public Sub SpinBox1_Change()

  iMoveAmount = Last.Value

End



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
Post Reply