Advertisement
ElectroPassion

Dice Simulator

Dec 16th, 2014
652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. # This is the source code, written in python 2.7, for a simple dice simulator
  2. # program. I, ElectroPassion, claim no copyright and you are welcome to modify,
  3. # utilize and distribute this code as you wish. As a beginner programmer, I
  4. # welcome comments, requests and general communication related to programming.
  5.  
  6. # Features:
  7. # -All rolls and tallies displayed as text. No dice graphics or rolling animations.
  8. # -Roll 1-4 six sided dice
  9. # -Tally the value of the dice last rolled
  10. # -Clear the cache
  11. # -Quit the program
  12.  
  13. from Tkinter import*
  14. from random import randint
  15.  
  16. def Roll1Dice():
  17. global LastRoll
  18. LastRoll = 1
  19. global Roll1
  20. Roll1 = randint(1,6)
  21. Dice1.insert(END, "Dice1 is: " + str(object=Roll1))
  22. return
  23.  
  24. def Roll2Dice():
  25. global LastRoll
  26. LastRoll = 2
  27. global Roll1
  28. global Roll2
  29. Roll1 = randint(1,6)
  30. Roll2 = randint(1,6)
  31. Dice1.insert(END, "Dice1 is: " + str(object=Roll1))
  32. Dice2.insert(END, "Dice2 is: " + str(object=Roll2))
  33. return
  34.  
  35. def Roll3Dice():
  36. global LastRoll
  37. LastRoll = 3
  38. global Roll1
  39. global Roll2
  40. global Roll3
  41. Roll1 = randint(1,6)
  42. Roll2 = randint(1,6)
  43. Roll3 = randint(1,6)
  44. Dice1.insert(END, "Dice1 is: " + str(object=Roll1))
  45. Dice2.insert(END, "Dice2 is: " + str(object=Roll2))
  46. Dice3.insert(END, "Dice2 is: " + str(object=Roll3))
  47. return
  48.  
  49. def Roll4Dice():
  50. global LastRoll
  51. LastRoll = 4
  52. global Roll1
  53. global Roll2
  54. global Roll3
  55. global Roll4
  56. Roll1 = randint(1,6)
  57. Roll2 = randint(1,6)
  58. Roll3 = randint(1,6)
  59. Roll4 = randint(1,6)
  60. Dice1.insert(END, "Dice1 is: " + str(object=Roll1))
  61. Dice2.insert(END, "Dice2 is: " + str(object=Roll2))
  62. Dice3.insert(END, "Dice2 is: " + str(object=Roll3))
  63. Dice4.insert(END, "Dice2 is: " + str(object=Roll4))
  64. return
  65.  
  66. def TallyDice():
  67. if LastRoll == 1:
  68. TallyA = Roll1
  69. if LastRoll == 2:
  70. TallyA = Roll1 + Roll2
  71. if LastRoll == 3:
  72. TallyA = Roll1 + Roll2 + Roll3
  73. if LastRoll == 4:
  74. TallyA = Roll1 + Roll2 + Roll3 + Roll4
  75. if LastRoll == 0:
  76. TallyDispA.insert(END, "Please Roll the Dice.")
  77. return
  78. TallyDispA.insert(END, "The tally is: " + str(object=TallyA))
  79. global LastRoll
  80. LastRoll = 0
  81. return
  82.  
  83. def ClearList():
  84. Dice1.delete(0, END)
  85. Dice2.delete(0, END)
  86. Dice3.delete(0, END)
  87. Dice4.delete(0, END)
  88. TallyDispA.delete(0, END)
  89. return
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97. RD = Frame()
  98. RD.grid()
  99. Label = Label(text = 'This is a dice simulator!')
  100. Label.grid(row=0,column=0,padx=1,sticky=W+E,columnspan=2)
  101. Dice1 = Listbox(bg="grey",fg="black")
  102. Dice1.grid(row=1,column=0,padx=1,sticky=W+E)
  103. Dice2 = Listbox(bg="grey",fg="black")
  104. Dice2.grid(row=1,column=1,padx=1,sticky=W+E)
  105. Dice3 = Listbox(bg="grey",fg="black")
  106. Dice3.grid(row=2,column=0,padx=1,sticky=W+E)
  107. Dice4 = Listbox(bg="grey",fg="black")
  108. Dice4.grid(row=2,column=1,padx=1,sticky=W+E)
  109. TallyDispA = Listbox(bg="grey",fg="black")
  110. TallyDispA.grid(row=3,column=0,padx=1,sticky=W+E,columnspan=2)
  111.  
  112.  
  113. Roll1Dice = Button(text="Roll One Die",command=Roll1Dice,fg="red")
  114. Roll1Dice.grid(row=4,column=0,padx=1,sticky=W+E)
  115. Roll2Dice = Button(text="Roll Two Dice",command=Roll2Dice,fg="red")
  116. Roll2Dice.grid(row=4,column=1,padx=1,sticky=W+E)
  117. Roll3Dice = Button(text="Roll Three Dice",command=Roll3Dice,fg="red")
  118. Roll3Dice.grid(row=5,column=0,padx=1,sticky=W+E)
  119. Roll4Dice = Button(text="Roll Four Dice",command=Roll4Dice,fg="red")
  120. Roll4Dice.grid(row=5,column=1,padx=1,sticky=W+E)
  121.  
  122.  
  123. TallyDice = Button(text="TallyDice",command=TallyDice,fg="red")
  124. TallyDice.grid(row=6,column=0,padx=1,sticky=W+E,columnspan=2)
  125. ClearList = Button(text="Clear List",command=ClearList,fg="red")
  126. ClearList.grid(row=7,column=0,padx=1,sticky=W+E,columnspan=2)
  127. Quit = Button(text="QuitGame",command=quit,fg="red")
  128. Quit.grid(row=8,column=0,padx=1,sticky=W+E,columnspan=2)
  129. RD.master.configure(background="blue")
  130. RD.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement