import time
import sys
import os
class CountdownTimer:
def __init__(self, seconds=60):
self.seconds = seconds
self.start_time = None
def clear_screen(self):
os.system('clear' if os.name == 'posix' else 'cls')
def format_time(self, seconds):
hours = seconds // 3600
minutes = (seconds % 3600) // 60
secs = seconds % 60
return f"{hours:02d}:{minutes:02d}:{secs:02d}"
def draw_progress_bar(self, current, total, width=50):
progress = current / total
filled = int(width * progress)
bar = "█" * filled + "░" * (width - filled)
percentage = progress * 100
return f"[{bar}] {percentage:.1f}%"
def display_countdown(self, remaining):
self.clear_screen()
print("\n" + "="*60)
print(" " * 20 + "🎯 倒计时程序 🎯")
print("="*60 + "\n")
time_str = self.format_time(remaining)
print(" " * 20 + "⏰ 剩余时间:")
print(" " * 20 + f" {time_str}")
print()
progress_bar = self.draw_progress_bar(self.seconds - remaining, self.seconds)
print(" " * 10 + progress_bar)
print()
elapsed = self.seconds - remaining
print(f"已过去: {self.format_time(elapsed)}")
print(f"总时长: {self.format_time(self.seconds)}")
print()
if remaining > 30:
print(" " * 15 + "🚀 倒计时进行中...")
elif remaining > 10:
print(" " * 15 + "⚡ 即将结束!")
else:
print(" " * 15 + "🔥 最后冲刺!")
print("\n" + "="*60)
def run(self):
print("倒计时开始!")
time.sleep(1)
self.start_time = time.time()
for i in range(self.seconds, -1, -1):
self.display_countdown(i)
if i > 0:
time.sleep(1)
else:
print("\n" + "="*60)
print(" " * 20 + "🎉 倒计时结束! 🎉")
print("="*60)
print("\n" + " " * 15 + "时间到!任务完成!")
print(" " * 15 + "🎊 恭喜! 🎊")
print()
for _ in range(3):
print(" " * 10 + "✨" * 10)
time.sleep(0.3)
self.clear_screen()
print(" " * 10 + "🎆" * 10)
time.sleep(0.3)
self.clear_screen()
def main():
try:
timer = CountdownTimer(30)
timer.run()
print("\n" + "="*60)
print("📊 统计信息:")
print(f"• 总倒计时时长: 30秒")
print(f"• 开始时间: {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())}")
print(f"• 结束时间: {time.strftime('%Y-%m-%d %H:%M:%S')}")
print("="*60)
except KeyboardInterrupt:
print("\n\n⚠️ 倒计时被用户中断")
except Exception as e:
print(f"\n❌ 发生错误: {e}")
if __name__ == "__main__":
main()