Rock-Paper-Scissor Game – Python Source Code

  • Code
  • Code With GUI
# Import required modules
import random

# Function to play the game
def RPS_Game():
    game_choices = ("rock", "paper", "scissor")

    while True:
        # Ask the player to choose rock, paper, or scissor and convert input to lowercase
        player_choice = input("Choose One (Rock/Paper/Scissor): ").lower()

        # Check if the player's choice is valid; if not, raise an error
        if player_choice not in game_choices:
            print ("Invalid Input. Please Choose From (Rock/Paper/Scissor)")
        else:
            break
        
    # Generate a random choice for the computer    
    computer_choice = (random.choice(game_choices))
        
    print(f"You Choose {player_choice}")
    print(f"Computer Choose {computer_choice}")
    
    # Based on the selections, determine the winner or declare a tie
    if (computer_choice == "rock" and player_choice == "paper") or \
       (computer_choice == "paper" and player_choice == "scissor") or \
       (computer_choice == "scissor" and player_choice == "rock"):
        print("You Win!")

    elif (computer_choice == player_choice):
        print("Game Tie!")
    
    else:
        print("Computer Win!")

# Function to play the Rock-Paper-Scissors game repeatedly   
def playing_game():
    input("\nClick Enter To Start The (Rock/Paper/Scissor) Game ")
    while True:
        # Play a single round of the game
        RPS_Game()

        # Ask the player if they want to play again
        User_Want = input("Do You Like To Play Again: Y For Yes / N For No: ").lower()

        # Check if the player wants to stop playing or playing again
        if User_Want not in ("yes", "y", " yes"):
            break 
    print("Thanks for playing!")

# Start the Rock-Paper-Scissors Game
playing_game()

# Rock-Paper-Scissor Game Source Code - By Codotix
# Import required modules
import random
import tkinter as tk

# Function to play the game
def playing_game(player_choice):
    game_choices = ("Rock", "Paper", "Scissor")
    computer_choice = random.choice(game_choices)

    # Update labels with player and computer choices
    player_choice_label.config(text=f"You chose: {player_choice}")
    computer_choice_label.config(text=f"Computer chose: {computer_choice}")

    # Based on the selections, determine the winner or declare a tie
    if player_choice == computer_choice:
        result_label.config(text="It's a Tie!")
    elif (
        (player_choice == "Rock" and computer_choice == "Scissor") or
        (player_choice == "Paper" and computer_choice == "Rock") or
        (player_choice == "Scissor" and computer_choice == "Paper")):
        result_label.config(text="You Win!")
    else:
        result_label.config(text="Computer Wins!")

# Create the main window
root = tk.Tk()
root.geometry("600x400")
root.title("Stone Paper Scissor Game")

# Title label
game_line = tk.Label(root, text="Choose From Rock | Paper | Scissor", font=("Arial", 25, "bold"))
game_line.pack()

# Frame to hold buttons
button_frame = tk.Frame(root, bg="grey", border=5)
button_frame.pack(padx=20, pady=20)

# Buttons for player choices
rock_button = tk.Button(button_frame, text="Rock", font=("arial", 20, "bold"), bd=5, command=lambda: playing_game("Rock"))
rock_button.grid(row=0, column=0, padx=10)

paper_button = tk.Button(button_frame, text="Paper", font=("arial", 20, "bold"), bd=5, command=lambda: playing_game("Paper"))
paper_button.grid(row=0, column=1, padx=10)

scissor_button = tk.Button(button_frame, text="Scissor", font=("arial", 20, "bold"), bd=5, command=lambda: playing_game("Scissor"))
scissor_button.grid(row=0, column=2, padx=10)

# Labels to display choices and results
player_choice_label = tk.Label(root, text="", font=("Arial", 16))
player_choice_label.pack()

computer_choice_label = tk.Label(root, text="", font=("Arial", 16))
computer_choice_label.pack()

result_label = tk.Label(root, text="", font=("Arial", 16))
result_label.pack()

# Exit button to quit the game
exit_button = tk.Button(root, text="  Exit  ", font=("arial", 20, "bold"), bd=5, command=root.quit)
exit_button.pack(pady=20)

# Start the GUI event loop
root.mainloop()

#  Rock-Paper-Scissor Game Source Code - By Codotix

Leave a Comment

Your email address will not be published. Required fields are marked *