From 2189d3914cb4194df2ca02eb33a2f0d11cd07844 Mon Sep 17 00:00:00 2001 From: Ritesh <112797055+Antiquely3059@users.noreply.github.com> Date: Wed, 15 May 2024 18:53:35 +0530 Subject: [PATCH] Create Rock Paper Scissors Game.md --- .../mini-projects/Rock Paper Scissors Game.md | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 contrib/mini-projects/Rock Paper Scissors Game.md diff --git a/contrib/mini-projects/Rock Paper Scissors Game.md b/contrib/mini-projects/Rock Paper Scissors Game.md new file mode 100644 index 0000000..ed815d5 --- /dev/null +++ b/contrib/mini-projects/Rock Paper Scissors Game.md @@ -0,0 +1,48 @@ +```python +import tkinter as tk +import random + +def determine_winner(user_choice, computer_choice): + """Determine the winner of the game.""" + if user_choice == computer_choice: + return "It's a tie!", computer_choice + elif (user_choice == "rock" and computer_choice == "scissors") or \ + (user_choice == "paper" and computer_choice == "rock") or \ + (user_choice == "scissors" and computer_choice == "paper"): + return "You win!", computer_choice + else: + return "Computer wins!", computer_choice + +def play_game(): + """Play the game and display the result.""" + user_choice = user_var.get() + computer_choice = random.choice(["rock", "paper", "scissors"]) + result, computer_pick = determine_winner(user_choice, computer_choice) + result_label.config(text=result) + computer_label.config(text=f"Computer picked: {computer_pick}") + +# Create main window +root = tk.Tk() +root.title("Rock Paper Scissors") + +# User choice options +user_var = tk.StringVar() +user_var.set("rock") # Default choice +choices = ["rock", "paper", "scissors"] +for choice in choices: + rb = tk.Radiobutton(root, text=choice, variable=user_var, value=choice) + rb.pack() + +# Play button +play_button = tk.Button(root, text="Play", command=play_game) +play_button.pack() + +# Result label +result_label = tk.Label(root, text="", font=("Helvetica", 16)) +result_label.pack() + +# Computer pick label +computer_label = tk.Label(root, text="", font=("Helvetica", 12)) +computer_label.pack() + +root.mainloop()