Rock , Paper, Scissors Game


  import tkinter as tk


from tkinter import messagebox

import random


# Choices

choices = ["Rock", "Paper", "Scissors"]


def determine_winner(player, computer):

    if player == computer:

        return "Draw"

    elif (player == "Rock" and computer == "Scissors") or \

         (player == "Paper" and computer == "Rock") or \

         (player == "Scissors" and computer == "Paper"):

        return "You Win!"

    else:

        return "Computer Wins!"


def player_choice(player):

    computer = random.choice(choices)

    result = determine_winner(player, computer)


    player_label.config(text=f"Your Choice: {player}")

    computer_label.config(text=f"Computer's Choice: {computer}")

    result_label.config(text=f"Result: {result}")


def reset_game():

    player_label.config(text="Your Choice: ")

    computer_label.config(text="Computer's Choice: ")

    result_label.config(text="Result: ")


# Create window

root = tk.Tk()

root.title("Rock, Paper, Scissors")

root.geometry("300x350")


title = tk.Label(root, text="Rock, Paper, Scissors", font=("Arial", 16))

title.pack(pady=10)


# Buttons

btn_frame = tk.Frame(root)

btn_frame.pack(pady=10)


tk.Button(btn_frame, text="Rock", width=10, command=lambda: player_choice("Rock")).grid(row=0, column=0, padx=5)

tk.Button(btn_frame, text="Paper", width=10, command=lambda: player_choice("Paper")).grid(row=0, column=1, padx=5)

tk.Button(btn_frame, text="Scissors", width=10, command=lambda: player_choice("Scissors")).grid(row=0, column=2, padx=5)


# Labels

player_label = tk.Label(root, text="Your Choice: ", font=("Arial", 12))

player_label.pack(pady=5)


computer_label = tk.Label(root, text="Computer's Choice: ", font=("Arial", 12))

computer_label.pack(pady=5)


result_label = tk.Label(root, text="Result: ", font=("Arial", 14, "bold"))

result_label.pack(pady=10)


# Reset and Exit

tk.Button(root, text="Play Again", command=reset_game).pack(pady=5)

tk.Button(root, text="Exit", command=root.destroy).pack()


root.mainloop()



#Output



Comments

Popular posts from this blog

Python Code

CV