📜  骰子模拟器与声音使用图形

📅  最后修改于: 2022-05-13 01:55:35.489000             🧑  作者: Mango

骰子模拟器与声音使用图形

图形被定义为以图形方式表示某些有意义信息的任何草图或绘图或特殊网络。计算机图形用于需要处理一组图像或以像素形式创建图像并在计算机上绘制的情况。它可用于数码摄影、电影、娱乐、电子产品和所有其他所需的核心技术。

在本文中,我们将讨论使用计算机图形模拟16的骰子。为了实现这个模拟,graphics.h 头文件用于制作骰子, dos.h用于播放掷骰子时的哔声,stdlib.h 头文件用于生成随机数的random()函数16

方法:

  • 使用函数initgraph()初始化图形路径。
  • 迭代一个循环直到程序结束并执行以下步骤:
    • 从用户那里获取字符输入并:
      • 如果字符是空格,则从范围[1, 6] 中生成任何随机数并使用矩形函数在正方形中显示该数字。
      • 如果字符为0,则跳出循环。
      • 如果字符不是O空格,则显示消息只按字符0空格
  • 模拟器结束后,使用函数closegraph()关闭图形。

下面是使用图形说明骰子模拟器的 C 程序:

C
// C program to illustrate the dice
// simulator using graphics
  
#include 
#include 
#include 
#include 
  
// 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();
}


输出

想要从精选的视频和练习题中学习,请查看C 基础到高级C 基础课程