|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | + |
| 4 | +import random |
| 5 | + |
| 6 | +from tkinter import * |
| 7 | + |
| 8 | +#variables and Dictionary |
| 9 | +#These are total events that could occur if/else can also be used but they are pain to implement |
| 10 | +schema={ |
| 11 | + "rock":{"rock":1,"paper":0,"scissors":2}, |
| 12 | + "paper":{"rock":2,"paper":1,"scissors":0}, |
| 13 | + "scissors":{"rock":0,"paper":2,"scissors":1} |
| 14 | + } |
| 15 | + |
| 16 | +comp_score=0 |
| 17 | + |
| 18 | +player_score=0 |
| 19 | + |
| 20 | +#functions |
| 21 | + |
| 22 | +def outcome_handler(user_choice): |
| 23 | + global comp_score |
| 24 | + global player_score |
| 25 | + outcomes=["rock","paper","scissors"] |
| 26 | + num=random.randint(0, 2) |
| 27 | + computer_choice=outcomes[num] |
| 28 | + result=schema[user_choice][computer_choice] |
| 29 | + |
| 30 | + #now config the labes acc to the choices |
| 31 | + Player_Choice_Label.config(fg="green",text="Player choice : "+str(user_choice)) |
| 32 | + |
| 33 | + Computer_Choice_Label.config(fg="red",text="Computer choice : "+str(computer_choice)) |
| 34 | + |
| 35 | + if result==2: |
| 36 | + player_score+=2 |
| 37 | + Player_Score_Label.config(text="Player : "+str(player_score)) |
| 38 | + Outcome_Label.config(fg="blue",bg="skyblue",text="Player-Won") |
| 39 | + elif result==1: |
| 40 | + player_score+=1 |
| 41 | + comp_score+=1 |
| 42 | + Player_Score_Label.config(text="Player : "+str(player_score)) |
| 43 | + Outcome_Label.config(fg="blue",bg="skyblue",text="Draw") |
| 44 | + Computer_Score_Label.config(text="Computer : "+str(comp_score)) |
| 45 | + elif result==0: |
| 46 | + comp_score+=2 |
| 47 | + Outcome_Label.config(fg="blue",bg="skyblue",text="Computer-Won") |
| 48 | + Computer_Score_Label.config(text="Computer : "+str(comp_score)) |
| 49 | +#main Screen |
| 50 | +master=Tk() |
| 51 | +master.title("RPS") |
| 52 | +#labels |
| 53 | +Label(master,text="Rock , Paper , Scissors",font=("Calibri",15)).grid(row=0,sticky=N,pady=10,padx=200) |
| 54 | + |
| 55 | +Label(master,text="Please Select an option",font=("Calibri",12)).grid(row=2,sticky=N) |
| 56 | + |
| 57 | +Player_Score_Label=Label(master,text="Player : 0",font=("Calibri",12)) #label for player Score |
| 58 | +Player_Score_Label.grid(row=3,sticky=W) |
| 59 | + |
| 60 | +Computer_Score_Label=Label(master,text="Computer : 0",font=("Calibri",12)) #label for computer score |
| 61 | +Computer_Score_Label.grid(row=3,sticky=E) |
| 62 | +#player and computer choice labels |
| 63 | +Player_Choice_Label=Label(master,font=("Calibri",12)) |
| 64 | +Player_Choice_Label.grid(row=5,sticky=W) |
| 65 | + |
| 66 | +Computer_Choice_Label=Label(master,font=("Calibri",12)) |
| 67 | +Computer_Choice_Label.grid(row=5,sticky=E) |
| 68 | +#outcome Labels |
| 69 | +Outcome_Label=Label(master,font=("Calibri",12)) |
| 70 | +Outcome_Label.grid(row=5,sticky=N,pady=10) |
| 71 | + |
| 72 | + |
| 73 | +#buttons |
| 74 | +Button(master,text="Rock",width=17,command=lambda:outcome_handler("rock")).grid(row=6,sticky=W,padx=10,pady=10) |
| 75 | +Button(master,text="Paper",width=17,command=lambda:outcome_handler("paper")).grid(row=6,sticky=N,pady=10) |
| 76 | +Button(master,text="Scissors",width=17,command=lambda:outcome_handler("scissors")).grid(row=6,sticky=E,padx=10,pady=10) |
| 77 | + |
| 78 | +#dummy label to create space at the end of master screen |
| 79 | +Label(master).grid(row=5) |
| 80 | +master.mainloop() |
0 commit comments