ASCII Ball in Box Animation
Ruby 1.9, 115 characters
The movement logic is quite similar to Danko's answer.
This version has been tested on Linux only.
p=0
loop{u=(?|+?\s*18+"|
")*8
u[165-21*(7-p%14).abs-(17-p%34).abs]=?*
p+=1
puts"\e[2J",r=?++?-*18+?+,u,r
sleep 0.1}
Powershell, 144 characters
Based on Joey's excellent answer, using the fact that the ball coordinates are a function of the frame index (i), so if you have something like x=n-abs(n-(i mod (2*n)))
, x will go from 0 to n, back to 0, and so on...
for(){cls
($l="+$('-'*18)+")
7..0|%{$j=$_
"|$(-join(17..0|%{'* '[$j-[Math]::abs(7-$i%14)-or$_-[Math]::abs(17-$i%34)]}))|"}
$l;$i++;sleep -m 100}
Ruby (179 174 147)
EDIT got rid of some more chars:
l=?++?-*18+?++?\n
c=?|+?\s*18+?|+?\n
p=22
u=v=1
loop{f=l+c*8+l
f[p]=?*
puts"\e[2J"+f
p+=(u=f[p+u]==' '?u:-u)+21*(v=f[p+21*v]==' '?v:-v)
sleep 0.1}
EDIT shaved off some chars, now 174:
l="+#{'-'*18}+\n";f=l+"|#{' '*18}|\n"*8+l;x=y=u=v=1
loop{f[21*y+x]='*';$><<"\e[2J\e[f"+f;f[21*y+x]=' '
u=f[21*y+x+u]==' '?u:-u;v=f[21*(y+v)+x]==' '?v:-v
x+=u;y+=v;sleep 0.1}
Ungolfed:
l="+#{'-'*18}+\n" # top and bottom lines
f=l+"|#{' '*18}|\n"*8+l # complete box as string
x=y=u=v=1 # x,y position; u,v next move
loop { #
f[21*y+x]='*' # add ball to box
$> << "\e[2J\e[f"+f # clear screen and print box with ball
f[21*y+x]=' ' # remove ball from box
u=f[21*y+x+u]==' '?u:-u # next move in x direction
v=f[21*(y+v)+x]==' '?v:-v # next move in y direction
x+=u # perform move
y+=v # --"--
sleep 0.1 # .zZZ...
} #