Skip to content

Commit 2129e28

Browse files
authored
Add files via upload
Signed-off-by: Bubbles The Dev <[email protected]>
1 parent 3f55458 commit 2129e28

File tree

2 files changed

+255
-0
lines changed

2 files changed

+255
-0
lines changed

main.py

+157
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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

readme.md

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Countdown Timer with Sound
2+
3+
A Python-based countdown timer built using Pygame and Tkinter. This application allows users to adjust the timer, import sound files (`.mp3` or `.wav`), and play a sound when the timer reaches a specific time. The user-friendly interface includes buttons to start, stop, increase, decrease, and clear the timer.
4+
5+
## 📥 How to Download the Repo for First-Time Users
6+
7+
- Click the link to read [**Instructions**](https://www.gitprojects.fnbubbles420.org/how-to-download-repos).
8+
9+
## Features
10+
11+
- Adjustable countdown timer.
12+
- Import sound files (`.mp3` or `.wav`) to play when the timer reaches a set time.
13+
- Simple, user-friendly interface built with Pygame.
14+
- Buttons to start, stop, increase, decrease, and reset the timer.
15+
16+
## Requirements
17+
18+
- Python 3.11
19+
- Pygame
20+
- Tkinter (usually comes pre-installed with Python on most systems)
21+
22+
### If you dont have a pet python here is a couple below:
23+
- **YOU ONLY NEED ONLY VERSION OF PYTHON TO RUN THIS !!**
24+
- [Python 3.11.6](https://github.com/KernFerm/Py3.11.6installer)
25+
- [Python 3.11.9](https://github.com/KernFerm/Py3.11.9installer)
26+
- [Python 3.12.1](https://github.com/KernFerm/Py3.12.1-installer-batch)
27+
28+
## Installation
29+
30+
1. **Clone the repository**:
31+
```
32+
git clone https://github.com/kernferm/countdown-timer-obs.git
33+
cd countdown-timer-obs
34+
```
35+
36+
2. **Install dependencies:** Install the required dependencies using pip:
37+
```
38+
pip install pygame
39+
```
40+
```
41+
pip install tk
42+
```
43+
44+
3. **Run the script:** Once dependencies are installed, run the script using Python:
45+
```
46+
python main.py
47+
```
48+
49+
## Usage
50+
51+
- **Adjust Timer:** Use the `+10 sec` and `-10 sec` buttons to increase or decrease the countdown time.
52+
- **Start/Stop Timer:** Use the `Start` button to start the countdown, and the `Stop` button to pause it.
53+
- **Import Sound:** Use the Import Sound button to select a `.mp3` or `.wav` file that will play when the timer reaches a specific time (e.g., 40 seconds).
54+
- **Clear Timer:** Use the `Clear Timer` button to reset the timer to its initial duration.
55+
56+
## Example
57+
When running the script, the interface will display:
58+
59+
- A countdown timer.
60+
- Buttons to adjust the timer, `start/stop` it, `import` a sound, and `reset` the timer.
61+
- Information about the sound file that is selected.
62+
63+
### Timer Controls
64+
65+
| Button | Description |
66+
|----------------|------------------------------------------|
67+
| **+10 sec** | Increases the countdown timer by 10 sec |
68+
| **-10 sec** | Decreases the countdown timer by 10 sec |
69+
| **Start** | Starts the countdown timer |
70+
| **Stop** | Pauses the countdown timer |
71+
| **Import Sound** | Imports a `.mp3` or `.wav` sound file |
72+
| **Clear Timer** | Resets the timer to the initial value |
73+
74+
## Code Overview
75+
**Timer Functionality**
76+
- The countdown timer is updated every second, and the user can adjust the time using the buttons:
77+
```
78+
# Increase the timer by 10 seconds
79+
duration += 10
80+
81+
# Decrease the timer by 10 seconds
82+
duration = max(0, duration - 10)
83+
```
84+
85+
## Sound Import
86+
- The user can import `.mp3` or `.wav` files, which will play when the timer reaches a specified point:
87+
```
88+
selected_file = filedialog.askopenfilename(
89+
title="Select Sound File",
90+
filetypes=[("Audio Files", "*.mp3 *.wav")]
91+
)
92+
```
93+
94+
95+
96+
97+
98+

0 commit comments

Comments
 (0)