109 lines
3.2 KiB
Plaintext
109 lines
3.2 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "c61d2477-fd76-4819-9696-b4077728c2df",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"%pip install edge_tts playsound"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "dfd3281a",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import random\n",
|
|
"import edge_tts\n",
|
|
"import datetime\n",
|
|
"from playsound import playsound\n",
|
|
"\n",
|
|
"def generate_digits(n):\n",
|
|
" \"\"\"\n",
|
|
" Generates a string of n random digits.\n",
|
|
" \"\"\"\n",
|
|
" return ''.join([str(random.randint(0, 9)) for _ in range(n)])\n",
|
|
"\n",
|
|
"async def speak_digits(digits):\n",
|
|
" \"\"\"\n",
|
|
" Speaks every digit in the string to the user with a pause in between.\n",
|
|
" \"\"\"\n",
|
|
" VOICE = \"en-US-GuyNeural\"\n",
|
|
" # VOICE = \"zh-CN-YunjianNeural\"\n",
|
|
"\n",
|
|
" # generate voice over file\n",
|
|
" OUTPUT_FILE = f'{datetime.datetime.now().strftime(\"%Y%m%d%H%M%S\")}.mp3'\n",
|
|
" communicate = edge_tts.Communicate(digits, VOICE)\n",
|
|
" await communicate.save(OUTPUT_FILE)\n",
|
|
" \n",
|
|
" # play the OUTPUT_FILE\n",
|
|
" playsound(OUTPUT_FILE)\n",
|
|
"\n",
|
|
"async def play_game(n):\n",
|
|
" \"\"\"\n",
|
|
" Plays the memory game.\n",
|
|
" \"\"\"\n",
|
|
" num_digits = n\n",
|
|
" while True:\n",
|
|
" # Generate a string of random digits\n",
|
|
" digits = generate_digits(num_digits)\n",
|
|
"\n",
|
|
" # a decoration to have edge_tts pronounce with breaks\n",
|
|
" voice_string = f\"{', '.join(d for d in digits)}, .. over!\"\n",
|
|
"\n",
|
|
" # Speak the digits to the user|\n",
|
|
" print(f\"You'll challenge {num_digits} digits to remember, please listen carefully...\")\n",
|
|
" await speak_digits(voice_string)\n",
|
|
"\n",
|
|
" # Ask the user to input what they heard\n",
|
|
" guess = input(\"Please enter what you heard: \")\n",
|
|
"\n",
|
|
" # Check if the guess is correct\n",
|
|
" if guess == digits:\n",
|
|
" num_digits += 1\n",
|
|
" print(\"Congratulations! You got it right!\") \n",
|
|
" else:\n",
|
|
" num_digits -= 1\n",
|
|
" print(\"Sorry, that's not correct. Keep trying! Let's hold back a little...\")\n",
|
|
" if num_digits < 1:\n",
|
|
" num_digits = 1\n",
|
|
"\n",
|
|
" # Ask the user if they want to continue playing\n",
|
|
" play_again = input(\"Do you want to play again? (y/n) \")\n",
|
|
" if play_again.lower() != 'y':\n",
|
|
" break\n",
|
|
"\n",
|
|
" print(\"Thanks for playing!\")\n",
|
|
"\n",
|
|
"start_length = 5\n",
|
|
"await play_game(start_length)"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3 (ipykernel)",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.8.18"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|