EGE图形库绘制的扫雷

匆匆完成,有不完善的地方,大体上功能倒是实现了扫雷的功能。
先给出一张完成的图:

EGE图形库绘制的扫雷_第1张图片

1.运行需要链接ege的库,还要包含ege的头文件graphics.h.
2.主函数中的四个getimage加载的是本地图片,这些图片文件应该与可执行文件在一个文件夹下。其中:
09.jpg是一张900x562的背景图片,就是上图中的妹子。
flag.png是一张30x30的图片,上面绘着小旗,就是上图中的绿旗那个方格,颜色加一个小旗。
grid.png也是一张30x30的图片,绘着没有点击时的方格,图中浅色方格,纯色加周围深色边框。
visible.png也是30x30,绘着点击后的方格颜色,即图中深色方格,纯色。


#include 
#include 
#include 
#include 


#define LONG 16
#define WIDTH 16
typedef struct G
{
    int features; //012345678 -1
    int visible;
    int flag;
}grids;

//load pictures
PIMAGE bkimg;
PIMAGE grid;
PIMAGE fimg;
PIMAGE temp;
int count_grid = 0;     //numbers, left clicked correctly
grids matrix[LONG][WIDTH];  //The main data matrix
int get[40];    //Save the locations of mines
int init_matrix();      //Value setting
void mainloop();        //Wait events
void handleclick(int x, int y, int type);       //handle clicked event
void show();        //print some information
void leftclick(int x, int y);       //Response about the event
void handlegameover();      //Response about the event of gameover

int main()
{
    /******************************
    -> set graphics information
    -> set background picture
    -> load pictures
    -> set matrix
    *******************************/
    initgraph(900, 562, 0);
    bkimg = newimage();
    grid = newimage();
    fimg = newimage();
    temp = newimage();
    setbkcolor(WHITE);
    setcolor(BLACK);
    setbkmode(TRANSPARENT);
    setfont(25, 0, "楷体");
    getimage(fimg, "flag.png");
    getimage(bkimg, "09.jpg");
    getimage(grid, "grid.png");
    getimage(temp, "visible.png");
    putimage_alphablend(NULL, bkimg, 0, 0, 0x80);
    for(int i = 0; i < 16; i++)
        for(int j = 0; j < 16; j++)
            putimage_alphablend(NULL, grid, i * 30, j * 30, 100);
    //161640
    if(!init_matrix())
    {
        printf("initialization fail!\nProcess will exit...\n");
        getch();
        exit(1);
    }
    printf("Initialization ........ ok\n");
    mainloop();
    getch();
    delimage(fimg);
    delimage(temp);
    delimage(grid);
    delimage(bkimg);
    closegraph();
    return 0;
}

void leftclick(int x, int y)
{
    count_grid++;
    putimage_alphablend(NULL, temp, y * 30, x * 30, 100);
    if(matrix[x][y].features != 0)
        xyprintf(y*30+8, x*30+3, "%d", matrix[x][y].features);
    matrix[x][y].visible = 1;//avoid dead circle
    if(matrix[x][y].features == 0)      //Recursion open grids
    {
        for(int i = x-1; i <= x+1; i++)
        {
            if(i >= 0 && i < LONG)
            for(int j = y-1; j <= y+1; j++)
            {
                if(j >= 0 && j < WIDTH)
                {
                    if(!matrix[i][j].visible)
                        leftclick(i, j);
                }
            }
        }
    }
    setfont(25, 0, "Consalos");
    setfillcolor(RGB(249, 204, 226));
    bar(600, 20, 750, 50);
    xyprintf(600, 23, "scores: %d", count_grid);
    setfont(25, 0, "楷体");
    if(count_grid >= 16 * 16 - 40)
    {
        //success
        setcolor(RGB(249, 204, 226));
        setfillcolor(RGB(249, 204, 226));
        bar(0, 0, 900, 562);
        putimage_alphablend(NULL, bkimg, 0, 0, 0x80);
        getch();
        exit(2);
    }
}

