Sound Mimic

Tags: short, beginner, game

Similar to the Simon electronic toy, this memorization game uses the third-party playsound module to play four different sounds, which correspond to the A, S, D, and F keys on the keyboard. As you successfully repeat the pattern the game gives you, the patterns get longer and longer. How many sounds can you hold in your short-term memory?

If you look at the code, you’ll see that the playsound.playsound() function is passed the filename of the sound to play. You can download the sound files from these URLs:

https://inventwithpython.com/soundA.wav

https://inventwithpython.com/soundS.wav

https://inventwithpython.com/soundD.wav

https://inventwithpython.com/soundF.wav

Place these files in the same folder as soundmimic.py before running the program. More information about the playsound module can be found at https://pypi.org/project/playsound/. Users on macOS must also install the pyobjc module from https://pypi.org/project/pyobjc/ for playsound to work.

sound_mimic.py
 1"""Sound Mimic, by Al Sweigart al@inventwithpython.com
 2A pattern-matching game with sounds. Try to memorize an increasingly
 3longer and longer pattern of letters. Inspired by the electronic game,
 4Simon.
 5This code is available at https://nostarch.com/big-book-small-python-programming
 6Tags: short, beginner, game"""
 7
 8import random, sys, time
 9
10# Download the sound files from these URLs (or use your own):
11# https://inventwithpython.com/soundA.wav
12# https://inventwithpython.com/soundS.wav
13# https://inventwithpython.com/soundD.wav
14# https://inventwithpython.com/soundF.wav
15
16try:
17    import playsound
18except ImportError:
19    print('The playsound module needs to be installed to run this')
20    print('program. On Windows, open a Command Prompt and run:')
21    print('pip install playsound')
22    print('On macOS and Linux, open a Terminal and run:')
23    print('pip3 install playsound')
24    sys.exit()
25
26
27print('''Sound Mimic, by Al Sweigart al@inventwithpython.com
28Try to memorize a pattern of A S D F letters (each with its own sound)
29as it gets longer and longer.''')
30
31input('Press Enter to begin...')
32
33pattern = ''
34while True:
35    print('\n' * 60)  # Clear the screen by printing several newlines.
36
37    # Add a random letter to the pattern:
38    pattern = pattern + random.choice('ASDF')
39
40    # Display the pattern (and play their sounds):
41    print('Pattern: ', end='')
42    for letter in pattern:
43        print(letter, end=' ', flush=True)
44        playsound.playsound('sound' + letter + '.wav')
45
46    time.sleep(1)  # Add a slight pause at the end.
47    print('\n' * 60)  # Clear the screen by printing several newlines.
48
49    # Let the player enter the pattern:
50    print('Enter the pattern:')
51    response = input('> ').upper()
52
53    if response != pattern:
54        print('Incorrect!')
55        print('The pattern was', pattern)
56    else:
57        print('Correct!')
58
59    for letter in pattern:
60        playsound.playsound('sound' + letter + '.wav')
61
62    if response != pattern:
63        print('You scored', len(pattern) - 1, 'points.')
64        print('Thanks for playing!')
65        break
66
67    time.sleep(1)

https://inventwithpython.com/bigbookpython/project71.html