Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added sounds/keyboardclick1.mp3
Binary file not shown.
Binary file added sounds/keyboardclick2.wav
Binary file not shown.
Binary file added sounds/keyboardclick3.wav
Binary file not shown.
60 changes: 58 additions & 2 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@
from tkinter import ttk
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg)
from pygame import mixer
import os
import platform

if(platform.system() == "Darwin"):
sounds = [os.path.abspath("sounds/keyboardclick1") + ".mp3", os.path.abspath("sounds/keyboardclick2") + ".wav", os.path.abspath("sounds/keyboardclick3") + ".wav"]
elif(platform.system() == "Windows"):
sounds = [os.path.abspath("sounds\keyboardclick1") + ".mp3", os.path.abspath("sounds\keyboardclick2") + ".wav", os.path.abspath("sounds\keyboardclick3") + ".wav"]
else:
sounds = [os.path.abspath("sounds\keyboardclick1") + ".mp3", os.path.abspath("sounds\keyboardclick2") + ".wav", os.path.abspath("sounds\keyboardclick3") + ".wav"]

should_play = True
soundID = 0

mixer.init()
mixer.music.load(sounds[soundID])

class Window:
def td(self, s):
Expand Down Expand Up @@ -42,6 +58,8 @@ def choose_wd(self):
def __init__(self):
self.stop_threads = False

self.soundID = soundID

self.window = Tk()
self.window.title('py-typer')
self.window.configure(background="gray25")
Expand All @@ -60,11 +78,12 @@ def __init__(self):

self.setup()

def clear(self):
def clear(self):
for widget in self.window.winfo_children():
widget.destroy()

def setup(self):
self.mixer_init()

self.x=[]
self.y=[]
Expand Down Expand Up @@ -116,8 +135,11 @@ def restart(self):
self.stop_threads = False
self.clear()
self.setup()
self.mixer_init()

def plot_graph(self):
self.mixer_uninit()

self.fig = Figure(figsize = (3, 2), dpi = 100)
self.fig.clf()
plot1 = self.fig.add_subplot()
Expand All @@ -136,19 +158,25 @@ def plot_graph(self):

def main_menu(self):
self.clear()
self.mixer_uninit()

results_label = Label(self.window, text=self.wpm + " " + self.accuracy, font=("roboto", 50, "bold"), background="gray25", fg="#ebc934")
results_label.grid(row=1, column=0)

restart_button = Button(self.window, text="Restart", font=("roboto", 30), background="gray25", command=self.restart, highlightbackground="gray25", fg="#ebc934")
restart_button.grid(row=2, column=0)


change_sound_button = Button(self.window, text="Sound", font=("roboto", 30), background="gray25", command=self.change_sound, highlightbackground="gray25", fg="#ebc934")
change_sound_button.grid(row=2, column=1)

mode_button = Button(self.window, text="Mode", font=("roboto", 30), highlightbackground="gray25", fg="#ebc934", background="gray25", bg="gray25", command=self.modes)
mode_button.grid(row=2, column=2)

self.plot_graph()

def key_press(self, event):
self.play_sound()

if not self.write_able:
return None
if event.keysym == "Shift_L" or event.keysym == "Shift_R":
Expand All @@ -169,6 +197,34 @@ def key_press(self, event):
self.calculate_accuracy()
except TclError: pass


def mixer_uninit(self):
mixer.quit()
should_play = False

def mixer_init(self):
mixer.init()
mixer.music.load(sounds[self.soundID])
should_play = True

def change_sound(self):
if(self.soundID == len(sounds) - 1):
self.soundID = 0
else:
self.soundID += 1

print(self.soundID)

mixer.init()
mixer.music.load(sounds[self.soundID])

self.play_sound()


def play_sound(self):
if(should_play):
mixer.music.play()

def start_timer(self):
self.timer_thread = threading.Thread(target=self.countdown_thread)
self.timer_thread.daemon = True
Expand Down