Advertisement
Stillkill

Rock Paper Scissors Python Game (Now with GUI :D)

May 2nd, 2018 (edited)
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.97 KB | None | 0 0
  1. '''
  2. Last Revision: 5/2/2018 - 18:56
  3. Current Version: RPS V2.0 Release
  4. '''
  5. #Just paste this into a .py and have the most fun you will ever have with a random number generator
  6. #All this now comes with a very shitty GUI :D
  7.  
  8.  
  9. #manual imports
  10. from tkinter import *
  11. import random
  12. import datetime
  13.  
  14. def setRock():
  15.     playerSelection.set("Rock")
  16.     rps(1)
  17.     winrateSet()
  18.  
  19. def setPaper():
  20.     playerSelection.set("Paper")
  21.     rps(2)
  22.     winrateSet()
  23.  
  24. def setScissors():
  25.     playerSelection.set("Scissors")
  26.     rps(3)
  27.     winrateSet()
  28.  
  29. def winrateSet():
  30.     wins = gamesWonCounterCount.get()
  31.     losses = gamesLostCounterCount.get()
  32.     ties = gamesTiedCounterCount.get()
  33.     winrateCalc = wins/(losses + ties + wins)
  34.     winrateCalc = round(winrateCalc, 2)
  35.     winrate.set(winrateCalc)
  36.  
  37. def saveRes():
  38.     currenttime = datetime.datetime.now()
  39.     currenttime = 'Time of round: ' + str(currenttime)  + '\n \n'
  40.     wins = gamesWonCounterCount.get()
  41.     losses = gamesLostCounterCount.get()
  42.     ties = gamesTiedCounterCount.get()
  43.     winrategotten = winrate.get()
  44.     winrateString = "Win Rate: " + str(winrategotten) + '\n \n'
  45.     winString = "Wins: " + str(wins) + '\n \n'
  46.     tieString = "Ties: " + str(ties) + '\n \n'
  47.     lossString = "Losses: " + str(losses) + '\n \n'
  48.     file = open("RPS Game Results.txt", "a+")
  49.     file.write('--------=Results=--------- \n')
  50.     file.write(currenttime)
  51.     file.write(winString)
  52.     file.write(tieString)
  53.     file.write(lossString)
  54.     file.write(winrateString)
  55.     file.writelines("\n \n \n")
  56.     if winrategotten < 0.50:
  57.         file.write('Well that was a bad round, wasnt it? \n \n \n')
  58.     else:
  59.         pass
  60.     file.close()
  61.  
  62.  
  63.  
  64. #play AI generator
  65. def rps(userPlay):
  66.     roll = random.randint(1, 3)
  67.     #Roll Values: 1-Rock, 2-Paper, 3-Scissors
  68.     if roll == 1:
  69.         if userPlay == 1:
  70.             tempIntVar = gamesTiedCounterCount.get()
  71.             return gamesTiedCounterCount.set(tempIntVar + 1), aiSelection.set("Rock"), gameResult.set("Tie")
  72.         elif userPlay == 2:
  73.             tempIntVar = gamesLostCounterCount.get()
  74.             return gamesLostCounterCount.set(tempIntVar + 1), aiSelection.set("Rock"), gameResult.set("You've lost!")
  75.         elif userPlay == 3:
  76.             tempIntVar = gamesWonCounterCount.get()
  77.             return gamesWonCounterCount.set(tempIntVar + 1), aiSelection.set("Rock"), gameResult.set("You've won!")
  78.         else:
  79.             print("That wasn't one of the choices! Try again!")
  80.     elif roll == 2:
  81.         if userPlay == 1:
  82.             tempIntVar = gamesWonCounterCount.get()
  83.             return gamesWonCounterCount.set(tempIntVar + 1), aiSelection.set("Scissors"), gameResult.set("You've won!")
  84.         elif userPlay == 2:
  85.             tempIntVar = gamesTiedCounterCount.get()
  86.             return gamesTiedCounterCount.set(tempIntVar + 1), aiSelection.set("Scissors"), gameResult.set("Tie")
  87.         elif userPlay == 3:
  88.             tempIntVar = gamesLostCounterCount.get()
  89.             return gamesLostCounterCount.set(tempIntVar + 1), aiSelection.set("Scissors"), gameResult.set("You've lost!")
  90.         else:
  91.             print("That wasn't one of the choices! Try again!")
  92.     elif roll == 3:
  93.         if userPlay == 1:
  94.             tempIntVar = gamesLostCounterCount.get()
  95.             return gamesLostCounterCount.set(tempIntVar + 1), aiSelection.set("Paper"), gameResult.set("You've lost!")
  96.         elif userPlay == 2:
  97.             tempIntVar = gamesWonCounterCount.get()
  98.             return gamesWonCounterCount.set(tempIntVar + 1), aiSelection.set("Paper"), gameResult.set("You've won!")
  99.         elif userPlay == 3:
  100.             tempIntVar = gamesTiedCounterCount.get()
  101.             return gamesTiedCounterCount.set(tempIntVar + 1), aiSelection.set("Paper"), gameResult.set("Tie")
  102.         else:
  103.             print("That wasn't one of the choices! Try again!")
  104.     else:
  105.         print('Something went wrong!')
  106.         print('rollGenerator error: Random rumber generator returned a number that was out of range.')
  107.  
  108.  
  109. #GUI segment
  110. mainwindow = Tk()
  111. font = ("verdana", 24)
  112. mainwindow.minsize(750, 320)
  113. mainwindow.geometry("750x320")
  114. gamesWonCounterCount = IntVar()
  115. gamesWonCounterCount.set(0)
  116. gamesTiedCounterCount = IntVar()
  117. gamesTiedCounterCount.set(0)
  118. gamesLostCounterCount = IntVar()
  119. gamesLostCounterCount.set(0)
  120. playerSelection = StringVar()
  121. aiSelection = StringVar()
  122. gameResult = StringVar()
  123. winrate = DoubleVar()
  124. winrate.set(0)
  125.  
  126. #row 1
  127. pSel = Label(mainwindow, text="You've selected:", font=font)
  128. pSel.grid(row=1, column=1)
  129.  
  130. aSel = Label(mainwindow, text="The AI selected:", font=font)
  131. aSel.grid(row=1, column=3)
  132. #row 2
  133. plselection = Label(mainwindow, textvariable=playerSelection, font=font)
  134. plselection.grid(row=2, column=1)
  135. gameResultLabel = Label(mainwindow, textvariable=gameResult,bg="blue", fg="red", font=font)
  136. gameResultLabel.grid(row=1, column=2)
  137. aiSelectionLabel = Label(mainwindow, textvariable=aiSelection, font=font)
  138. aiSelectionLabel.grid(row=2, column=3)
  139. #row 3
  140. winLabel = Label(mainwindow, text="Wins", bg="Green", font=font)
  141. winLabel.grid(row=3, column=1)
  142. lossLabel = Label(mainwindow, text="Losses", bg="Red", font=font)
  143. lossLabel.grid(row=3, column=3)
  144. tieLabel = Label(mainwindow, text="Ties", bg="Yellow", font=font)
  145. tieLabel.grid(row=3, column=2)
  146. #row 4
  147. gamesWonCounter = Label(mainwindow, textvariable=gamesWonCounterCount, bg="Green", font=font,)
  148. gamesWonCounter.grid(row=4, column=1)
  149. gamesPlayedCounter = Label(mainwindow,textvariable=gamesTiedCounterCount, bg="Yellow", font=font,)
  150. gamesPlayedCounter.grid(row=4, column=2)
  151. gamesLostCounter = Label(mainwindow, textvariable=gamesLostCounterCount, bg="Red", font=font)
  152. gamesLostCounter.grid(row=4,column=3)
  153. #row 5
  154. rockButton = Button(mainwindow, text="Rock", font=font, command=setRock)
  155. rockButton.grid(row=5, column=1)
  156. paperButton = Button(mainwindow, text="Paper", font=font, command=setPaper)
  157. paperButton.grid(row=5, column=2)
  158. scissorButton = Button(mainwindow, text="Scissors", font=font, command=setScissors)
  159. scissorButton.grid(row=5, column=3)
  160. #row 6
  161. winrateLabel = Label(mainwindow, text="Current winrate", font=font)
  162. winrateLabel.grid(row=6, column=1)
  163. winrateLabelMath = Label(mainwindow, textvariable=winrate, font=font)
  164. winrateLabelMath.grid(row=6, column=2)
  165. saveResButton = Button(mainwindow, text="Save results", command=saveRes)
  166. saveResButton.grid(row=6, column=3)
  167. mainwindow.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement