SDL(PIN图)(每次自己随机分配)(参考了Anjaxs的随笔)

#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>  /*用显示图片*/
#include <time.h>



int main(int argc,char* argv[])
{
/*************************************************
产生随机数
**************************************************/
    int c[9] = {0};//用来产生随机数
    int cnt[9] = {0};//
    int count = 0;

    srand(time(NULL));
    while ( 1 )
    {
        int r = rand()%9;

        if (++c[r] > 1)
            continue;
        else
        {

            cnt[count]=r;
            if (count++ >7)
                break;
        }
    }
/*************************************************
将随机数与初始值链接,使得每次的图形位置不一样
**************************************************/

    int WEI1,WEI2;
    char quit = 0;

    int a,b;
    SDL_Event event;
    SDL_Init(SDL_INIT_VIDEO);

    SDL_Window* window=SDL_CreateWindow(
                     "pintu——SUNFLOWER",
                     SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,
                     600,600,
                     SDL_WINDOW_SHOWN
                     );
    SDL_Surface* surface =SDL_GetWindowSurface(window);



    SDL_Surface* face1 =IMG_Load("face1.png");
    SDL_Surface* face2 =IMG_Load("face2.png");
    SDL_Surface* face3 =IMG_Load("face3.png");
    SDL_Surface* face4 =IMG_Load("face4.png");
    SDL_Surface* face5 =IMG_Load("face5.png");
    SDL_Surface* face6 =IMG_Load("face6.png");
    SDL_Surface* face7 =IMG_Load("face7.png");
    SDL_Surface* face8 =IMG_Load("face8.png");
    SDL_Surface* face0 =IMG_Load("NULL.png");
    SDL_Surface* win =IMG_Load("win.jpg");


    SDL_Surface *faceMatrixabc[3][3]={ {face1,face2,face3},
                                       {face4,face5,face6},
                                       {face7,face8,face0}  };
    SDL_Surface *faceMatrix[3][3];

    count=0;
    for(a=0;a<3;a++){
        for(b=0;b<3;b++){
            WEI1=cnt[count]/3;
            WEI2=cnt[count]%3;
            count++;
            faceMatrix[WEI1][WEI2]=faceMatrixabc[a][b];
        }
    }


    SDL_Rect rect;
    rect.x=0;
    rect.y=0;


    SDL_Rect null;
    null.x=cnt[count-1]/3;
    null.y=cnt[count-1]%3;
/*************************************************
显示函数
**************************************************/


    void display(){
    for(a=0;a<3;a++){
        for(b=0;b<3;b++){

            SDL_BlitSurface(faceMatrix[a][b],NULL,surface,&rect);
            rect.x+=200;
            SDL_UpdateWindowSurface(window);
        }
        rect.x=0;
        rect.y+=200;

    }
    rect.x=0;
    rect.y=0;
    }
/*************************************************
判断函数

**************************************************/
    void judge(){
        if ( face1==faceMatrix[0][0] && face2==faceMatrix[0][1] && face3==faceMatrix[0][2] &&
             face4==faceMatrix[1][0] && face5==faceMatrix[1][1] && face6==faceMatrix[1][2] &&
             face7==faceMatrix[2][0] && face8==faceMatrix[2][1] )
        {
            SDL_BlitSurface(win,NULL,surface,NULL);
            SDL_UpdateWindowSurface(window);
            SDL_Delay(2000);
            quit = 1;

            }}
    display();

/*************************************************
图形运动
**************************************************/
    while(quit==0){
        while(SDL_PollEvent(&event))
        {
            if(event.type == SDL_QUIT){
                quit = 1;
            }
            else if(event.type == SDL_MOUSEBUTTONDOWN)
            {
                if(event.button.button == SDL_BUTTON_LEFT){
                    printf("left\n");
                }
                else if(event.button.button == SDL_BUTTON_RIGHT){
                    printf("right\n");
                }

            }



            else if(event.type == SDL_KEYDOWN)
            {

                if(event.key.keysym.sym==SDLK_UP){
                    if (null.x <= 1)
                    {
                        faceMatrix[null.x][null.y] = faceMatrix[null.x+1][null.y];
                        faceMatrix[null.x+1][null.y] = face0;
                        null.x += 1;
                    }

                }
                else if (event.key.keysym.sym == SDLK_DOWN)
                {
                    if (null.x >= 1)
                    {
                        faceMatrix[null.x][null.y] = faceMatrix[null.x-1][null.y];
                        faceMatrix[null.x-1][null.y] = face0;
                        null.x -= 1;
                    }
                }
                else if (event.key.keysym.sym == SDLK_LEFT)
                {
                    if (null.y <= 1)
                    {
                        faceMatrix[null.x][null.y] = faceMatrix[null.x][null.y+1];
                        faceMatrix[null.x][null.y+1] = face0;
                        null.y += 1;
                    }
                }
                else if (event.key.keysym.sym == SDLK_RIGHT)
                {
                    if (null.y >= 1)
                    {
                        faceMatrix[null.x][null.y] = faceMatrix[null.x][null.y-1];
                        faceMatrix[null.x][null.y-1] = face0;
                        null.y -= 1;
                    }
                }
            }
            display();
        }
        judge();
        SDL_UpdateWindowSurface(window);

    }

/*************************************************
释放空间
**************************************************/
    for (a=0; a<3; a++)
        for (b=0; b<3; b++)
            SDL_FreeSurface(faceMatrix[a][b]);

    SDL_DestroyWindow(window);
    SDL_Quit();

    return 0;
}

posted on 2016-04-30 18:58  renzhaoyang  阅读(395)  评论(0编辑  收藏  举报

导航