Linux-based SDL Graphics Library Memory flop games

Source: Internet
Author: User
Step 1: display images

Use a two-dimensional array to display eight 90x90 paired images, which are used for flip. The background image is displayed first. Define a two-dimensional array PIC [4] [4] to indicate the location where each small image is stored. The array contains 0 ~ The number of 7. If the current position is 0, the image0 image is to be displayed. In this way, all small images can be displayed.

Step 2: Implement flip

Based on the mouse clicking position, flip the specified small image and turn it back automatically, mainly for obtaining and locating the mouse; define a two-dimensional array flag [mouse_y/90] [mouse_x/90] to indicate whether there is any flip. Divide the Coordinate Position of the mouse click by 90 to determine which small image is clicked.

Step 3: game logic flip

The so-called logical flip is to flip or not flip an image according to the rules of the game, that is, when the number of mouse clicks is an even number, determine whether the previous image is the same as the current image, if it is the same, do not flip the two images back to the back. Otherwise, the two images are flipped back to the back. How can we determine whether two images are the same, the solution is to determine whether the two images are the same based on the first two-dimensional array PIC [4] [4. Until all the pictures are flipped and the game ends. Finally, the powerful feature of SDL is used to create an animated interface to end the game.

The complete code is as follows:

# Include "SDL/SDL. H "# include" SDL/sdl_image.h "# include <string> # include <stdio. h> # include <time. h> const int screen_width = 360; // screen width const int screen_height = 360; // screen height const int screen_bpp = 32; // screen pixel const int frames_per_second = 10; const int foo_width = 90; // small image 90*90 const int foo_height = 90; sdl_surface * Foo = NULL; sdl_surface * screen = NULL; sdl_surface * image0 = NULL; sdl_surface * image1 = nu Ll; 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; // defines a two-dimensional array, used to save the image index int PIC [4] [4]; bool flag [4] [4]; // to mark whether the image is opened bool mousecheck; // to record the mouse click status int pic_x, pic_y; // image index int mouse_x; int mouse_y; // rat Click position bool quit = false; bool lock; int gamestate; // The current game status const int begin = 0; // The start status const int run = 1; // running state const int end = 2; // end state 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 ;} // read the image sdl_surface * load_image (STD: String filename) {sdl_surface * loadedimag E = 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 ;} // display the image 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; // Initialization For (INT I = 0; I <4; I ++) {for (Int J = 0; j <4; j ++) {PIC [I] [J] = K ++; K % = 8 ;}/// random 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 ;}} // initialize 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; Menure CT [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_im Age ("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_f Reesurface (FOO); sdl_freesurface (endbg); sdl_freesurface (image0); reverse (image1); reverse (image2); reverse (image3); reverse (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) {// get the new mouse offset position X = event. motion. x; y = E Vent. motion. y; If (Y> = 250 & Y <= 280 & x> = 230 & x <355) {// The first menurect [0]. y = 30;} else if (Y> = 290 & Y <= 320 & x> = 230 & x <355) {// The second menurect [1]. y = 90;} else {menurect [0]. y = 0; menurect [1]. y = 60 ;}// press if (event. type = sdl_mousebuttonup) {// If the left mouse button has been opened if (event. button. button = sdl_button_left) {// get the cursor offset position X = event. button. x; y = event. button. y; // if you move the cursor over the image if (gamestate = begin) {If (Y> = 250 & Y <= 280 && X> = 230 & x <355) {// The first gamestate = run ;} else if (Y >= 290 & Y <= 320 & x> = 230 & x <355) {// second quit = true ;}} else if (gamestate = run) {If (Lock &&! Flag [Y/90] [x/90]) // if not, open {flag [Y/90] [x/90] = true; mousecheck =! Mousecheck; // change the current state mouse_x = x; mouse_y = y; lock = false ;}}} void logic () {If (gamestate = run & lock = false) {// determine whether to open a pair of IF (mousecheck) // double {If (pic_x! =-1) & (PIC [pic_x] [pic_y]! = PIC [mouse_y/90] [mouse_x/90]) {// obtain the inverse? Flag [pic_x] [pic_y] = false; flag [mouse_y/90] [mouse_x/90] = false ;}} else // singular number, recording position {pic_x = mouse_y/90; pic_y = mouse_x/90;} Lock = true; // you can determine whether to enable bool isopen = true; For (INT I = 0; I <4; I ++) {for (Int J = 0; j <4; j ++) {If (flag [I] [J] = false) // Have you opened {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) // If the start status is displayed, the start image {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) {// traverse the image index and display the image at the corresponding position for (INT I = 0; I <4; I ++) {for (Int J = 0; j <4; j ++) {If (flag [I] [J]) // open {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 // not opened {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 (); // initialize a two-dimensional array 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 ;}

Run the program on your own.

Download image address: http://download.csdn.net/detail/shiren_bod/4409888

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.