|
| 1 | +import datetime |
| 2 | +import time |
| 3 | +import pygame |
| 4 | + |
| 5 | +def set_alarm(alarm_time): |
| 6 | + print(f"Alarm set for {alarm_time}") |
| 7 | + |
| 8 | + while True: |
| 9 | + # Get the current time |
| 10 | + current_time = datetime.datetime.now().strftime("%H:%M:%S") |
| 11 | + print(f"Current Time: {current_time}", end="\r") |
| 12 | + |
| 13 | + # Check if the current time matches the alarm time |
| 14 | + if current_time == alarm_time: |
| 15 | + print("\nWake up! It's time!") |
| 16 | + |
| 17 | + # Initialize pygame mixer and play the alarm sound |
| 18 | + pygame.mixer.init() |
| 19 | + pygame.mixer.music.load('alarm_sound.mp3') # Make sure your alarm sound is in the same directory |
| 20 | + pygame.mixer.music.play() |
| 21 | + |
| 22 | + # Wait for the music to finish |
| 23 | + while pygame.mixer.music.get_busy(): |
| 24 | + time.sleep(1) |
| 25 | + |
| 26 | + break |
| 27 | + |
| 28 | + # Wait for 1 second before checking the time again |
| 29 | + time.sleep(1) |
| 30 | + |
| 31 | +if __name__ == "__main__": |
| 32 | + # User input for the alarm time (HH:MM:SS format) |
| 33 | + alarm_time = input("Enter the alarm time (HH:MM:SS): ") |
| 34 | + set_alarm(alarm_time) |
0 commit comments