基於Linux系統的SDL圖形庫記憶翻牌小遊戲

來源:互聯網
上載者:User
步驟1:顯示圖片

用二維數組來顯示8張90×90的配對圖片,用來翻轉的;首先顯示的是背景圖片。定義一個二維數組pic[4][4] 來表示每張小圖片存放的位置,數組裡面存放0~7的數字,當前位置存放是0,就表示該位置要顯示image0小圖片。這樣就可以顯示所有小圖片了。

步驟2: 實現可以翻轉

根據滑鼠點擊的位置,翻轉指定的小圖片,而且要自動的翻轉回來,主要是滑鼠的擷取和定位;定義一個二維數組flag[mouse_y/90][mouse_x/90] 來標誌有沒有翻轉,滑鼠點擊的座標位置除以90就可以確定點擊的是哪張小圖片。

步驟3: 遊戲邏輯翻轉

所謂邏輯翻轉就是根據遊戲的規則翻轉和不翻轉圖片,就是當滑鼠點擊次數為偶數時,判斷前面的一張圖片和當前這張圖片是否相同,如果是相同的,這兩張圖片就不要翻轉回背面,否則,就把這兩張圖片都翻轉回背面。主要還是怎樣判斷兩張圖片是否相同,解決方案是根據一開始的二維數組pic[4][4]裡面存放的數字是否是相同的來判斷兩張圖片是否是相同的。直到所有的小圖片都被翻轉了遊戲就結束。最後利用了SDL的強大功能弄了一個動畫的介面來結束遊戲。

下面是完整的代碼:

