User manual
time.sleep(ANIMATIONDELAY)
If the new length is greater than the old length, we increment until the new length is
reached. Similarly, if the new length is less than the old length, we decrement. Each time,
wait a fraction of a second for the animation effect.
Incrementing is incredibly simple: just add one bar and display it. The process of setting the
cursor position and displaying a character comes up several times, so I refactored it into a
little helper-function called ShowBars.
def ShowBars(row,col,numBars):
GotoXY(row,col)
if numBars==0:
SendChar(' ')
else:
SendByte(numBars-1,True)
def IncrementHBar(row,length):
#increase the number of horizontal bars by one
fullChars = length / 5
bars = length % 5
bars += 1 #add one bar
ShowBars(row,fullChars,bars)
Decrementing is a little trickier, because the number of bars in the final character cannot be
allowed to go below zero . For example, a value of 25 has 5 full characters evenly with no
extra bars. When one is subtracted, and the number of full characters decreases and the
number of bars goes to 4:
def DecrementHBar(row,length):
#reduce the number of horizontal bars by one
fullChars = length / 5
bars = length % 5
bars -= 1
if bars<0:
bars = 4
fullChars -= 1
ShowBars(row,fullChars,bars)
That works, but looks a bit cumbersome. Whenever things look messy, I like to play around
with the code a bit. Sometimes a simpler and more intuitive solution will present itself. In
this case, try doing the length decrement first, and let our integer divide and modulo
functions do all the work:
def DecrementHBar(row,length):
#reduce the number of horizontal bars by one
length -= 1 #subtract one bar
fullChars = length / 5
bars = length % 5
ShowBars(row,fullChars,bars)
That looks much better. Sometimes you can get ‘stuck’ because of prior assumptions and
decisions. Don’t be afraid of starting over or reworking the code if it doesn’t flow the way
you want.