|
| 1 | +import pygame |
| 2 | +import sys |
| 3 | +import os |
| 4 | +import tkinter as tk |
| 5 | +from tkinter import filedialog, messagebox |
| 6 | + |
| 7 | +def select_sound(): |
| 8 | + """Open a file dialog to select a .mp3 or .wav file.""" |
| 9 | + root = tk.Tk() |
| 10 | + root.withdraw() # Hide the root window |
| 11 | + file_path = filedialog.askopenfilename( |
| 12 | + title="Select Sound File", |
| 13 | + filetypes=[("Audio Files", "*.mp3 *.wav")] |
| 14 | + ) |
| 15 | + return file_path if file_path else None |
| 16 | + |
| 17 | +def check_sound_duration(file_path): |
| 18 | + """Check if the sound file is less than or equal to 2 minutes.""" |
| 19 | + if file_path.endswith('.mp3') or file_path.endswith('.wav'): |
| 20 | + sound = pygame.mixer.Sound(file_path) |
| 21 | + duration = sound.get_length() |
| 22 | + if duration <= 120: # 120 seconds = 2 minutes |
| 23 | + return True |
| 24 | + else: |
| 25 | + messagebox.showerror("Invalid File", "The file exceeds 2 minutes.") |
| 26 | + return False |
| 27 | + return False |
| 28 | + |
| 29 | +def countdown_timer(initial_duration): |
| 30 | + pygame.init() |
| 31 | + font = pygame.font.SysFont('Arial', 60) |
| 32 | + small_font = pygame.font.SysFont('Arial', 24) # Font for showing sound file name |
| 33 | + button_font = pygame.font.SysFont('Arial', 30) # Font for button text |
| 34 | + width, height = 600, 700 # Window size with more height for button layout |
| 35 | + screen = pygame.display.set_mode((width, height)) |
| 36 | + pygame.display.set_caption('Bubbles The Dev - Countdown Timer') |
| 37 | + |
| 38 | + # Initialize mixer for sound playback |
| 39 | + pygame.mixer.init() |
| 40 | + |
| 41 | + sound_file = None |
| 42 | + sound_file_name = "No sound selected" |
| 43 | + peanut_butter = None |
| 44 | + |
| 45 | + clock = pygame.time.Clock() |
| 46 | + duration = initial_duration |
| 47 | + start_ticks = 0 |
| 48 | + is_running = False |
| 49 | + |
| 50 | + while True: |
| 51 | + for event in pygame.event.get(): |
| 52 | + if event.type == pygame.QUIT: |
| 53 | + pygame.quit() |
| 54 | + sys.exit() |
| 55 | + elif event.type == pygame.MOUSEBUTTONDOWN: |
| 56 | + # Handle button click to import sound |
| 57 | + if 100 <= event.pos[0] <= 500 and 580 <= event.pos[1] <= 630: |
| 58 | + selected_file = select_sound() |
| 59 | + if selected_file and check_sound_duration(selected_file): |
| 60 | + sound_file = selected_file |
| 61 | + peanut_butter = pygame.mixer.Sound(sound_file) |
| 62 | + peanut_butter.set_volume(0.26) |
| 63 | + sound_file_name = os.path.basename(sound_file) # Get just the file name |
| 64 | + |
| 65 | + # Increase the timer by 10 seconds |
| 66 | + elif 100 <= event.pos[0] <= 250 and 480 <= event.pos[1] <= 530: |
| 67 | + duration += 10 |
| 68 | + |
| 69 | + # Decrease the timer by 10 seconds, but don't go below 0 |
| 70 | + elif 350 <= event.pos[0] <= 500 and 480 <= event.pos[1] <= 530: |
| 71 | + duration = max(0, duration - 10) |
| 72 | + |
| 73 | + # Start the timer |
| 74 | + elif 100 <= event.pos[0] <= 250 and 530 <= event.pos[1] <= 580: |
| 75 | + if not is_running: |
| 76 | + is_running = True |
| 77 | + start_ticks = pygame.time.get_ticks() |
| 78 | + |
| 79 | + # Stop the timer |
| 80 | + elif 350 <= event.pos[0] <= 500 and 530 <= event.pos[1] <= 580: |
| 81 | + is_running = False |
| 82 | + |
| 83 | + # Clear the timer |
| 84 | + elif 100 <= event.pos[0] <= 500 and 630 <= event.pos[1] <= 680: |
| 85 | + duration = initial_duration # Reset the timer back to the initial value |
| 86 | + is_running = False # Stop the timer if it's running |
| 87 | + |
| 88 | + minutes, seconds = divmod(duration, 60) |
| 89 | + timer_str = '{:02d}:{:02d}'.format(minutes, seconds) |
| 90 | + |
| 91 | + screen.fill((0, 0, 0)) |
| 92 | + text = font.render(timer_str, True, (255, 255, 255)) |
| 93 | + text_rect = text.get_rect(center=(width // 2, height // 2 - 200)) |
| 94 | + screen.blit(text, text_rect) |
| 95 | + |
| 96 | + # Show the selected sound file |
| 97 | + sound_text = small_font.render(f"Playing sound: {sound_file_name}", True, (255, 255, 255)) |
| 98 | + sound_rect = sound_text.get_rect(center=(width // 2, height // 2 - 140)) |
| 99 | + screen.blit(sound_text, sound_rect) |
| 100 | + |
| 101 | + # First row: Increase, Decrease |
| 102 | + pygame.draw.rect(screen, (0, 255, 255), (100, 480, 150, 50)) # Increase button |
| 103 | + increase_text = button_font.render("+10 sec", True, (0, 0, 0)) |
| 104 | + increase_rect = increase_text.get_rect(center=(175, 505)) |
| 105 | + screen.blit(increase_text, increase_rect) |
| 106 | + |
| 107 | + pygame.draw.rect(screen, (255, 0, 0), (350, 480, 150, 50)) # Decrease button |
| 108 | + decrease_text = button_font.render("-10 sec", True, (0, 0, 0)) |
| 109 | + decrease_rect = decrease_text.get_rect(center=(425, 505)) |
| 110 | + screen.blit(decrease_text, decrease_rect) |
| 111 | + |
| 112 | + # Second row: Start, Stop |
| 113 | + pygame.draw.rect(screen, (0, 255, 0), (100, 530, 150, 50)) # Start button |
| 114 | + start_text = button_font.render("Start", True, (0, 0, 0)) |
| 115 | + start_rect = start_text.get_rect(center=(175, 555)) |
| 116 | + screen.blit(start_text, start_rect) |
| 117 | + |
| 118 | + pygame.draw.rect(screen, (255, 0, 0), (350, 530, 150, 50)) # Stop button |
| 119 | + stop_text = button_font.render("Stop", True, (0, 0, 0)) |
| 120 | + stop_rect = stop_text.get_rect(center=(425, 555)) |
| 121 | + screen.blit(stop_text, stop_rect) |
| 122 | + |
| 123 | + # Third row: Import Sound and Clear Timer |
| 124 | + pygame.draw.rect(screen, (0, 255, 0), (100, 580, 400, 50)) # Import Sound button |
| 125 | + button_text = button_font.render("Import Sound", True, (0, 0, 0)) |
| 126 | + button_rect = button_text.get_rect(center=(300, 605)) |
| 127 | + screen.blit(button_text, button_rect) |
| 128 | + |
| 129 | + pygame.draw.rect(screen, (255, 255, 0), (100, 630, 400, 50)) # Clear Timer button |
| 130 | + clear_text = button_font.render("Clear Timer", True, (0, 0, 0)) |
| 131 | + clear_rect = clear_text.get_rect(center=(300, 655)) |
| 132 | + screen.blit(clear_text, clear_rect) |
| 133 | + |
| 134 | + pygame.display.update() |
| 135 | + |
| 136 | + # Check the time elapsed only if the timer is running |
| 137 | + if is_running: |
| 138 | + elapsed_time = (pygame.time.get_ticks() - start_ticks) / 1000 |
| 139 | + if elapsed_time >= 1 and duration > 0: |
| 140 | + duration -= 1 |
| 141 | + start_ticks = pygame.time.get_ticks() # Reset after each second |
| 142 | + |
| 143 | + clock.tick(30) # Reduces CPU usage |
| 144 | + |
| 145 | + # Play the sound when timer is at 40 seconds |
| 146 | + if duration == 40 and peanut_butter and not pygame.mixer.get_busy(): # Play once when the timer hits 40 seconds |
| 147 | + peanut_butter.play() |
| 148 | + |
| 149 | + # Stop the timer when it reaches 0 |
| 150 | + if duration == 0: |
| 151 | + is_running = False |
| 152 | + break |
| 153 | + |
| 154 | + pygame.quit() |
| 155 | + |
| 156 | +if __name__ == "__main__": |
| 157 | + countdown_timer(60) # Initial timer duration set to 60 seconds |
0 commit comments