I made this Rock Paper Scissors Game with GUI.
It works fine. But what can I do to make it simpler? Anybody?
import random
class RPC:
def __init__(self):
self.comChoice = ''
self.choices = ['Rock','Paper','Scissors']
def chooseCom(self):
self.comChoice = self.choices[random.randint(0,2)]
return self.comChoice
def player(self, choice, Com):
if choice == 'rock' and Com == 'Scissors':
return ('You won!')
elif choice == 'paper' and Com == 'Rock':
return ('You won!')
elif choice == 'scissors' and Com == 'Paper':
return('You won!')
elif choice == 'scissors' and Com == 'Rock':
return ('You lost!')
elif choice == 'rock' and Com== 'Paper':
return('You lost!')
elif choice == 'paper' and Com == 'Scissors':
return ('You lost!')
else:
return ('Tie!')
from tkinter import *
import RPCclass
def ComputerChoice():
game = RPCclass.RPC()
c = game.chooseCom()
return c
def winner(choice, Com):
game = RPCclass.RPC()
w = game.player(choice, Com)
return w
def numWin(self, choice, Com):
game = RPCclass.RPC()
a = game.player(choice, Com)
if a == 'You won!':
self.numPlayerWin +=1
elif a == 'You lost!':
self.numComWin +=1
else:
self.numPlayerWin += 0
self.numComWin +=0
self.message4.config(text = 'Player won ' + \
str(self.numPlayerWin) + ' times.')
self.message5.config(text = 'Computer won ' + \
str(self.numComWin) + ' times.')
class GUI:
def __init__(self):
root = Tk()
root.title('Welcome to Rock Paper Scissors')
self.numComWin = 0
self.numPlayerWin = 0
self.label = Label(root, text='"Choose Rock, Paper, or Scissors."',
fg='Black',
bd=10)
self.label.grid(row=5, columnspan = 3)
self.button1 = Button(root, text='Rock',
bg='Blue',
fg='white',
padx=10, pady=5,
command=self.buttonAction1)
self.button1.grid(row=7, column=0, rowspan=2)
self.button2 = Button(root, text='Paper',
bg='Blue',
fg='white',
padx=10, pady=5,
command=self.buttonAction2)
self.button2.grid(row=7, column =1, rowspan =2)
self.button3 = Button(root, text='Scissors',
bg='blue',
fg='white',
padx=10, pady=5,
command=self.buttonAction3)
self.button3.grid(row=7, column =2, rowspan=2)
self.button4 = Button(root, text='Reset',
bg='black',
fg='white',
padx=10, pady=5,
command=self.buttonAction4)
self.button4.grid(row=12, column =1, rowspan=2)
self.message1 = Label(root, text='',fg='Black')
self.message1.grid(row=9, columnspan=3)
self.message2 = Label(root, text='',fg='Black')
self.message2.grid(row=10, columnspan=3)
self.message3 = Label(root, text='',fg='Red', font=('Times',16,'bold'))
self.message3.grid(row=11, columnspan=3)
self.message4 = Label(root, text='',fg='black')
self.message4.grid(row=12, column=0, rowspan=2)
self.message5 = Label(root, text='',fg='black')
self.message5.grid(row=12, column=2, rowspan=2)
root.mainloop()
def buttonAction1(self):
choice = 'rock'
self.message1.config(text= 'You choose Rock.')
Com = ComputerChoice()
self.message2.config(text= 'Computer has chosen %s.' % Com)
self.message3.config(text = winner(choice, Com))
numWin(self,choice, Com)
def buttonAction2(self):
choice = 'paper'
self.message1.config(text= 'You choose Paper.')
Com = ComputerChoice()
self.message2.config(text= 'Computer has chosen %s.' % Com)
self.message3.config(text = winner(choice, Com))
numWin(self,choice, Com)
def buttonAction3(self):
choice = 'scissors'
self.message1.config(text= 'You choose Scissors.')
Com = ComputerChoice()
self.message2.config(text= 'Computer has chosen %s.' % Com)
self.message3.config(text = winner(choice, Com))
numWin(self,choice, Com)
def buttonAction4(self):
self.message1.config(text= '')
self.message2.config(text= '')
self.message3.config(text= '')
self.numComWin = 0
self.numPlayerWin = 0
numWin(self,0, 0)
self.message4.config(text = '')
self.message5.config(text = '')
GUI()