A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://inventwithpython.com/bigbookpython/project71.html below:

Sound Mimic

#71
Sound Mimic

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.

The Program in Action

When you run soundmimic.py, the output will look like this:

Sound Mimic, by Al Sweigart [email protected]
Try to memorize a pattern of A S D F letters (each with its own sound)
as it gets longer and longer.
Press Enter to begin...
<screen clears>
Pattern: S
<screen clears>
Enter the pattern:
> s
Correct!
<screen clears>
Pattern: S F
<screen clears>
Enter the pattern:
> sf
Correct!
<screen clears>
Pattern: S F F
<screen clears>
Enter the pattern:
> sff
Correct!
<screen clears>
Pattern: S F F D
--snip--
How It Works

This program imports the playsound module, which can play sound files. The module has one function, playsound(), to which you can pass the filename of a .wav or .mp3 file to play. On each round of the game, the program appends a randomly chosen letter (either A, S, D, or F) to the pattern list and plays the sounds in this list. As the pattern list grows longer, so does the pattern of sound files the player must memorize.

 1. """Sound Mimic, by Al Sweigart [email protected]
 2. A pattern-matching game with sounds. Try to memorize an increasingly
 3. longer and longer pattern of letters. Inspired by the electronic game
 4. Simon.
 5. View this code at https://nostarch.com/big-book-small-python-projects
 6. Tags: short, beginner, game"""
 7.
 8. import 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.
16. try:
17.     import playsound
18. except 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.
27. print('''Sound Mimic, by Al Sweigart [email protected]
28. Try to memorize a pattern of A S D F letters (each with its own sound)
29. as it gets longer and longer.''')
30.
31. input('Press Enter to begin...')
32.
33. pattern = ''
34. while 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)
Exploring the Program

Try to find the answers to the following questions. Experiment with some modifications to the code and rerun the program to see what effect the changes have.

  1. What happens if you delete or comment out print('\n' * 60) on line 47?
  2. What happens if you change response != pattern on line 62 to False?

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4