Let’s create a simple Rock Paper Scissor game in Python without using third-party game libraries like PyGame. The player will initially have the option to choose either Rock, Paper, or Scissors in this game. The winner is chosen according to the game’s rules after the computer randomly selects the last two alternatives.
Rock-Paper-Scissors is a popular and straightforward game often used to settle minor disputes among children. Its appeal lies in its competitive nature, as children readily agree to play when a parent or caregiver suggests it.
The game’s rules are straightforward: Rock beats Scissors, Scissors beats Paper, and Paper beats Rock. The winner is determined after the players make their choices, and the loser typically accepts the outcome.
It serves as a quick and fair way to resolve disagreements. Interestingly, if a child refuses to participate in a Rock-Paper-Scissors game to settle a dispute, it may indicate that the issue at hand is more serious.
1. This is a Python implementation of the classic Rock-Paper-Scissors game that brings back childhood memories.
2. Choosing rock will beat scissors (“rock crushes scissors” or “breaks scissors” or sometimes “blunts scissors”), but it will lose to paper (“paper covers rock”).
3. Selecting paper will lose to scissors (“scissors cuts paper”).
4. The objective is to defeat the opponent by choosing a weapon that defeats their choice according to the following rules: Rock smashes (or breaks or blunts), scissors (rock wins), scissors cut paper (scissors win), and paper covers rock (paper wins).
Here are the prerequisites to build rock paper scissor game in Python
1. Basic Python Knowledge
2. VS Code or Any Text Editor
3. Python Environment: The code requires a Python environment to run. Ensure that Python is installed on the system and the code is executed using a Python interpreter.
4. Importing the Random Module: The code starts by importing the random module. Make sure to have the random module available in the Python environment
# import random module import random # Print multiline instruction # performstring concatenation of string print("Winning Rules of the Rock paper scissor game as follows: \n" + "Rock vs paper->paper wins \n" + "Rock vs scissor->Rock wins \n" + "paper vs scissor->scissor wins \n") while True: print("Enter choice \n 1 for Rock, \n 2 for paper, and \n 3 for scissor \n") # take the input from user choice = int(input("User turn: ")) # OR is the short-circuit operator # if any one of the condition is true # then it returns True value # looping until user enters invalid input while choice > 3 or choice < 1: choice = int(input("enter valid input: ")) # initialise value of choice_name variable # corresponding to the choice value if choice == 1: choice_name = 'Rock' elif choice == 2: choice_name = 'paper' else: choice_name = 'scissor' # print user choice print("user choice is: " + choice_name) print("\nNow its computer turn.......") # Computer chooses randomly any number # among 1 , 2 and 3. Using randint method # of random module comp_choice = random.randint(1, 3) # looping until comp_choice value # is equal to the choice value while comp_choice == choice: comp_choice = random.randint(1, 3) # initialize value of comp_choice_name # variable corresponding to the choice value if comp_choice == 1: comp_choice_name = 'Rock' elif comp_choice == 2: comp_choice_name = 'paper' else: comp_choice_name = 'scissor' print("Computer choice is: " + comp_choice_name) print(choice_name + " V/s " + comp_choice_name) # we need to check of a draw if choice == comp_choice: print("Draw=> ", end="") result = Draw # condition for winning if((choice == 1 and comp_choice == 2) or (choice == 2 and comp_choice == 1)): print("paper wins => ", end="") result = "paper" elif((choice == 1 and comp_choice == 3) or (choice == 3 and comp_choice == 1)): print("Rock wins =>", end="") result = "Rock" else: print("scissor wins =>", end="") result = "scissor" # Printing either user or computer wins or draw if result == Draw: print("<== Its a tie ==>") if result == choice_name: print("<== User wins ==>") else: print("<== Computer wins ==>") print("Do you want to play again? (Y/N)") ans = input().lower # if user input n or N then the condition is True if ans == 'n': break # after coming out of the while loop # we print thanks for playing print("\nThanks for playing")
In this output, we can see that computer wins by choosing Paper, and the user loses by choosing Rock.
Explanation of the Code
1. Initially, we have displayed the rules for the Rock Paper Scissor Game.
2. In the first line of the code, we have displayed “Rock vs Paper -> Paper Wins.” This is so that paper will win if a player chooses a rock and plays against an opponent who selects paper.
3. We have followed the output “Rock vs scissor -> Rock wins.” This shows that when played against scissors, rock prevails.
4. And the last line we displayed, “paper vs scissors -> scissors wins.” This proves that when playing scissors versus paper, paper prevails.
5. We developed the programming in such a way that it will produce the line “Winning Rules of the Rock, Paper, Scissors Game as Follows: Rock vs Paper -> Paper Wins, Rock vs Scissor -> Rock Wins, Paper vs Scissor -> Scissor Wins.”
6. Then we displayed a message to start the game by asking the user to make a decision.
7. Next, we determined if the input is 1, 2, or 3. We assigned the values “Rock” if choice == 1, “paper” if choice == 2, and “scissor” if choice == 3 to the variable choice_name if it doesn’t match any of these values.
8. The user is prompted to input their computer turn in the next section of the programme.
9. A number between 1 and 3 is chosen using a random number generator.
10. Here, we ask the user to choose amongst the options of rock, paper, or scissors.
11. After the user makes their selection, the next one of these alternatives at random will represent the computer’s turn.
12. The comp_choice_name variable contains the value that was selected at random.
13. Program keeps looping until comp_choice and choice are equal.
14. Comp_choice is picked at random from the range of 1 to 3 during each loop iteration and saved in comp_choice_name.
15. If the computer has selected rock as its turn, we check here for comp_choice equals choice or not.
16. Then we displayed the outputs of both options, showing what happened (the human selected rock vs paper and the computer selected rock vs scissors).
17. And displayed the result of the winner with its choice!
18. After that, ask the user to continue the game with Yes/No prompt.
19. Here, if the user selects “yes”, the game continues with the above-following procedure. If “no” game ends. At last, we displayed the “Thanks for playing” message.
Hence, we have successfully built the Python application that allows us to play the rock, paper, scissor game in Python which is a classic and nostalgic game and sometimes acts as a deciding factor in performing any activity.
Sulagna is an experienced Python developer and developed web applications, data analysis tools, and automation scripts using Python. With several years of hands-on experience in Python development, she has gained a deep understanding of the language and its extensive ecosystem of libraries and frameworks. Sulagna has over 5 years of experince as a technical content writer/reviewer. She has contributed articles and tutorials to reputable online platforms, sharing her knowledge and insights on Python development best practices, web development, and data analysis.