Jump to content

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?


$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" )


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
Link to post
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.

width = 99
height = 99
radius = 3
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.

Link to post
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.


while 1
    if $x>@Desktopwidth or $x<0 then 
        $r1=$r1 + Random(-5,5)
    if $y>@DesktopHeight or $y<0 then 
        $r2=$r2 + Random(-5,5)

AutoIt3, the MACGYVER Pocket Knife for computers.

Link to post
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



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
$radius = 3
$movementX = 1
$movementY = 1
$centerX = 3
$centerY = 3

GuiSetState( )
GuiSetBKColor ( 0xFF0000 )

While 1
$pos = WInGetPos( "ball" )
$x = $pos[0]
$y = $pos[1]

$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 )

func myexit()
Link to post
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

Link to post
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 :(

Link to post
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.


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[0] + $w/2
  $bY = $bpos[1] + $h/2

  $p1pos = MouseGetPos ()
  $p1X = $p1pos[0]
  $p1Y = $p1pos[1]
  If $p1pos[1] > ($height - $ph*$h/2) Then
    $p1Y = $height - $ph*$h/2
  ElseIf $p1pos[1] < $ph*$h/2 Then
    $p1Y = $ph*$h/2

  WinMove ("paddle", "", $p1X - $w/2, $p1Y - $ph*$h/2)
  $bX = $bX + $bDeltaX
  $bY = $bY + $bDeltaY
    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
  WinMove ("ball", "", $bx - $w/2, $by - $h/2 )

func myexit()

EDIT: new and improved paddle code now doesn't interfere with taskbar.

Edited by Smed

601DisengageEnd Program

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...