# pseudo-random bounce movements

## Recommended Posts

I'm sure you have seen those objects that bounce around the screen in your screensaver. I've been trying to do something like it just for fun...

Does anyone know how i could do this?

```#Include<GUIConstants.au3>

\$w = 10
\$h = 10
\$l = 0
\$t = 0
\$speed = 1
\$bggui = GUICreate("ball", \$w, \$h, \$l, \$t, \$WS_POPUP, \$WS_EX_TOPMOST )
GuiSetState( )
GuiSetBKColor ( 0x000000 )

While 1

\$pos = WInGetPos( "ball" )

;movements

WEnd```

maybe that will help a little for the foundation...but the hard part is still left... ive come kind of close but i dont think it was what i was trying to do so i cut that part out... any help would be great...

Edited by datkewlguy
##### Share on other sites

Since you're doing this for fun, I won't write running code. Otherwise I'd steal your fun! Imagine your square is 100 x 100 (or 0->99 by 0->99). Imagine your circle is three pixels in radius.

```Pseudocode:
width = 99
height = 99
movementX = 1
movementY = 1
centerX = 3 (leave room for three pixels to left)
centerY = 3 (ditto for top)

loop start
show circle at centerX x centerY
centerX = centerX  + movementX
centerY = centerY + movementY

if ( (centerX - radius ) == 0) or ((centerX + radius) == width)
movementX = movementX * -1 (reverse direction)

if ( (centerY - radius ) == 0) or ((centerY + radius) == height)
movementY = movementY * -1 (reverse direction)

loop end```

This will cause a constant diagonal movement. Make sure your width and height is not evenly divisible by your radius, otherwise you'll get a diamond pattern.

Post back when done!!

How's my riding? Dial 1-800-Wait-There

Trying to use a computer with McAfee installed is like trying to read a book at a rock concert.

##### Share on other sites

Here is one way of doing it. There are far better ways. I used a simple random to determine the bounce.

Basically a straight movement is a simple ratio. when it gets to the top or bottom, invert the amount of horizontal adjustment. ( subtract if adding, add iif you were subtracting)

Do the same for the vertical movement.

I gave a random -5 to 5 ratio adjustment to the horizontal factor each time it hits top or bottom, and same for the vertical. This allowed a change in the angle, but a beliveable one (not like a superball spin type bounce.)

If you want to get all mathy, you might use one of these type (Cos, Tan, ASin, ACos, ATan) Now that you have the idea, try your hand at it.

```\$random=Random(1,40)
\$x=0
\$y=0
\$y1=1
\$x1=1
\$r1=10
\$r2=2+\$random

while 1
\$x=\$x+\$r1/\$r2*\$x1
\$y=\$y+\$r2/\$r1*\$y1
if \$x>@Desktopwidth or \$x<0 then
\$x1=\$x1*(-1)
\$r1=\$r1 + Random(-5,5)
EndIf

if \$y>@DesktopHeight or \$y<0 then
\$y1=\$y1*(-1)
\$r2=\$r2 + Random(-5,5)
EndIf

tooltip("O",\$x,\$y)
sleep(10)
WEnd```

AutoIt3, the MACGYVER Pocket Knife for computers.

##### Share on other sites

hey thanks everybody, here is what i was able to come up with, i want to now create a paddle gui that can interact with the ball (i did this with the mouse but it was kind of glitchy) if anyone knows how or has other general improvements, they would be greatly appreciated... thx

code:

```#Include<GUIConstants.au3>

HotKeySet("^e", "myexit")

\$w = 10
\$h = 10
\$l = 0
\$t = (@desktopheight) -\$h
\$speed = 1
\$bggui = GUICreate("ball", \$w, \$h, \$l, \$t, \$WS_POPUP, \$WS_EX_TOPMOST + \$WS_EX_TOOLWINDOW + \$WS_EX_CLIENTEDGE )

\$width = (@desktopwidth) -\$w
\$height = (@desktopheight) -\$h
\$movementX = 1
\$movementY = 1
\$centerX = 3
\$centerY = 3

GuiSetState( )
GuiSetBKColor ( 0xFF0000 )

While 1

\$pos = WInGetPos( "ball" )
\$x = \$pos
\$y = \$pos

\$centerX = \$centerX  + \$movementX
\$centerY = \$centerY + \$movementY

if ( (\$centerX - \$radius ) == 0) or ((\$centerX + \$radius) == \$width) Then \$movementX = \$movementX * -1

if ( (\$centerY - \$radius ) == 0) or ((\$centerY + \$radius) == \$height) Then \$movementY = \$movementY * -1

WinMove( "ball", "", \$x + \$movementX, \$y - \$movementY )

Sleep(10)

WEnd

func myexit()
Exit
endfunc```
##### Share on other sites