#include "SDL/SDL.h"#include "SDL/SDL_image.h"#include <string>#include <stdio.h>#include <time.h>const int SCREEN_WIDTH = 360;    //螢幕寬const int SCREEN_HEIGHT =  360; //螢幕高const int SCREEN_BPP = 32;          //螢幕像素const int FRAMES_PER_SECOND = 10;const int FOO_WIDTH = 90;    //小圖片90*90const int FOO_HEIGHT = 90;SDL_Surface * foo = NULL;SDL_Surface * screen = NULL;SDL_Surface *image0 = NULL;SDL_Surface *image1 = NULL;SDL_Surface *image2 = NULL;SDL_Surface *image3 = NULL;SDL_Surface *image4 = NULL;SDL_Surface *image5 = NULL;SDL_Surface *image6 = NULL;SDL_Surface *image7 = NULL;SDL_Surface *Menu = NULL;SDL_Surface *beginBg = NULL;SDL_Surface * endBg = NULL;SDL_Event  event;//定義一個二維數組,用來儲存圖片索引int pic[4][4];bool flag[4][4]; //標記映像是否翻開bool mousecheck; //記錄滑鼠點擊狀態int pic_x, pic_y; //圖片索引int mouse_x;int mouse_y; //滑鼠點擊位置bool quit = false;bool lock;int GameState;//遊戲目前狀態const int BEGIN = 0;//開始狀態const int RUN = 1;//運行狀態const int END = 2;//結束狀態SDL_Rect MenuRect[2];SDL_Rect EndRect[2];int end_y;class Foo{private:int offSet;int velocity;int frame;public:Foo();int handle_events();void show();};Foo Show;Foo::Foo(){offSet = 0;velocity = 0;}//讀入圖片SDL_Surface *load_image(std::string filename){SDL_Surface * loadedImage = NULL;SDL_Surface * optimizedImage = NULL;loadedImage = IMG_Load(filename.c_str());if(loadedImage != NULL){optimizedImage = SDL_DisplayFormat(loadedImage);SDL_FreeSurface(loadedImage);if(optimizedImage != NULL){SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, \SDL_MapRGB(optimizedImage->format, 0, 0xF8, 0xF8));}}return optimizedImage;}//顯示圖片void apply_surface(int x, int y, SDL_Surface *source, \SDL_Surface * destination,  SDL_Rect *clip = NULL){SDL_Rect offset;offset.x = x;offset.y = y;SDL_BlitSurface(source, clip, destination, &offset);}//初始化圖片索引的函數void initIndex(){srand(time(NULL));int k=0; //初始化for(int i=0; i<4; i++){for(int j=0; j<4; j++){pic[i][j] = k++;k %= 8;}}//隨機for(int i=0; i<4; i++){for(int j=0; j<4; j++){int x = rand()%4;int y = rand()%4;int temp = pic[i][j];pic[i][j] = pic[x][y];pic[x][y] = temp;}}//初始化為背面memset(flag, 0, sizeof(flag));mousecheck = true;pic_x = -1;lock = false;quit = false;end_y = 30;GameState = BEGIN;MenuRect[0].x = 0;MenuRect[0].y = 0;MenuRect[0].w= 125;MenuRect[0].h = 30;MenuRect[1].x = 0;MenuRect[1].y = 60;MenuRect[1].w= 125;MenuRect[1].h = 30;EndRect[0].x = 0;EndRect[0].y = 150;EndRect[0].w = 360;EndRect[0].h = 30;EndRect[1].x = 0;EndRect[1].y = 180;EndRect[1].w = 360;EndRect[1].h = 30;}bool init(){if(SDL_Init(SDL_INIT_EVERYTHING) == -1){return false;}screen = SDL_SetVideoMode( SCREEN_WIDTH, \                          SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);if( screen == NULL){return false;}SDL_WM_SetCaption("Memory Game", NULL);return true;}bool load_files(){beginBg = load_image("pic/begin.bmp");endBg = load_image("pic/end.bmp");Menu = load_image("pic/menu.bmp");foo = load_image("pic/bgimage.bmp");image0 = load_image("pic/image0.bmp");image1 = load_image("pic/image1.bmp");image2 = load_image("pic/image2.bmp");image3 = load_image("pic/image3.bmp");image4 = load_image("pic/image4.bmp");image5 = load_image("pic/image5.bmp");image6 = load_image("pic/image6.bmp");image7 = load_image("pic/image7.bmp");if( NULL ==  foo  || NULL == endBg || NULL == Menu || NULL == beginBg || NULL == image0         \|| NULL == image1 ||  NULL == image2 || NULL == image3 || NULL == image4            \||  NULL == image5 || NULL == image6 || NULL == image7 ){return false;}return true;}void clean_up(){SDL_FreeSurface(foo);SDL_FreeSurface(endBg);SDL_FreeSurface(image0);SDL_FreeSurface(image1);SDL_FreeSurface(image2);SDL_FreeSurface(image3);SDL_FreeSurface(image4);SDL_FreeSurface(image5);SDL_FreeSurface(image6);SDL_FreeSurface(image7);SDL_FreeSurface(Menu);SDL_FreeSurface(beginBg);SDL_Quit();}int Foo::handle_events(){int x =0, y = 0;if(event.type == SDL_MOUSEMOTION){//擷取新滑鼠位移位置x = event.motion.x;y = event.motion.y;if(y>=250 && y<=280 && x>= 230 && x<355){//第一項MenuRect[0].y = 30;}else if(y>=290 && y<=320 && x>= 230 && x<355){//第二項MenuRect[1].y = 90;}else{MenuRect[0].y = 0;MenuRect[1].y = 60;}}//滑鼠按鍵按下if( event.type == SDL_MOUSEBUTTONUP){//如果滑鼠左鍵已經放開if( event.button.button == SDL_BUTTON_LEFT){//擷取滑鼠的位移位置x = event.button.x;y = event.button.y;//如果滑鼠在圖片上if(GameState ==  BEGIN){if(y>=250 && y<=280 && x>= 230 && x<355){//第一項GameState = RUN;}else if(y>=290 && y<=320 && x>= 230 && x<355){//第二項quit = true;}}else if(GameState == RUN){if(lock && !flag[y/90][x/90]) //如果沒翻開,那就翻開{flag[y/90][x/90] = true;mousecheck = !mousecheck; //變換目前狀態mouse_x = x;mouse_y = y;lock = false;}}}}}void logic(){ if(GameState == RUN && lock == false){//判斷是否翻開一對if(mousecheck) //雙{if((pic_x != -1)&&(pic[pic_x][pic_y] != pic[mouse_y/90][mouse_x/90]) ){//取反?flag[pic_x][pic_y] = false;flag[mouse_y/90][mouse_x/90] = false;}}else//單數,記錄位置{pic_x = mouse_y/90;pic_y = mouse_x/90;}lock = true;//判斷是否全部翻開bool isopen = true;for(int i=0; i<4; i++){for(int j=0; j<4; j++){if(flag[i][j] == false) //有沒有翻開的{isopen = false;}}}if(isopen) {GameState = END;}} else if(GameState == END) {end_y += 30;if(end_y > 180){end_y = 180;//quit = true;}EndRect[0].y = 180-end_y;EndRect[0].h = end_y;EndRect[1].h = end_y; }}void Foo::show(){if(GameState == BEGIN)//如果開始狀態,顯示開始圖片{apply_surface(0, 0, beginBg, screen);for(int i=0; i<2; i++){apply_surface(230,  250+i*40, Menu, screen, &MenuRect[i]);}}else if(GameState == RUN){//遍曆映像索引,在相應位置顯示映像for(int i=0; i<4; i++){for(int j=0; j<4; j++){if(flag[i][j]) //翻開{switch(pic[i][j]){case 0: apply_surface(90*j,90*i, image0, screen); break;case 1: apply_surface(90*j,90*i, image1, screen); break;case 2: apply_surface(90*j,90*i, image2, screen); break;case 3: apply_surface(90*j,90*i, image3, screen); break;case 4: apply_surface(90*j,90*i, image4, screen); break;case 5: apply_surface(90*j,90*i, image5, screen); break;case 6: apply_surface(90*j,90*i, image6, screen); break;case 7: apply_surface(90*j,90*i, image7, screen); break;}}else//沒翻開{apply_surface(90*j,90*i, foo, screen);}}}}else if(GameState == END){//apply_surface(0, 0, endBg, screen);apply_surface(0, 0, endBg, screen, &EndRect[0]);apply_surface(0, 360-end_y, endBg, screen, &EndRect[1]);}}int main(int argc, char *args[]){if( init() == false){printf("init error...\n");return 1;}if( load_files() == false){printf("load_files error...\n");return 1;}initIndex();  //初始化二維數組    while( quit == false )    {logic();        //While there's events to handle        while( SDL_PollEvent( &event ) )        {            Show.handle_events();            //If the user has Xed out the window            if( event.type == SDL_QUIT )            {                //Quit the program                quit = true;            }if(event.type == SDL_KEYDOWN){switch(event.key.keysym.sym){case SDLK_ESCAPE:{printf("escape button is pressed\n");quit = true;break;}}}             }                Show.show();        //Update the screen        if( SDL_Flip( screen ) == -1 )        {            return 1;        }if(GameState == RUN)SDL_Delay(500);if(GameState == END)SDL_Delay(150);}    //Clean up    clean_up();    return 0;}

運行片,請讀者自行運行。

下載圖片地址:http://download.csdn.net/detail/shiren_bod/4409888

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.