📜  使用计算机图形设计积木游戏的程序

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

使用计算机图形设计积木游戏的程序

在 C 图形中, graphics.h 函数用于绘制不同的形状,如圆形、矩形等,以不同的格式(不同的字体和颜色)显示文本(任何消息)。通过使用 graphics.h 程序,可以设计动画和游戏。这些对初学者很有用。

使用的功能:

  • rectangle(l, t, r, b) 来自 graphics.h 头文件的函数,它从左(l)到右(r)和从上(t)到下(b)绘制一个矩形。
  • line(a1, b1, a2, b2) : graphics.h 头文件中的一个函数,它从 (a1, b1) 点到 (a2, b2) 点绘制一条线。
  • setfillstyle(pattern, color) : graphics.h 头文件中的一个函数,通过它可以给出绘图模式和特定颜色。
  • floodfill(a, b, c):来自 graphics.h 头文件的一个函数,通过它可以为特定的有界区域着色,以 (a, b) 为中心,c 是边框颜色。
  • outtextxy(int x, int y, char * 字符串) : graphics.h 头文件中的一个函数,通过它可以打印任何语句,其中 x, y 是点的坐标,第三个参数包含字符串的地址要显示。
  • settextstyle(int font, int direction, int font_size) graphics.h 头文件中的函数,可用于可打印文本的样式,其中 font 参数指定文本的字体。方向可以是 HORIZ_DIR(从左到右)或 VERT_DIR(从下到上)。

方法:

  • 这是一个砖块游戏,从上面来的砖块是随机颜色的。
  • 有3个 可以放置砖块的柱子。
  • 如果两块相同颜色的积木相邻,它们都会消失,分数会增加 10 分。
  • 如果砖块接触到游戏区域的顶部,那么游戏就结束了。下面是一段视频来解释游戏的运作方式。

下面是实现相同的程序:

C
// C program for the above approach
#include 
#include 
#include 
#include 
#include 
#include 
 
void outline()
{
    int u = 400;
 
    // Storing Box Outline
    rectangle(700, 300, 1000, 900);
 
    // Vertical Lines To Make
    // Divisions
    line(800, 300, 800, 900);
    line(900, 300, 900, 900);
 
    // Making Horizontal Line Using
    // While Function
    while (u <= 900) {
        line(700, u, 1000, u);
        u = u + 100;
    }
 
    // Printing The Instruction
    // On Screen
    settextstyle(10, 0, 3);
    outtextxy(20, 100, "FOR MOVE LEFT PRESS 'l'");
    outtextxy(20, 200, "FOR MOVE RIGHT PRESS 'r'");
    outtextxy(20, 300, "FOR MOVE MIDDLE PRESS 'm'");
}
 
