Skip to content

added rock paper scissor #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 4, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions Mini Project/r-p-s/game.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import random
import tkinter as tk
from PIL import Image,ImageTk



window=tk.Tk()
window.geometry("300x500")
window.title("Scissor Paper Rock @RajChowdhury ")

image=Image.open('aa.jpg')
image.thumbnail((300,300),Image.ANTIALIAS)
photo=ImageTk.PhotoImage(image)
label_image=tk.Label(image=photo)
label_image.grid(column=15,row=0)

#global variables
USER_SCORE=0
COMP_SCORE=0
USER_CHOICE=""
COMP_CHOICE=""



def choice_to_number(choice):
rps={'scissor':0,'paper':1,'rock':2}
return rps[choice]

def number_to_choice(number):
rps={0:'scissor',1:'paper',2:'rock'}
return rps[number]

def random_computer_choice():
return random.choice(['scissor','paper','rock'])

def result(human_choice,comp_choice):
global USER_SCORE
global COMP_SCORE

user=choice_to_number(human_choice)
comp=choice_to_number(comp_choice)

if(user==comp):
print("Tie")
elif((user-comp)%3==1):
print("Sorry !! Com win")
USER_SCORE+=1
else:
print("Congarts !! You win")
COMP_SCORE+=1


#Text
text_area=tk.Text(master=window,height=12,width=30)
text_area.grid(column=15,row=4)
answer="Your Choice: {uc} \nComputer's Choice : {cc} \n Your Score : {u} \n Computer Score : {c} \n\n made by diwas pandey ".format(uc=USER_CHOICE,cc=COMP_CHOICE,u=USER_SCORE,c=COMP_SCORE, font=('arial',24,'bold'))
text_area.insert(tk.END,answer)


#Event Handling
def rock():
global USER_CHOICE
global COMP_CHOICE

USER_CHOICE='rock'
COMP_CHOICE=random_computer_choice()
result(USER_CHOICE,COMP_CHOICE)

def paper():
global USER_CHOICE
global COMP_CHOICE

USER_CHOICE='paper'
COMP_CHOICE=random_computer_choice()
result(USER_CHOICE,COMP_CHOICE)

def scissor():
global USER_CHOICE
global COMP_CHOICE

USER_CHOICE='scissor'
COMP_CHOICE=random_computer_choice()
result(USER_CHOICE,COMP_CHOICE)

#buttons
button1=tk.Button(text=" Scissor ",bg="red",command=scissor, height=1,width=8,font=('arial',15,'bold'))
button1.grid(column=15,row=1)
button2=tk.Button(text=" Paper ",bg="pink",command=paper, height=1,width=8,font=('arial',15,'bold'))
button2.grid(column=15,row=2)
button3=tk.Button(text=" Rock ",bg="yellow",command=rock, height=1,width=8,font=('arial',15,'bold'))
button3.grid(column=15,row=3)



window.mainloop()