#!/bin/sh -u # falling object demo showing use of acceleration and deltay variables # # $0 [ deltay [ accel ] ] # # This loop demonstrates an object "falling" from the top of the # screen to the bottom. Each iteration of the loop causes the # acceleration of the object to be added to the change in Y ($deltay). # # You can start the deltay at the value given on the command line. A # value of 0 lets the object fall from a standing start. A negative # value (try -6) causes the object start with upward motion, as if it # had been thrown up in the air. (Of course you won't see the object # until it falls back into the range of the terminal screen.) # # The accel argument determines how fast the object falls (how strong # gravity is pulling it down each iteration). Default is "1". # Negative acceleration causes the object to be "pushed" up instead of # falling (and the loop will never finish). Try this command line: $0 5 -1 # and watch the object fall down briefly, slow, stop, then go up forever. # # NOTE: This script fragment is incomplete - it does not follow all # the script guidelines set out in "script_style.txt". It also # contains many "Instructor-Style" comments that you must not use in # your own scripts. See script_style.txt for details. # # -IAN! idallen@idallen.ca PATH=/bin:/usr/bin ; export PATH LC_COLLATE=C ; export LC_COLLATE LANG=C ; export LANG umask 022 cols=$( tput cols ) lines=$( tput lines ) let xlimit=cols-1 let ylimit=lines-1 tput clear # Set X to display the output values half-way across the terminal # screen. Object's Y position starts at top of screen with y=0. Current # change in Y (Delta-Y) starts at value given on command line (default # is 0) and increases by the value of $accel on each iteration. # Acceleration can be second argument on command line (default is 1). # Acceleration is added to $deltay each loop iteration. Negative # acceleration will "push" the object up, and the loop will never end. # let x=xlimit/2 let y=0 let deltay=${1-"0"} # default deltay value is 0 if argument $1 is missing let accel=${2-"1"} # default accel value is 1 if argument $2 is missing while : ; do let y=y+deltay let deltay=deltay+accel # In this script, the display values of X,Y are the same as the # program values, except when the program values don't fit on the # terminal screen. The display values must be non-negative and fit # on terminal screen, so we have code that limits the display values # (not the program values!) if the display values go out-of-bounds. # The "tput el" clears from cursor to end-of-line. # let dispx=x [ $dispx -lt 0 ] && let dispx=0 [ $dispx -gt $xlimit ] && let dispx=xlimit let dispy=y [ $dispy -lt 0 ] && let dispy=0 [ $dispy -gt $ylimit ] && let dispy=ylimit tput cup $dispy $dispx tput el # clear from cursor position to end-of-line echo -n "* y=$y dispy=$dispy deltay=$deltay" # Break out of the loop when the object hits the bottom line. # If $accel is negative, this may never happen! # [ $dispy -ge $ylimit ] && break sleep 0.5 done # Put the cursor at the start of the last line on the screen. # tput cup $ylimit 0