Installing Image Processing Module:
Image processing module will require if you want to create the dice rolling simulator with GUI. Run the following commands from the terminal to update the pip and install the Pillow module.
$ python3 -m pip install --upgrade Pillow
Example-1: Creating a simple dice rolling simulator
A simple dice rolling simulator can be created using Python random module in this tutorial. This script will generate the dice output in the terminal. So, no image processing module is used in the script. A variable named dice is used to take any number from 1 to 6 randomly. The roll_the_dice() function is defined here to generate the output based on the random value. The infinite while loop is used in the script to roll the dice infinite times until the user types anything without ‘y’.
import random
# Define infinite loop
while True:
# Generate a random number between 1 and 6
dice = random.randint(1, 6)
# Define the function to print the output based on the dice value
def roll_the_dice(dice):
switcher = {
1: "[ ]\n| 0 |\n[ ]",
2: "[0 ]\n| |\n[ 0]",
3: "[0 ]\n| 0 |\n[ 0]",
4: "[0 0]\n| |\n[0 0]",
5: "[0 0]\n| 0 |\n[0 0]",
6: "[ 0 0 ]\n| 0 0 |\n[ 0 0 ]"
}
return switcher.get(dice)
# Call the function
print(roll_the_dice(dice))
# Ask user for rolling the dice again
answer = input("Do you want to roll the dice again(y/n)? : ")
# Terminate the loop if the user type anything without 'y'
if answer != 'y':
exit(0)
Output:
The output of the above script will vary in each iteration of the loop for using random integer numbers. In the following output, 5 is generated in the first iteration of the loop displayed using 5 zeros. Next, the user typed ‘y’ to continue the loop, and 4 has generated in the second iteration of the loop and displayed the value using zeros like before. The script has terminated when the user types ‘n’.
Example-2: Create a dice-rolling simulator with images
You have to create six dice images before executing the script of the example. You can create the images by using any image-creating application. The following six images have been created to use in the script of this example. Keep the images in the same location where you will create the python file to implement the simulator.
The way of creating a dice-rolling simulator with GUI is shown in the following script. Many modules exist in Python to create GUI applications. Tkinter module is used in this script to implement GUI based dice rolling simulator. Image and ImageTk modules are used in the script to display dice images. The random module is used to select the dice image randomly. At the beginning of the script, the Tkinter object has created to define the window size and position where the dice image will be displayed. Here, the window position has been set centrally. Next, a list variable has declared with the six dice image names. random.choice() function is used to randomly select one index of the image name from the list and get the image name based on the index value. expand=True has used in the pack() function to add extra space when adding a widget in the window. A label has been added in the window to display the dice image, and a button has been added in the window to display the next dice image. The roll_the_dice() function has associated with the button to generate the next dice image. This function will be called when the user will press or click the ‘ROLL THE DICE’ button.
import tkinter
# Import Image and ImageTk modules
from PIL import Image, ImageTk
# Import random module
import random
# Define object to display the main window of the application
win = tkinter.Tk()
# Set the title of the main window
win.title('Rolling Dice')
# Set the height and width of the main window
win.geometry('350x280')
# Set the position of the window
win.eval('tk::PlaceWindow . center')
# Define list with the six dice image names
dice_images = ['d1.png', 'd2.png', 'd3.png', 'd4.png', 'd5.png', 'd6.png']
# Generate any index within 0 to 6 randomly to get the current dice image
img= ImageTk.PhotoImage(Image.open(random.choice(dice_images)))
# Define label to display the selected image
lbl_image = tkinter.Label(win, image=img)
lbl_image.image = img
# Set the label widget inside the parent window
lbl_image.pack(expand=True)
# Define function to change the dice image when the button is clicked
def roll_the_dice():
img= ImageTk.PhotoImage(Image.open(random.choice(dice_images)))
# update image
lbl_image.configure(image=img)
# keep a reference
lbl_image.image = img
'''
Define button, set the button text and set command
to call the roll_the_dice() function
'''
btn = tkinter.Button(win, text='ROLL THE DICE', command=roll_the_dice)
# Set the button inside the parent window
btn.pack(expand=True)
# Call the mainloop of Tkinter to open the main window
win.mainloop()
Output:
Like the previous example, the dice image will be selected randomly every time the button will be pressed. In the following output, the dice image of six has been generated after executing the script.
The dice image of three has been generated after pressing the button.
Conclusion:
Two different ways of creating a dice-rolling simulator using Python script have been explained in this tutorial. It will help the readers to know how this type of application can be implemented in Python.