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:
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...
Enter the pattern:
Pattern: S F
Enter the pattern:
Pattern: S F F
Enter the pattern:
Pattern: S F F D
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. This code is available at https://nostarch.com/big-book-small-python-programming 6. Tags: short, beginner, game""" 7. 8. importrandom,sys,time 9. 10. # Download the sound files from these URLs (or use your own):11. # https://inventwithpython.com/soundA.wav12. # https://inventwithpython.com/soundS.wav13. # https://inventwithpython.com/soundD.wav14. # https://inventwithpython.com/soundF.wav15. 16. try:17. importplaysound18. exceptImportError: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. whileTrue: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. forletterinpattern: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. ifresponse!=pattern:54. print('Incorrect!')55. print('The pattern was',pattern)56. else:57. print('Correct!')58. 59. forletterinpattern:60. playsound.playsound('sound'+letter+'.wav')61. 62. ifresponse!=pattern:63. print('You scored',len(pattern)-1,'points.')64. print('Thanks for playing!')65. break66. 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.
What happens if you delete or comment out print('\n' * 60) on line 47?
What happens if you change response != pattern on line 62 to False?