Dice Rolling Simulator using Python
Time again for the game script using python. We can play many games using dice like Ludo, Snake and ladders, etc….Let’s design our dice with some basic knowledge about python. So here is the interesting dice rolling simulator project with code in python.
Dice rolling simulator in Python
Dice is a small cube that has 1 to 6 numbers on its sides which is used in games for providing random numbers. We will design a “Roll the dice” program to roll the dice using the random and Tkinter module.
Project Prerequisites
The Dice rolling simulator with GUI project in python requires you to have basic knowledge of python programming, Tkinter, and random modules.
- Tkinter — For User Interface (UI)
- random–It is for generating the random numbers in python
- PIL- It is one of the important modules for image processing in python.
Download Dice Rolling Simulator Python Program
Please download the source code of python dice rolling simulator: Dice Rolling Simulator Python Code
Steps to Build the Python Project on Dice Rolling Simulator
- Import the necessary Libraries
- Creating a GUI window
- Formatting a list of images to be displayed randomly
- Defining roll the dice function and creating buttons
- Main Function
1. Import the Libraries:
- Tkinter – It is the inbuilt python module useful to create GUI applications. To install Tkinter, go to command prompt and type pip install Tkinter
- random – It is used to generate the random numbers in python
- PIL – It is one of the important modules and it is used for image processing in python.
import tkinter from PIL import Image,ImageTk import random
2. Creating a GUI window:
- root=tkinter.Tk () This line of code automatically creates a GUI window with a title bar, close button
- We can change the title using title of root window using root.title(” “)
#top level widgets which represents the main window of the Dice Rolling Simulator in python from PIL.ImageTk import PhotoImage root=tkinter.Tk() root.geometry('400x400') root.title("PythonGeeks-Roll the Dice")
3. Formatting a list of images to be displayed randomly:
- The PhotoImage widget helps to display an image for a label or button.
- Label() is a widget that is useful to implement display boxes where you can place text or images.
dice = ['die1.png', 'die2.png', 'die3.png', 'die4.png', 'die5.png', 'die6.png'] #simulating the dice with random variables 1 to 6 and generate image using Python image1=ImageTk.PhotoImage(Image.open(random.choice(dice))) #constructing a Label widget for image label1=tkinter.Label(root,image=image1) label1.image=image1 #packing a widget in the parent widget label1.pack(expand=True)
4. Defining roll the dice function and creating buttons:
- The BUTTON widget is for displaying text or images assigned to buttons. When we click the button, the command will use rolling_dice function.
- We pack the button widget in parent widget.
- In the rolling_dice function, we use the label1.configure to update the image.
def rolling_dice(): image1=ImageTk.PhotoImage(Image.open(random.choice(dice))) #update image based on dice roll label1.configure(image=image1) #keep a reference label1.image=image1 #adding buttons,and command will use rolling_dice function button=tkinter.Button(root,text="Roll the dice",fg="green",command=rolling_dice) #pack a widget in parent widget button.pack(expand="True")
5. Main Function:
- root.mainloop() is the main method of the window. This method will loop forever that execution what we want to execute.
#call the mainloop root.mainloop()
Python Dice Rolling Simulator Output
Summary
Yay!! With this project in python, we have successfully developed and executed the Dice rolling simulator application in python. Now you can just click the button to get the random number. We used tkinter, PIL and random modules to develop our project. Executing different functions and using many widgets and using loops helps us to develop our python programming skills.
Mini projects for BCA Final year students using Python programming [PPT and Source code]