diff --git a/audio.py b/audio.py new file mode 100644 index 0000000..1a41a2a --- /dev/null +++ b/audio.py @@ -0,0 +1,54 @@ +from pygame import mixer + + +class Audio: + background_music = 0 + click_sound = 0 + match_made_sound = 0 + no_match_sound = 0 + game_success_sound = 0 + + @staticmethod + def start_audio(): + mixer.init() + + # Background music is Fisticuffs in Frederick Street, by Christophe Saunière + mixer.music.load('audio/background.wav') + + # Click sound... I don't remember where I got it. Sorry for no link. + Audio.click_sound = mixer.Sound('audio/mouse_click.wav') + + # Match made sound + # https://pixabay.com/?utm_source=link-attribution&utm_medium=referral&utm_campaign=music&utm_content=43637 + Audio.match_made_sound = mixer.Sound('audio/match_made.wav') + + # no_match sound is from https://freesound.org/people/distillerystudio/sounds/327738/ + Audio.no_match_sound = mixer.Sound('audio/no_match.wav') + + # game_success_sound is from https://freesound.org/people/jimhancock/sounds/376318/ + Audio.game_success_sound = mixer.Sound('audio/game_success.wav') + + @staticmethod + def play_background_music(): + mixer.music.play(-1) + mixer.music.set_volume(0.5) + + @staticmethod + def stop_background_music(): + mixer.music.stop() + + @staticmethod + def click(): + Audio.click_sound.play() + + @staticmethod + def match_made(): + Audio.match_made_sound.play() + + @staticmethod + def no_match(): + Audio.no_match_sound.play() + + @staticmethod + def game_success(): + Audio.game_success_sound.play() diff --git a/audio/game_success.wav b/audio/game_success.wav new file mode 100644 index 0000000..6694594 Binary files /dev/null and b/audio/game_success.wav differ diff --git a/audio/mouse_click.wav b/audio/mouse_click.wav new file mode 100644 index 0000000..9eb9858 Binary files /dev/null and b/audio/mouse_click.wav differ diff --git a/audio/no_match.wav b/audio/no_match.wav new file mode 100644 index 0000000..cd64c9a Binary files /dev/null and b/audio/no_match.wav differ diff --git a/main.py b/main.py index 845770c..c7eaa0c 100644 --- a/main.py +++ b/main.py @@ -1,3 +1,5 @@ +from audio import Audio + try: import os import random @@ -14,8 +16,12 @@ try: screen = turtle.Screen() turtle.bgcolor('#46a38d') screen.setup(WIDTH, HEIGHT) - score_text = turtle.Turtle() + Audio.start_audio() + Audio.play_background_music() + + # Scoreboard + score_text = turtle.Turtle() score_text.hideturtle() score_text.penup() score_text.goto((WIDTH - HEIGHT) // 2, HEIGHT * .25) @@ -42,7 +48,6 @@ try: cards = [] for file in image_files: path = f'images/{file}' - print(path) cards.append(Card(path)) # Move sprites @@ -53,32 +58,29 @@ try: x, y = coord_translation((210 * (i % 4)) + 105, (210 * int(i / 4)) + 105) cards[i].goto(x, y) + score = 0 + game_is_running = True + clicked_cards = [] def clicked_card(x, y): """ :return: The card which was clicked """ + Audio.click() global clicked_cards for card in cards: if card.is_mouse_over(x, y): if len(clicked_cards) == 2: clicked_cards = [] - print(cards.index(card)) card.to_front() clicked_cards.append(card) - score = 0 - - def update_score(): score_text.clear() score_text.write(f'Score: {score}', font=('Arial', 24, 'bold')) - game_is_running = True - clicked_cards = [] - screen.onclick(fun=clicked_card) while game_is_running: @@ -88,13 +90,13 @@ try: time.sleep(1) clicked_cards[0].to_back() clicked_cards[1].to_back() - print('Wrong!') score -= 1 update_score() + Audio.no_match() else: - print('Correct!') score += 5 update_score() + Audio.match_made() clicked_cards = [] screen.update()