Created
March 10, 2023 10:54
-
-
Save R3DHULK/73e7a8f2b2c6d5239f47850782268ff2 to your computer and use it in GitHub Desktop.
GUI Based Rock Paper Scissor Game With Scoreboard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import tkinter as tk | |
# Define game constants | |
OPTIONS = ['rock', 'paper', 'scissors'] | |
WIN_CONDITIONS = { | |
'rock': 'scissors', | |
'paper': 'rock', | |
'scissors': 'paper' | |
} | |
# Initialize scores | |
player_score = 0 | |
computer_score = 0 | |
# Create GUI window | |
root = tk.Tk() | |
root.title("Rock Paper Scissors") | |
# Define score label widgets | |
player_label = tk.Label(root, text="Player: 0") | |
player_label.grid(row=0, column=0) | |
computer_label = tk.Label(root, text="Computer: 0") | |
computer_label.grid(row=1, column=0) | |
result_label = tk.Label(root, text="") | |
result_label.grid(row=2, column=0) | |
# Define button click functions | |
def play(option): | |
global player_score, computer_score | |
computer_choice = random.choice(OPTIONS) | |
if option == computer_choice: | |
result_label.config(text="Tie game!") | |
elif WIN_CONDITIONS[option] == computer_choice: | |
result_label.config(text="Player wins!") | |
player_score += 1 | |
player_label.config(text="Player: {}".format(player_score)) | |
else: | |
result_label.config(text="Computer wins!") | |
computer_score += 1 | |
computer_label.config(text="Computer: {}".format(computer_score)) | |
# Define button widgets | |
rock_button = tk.Button(root, text="Rock", command=lambda: play('rock')) | |
rock_button.grid(row=3, column=0, padx=10, pady=10) | |
paper_button = tk.Button(root, text="Paper", command=lambda: play('paper')) | |
paper_button.grid(row=3, column=1, padx=10, pady=10) | |
scissors_button = tk.Button(root, text="Scissors", | |
command=lambda: play('scissors')) | |
scissors_button.grid(row=3, column=2, padx=10, pady=10) | |
# Start GUI loop | |
root.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment