In India alone, cricket is worth a whopping 5.3 billion dollars.
Take this game one step further by building a hand cricket game using Python.
What Is Hand Cricket?
Hand cricket is a game in which two play compete against each other using their fingers.
In each turn, both players display a number using their fingers at the same time.
If the scores match, the batsman is out, otherwise they gain their number as runs.
Finally, the player who scores the most wins the game.
it’s possible for you to build this game and play it against a computer too.
Instead of holding out fingers to represent the score, you enter it into the program.
Python is one of the easiest and most convenient languages to use.
you could start by building a digital clock, rolling dice, quiz, or a word counter.
you could also gain inspiration from thesePython project ideas for beginners.
Import the random library, required for the initial toss and the computer’s choices.
Define a function namedinput_numthat accepts two arguments,minandmaxrespectively to validate the user’s input.
If the number is within the range, return it.
Define a functioninningsthat accepts the current batsman and the runs to chase as input arguments.
Ask the user to enter numbers within the range of one to six.
Initialize a variabletotalto store the total runs scored by a batsman and set it to zero.
Begin an infinitewhileloop and call theinput_num()function to store and validate the users input.
Use the random module’srandint()function to choose a random number between one and six for the computer.
Display the choices of both players, then check whether they are equal.
Otherwise, check who is batting and add the appropriate value to the total score, eitherpnumorcnum.
Display the current score of the batsman.
To do so, test whether theto_chasevalue is not empty and if the batsmans score is greater.
If so, return their winning score.
Display the message for toss and validate the user’s choice using theinput_num()function.
Toss the coin and store the result in the variablecoin.
Initialize the default value ofplayer_bowlsto false.
Take the user’s choice and peek if he has entered one.
If yes, evaluate the expression to true and store in theplayer_bowlsvariable.
Display the user’s choice.
Display the computer’s choice and begin the innings.
Ifplayer_bowlsis true, the computer bats first and vice versa.
Calculate the runs scored in an innings using the function you defined earlier.
The function requires the name of the current batsman and the runs to chase.
As the first batsman sets the score, make it an optional parameter and passNone.
For the second innings, pass the batsman’s score who set the target.
If the computer scores less than the user, the user wins, and vice versa.
In case the scores are the same, display a tie.
Put all the code together and get ready to play hand cricket anytime, anywhere right at your fingertips.
Each inning continues until the numbers match and one is out.
In the end, the computer compares the final score and declares the winner.
You do not have to be an expert to start coding and experimenting in Python.
With just the right fundamentals, you’re free to build some amazing terminal games.
As you advance further, explore the PyGame library to build your very own graphical video games as well.