back
#!/usr/local/bin/perl
# setup values:
# x, y - ball position
# m, n - ball velocity (m sideways, n up down).
# $| - output buffer control (necessary).
$x=$y=4;
$m=2;
$n=$|=1;
{
printf
"\ec". # clear the screen
"\e[$y;$x\H ". # draw a space in the last ball position
"\e[%d;%dH*". # draw the ball (%d sucks value from later on)
"\e[$l;H ". # draw space in last left paddle position
"\e[%dH$d". # draw left paddle (%d again sucks values from later on).
# $d is set later
"\e[$r;40H \e[%d;40H$d", # same for right paddle.
$y+=$n*=$y<2|$y>10?-1:1, # change the y value of the ball
# and push into %d above
$x+=$m*=$x<3|$x>37?-1:1, # same for x
$l+=($c=$l-($m>0?6:$y))?$c>0?-1:1:0, # move left paddle center
# if ball going away, otherwise move to $y
$r+=($c=$r-($m<0?6:$y))?$c>0?-1:1:0, # same for right paddle
# setup $d - draws the paddle, and for space reasons, also draws the net.
$d="\e[7m \e[m";
$d.="\e[$_;20H|\n"for(1..11);
select$q,$q,$q,0.1; # wait.
redo # and do again.
}
This compresses to this:
$m=2;$x=$y=4;$n=$|=1;{printf"\ec\e[$y;$x\H \e[%d;%dH*\e[$l;H \e[%dH$d\e"
."[$r;40H \e[%d;40H$d",$y+=$n*=$y<2|$y>10?-1:1,$x+=$m*=$x<3|$x>37?-1:1,$
l+=($c=$l-($m>0?6:$y))?$c>0?-1:1:0,$r+=($c=$r-($m<0?6:$y))?$c>0?-1:1:0,$
d="\e[7m \e[m";$d.="\e[$_;20H|\n"for(1..11);select$q,$q,$q,0.1;redo}