How to make a quiz with Audio recognition in python with pygame

https://github.com/formazione/audioquiz.git

Code

import pygame
import os
from glob import glob
import time
import speech_recognition as sr
from random import shuffle

gameDisplay = pygame.display.set_mode((400, 400))

pngs = [x for x in glob("animals\\*.PNG")]
names = [x.split(".")[0] for x in glob("animals\\*.PNG")]

animals = {k:v for k, v in zip(pngs, names)}
print(animals)


print(pngs)
print(names)
keys = list(animals.keys())
shuffle(keys)

score = 0
for animal in keys:
    guess_counter = 0
    carImg = pygame.image.load(os.path.join('', animal))
    gameDisplay.blit(carImg,(130,0))
    pygame.display.update()
    # pygame.mixer.Sound.play(Tiger)
    # pygame.mixer.music.stop()
    # time.sleep(1)

    r = sr.Recognizer()
    with sr.Microphone() as source:
        print ('What\'s his name!')
        audio = r.listen(source)
        try:
            text = r.recognize_google(audio)
            print(text)
        except:
            print('Did not get that try Again')
            text=''
        if text == animals[animal].split("\\")[1]:
            print('good job\n=========\n\n') 
            #pygame.mixer.Sound.play(right)
            # pygame.mixer.music.stop()
            score += 1
            # break
        else:
            if guess_counter < 3:
                print('wrong try again\n')
                # pygame.mixer.Sound.play(wrong)
                # pygame.mixer.music.stop()
                # time.sleep(1)
                guess_counter += 1
            else:
                pygame.quit()

                  
    gameDisplay.fill((0,0,0))
    print(f"{score}")

pygame.quit()

Put the images with animals name, as many as you want or even images of any kind, into this folder called animals

Try this version with also the audio that tells you to say what is the name of the animal and if you are right or wrong.

import pygame
import os
from glob import glob
import time
import speech_recognition as sr
from random import shuffle
from gtts import gTTS
from io import BytesIO




pygame.init()
pygame.mixer.init()
gameDisplay = pygame.display.set_mode((400, 400))

pngs = [x for x in glob("animals\\*.PNG")]
names = [x.split(".")[0] for x in glob("animals\\*.PNG")]

animals = {k:v for k, v in zip(pngs, names)}
print(animals)

def speak(text, language='en'):
    mp3_fo = BytesIO()
    tts = gTTS(text, lang=language)
    tts.write_to_fp(mp3_fo)
    pygame.mixer.music.load(mp3_fo, 'mp3')
    pygame.mixer.music.play()



print(pngs)
print(names)
keys = list(animals.keys())
shuffle(keys)

score = 0
for animal in keys:
    guess_counter = 0
    carImg = pygame.image.load(os.path.join('', animal))
    gameDisplay.blit(carImg,(130,0))
    pygame.display.update()
    # pygame.mixer.Sound.play(Tiger)
    # pygame.mixer.music.stop()
    # time.sleep(1)

    r = sr.Recognizer()
    with sr.Microphone() as source:
        print ('What\'s his name!')
        speak("What do you call this")
        audio = r.listen(source)
        try:
            text = r.recognize_google(audio)
            print(text)
        except:
            print('Did not get that try Again')
            speak("No, it's wrong")
            time.sleep(1)
            text=''
        if text == animals[animal].split("\\")[1]:
            print('good job\n=========\n\n')
            speak("you are right")
            time.sleep(1)
            #pygame.mixer.Sound.play(right)
            # pygame.mixer.music.stop()
            score += 1
            # break
        else:
            if guess_counter < 3:
                print('wrong try again\n')
                speak("No, it's wrong")
                time.sleep(1)
                # pygame.mixer.Sound.play(wrong)
                # pygame.mixer.music.stop()
                # time.sleep(1)
                guess_counter += 1
            else:
                pygame.quit()

                  
    gameDisplay.fill((0,0,0))
    print(f"{score}")

pygame.quit()

Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.