Open In App

Dice simulator with sound using graphics

Last Updated : 28 Jun, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Graphics are defined as any sketch or a drawing or a special network that pictorially represents some meaningful information. Computer Graphics is used where a set of images needs to be manipulated or the creation of the image in the form of pixels and is drawn on the computer. It can be used in digital photography, film, entertainment, electronic gadgets, and all other core technologies which are required.

In this article, we will discuss the simulation of the dice valued from 1 to 6 using computer graphics. To implement this simulation, graphics.h header file is used for making dice, dos.h is used to play the beep sound when dice are thrown and the stdlib.h header file is used for the random() function that generates random numbers from 1 to 6.

Approach:

  • Initialize a graphic path using the function initgraph().
  • Iterate a loop until the program is ended and perform the following steps:
    • Take character input from the user and:
      • If the character is space then generate any random number from the range [1, 6] and display that number in the square using the rectangle function.
      • If the character is 0 then break out of the loop.
      • If the character is other than O or space then display the message to press characters only 0 and space.
  • After the simulator is ended, then close the graphics using the function closegraph().

Below is the C program to illustrate the dice simulator using graphics:

C




// C program to illustrate the dice
// simulator using graphics
  
#include <conio.h>
#include <graphics.h>
#include <stdio.h>
#include <stdlib.h>
  
// Driver Code
void main()
{
    int gd = DETECT, gm, i;
    int font = 7, direction = 0;
    int font_size = 4, r;
    char press, key, num[1];
    char value[6] = "624531";
  
    // Initialize the graphics path
    initgraph(&gd, &gm, "C:\\TC\\BGI");
  
    // Heading
    setcolor(WHITE);
    settextstyle(font, direction,
                 font_size);
  
    outtextxy(100, 10,
              "-----DICE SIMULATOR-----");
  
    setcolor(2);
    settextstyle(10, 0, 1);
    outtextxy(120, 180,
              "\"enter (space) to"
              " throw dice\"");
    press = ' ';
  
    // Iterate until ask to throw
    // the dice
    while (1) {
        key = getch();
        if (press == key) {
  
            // Beep sound after
            // throwing dice
            sound(3000);
            delay(10);
            nosound();
            cleardevice();
            for (i = 0; i < 40; i++) {
                delay(5);
  
                // Rectangle
                rectangle(270 + i, 190 + i,
                          350 - i, 270 - i);
            }
  
            setcolor(WHITE);
            settextstyle(font, direction,
                         font_size);
            outtextxy(100, 10,
                      "-----DICE SIMULATOR-----");
  
            setcolor(10);
            settextstyle(10, 0, 1);
  
            // Print the message to
            // display the game
            outtextxy(5, 60,
                      "\"press 0 (zero) "
                      "for exit.\"");
            outtextxy(
                5, 100,
                "\"enter (space) to "
                "throw dice again.\"\n");
  
            for (i = 1; i < 40; i++) {
                delay(5);
                setcolor(1);
  
                // Rectangle
                rectangle(310 - i, 230 - i,
                          310 + i, 230 + i);
            }
  
            // Generate a random number
            // between 1 to 6
            r = random(6);
            num[1] = value[r];
  
            setcolor(6);
  
            // Update the style of text
            settextstyle(1, 0, 6);
  
            // Print the number
            outtextxy(300, 200, num);
        }
        else if (key == '0') {
            break;
        }
        else {
  
            // Clear the device
            cleardevice();
  
            // Update background color
            setcolor(WHITE);
            settextstyle(font, direction,
                         font_size);
  
            // Print the message
            outtextxy(100, 10,
                      "-----DICE SIM"
                      "ULATOR-----");
  
            setcolor(4);
            settextstyle(10, 0, 1);
  
            // For wrong key pressed
            outtextxy(170, 110,
                      "\"you enter wrong key\"");
            setcolor(14);
  
            // Exiting the code
            outtextxy(170, 170,
                      "\"Enter 0 (zero)"
                      " for exit\"");
            setcolor(9);
            outtextxy(300, 220, "(or)");
            setcolor(5);
  
            // For starting the dice
            // roll again
            outtextxy(
                90, 270,
                "\"Enter (space) to"
                " throw dice again\"");
        }
    }
    closegraph();
}


Output
 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads