Dice Rolling Simulator in Python – [GUI Source Code]

This article will build a classic dice rolling simulator in python.

Dice Rolling Simulator in Python

WHAT IS DICE ROLLING SIMULATOR IN PYTHON?

A dice rolling simulator in python is nothing but a computer game or a program that simulates the same as regular dice in which users roll dice, and a random number gets shown on the computer screen.

You can also see how to create a mad libs game in python that helps you build a fun python game.

PROJECT PREREQUISITES:

In this dice rolling simulator project in python, you just need to know basic python. That includes.

  • Python print() function
  • Python Input / Output function
  • Python loops and conditional statements
  • Python Tkinter

WHAT WILL YOU LEARN?

  • End to End development of Python game.
  • Basic Input / Output implementation.
  • Python GUI Development

HOW TO MAKE A DICE ROLLING SIMULATOR IN PYTHON?

First, let us implement a simple implementation of the dice rolling simulator then we will jump to the advanced GUI development.

Here we will use random.randint() function is used to generate random numbers based on the given input range.

Step 1: First, we will import the random function and declare the necessary variable.

import random
x = "y"

Step 2: Now, with the simple implementation of a while loop and a condition startment we will develop a dice rolling simulator.

while x == "y":

  no = random.randint(1,6)

  if no == 1:
    print("[-----]")
    print("[     ]")
    print("[  0  ]")
    print("[     ]")
    print("[-----]")
  if no == 2:
    print("[-----]")
    print("[  0  ]")
    print("[     ]")
    print("[  0  ]")
    print("[-----]")
  if no == 3:
    print("[-----]")
    print("[     ]")
    print("[0 0 0]")
    print("[     ]")
    print("[-----]")
  if no == 4:
    print("[-----]")
    print("[ 0 0 ]")
    print("[     ]")
    print("[ 0 0 ]")
    print("[-----]")
  if no == 5:
    print("[-----]")
    print("[ 0 0 ]")
    print("[  0  ]")
    print("[ 0 0 ]")
    print("[-----]")
  if no == 6:
    print("[-----]")
    print("[0 0 0]")
    print("[     ]")
    print("[0 0 0]")
    print("[-----]")

  x=input("press y to roll again and n to exit:") 
  print("\n")

Explanation: Here we are using randon function with the while loop which will generate a number between 1 to 6 everytime user will press y key. Then the if condition will check the number and will print the dice based on the generated number.

Now let develop a GUI program to build a dice rolling simulator in python using python tkinter.

WHAT IS TKINTER?

Tkinter is a python packages that is design to develop GUI (Graphical User Interface) program using python programming language. It provides very easy object oriented interface to build GUI program and it can be run on most of operating system like Windows, Mac or Linux.

So let start by installing tkinter in our system by suing below command.

pip install tk

Step 1: First we will import tkinter and random libraries as we imported in above program.

import random
import tkinter as tk

Step 2: Now we will build  a  basic interface of our python program using below command.

root = tk.Tk()

root.geometry('600×600')

root.title('PythonScholar Roll Dice')

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

#label to introduce
label2 = tk.Label(root, text='Click to Roll Dice', font=('Helvetica',10))

label2.place(x=150,y=400)

Here we are initialization tkinter with variable root and we are using geometry method to define the size of the GUI window and then we are using Label method to print the necessary label on the GUI window.

Step 3: we have define a user-defined function to make a main machanizam to generate a dice it this function will be a core of our program.

def roll_dice():
    value = ['\u2680', '\u2681', '\u2682', '\u2683', '\u2684', '\u2685']
    result=random.choice(value)
    label.configure(text=result)
    label.pack()
    if(result=='\u2680'):
        label3=tk.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=tk.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=tk.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=tk.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=tk.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=tk.Label(root,text='You rolled a six! Click roll dice to roll again.',font=('Helvetica',10))
        label3.place(x=150,y=450)

Step 4: Here we will close or program and adding a button to call our user-defined function.

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

And last we will end our program by using mainloop() method, and tkinter will only execute when a mainloop() method is called.

Here is the GUI output of our dice rolling simulator program.

Dice Photo
Dice Photo 2

Download Dice Rolling Simulator in Python from github

CONCLUSION

In this tutorial we have learn to implement the basic tkinter package to develop a dice rolling simulator and we are also using random function to auto generate the random numbers.