-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlarm_Clock.py
More file actions
50 lines (39 loc) · 1.33 KB
/
Alarm_Clock.py
File metadata and controls
50 lines (39 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import pygame
import datetime
import time
# Initialize pygame mixer for sound
pygame.mixer.init()
# Load your alarm sound (you can replace with your own file)
# You can use .wav or .mp3
pygame.mixer.music.load("vbgm.mp3")
# Function to play alarm sound
def play_alarm():
pygame.mixer.music.play(-1) # -1 means loop the sound
print("⏰ Alarm is ringing! Press Ctrl+C to stop.")
# Get user input for alarm time
alarm_time = input("Enter alarm time (HH:MM:SS) in 24-hour format: ")
# Validate and parse input
try:
alarm_hour, alarm_minute, alarm_second = map(int, alarm_time.split(":"))
except ValueError:
print("❌ Invalid time format. Please use HH:MM:SS")
exit()
print(f"✅ Alarm set for {alarm_hour:02d}:{alarm_minute:02d}:{alarm_second:02d}")
# Main loop to check time
try:
while True:
# Get current time
now = datetime.datetime.now()
current_time = now.strftime("%H:%M:%S")
# Print current time (optional)
print("Current Time:", current_time, end="\r")
# Check if alarm time matches current time
if (now.hour == alarm_hour and
now.minute == alarm_minute and
now.second == alarm_second):
play_alarm()
break
time.sleep(1)
except KeyboardInterrupt:
print("\n🛑 Alarm cancelled.")
pygame.mixer.music.stop()