Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save damncabbage/a84d37ffa192c1d102c1431abdc83c6f to your computer and use it in GitHub Desktop.

Select an option

Save damncabbage/a84d37ffa192c1d102c1431abdc83c6f to your computer and use it in GitHub Desktop.

Revisions

  1. @psygnisfive psygnisfive revised this gist Jul 3, 2019. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion punch_tape_animation.py
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,4 @@
    import sys
    import subprocess as sp
    from time import sleep
    import random
    import cursor
  2. @psygnisfive psygnisfive created this gist Jul 3, 2019.
    90 changes: 90 additions & 0 deletions punch_tape_animation.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,90 @@
    import sys
    import subprocess as sp
    from time import sleep
    import random
    import cursor



    class Renderer:

    def __init__(self, width, height):
    self.width = int(width)
    self.height = int(height)
    self.content = [ width * ' ' for i in range(0,height) ]
    self.currentX = 0
    self.currentY = 0

    def render(self):
    self.moveToOrigin()
    sys.stdout.write('\n'.join(self.content))
    sys.stdout.flush()
    self.currentX = len(self.content[0])
    self.currentY = len(self.content) - 1

    def moveToOrigin(self):
    self.moveBy(-self.currentX,-self.currentY)

    def moveBy(self,x,y):
    self.currentX += x
    self.currentY += y

    if x < 0:
    xMove = '\033[%iD' % -x
    elif x > 0:
    xMove = '\033[%iC' % x
    else:
    xMove = ''

    if y < 0:
    yMove = '\033[%iA' % -y
    elif y > 0:
    yMove = '\033[%iB' % y
    else:
    yMove = ''

    #print(x,y)
    sys.stdout.write(xMove + yMove)

    def setChar(self, char):
    self.setCharAt(self.currentX, self.currentY, char)

    def setCharAt(self, x, y, char):
    self.content[y] = self.content[y][0:x] + char[0] + self.content[y][x+1:]

    def setContent(self, content):
    self.content = content

    cursor.hide()
    try:

    print()

    w = 80
    h = 7
    framerate = 3
    renderer = Renderer(w,h)

    spoke_hole = '◌'
    data_holes = ' ●'

    lines = [[],[],[],[],[],[],[]]

    while True:
    renderer.setContent([ ''.join(lines[i]) for i in range(0,h) ])
    renderer.render()
    for i in range(0,h):
    if i == 6:
    lines[i] += [' ',' ']
    elif i == 2:
    lines[i] = [spoke_hole,' '] + lines[i]
    else:
    lines[i] = [random.choice(data_holes),' '] + lines[i]

    lines[i] = lines[i][0:w]
    sleep(1/framerate)

    except KeyboardInterrupt: pass
    except Exception: pass

    cursor.show()