Audio added, game completed
This commit is contained in:
parent
f95288736c
commit
d1c265eac9
5 changed files with 67 additions and 11 deletions
54
audio.py
Normal file
54
audio.py
Normal file
|
@ -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()
|
BIN
audio/game_success.wav
Normal file
BIN
audio/game_success.wav
Normal file
Binary file not shown.
BIN
audio/mouse_click.wav
Normal file
BIN
audio/mouse_click.wav
Normal file
Binary file not shown.
BIN
audio/no_match.wav
Normal file
BIN
audio/no_match.wav
Normal file
Binary file not shown.
24
main.py
24
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()
|
||||
|
||||
|
|
Loading…
Reference in a new issue