SDL制作拼图游戏

时间:2022-02-24 12:15:09

看完教程第三集后,好像自己能用这个来写一个简单的拼图游戏,第一次写出个带界面的游戏,好有成就感。

SDL制作拼图游戏SDL制作拼图游戏

图片是自己慢慢截左上部分8个脸。

#include <stdio.h>
#include <stdlib.h>
#include <SDL.h>
#include <SDL_image.h>

struct pos{
    int x,y;
};

int main(int argc, char *argv[])
{
    SDL_Init(SDL_INIT_VIDEO);
    {

    SDL_Window *window = SDL_CreateWindow("Picture Game",
                            SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
                            , ,
                            SDL_WINDOW_SHOWN);
    SDL_Surface *surface = SDL_GetWindowSurface(window);
    SDL_Surface *face = IMG_Load("face.png");
    SDL_Surface *face1 = IMG_Load("face1.png");
    SDL_Surface *face2 = IMG_Load("face2.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 *face9 = IMG_Load("face9.png");
    SDL_Surface *heitu = IMG_Load("null.png");
    SDL_Rect rect;
    SDL_Surface *faceMatrix[][]={ {face7,face8,face9},
                                    {face4,face2,face5},
                                    {face1,face6,NULL}  };
    int i, j;
    ;
    struct pos null;
    SDL_Event event;
    ;
    ;

    SDL_BlitSurface(face,NULL,surface,NULL);

    )
    {
        while (SDL_PollEvent(&event))
        {
            if (event.type == SDL_QUIT)
                quit = ;
            else if (event.type == SDL_MOUSEBUTTONDOWN)
            {
                if (event.button.button == SDL_BUTTON_LEFT)
                    printf("Mouse Left Button \n");
                else if (event.button.button == SDL_BUTTON_RIGHT)
                    printf("Mouse Right Button \n");
            }
            else if (event.type == SDL_KEYDOWN)
            {

                if (event.key.keysym.sym == SDLK_UP)
                {
                    )
                    {
                        faceMatrix[][null.y];
                        faceMatrix[][null.y] = NULL;
                        ;
                    }
                }
                else if (event.key.keysym.sym == SDLK_DOWN)
                {
                    )
                    {
                        faceMatrix[][null.y];
                        faceMatrix[][null.y] = NULL;
                        ;
                    }
                }
                else if (event.key.keysym.sym == SDLK_LEFT)
                {
                    )
                    {
                        faceMatrix[];
                        faceMatrix[] = NULL;
                        ;
                    }
                }
                else if (event.key.keysym.sym == SDLK_RIGHT)
                {
                    )
                    {
                        faceMatrix[];
                        faceMatrix[] = NULL;
                        ;
                    }
                }
                else
                    printf("keyboard\n");
            }
        }
        //画图
        ; i<; i++)
        {
            ; j<; j++)
            {
                if (faceMatrix[i][j]!=NULL)
                {
                    rect.y = + i*;
                    rect.x =  + j*;
                    SDL_BlitSurface(faceMatrix[i][j],NULL,surface,&rect);
                }
                else
                {
                    rect.y = + i*;
                    rect.x =  + j*;
                    SDL_BlitSurface(heitu,NULL,surface,&rect);
                }
            }
        }

        //判断胜利
        ][] && face8==faceMatrix[][] && face9==faceMatrix[][] &&
             face4==faceMatrix[][] && face5==faceMatrix[][] && face6==faceMatrix[][] &&
             face1==faceMatrix[][] && face2==faceMatrix[][] )
        {
            printf("游戏胜利! 按任意键结束\n");
            quit = ;
        }

        //SDL_FillRect(surface,NULL,0); 整个画面涂黑
        SDL_UpdateWindowSurface(window);
    }

    ; i<; i++)
        ; j<; j++)
            SDL_FreeSurface(faceMatrix[i][j]);
    SDL_FreeSurface(heitu);
    SDL_FreeSurface(face);
    SDL_DestroyWindow(window);
    SDL_Quit();
    }
    getchar();
    ;
}

这个还没有实现图片的随机分布。有兴趣的可以自己实现下。