By Faizan Ahmad
This game is developed so that users can play the famous 'Stone Paper Scissors' game even if they are alone. They do not need any other person but just a computer to play this game.
Modules being used:-
1.'import speech_recognition' :-It performs speech recognition, with support for several engines and APIs, online and offline. To install this module you just need to copy and paste the below code in terminal or command prompt.
pip install SpeechRecognition
2.'import pyttsx3' :- It is a text-to-speech conversion library in Python and it also work offline. Installation syntax is given below:-
pip install pyttsx3
3.‘import random’ :- The pseudo-random variables are generated by the random module, which is a built-in module. It can be used to do things like generate a random number, pick random elements from a list, shuffle elements randomly, and so on. It is already installed in Python, so no need to pip install anything.
import random as rdm import speech_recognition as sr #Speech recognition library import pyttsx3 #Text to speech library
Objects being made:-
listener = sr.Recognizer() engine = pyttsx3.init()
Functions in the code:-
1.‘def talk(text)’ :- This function will take the text as a parameter and let the system speak the text passed. After this runAndWait() function is invoked to block while processing all currently queued commands.
def talk(text): print(text) engine.say(text) engine.runAndWait()
2.‘def main()’ :-
def main(): comp=0 ch='y' rnd=0 user=0 ll=['stone','paper','scissors'] while ch=='y' or ch=='Y': temp=0 sys_choice=rdm.choice(ll) talk("Please enter your choice:") user_choice=(input()) rnd=rnd+1
if sys_choice=='stone': if user_choice=='s' or user_choice=='S': talk('This round is tied!') flag='stone' pass elif user_choice=='p' or user_choice=='P': user=user+1 flag='paper' talk('You won this round!') elif user_choice=='c' or user_choice=='C': talk('Computer won this round!') comp=comp+1 flag='scissors' else: talk("You have pressed wrong option!!!") temp=1 rnd=rnd-1 elif sys_choice=='paper': if user_choice=='s' or user_choice=='S': talk('Computer won this round!') comp=comp+1 flag='stone' elif user_choice=='p' or user_choice=='P': talk('This round is tied!') flag='paper' pass elif user_choice=='c' or user_choice=='C': talk('You won this round!') user=user+1 flag='scissors' else: talk("You typed wrong option!!!") temp=1 rnd=rnd-1 elif sys_choice=='scissors': if user_choice=='s' or user_choice=='S': talk('You won this round!') user=user+1 flag='stone' elif user_choice=='p' or user_choice=='P': talk('Computer won this round!') comp=comp+1 flag='paper' elif user_choice=='c' or user_choice=='C': talk('This round is tied!') flag='scissors' pass else: talk("You typed wrong option!!!") temp=1 rnd=rnd-1 if temp==0: talk("You chose: "+flag) talk("Computer chose: "+sys_choice) talk("Score of user by round "+str(rnd)+' is: '+str(user)+' point') talk("Score of computer by round "+str(rnd)+' is: '+str(comp)+' point') elif temp==1: pass talk("Enter 'y' to play again:") ch=input() talk("Final score is being calculated:") talk("Final score of yours: "+str(user)+' point') talk("Final score of computer: "+str(comp)+' point') if user>comp: talk("Congratulations!!! You have won the game!")
Interesting feature:-
System will speak out everything whatever will be printed on the screen, for better experience and enjoyment.
Assembling the codes to understand the flow of the program better:-
import random as rdm import speech_recognition as sr import pyttsx3 listener = sr.Recognizer() engine = pyttsx3.init() def talk(text): print(text) engine.say(text) engine.runAndWait() def main(): comp=0 ch='y' rnd=0 user=0 comp=0 ll=['stone','paper','scissor'] talk("Welcome to the 'Stone Paper Scissor' game.") talk("Press 's' for stone \nPress 'p' for paper \nPress 'c' for Scissor") while ch=='y' or ch=='Y': temp=0 sys_choice=rdm.choice(ll) talk("Please enter your choice:") user_choice=(input()) rnd=rnd+1 if sys_choice=='stone': if user_choice=='s' or user_choice=='S': talk('This round is tied!') flag='stone' pass elif user_choice=='p' or user_choice=='P': user=user+1 flag='paper' talk('You won this round!') elif user_choice=='c' or user_choice=='C': talk('Computer won this round!') comp=comp+1 flag='scissor' else: talk("You have pressed wrong option!!!") temp=1 rnd=rnd-1 elif sys_choice=='paper': if user_choice=='s' or user_choice=='S': talk('Computer won this round!') comp=comp+1 flag='stone' elif user_choice=='p' or user_choice=='P': talk('This round is tied!') flag='paper' pass elif user_choice=='c' or user_choice=='C': talk('You won this round!') user=user+1 flag='scissor' else: talk("You typed wrong option!!!") temp=1 rnd=rnd-1 elif sys_choice=='scissor': if user_choice=='s' or user_choice=='S': talk('You won this round!') user=user+1 flag='stone' elif user_choice=='p' or user_choice=='P': talk('Computer won this round!') comp=comp+1 flag='paper' elif user_choice=='c' or user_choice=='C': talk('This round is tied!') flag='scissor' pass else: talk("You typed wrong option!!!") temp=1 rnd=rnd-1 if temp==0: talk("You chose: "+flag) talk("Computer chose: "+sys_choice) talk("Score of user by round "+str(rnd)+' is: '+str(user)+' point') talk("Score of computer by round "+str(rnd)+' is: '+str(comp)+' point') elif temp==1: pass talk("Enter 'y' to play again:") ch=input() talk("Final score is being calculated:") talk("Final score of user: "+str(user)+' point') talk("Final score of computer: "+str(comp)+' point') if user>comp: talk("Congratulations!!! You have won the game!")
Hope you will enjoy this game!!!
Submitted by Faizan Ahmad (fpawn123)
Download packets of source code on Coders Packet
Comments