Overview: Python Game Tutorial
Let’s get started with a Python game tutorial 2025: Rock, Paper, Scissors. Here we will take a look at how to write a Python program for Rock, Paper, Scissors. Along with will also explore other essential Python concepts.
What’s Next? - Python Game Tutorial
In this article, we will explore how to write Python game program for Rock, Paper, Scissors. With this we will explore more Python concepts like conditional statements, loops, error handling, etc.
Complete Code:
import random
def game():
game_options = ["Rock", "Paper", "Scissors"]
while True:
print("\n1. Rock")
print("2. Paper")
print("3. Scissors")
print("4. Exit")
try:
user_option = int(input("Choose any one number to start the game (1-4): "))
except ValueError:
print("Please enter a valid number!")
continue
if user_option == 4:
print("\nThanks for playing!")
break
if user_option not in [1, 2, 3]:
print("\nInvalid choice! Please select 1, 2, 3, or 4.")
continue
user_choice = game_options[user_option - 1]
computer_index = random.randint(0, 2)
computer_choice = game_options[computer_index]
print(f"\nUser: {user_choice}")
print(f"Computer: {computer_choice}")
if user_choice == computer_choice:
print("\nGame Tie! Play Again...")
continue
# Correct game logic for winning conditions
if (user_choice == "Rock" and computer_choice == "Scissors") or \
(user_choice == "Paper" and computer_choice == "Rock") or \
(user_choice == "Scissors" and computer_choice == "Paper"):
print(f"'{user_choice}' beats '{computer_choice}' | Winner: USER")
else:
print(f"'{computer_choice}' beats '{user_choice}' | Winner: COMPUTER")
game()
Example Output:
- Rock
- Paper
- Scissors
- Exit
Choose any one number to start the game (1-4): 1
User: Rock
Computer: Scissors
'Rock' beats 'Scissors' | Winner: USER
Explanation: Step by Step
Step 1: Game Start Function
def game()
Explanation: The whole game is written under function to run the whole program together and would not have to write the program again over each run.
Step 2: Game Options Setup
game_options = ["Rock", "Paper", "Scissors"]
Explanation: A list is declared which stored 3 game options.
Step 3: Infinite Loop (Until User Exits)
while True:
Explanation: Since, the ‘while’ loop condition is ‘True’, means loop condition will be always executed until the user selects ‘exit’ from the program.
Step 4: Show Menu
print("\n1. Rock")
print("2. Paper")
print("3. Scissors")
print("4. Exit")
Explanation: Here, we have 4 game options a user can choose to start the game and given each number is associated with a game option. \n → it displays output to the new line.
Step 5: Get User Input
user_option = int(input("Choose any one number to start the game (1-4): "))
Explanation: Here, by using the input() function, we asked the user to enter any one number from (1-4) to select any one option to start the game.
By default the input() function returns the string type of the output but we need an ‘int’ type therefore, we convert the output using the ‘int’ function before the input() function.
Step 6: Exit Check
if user_option == 4:
print("\nThanks for playing!")
break
Explanation: Here, if the user chooses 4, execute the print() function and end the game.
Step 7: Validate Input
if user_option not in [1, 2, 3]:
print("\nInvalid choice! Please select 1, 2, 3, or 4.")
continue
Explanation: If the user inputs something else (like 5 or any number), it shows an error and asks again. The Python reserve keyword ‘continue’ allows the Python to continue the game and ask the user again.
Step 8: Set User & Computer Choices
user_choice = game_options[user_option - 1]
computer_index = random.randint(0, 2)
computer_choice = game_options[computer_index]
Explanation:
For user to choose option:
User choice is mapped from the list using user_option - 1. The computer randomly picks Rock, Paper, or Scissors. Since, here game options are numbered from 1 but in Python, the index number starts from 0, therefore; -1 is used to adjust the game options according to the index number.
For computer to choose option:
Here, we used a random function along with randint which allows users to select any random number + within the given range (both first and last number is included). Range is defined from 0 to 2 because the computer chooses the game option according to the index number. The variable computer_choice is mapped from the list to take out the game option based on the index number chosen by the computer.
Step 9: Show Choices
print(f"\nUser: {user_choice}")
print(f"Computer: {computer_choice}")
Explanation: Displays what the user and computer selected.
Step 10: Check for Tie Case
if user_choice == computer_choice:
print("\nGame Tie! Play Again...")
continue
Explanation: If both choose the same game option, it’s a tie, and the game asks to play again.
Step 11: Determine Winner
if (user_choice == "Rock" and computer_choice == "Scissors") or \
(user_choice == "Paper" and computer_choice == "Rock") or \
(user_choice == "Scissors" and computer_choice == "Paper"):
print(f"'{user_choice}' beats '{computer_choice}' | Winner: USER")
else:
print(f"'{computer_choice}' beats '{user_choice}' | Winner: COMPUTER")
Explanation: Here we check the possibilities of game options where a user can beat a computer and a computer can beat a user.
Applies simple rules:
- Rock beats Scissors
- Paper beats Rock
- Scissors beats Paper
If the user wins, it shows the user as the winner; otherwise, the computer wins.
Step 12: End of Loop
After each round, the game goes back to step 2, unless the user exits.
FOR MORE PYTHON BEGINNER PROJECTS:
Top comments (0)