OMG! Pong! Or at least its evil twin Squash. How soon 'till we can start organizing the International NetPong Tournament? No sarcasm there... I wanna play! 601DisengageEnd Program

##### Share on other sites

lol i know i cant wait to create Desktop Pong... the pong where 2 ppl can play against eachother no matter what else they're doing, with every so many seconds the speed would increase... someone help me, im maxed out after doing this much so my brain is FRIED... lol ##### Share on other sites

I swear, I tried really really hard to make only minimal code changes to what you had already written, and I failed misserably. Here's a version with a paddle.

```#Include<GUIConstants.au3>

HotKeySet("^e", "myexit")

\$w = 9
\$h = 9

\$ph = 8

;\$bggui = GUICreate("ball", \$w, \$h, \$w, \$h, \$WS_POPUP, \$WS_EX_TOPMOST + \$WS_EX_TOOLWINDOW + \$WS_EX_CLIENTEDGE )
\$bggui = GUICreate("ball", \$w, \$h, \$w, \$h, \$WS_POPUP, \$WS_EX_TOPMOST + \$WS_EX_TOOLWINDOW)
GuiSetState ()
GuiSetBKColor (0xFF0000, \$bggui)
;\$pg1gui = GUICreate("paddle", \$w, \$ph*\$h, \$w, @desktopheight - \$ph*\$h/2, \$WS_POPUP, \$WS_EX_TOPMOST + \$WS_EX_TOOLWINDOW + \$WS_EX_CLIENTEDGE )
\$pg1gui = GUICreate("paddle", \$w, \$ph*\$h, \$w, @desktopheight - \$ph*\$h/2, \$WS_POPUP, \$WS_EX_TOPMOST + \$WS_EX_TOOLWINDOW)
GuiSetState ()
GuiSetBKColor (0x00FF00, \$pg1gui)

\$width = @desktopwidth
\$height = @desktopheight - 30; taskbar should be sacrosanct
\$bDeltaX = 1
\$bDeltaY = 1

While 1
\$bpos = WinGetPos ("ball")
\$bX = \$bpos + \$w/2
\$bY = \$bpos + \$h/2

\$p1pos = MouseGetPos ()
\$p1X = \$p1pos
\$p1Y = \$p1pos
If \$p1pos > (\$height - \$ph*\$h/2) Then
\$p1Y = \$height - \$ph*\$h/2
ElseIf \$p1pos < \$ph*\$h/2 Then
\$p1Y = \$ph*\$h/2
EndIf

WinMove ("paddle", "", \$p1X - \$w/2, \$p1Y - \$ph*\$h/2)
\$bX = \$bX + \$bDeltaX
\$bY = \$bY + \$bDeltaY
Select
Case (\$bX - \$w/2) < 0 ; Ball hits Left Edge of Desktop
MsgBox (0, "Point Scored!", "Right side scores a point")
\$bx = \$width - \$w
\$bDeltaX = -1
Case (\$bX + \$w/2) > \$width ; Ball hits Right Edge of Desktop
MsgBox (0, "Point Scored!", "Left Side scores a point")
\$bx = \$w
\$bDeltaX = 1
Case ((\$bY - \$h/2) < 0) Or ((\$bY + \$h/2) > \$height) ; Ball hits Top or Bottom
\$bDeltaY = -\$bDeltaY
Case (Abs (\$bX - \$p1X) < \$w) And (Abs (\$bY - \$p1Y) < \$h*\$ph/2) ; Ball is too near paddle
\$bDeltaX = -\$bDeltaX
EndSelect
WinMove ("ball", "", \$bx - \$w/2, \$by - \$h/2 )
Sleep(10)
WEnd

func myexit()
Exit
endfunc```

Edited by Smed

601DisengageEnd Program

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...