// Driver Code
void main()
{
    int gd = DETECT, gm;
 
    // Initialize of gdriver with
    // DETECT macros
    initgraph(&gd, &gm, "C:\\turboc3\\bgi");
 
    // Declaring & Initialising Variables
    int a = 800, b = 50, c = 900, d = 150,
        x, left[10], right[10], mid[10],
        up = 5, low = 2, lb = 900, mb = 900,
        rb = 900, i = 0, j = 0, k = 0,
        score = 0, p;
    char z, str[3];
 
    // Calling outline() function
    outline();
 
    // Creating Infinite Loop To Make
    // Continuous Use OF Game
    while (1) {
        // Creating & Naming The
        // Brick Box
        settextstyle(8, 0, 2);
        outtextxy(800, 10, "Brick Box");
        rectangle(a, b, c, d);
 
        // Generating Random Color Number
        x = (rand() % (up - low + 1)) + low;
        setfillstyle(SOLID_FILL, x);
        floodfill(a + 5, d - 5, 15);
 
        // Getting Input To Move Bricks
        z = getch();
 
        // Operations For Place The Bricks
        // In Left Side
        if (z == 'l') {
            // Creating & Naming The Left
            // Sub-Box
            settextstyle(8, 0, 2);
            outtextxy(630, 10, "Left Sub-Box");
 
            rectangle(a - 150, b, c - 150, d);
            floodfill(a - 55, d - 5, 15);
            delay(200);
            setfillstyle(SOLID_FILL, BLACK);
            floodfill(a - 55, d - 5, 15);
            rectangle(a - 100, lb - 100,
                      c - 100, lb);
            setfillstyle(SOLID_FILL, x);
            floodfill(a - 5, lb - 5, 15);
            delay(200);
 
            // Decreasing The Base Of
            // Left Side
            lb = lb - 100;
 
            // Storing Corresponding Color
            // In Left[] Array
            left[i] = x;
            i++;
 
            // Process To Check Is There Any
            // Matching In The Left Side By
            // Upside Down
            if (left[0] == left[1]) {
                lb = 900;
                setfillstyle(SOLID_FILL, BLACK);
                floodfill(a - 5, lb - 5, 15);
                floodfill(a - 5, lb - 105, 15);
                score = score + 10;
                i = 0;
                left[1] = 0;
            }
            else if (left[1] == left[2]) {
                lb = 800;
                setfillstyle(SOLID_FILL, BLACK);
                floodfill(a - 5, lb - 5, 15);
                floodfill(a - 5, lb - 105, 15);
                score = score + 10;
                i = 1;
                left[2] = 0;
            }
            else if (left[2] == left[3]) {
                lb = 700;
                setfillstyle(SOLID_FILL, BLACK);
                floodfill(a - 5, lb - 5, 15);
                floodfill(a - 5, lb - 105, 15);
                score = score + 10;
                i = 2;
                left[3] = 0;
            }
            else if (left[3] == left[4]) {
                lb = 600;
                setfillstyle(SOLID_FILL, BLACK);
                floodfill(a - 5, lb - 5, 15);
                floodfill(a - 5, lb - 105, 15);
                score = score + 10;
                i = 3;
                left[4] = 0;
            }
            else if (left[4] == left[5]) {
                lb = 500;
                setfillstyle(SOLID_FILL, BLACK);
                floodfill(a - 5, lb - 5, 15);
                floodfill(a - 5, lb - 105, 15);
                score = score + 10;
                i = 4;
                left[5] = 0;
            }
            else
                p = 0;
 
            // Loop Breaking Condition For
            // Left Side
            if (lb < 400)
                break;
        }
 
        // Operations For Place The Bricks
        // In Right Side
        else if (z == 'r') {
            // Creating & Naming The Left
            // Sub-Box
            settextstyle(8, 0, 2);
            outtextxy(970, 10, "Right Sub-Box");
 
            rectangle(a + 150, b, c + 150, d);
            floodfill(a + 155, d - 5, 15);
            delay(200);
            setfillstyle(SOLID_FILL, BLACK);
            floodfill(a + 155, d - 5, 15);
            rectangle(a + 100, rb - 100,
                      c + 100, rb);
            setfillstyle(SOLID_FILL, x);
            floodfill(a + 105, rb - 5, 15);
            delay(200);
 
            // Decreasing The Base Of
            // Right Side
            rb = rb - 100;
 
            // Storing Corresponding Color
            // In Right[] Array
            right[j] = x;
            j++;
 
            // Process to Check Is There Any Matching
            // In The Right Side By Upside Down
            if (right[0] == right[1]) {
                rb = 900;
                setfillstyle(SOLID_FILL, BLACK);
                floodfill(a + 105, rb - 5, 15);
                floodfill(a + 105, rb - 105, 15);
                score = score + 10;
                j = 0;
                right[1] = 0;
            }
            else if (right[1] == right[2]) {
                rb = 800;
                setfillstyle(SOLID_FILL, BLACK);
                floodfill(a + 105, rb - 5, 15);
                floodfill(a + 105, rb - 105, 15);
                score = score + 10;
                j = 1;
                right[2] = 0;
            }
            else if (right[2] == right[3]) {
                rb = 700;
                setfillstyle(SOLID_FILL, BLACK);
                floodfill(a + 105, rb - 5, 15);
                floodfill(a + 105, rb - 105, 15);
                score = score + 10;
                j = 2;
                right[3] = 0;
            }
            else if (right[3] == right[4]) {
                rb = 600;
                setfillstyle(SOLID_FILL, BLACK);
                floodfill(a + 105, rb - 5, 15);
                floodfill(a + 105, rb - 105, 15);
                score = score + 10;
                j = 3;
                right[4] = 0;
            }
            else if (right[4] == right[5]) {
                rb = 500;
                setfillstyle(SOLID_FILL, BLACK);
                floodfill(a + 105, rb - 5, 15);
                floodfill(a + 105, rb - 105, 15);
                score = score + 10;
                j = 4;
                right[5] = 0;
            }
            else
                p = 0;
 
            // Loop Breaking Condition For
            // Right Side
            if (rb - 100 < 300)
                break;
        }
 
        // Operations For Place The Bricks
        // In Middle Side
        else {
            rectangle(a, mb - 100, c, mb);
            floodfill(a + 5, mb - 5, 15);
            delay(200);
 
            // Decreasing The Base Of
            // Middle Side
            mb = mb - 100;
 
            // Storing Corresponding Color In
            // Mid[] Array
            mid[k] = x;
            k++;
 
            // Process To Check Is There Any
            // Matching In The Middle Side
            // By Upside Down
            if (mid[0] == mid[1]) {
                mb = 900;
                setfillstyle(SOLID_FILL, BLACK);
                floodfill(a + 5, mb - 5, 15);
                floodfill(a + 5, mb - 105, 15);
                score = score + 10;
                k = 0;
                mid[1] = 0;
            }
            else if (mid[1] == mid[2]) {
                mb = 800;
                setfillstyle(SOLID_FILL, BLACK);
                floodfill(a + 5, mb - 5, 15);
                floodfill(a + 5, mb - 105, 15);
                score = score + 10;
                k = 1;
                mid[2] = 0;
            }
            else if (mid[2] == mid[3]) {
                mb = 700;
                setfillstyle(SOLID_FILL, BLACK);
                floodfill(a + 5, mb - 5, 15);
                floodfill(a + 5, mb - 105, 15);
                score = score + 10;
                k = 2;
                mid[3] = 0;
            }
            else if (mid[3] == mid[4]) {
                mb = 600;
                setfillstyle(SOLID_FILL, BLACK);
                floodfill(a + 5, mb - 5, 15);
                floodfill(a + 5, mb - 105, 15);
                score = score + 10;
                k = 3;
                mid[4] = 0;
            }
            else if (mid[4] == mid[5]) {
                mb = 500;
                setfillstyle(SOLID_FILL, BLACK);
                floodfill(a + 5, mb - 5, 15);
                floodfill(a + 5, mb - 105, 15);
                score = score + 10;
                k = 4;
                mid[5] = 0;
            }
            else
                p = 0;
 
            // Loop Breaking Condition For
            // Middle Side
            if (mb - 100 < 300)
                break;
        }
 
        // Time Delay
        delay(200);
 
        // Display Score Whenever There Is
        // Any Match
        sprintf(str, "%d", score);
        outtextxy(100, 600, "SCORE: ");
        outtextxy(100, 700, str);
    }
 
    // Time Delay
    delay(500);
 
    // Clearing The Screen When Game
    // Is Over
    cleardevice();
 
    // Holding screen for a while
    getch();
 
    // Close the initialized gdriver
    closegraph();
}


输出:

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