Dice Rolling Simulator in Python [with source code]

In the world of board games, dice have been the most common thing linking most of the board games. We end up losing the dice because of their small size. How about building a dice that we cannot lose. Let’s create a dice rolling simulator in python

Dice Rolling Simulator Project

In this python project, we are going to build a simple dice roll program using Python.

It is a simple program and you will gain knowledge about unicode strings. Apart from this, we used random module to generate the dice numbers randomly

We also use tkinter to create a graphical user interface, user just need to click on roll button which will generate a random number between 1 and 6.

Project Prerequisites

Dice rolling simulator project requires basic knowledge of python and tkinter.

To install tkinter, please run below command:

pip install tkinter

Download Dice Rolling Simulator Python Program

Please download the source code of python dice rolling simulator: Dice Rolling Simulator Python Code

Project File Structure

First, let’s look into the dice rolling simulator python program overview:

  1. Import the random and tkinter modules.
  2. Create the GUI.
  3. Create the dice roll function and the button.
  4. Call the GUI.

1. Import the required tkinter and random packages

import random
import tkinter

Code Explanation:

Let’s begin with the start of the program by importing the required libraries

  • Random: This library is used to select a random value from the given values.
  • Tkinter library: Tkinter is the standard GUI library for Python. Tkinter is used to make the GUI in python

2. Create the GUI

root = tkinter.Tk()
root.geometry('600x600')
root.title('ProjectGurukul Roll Dice')

#label to print result
label = tkinter.Label(root, text='', font=('Helvetica', 260))

#label to introduce
label2 = tkinter.Label(root, text='Welcome to ProjectGurukul Dice roll. Click to roll dice ', font=('Helvetica',10))
label2.place(x=150,y=400)

Explanation:

The main function part involves creation of the Tk root widget.

Label()- This function creates a label that can make text appear on the GUI.

3. Create the function:

def roll_dice():
    value = ['\u2680', '\u2681', '\u2682', '\u2683', '\u2684', '\u2685']
    result=random.choice(value)
    label.configure(text=result)
    label.pack()
    if(result=='\u2680'):
        label3=tkinter.Label(root,text='You rolled a one! Click roll dice to roll again.',font=('Helvetica',10))
        label3.place(x=150,y=450)
    elif(result=='\u2681'):
        label3=tkinter.Label(root,text='You rolled a two! Click roll dice to roll again.',font=('Helvetica',10))
        label3.place(x=150,y=450)
    elif(result=='\u2682'):
        label3=tkinter.Label(root,text='You rolled a three! Click roll dice to roll again.',font=('Helvetica',10))
        label3.place(x=150,y=450)
    elif(result=='\u2683'):
        label3=tkinter.Label(root,text='You rolled a four! Click roll dice to roll again.',font=('Helvetica',10))
        label3.place(x=150,y=450)
    elif(result=='\u2684'):
        label3=tkinter.Label(root,text='You rolled a five! Click roll dice to roll again.',font=('Helvetica',10))
        label3.place(x=150,y=450)
    elif(result=='\u2685'):
        label3=tkinter.Label(root,text='You rolled a six! Click roll dice to roll again.',font=('Helvetica',10))
        label3.place(x=150,y=450)

Explanation:

  • The function roll_dice is used to select one of the values and then print the value.
  • Value: This stores all the unicode values for the face of the dice.
  • Result: This stores a random value selected from value.
  • We then print the value using a label.
  • The if else-if statements are used to tell the number in text form and ask the user to press the button to roll again.

4. Create the button.

button = tkinter.Button(root, text='roll dice', foreground='red', command=roll_dice)
button.pack()
root.mainloop()

Explanation:

  • Button()- This function creates a button and sets a function to perform when pressed.
  • The button calls the function dice_roll.
  • Mainloop()- This function is used to update the GUI and wait for the events to happen.

Python Dice Rolling Simulator Output

This is the output screen of dice rolling simulator when we press the “roll dice” button:

python dice rolling simulator output

Summary

We have successfully created python dice rolling simulator. We have learned the use of the classes: random – to generate random numbers when we roll dice and Tkinter: to create a simple user interface. We also learned the basics of classes. Now you can get back to board games using this dice rolling simulator.

Leave a Reply

Your email address will not be published. Required fields are marked *