void mainloop()
{
    int start = fclock();
    int temp;
    int posx, posy;
    printf("Start main loop.......\n");
    //listen message from mouse
    mouse_msg msg = {0};
    int down = 0;
    for(; is_run(); delay_fps(60))
    {
        printf("Listen mouse envnts........\n");
        temp = fclock();
        if(temp != start)
        {
            start = temp;
            setfillcolor(RGB(200, 200, 200));
            bar(600, 100, 800, 130);
            xyprintf(600, 100, "time = %d sec", start);
        }

        while(mousemsg())
            msg = getmouse();
        printf("Get mouse event.........\n");
        //if clicked down
        if((int)msg.is_up() && down)
        {
            down = 0;
            posx = msg.x / 30;
            posy = msg.y / 30;
            //I suppose that you use just left and right
            handleclick(posy, posx, (int)msg.is_right());
        }
        down = msg.is_down();
    }
}

void handleclick(int x, int y, int type)
{
    //right click
    if(type)
    {
        printf("Mouse right key event...........\n");
        if(matrix[x][y].visible)        //if this is not unknown grid
        {
            if(matrix[x][y].flag)
            {
                matrix[x][y].flag = 0;
                matrix[x][y].visible = 0;
                putimage_alphablend(NULL, grid, y * 30, x * 30, 0xff);
            }
            else
                return;
        }
        else        //This grid is not visible
        {
            matrix[x][y].visible = 1;
            matrix[x][y].flag = 1;
            putimage_alphablend(NULL, fimg, y * 30, x * 30, 0xff);
        }
    }
    //left key clicked
    else
    {
        printf("Mouse left key event...........\n");
        if(!matrix[x][y].visible)
        {
            if(matrix[x][y].features == -1)
                handlegameover();  //game over
            else
                leftclick(x, y);
        }
    }
}

void handlegameover()
{
    int x;
    int y;
    for(int i = 0; i < LONG; i++)   //Disable response
    {
        for(int j = 0; j < WIDTH; j++)
        {
            matrix[i][j].visible = 1;
            matrix[i][j].flag = 0;
        }
    }
    for(int i = 0; i < 40; i++)
    {
        x = get[i] / WIDTH;
        y = get[i] % LONG;
        for(int j = 300; j > 10; j--)
        {
            setfillcolor(BLACK);
            bar(y*30, x*30, y*30+30, x*30+30);
            for(int k = 0; k < 50000; k++);
            //delay(100);
            //circle(y*30+15, x*30+15, j);
        }
    }
    printf("You lost...............\n");
}

int init_matrix()
{
    //generate random 40 numbers between 0 to 255
    //and they are not same
    int s[256];
    printf("Initialization start...");
    int num;
    srand(time(NULL)); //generate random seed
    for(int i = 0; i < 256; i++)
        s[i] = i;
    for(int i = 0; i < 40; i++)
    {
        num = rand() % 256;
        while(s[num] == -1)
            num = rand() % 256;
        get[i] = num;
        s[num] = -1;
        //write mines
        matrix[num/LONG][num%WIDTH].visible = 0;
        matrix[num/LONG][num%WIDTH].flag = 0;
        matrix[num/LONG][num%WIDTH].features = -1;
    }
    //generate total matrix
    for(int i = 0; i < 40; i++)
    {
        //I will follow all mines
        for(int j = get[i]/WIDTH - 1; j <= get[i]/WIDTH + 1; j++)
        {
            //I will set the value of the grids around a mine
            if(j >= 0 && j < LONG)
                for(int k = get[i]%LONG - 1; k <= get[i]%LONG + 1; k++)
                {
                    if(k >= 0 && k < WIDTH) //we need to handle mine by mine correctly
                    if(matrix[j][k].features != -1)
                        matrix[j][k].features++;
                }
        }
    }
    show();
    return 1;
}

void show()
{
    for(int i = 0; i < LONG; i++)
    {
        printf("\n");
        for(int j = 0; j < WIDTH; j++)
        {
            printf("%3d", matrix[i][j].features);
        }
    }
    printf("\n");
}

你可能感兴趣的:(C)