Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This is the source code, written in python 2.7, for a simple dice simulator
- # program. I, ElectroPassion, claim no copyright and you are welcome to modify,
- # utilize and distribute this code as you wish. As a beginner programmer, I
- # welcome comments, requests and general communication related to programming.
- # Features:
- # -All rolls and tallies displayed as text. No dice graphics or rolling animations.
- # -Roll 1-4 six sided dice
- # -Tally the value of the dice last rolled
- # -Clear the cache
- # -Quit the program
- from Tkinter import*
- from random import randint
- def Roll1Dice():
- global LastRoll
- LastRoll = 1
- global Roll1
- Roll1 = randint(1,6)
- Dice1.insert(END, "Dice1 is: " + str(object=Roll1))
- return
- def Roll2Dice():
- global LastRoll
- LastRoll = 2
- global Roll1
- global Roll2
- Roll1 = randint(1,6)
- Roll2 = randint(1,6)
- Dice1.insert(END, "Dice1 is: " + str(object=Roll1))
- Dice2.insert(END, "Dice2 is: " + str(object=Roll2))
- return
- def Roll3Dice():
- global LastRoll
- LastRoll = 3
- global Roll1
- global Roll2
- global Roll3
- Roll1 = randint(1,6)
- Roll2 = randint(1,6)
- Roll3 = randint(1,6)
- Dice1.insert(END, "Dice1 is: " + str(object=Roll1))
- Dice2.insert(END, "Dice2 is: " + str(object=Roll2))
- Dice3.insert(END, "Dice2 is: " + str(object=Roll3))
- return
- def Roll4Dice():
- global LastRoll
- LastRoll = 4
- global Roll1
- global Roll2
- global Roll3
- global Roll4
- Roll1 = randint(1,6)
- Roll2 = randint(1,6)
- Roll3 = randint(1,6)
- Roll4 = randint(1,6)
- Dice1.insert(END, "Dice1 is: " + str(object=Roll1))
- Dice2.insert(END, "Dice2 is: " + str(object=Roll2))
- Dice3.insert(END, "Dice2 is: " + str(object=Roll3))
- Dice4.insert(END, "Dice2 is: " + str(object=Roll4))
- return
- def TallyDice():
- if LastRoll == 1:
- TallyA = Roll1
- if LastRoll == 2:
- TallyA = Roll1 + Roll2
- if LastRoll == 3:
- TallyA = Roll1 + Roll2 + Roll3
- if LastRoll == 4:
- TallyA = Roll1 + Roll2 + Roll3 + Roll4
- if LastRoll == 0:
- TallyDispA.insert(END, "Please Roll the Dice.")
- return
- TallyDispA.insert(END, "The tally is: " + str(object=TallyA))
- global LastRoll
- LastRoll = 0
- return
- def ClearList():
- Dice1.delete(0, END)
- Dice2.delete(0, END)
- Dice3.delete(0, END)
- Dice4.delete(0, END)
- TallyDispA.delete(0, END)
- return
- RD = Frame()
- RD.grid()
- Label = Label(text = 'This is a dice simulator!')
- Label.grid(row=0,column=0,padx=1,sticky=W+E,columnspan=2)
- Dice1 = Listbox(bg="grey",fg="black")
- Dice1.grid(row=1,column=0,padx=1,sticky=W+E)
- Dice2 = Listbox(bg="grey",fg="black")
- Dice2.grid(row=1,column=1,padx=1,sticky=W+E)
- Dice3 = Listbox(bg="grey",fg="black")
- Dice3.grid(row=2,column=0,padx=1,sticky=W+E)
- Dice4 = Listbox(bg="grey",fg="black")
- Dice4.grid(row=2,column=1,padx=1,sticky=W+E)
- TallyDispA = Listbox(bg="grey",fg="black")
- TallyDispA.grid(row=3,column=0,padx=1,sticky=W+E,columnspan=2)
- Roll1Dice = Button(text="Roll One Die",command=Roll1Dice,fg="red")
- Roll1Dice.grid(row=4,column=0,padx=1,sticky=W+E)
- Roll2Dice = Button(text="Roll Two Dice",command=Roll2Dice,fg="red")
- Roll2Dice.grid(row=4,column=1,padx=1,sticky=W+E)
- Roll3Dice = Button(text="Roll Three Dice",command=Roll3Dice,fg="red")
- Roll3Dice.grid(row=5,column=0,padx=1,sticky=W+E)
- Roll4Dice = Button(text="Roll Four Dice",command=Roll4Dice,fg="red")
- Roll4Dice.grid(row=5,column=1,padx=1,sticky=W+E)
- TallyDice = Button(text="TallyDice",command=TallyDice,fg="red")
- TallyDice.grid(row=6,column=0,padx=1,sticky=W+E,columnspan=2)
- ClearList = Button(text="Clear List",command=ClearList,fg="red")
- ClearList.grid(row=7,column=0,padx=1,sticky=W+E,columnspan=2)
- Quit = Button(text="QuitGame",command=quit,fg="red")
- Quit.grid(row=8,column=0,padx=1,sticky=W+E,columnspan=2)
- RD.master.configure(background="blue")
- RD.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement