Carcigenicate

Wave Test

Apr 5th, 2014
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.50 KB | None | 0 0
  1. import threading
  2. from time import sleep
  3. from random import randint
  4. import string
  5.  
  6. class Bouncer (threading.Thread):
  7.     def __init__ (self):
  8.         threading.Thread.__init__ (self)
  9.         self.max = 59 #Max width of the screen
  10.         self.sleeptime = 50 #MS delay. Sets wave speed
  11.         self.sleeplock = threading.Lock ()
  12.         self.chars = string.ascii_letters + string.digits + string.punctuation
  13.  
  14.     def rand_char (self):
  15.         return self.chars [randint (0, len (self.chars) - 1)]
  16.  
  17.     def set_rate (self, nvalue):
  18.         with self.sleeplock:
  19.             try:
  20.                 nvalue = int (nvalue)
  21.                 self.sleeptime = nvalue
  22.             except ValueError as e:
  23.                 self.sleeptime = 2000
  24.                 print ("Incorrect input type:", e)
  25.            
  26.     def wave (self):
  27.         pad = 0
  28.         dir = 'r'
  29.         padchar = ' '
  30.         while True:
  31.             if dir == 'l':
  32.                 pad += -1
  33.             elif dir =='r':
  34.                 pad += 1
  35.  
  36.             if randint (0, 1000) == 0:
  37.                 padchar = '!'
  38.  
  39.             yield (padchar * pad)
  40.  
  41.             if pad > self.max:
  42.                 dir = 'l'
  43.             elif pad < 0:
  44.                 dir = 'r'
  45.             padchar = ' '
  46.  
  47.     def run (self):
  48.         for p in self.wave ():
  49.             print (p + self.rand_char ())
  50.             with self.sleeplock:
  51.                 sleep (self.sleeptime / 1000)
  52.  
  53. wave = Bouncer ()
  54. wave.start ()
  55.  
  56. while True:
  57.     wave.set_rate (input (""))
Advertisement
Add Comment
Please, Sign In to add comment