Start clock class


/ Published in: Python
Save to your folder(s)

This class implements a clock for counting down the starting sequence of a sailing race start. There are methods for adjusting the clock before and after it has been started. Could be used with text, graphic or audio interfaces.


Copy this code and paste it in your HTML
  1. class YachtTimer:
  2. running = 0
  3. time_ceil = 0
  4. time_start = None
  5. start_from = -300
  6.  
  7. def seconds(self):
  8. if self.running:
  9. return(self.start_from + time.clock() - self.time_start)
  10. else:
  11. return(self.start_from)
  12.  
  13. def reset(self, s):
  14. self.running = 0
  15. self.start_from = s
  16.  
  17. def start(self):
  18. if not self.running:
  19. self.running = 1
  20. self.time_start = time.clock()
  21.  
  22. def adjust(self, sec):
  23. if self.running:
  24. self.time_start -= sec
  25. else:
  26. self.start_from += sec
  27.  
  28. def ceil(self):
  29. if self.running:
  30. skip = self.seconds()%60
  31. if time.clock()-self.time_ceil < 1:
  32. skip += 60
  33. self.adjust(-skip)
  34. self.time_ceil = time.clock()
  35. else:
  36. self.start_from -= 60
  37.  
  38. def floor(self):
  39. if self.running:
  40. skip = self.seconds()%60
  41. self.adjust(60-skip)
  42. else:
  43. self.start_from += 60
  44.  
  45. def round(self):
  46. skip = self.seconds()%60
  47. if skip > 30:
  48. self.adjust(60-skip)
  49. else:
  50. self.adjust(-skip)

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.