码迷,mamicode.com
首页 > 其他好文 > 详细

SDL制作拼图游戏

时间:2016-04-26 17:36:26      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:

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

技术分享技术分享图片是自己慢慢截左上部分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,
                            800, 600,
                            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[3][3]={ {face7,face8,face9},
                                    {face4,face2,face5},
                                    {face1,face6,NULL}  };
    int i, j;
    int quit = 0;
    struct pos null;
    SDL_Event event;
    null.x = 2;
    null.y = 2;
    
    SDL_BlitSurface(face,NULL,surface,NULL);
    
    
    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("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)
                {
                    if (null.x <= 1)
                    {
                        faceMatrix[null.x][null.y] = faceMatrix[null.x+1][null.y];
                        faceMatrix[null.x+1][null.y] = NULL;
                        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] = NULL;
                        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] = NULL;
                        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] = NULL;
                        null.y -= 1;
                    }
                }
                else
                    printf("keyboard\n");
            }
        }
        //画图
        for (i=0; i<3; i++)
        {
            for (j=0; j<3; j++)
            {
                if (faceMatrix[i][j]!=NULL)
                {
                    rect.y =100 + i*110;
                    rect.x = 300 + j*90;
                    SDL_BlitSurface(faceMatrix[i][j],NULL,surface,&rect);
                }
                else
                {
                    rect.y =100 + i*110;
                    rect.x = 300 + j*90;
                    SDL_BlitSurface(heitu,NULL,surface,&rect);
                }
            }
        }

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

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

    
    for (i=0; i<3; i++)
        for (j=0; j<3; j++)
            SDL_FreeSurface(faceMatrix[i][j]);
    SDL_FreeSurface(heitu);
    SDL_FreeSurface(face);
    SDL_DestroyWindow(window);
    SDL_Quit();
    }
    getchar();
    return 0;
}

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

SDL制作拼图游戏

标签:

原文地址:http://www.cnblogs.com/jiasheng/p/5435807.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!