Coders Packet

GUI Dice Simulator in Python Using Pygame

By Shubham Menroy

It is a simple GUI-based dice simulator developed in Python using Pygame which simulates dice roll outcomes.

It is a basic project developed in python using pygame for beginners.

It is a simple interesting dice simulator that simulates dice roll outcomes. It is a 2d simulator/game developed in python using pygame. 

Pygame is a module that is used to develop games in python. (For more details Click Here)

So now we discuss what we require to build this simulator:-

So as we knew a dice has 6 faces and each has a different number in a range from 1 to 6. So to simulate dice rolls we require 6 images each image of each face so now we require a random face to get each outcome of dice roll so we use the random module to generate random output in the range of 1 to 6 so we can get random rolls outcomes.

Now let us discuss the programming part:-

First, we have to import 3 module

import pygame
import random
import os

Now we have to just initialize pygame and create a pygame window of the required height and width.

width, height = 520, 500
win = pygame.display.set_mode((width, height))
pygame.display.set_caption("Dice Simulator")


Next, we have to just include images using the os module and create a function to draw a window whenever it calls

def draw_window(dice, index):#passing two parametes dice which includes coodinates of dice on the screen 
    Dice_Image = pygame.image.load(os.path.join('images', 'd{}.png'.format(index)))#including image using os 
    text_for_Space = SMALL_FONT.render('Press Space Bar', True, WHITE)# Rendering the text so that we can add it on the screen  
    text_for_OR = SMALL_FONT.render('OR', True, WHITE)
    text_for_Click = SMALL_FONT.render('Click on dice to Roll', True, WHITE)
    win.fill(RED)#Filling the background color 
    win.blit(text_for_Space, (175, 10))# Placing the text on the screen 
    win.blit(text_for_OR, (235, 35))
    win.blit(text_for_Click, (165, 60))
    win.blit(pygame.transform.scale(Dice_Image, (200, 200)), (dice.x, dice.y))# Adding the image of the dice 
    pygame.display.update()# now finally updating the screen 


So now finally we are creating the main function of the code which includes all the callings of other functions and also all remaining operations that are required to develop our simulator/game.

def main():
    dice = pygame.Rect(160, 150, 200, 200)# creating a rectangle in which we have to place the image 
    n = 1 #creating a integer variable to keep track of the random image have to display on the screen 
    clock = pygame.time.Clock()#Pygame clock initiated 
    run = True # to run infinite loop
    while run:
        for event in pygame.event.get():# reading event from the screen to keep track if ther any command is given to roll the dice 
            if event.type == pygame.QUIT:#Execute when we close the game window
                run = False
            if event.type == pygame.KEYDOWN:#Execute when any keyboad key pressed
                if event.key == pygame.K_SPACE:
                    n = random.randrange(1, 7)#generating random number 
                    draw_window(dice, n)#calling draw window function 
            if event.type == pygame.MOUSEBUTTONDOWN:#To keep track of any mouse button pressed
                mouse_pos = event.pos
                if dice.collidepoint(mouse_pos):#It became true when dice rectangle coordinates are same of the mouse pointer 
                    n = random.randrange(1, 7)
                    draw_window(dice, n)
        draw_window(dice, n)

Woohoo, finally we understand how to build or program this game/simulator. So these code snippets are not complete code to get the complete code and required assets to run this simulator you have to just download the packet.

Thank you for reading this blog. 



Download project

Reviews Report

Submitted by Shubham Menroy (shubham9672)

Download packets of source code on Coders Packet