很早以前用turbo c 2.0写的第一个游戏,今天看了下allegro 5的介绍,

在ubuntu上的code::blocks上自己编译了allegro5,重新写了这个小游戏,代码很短,就不写注释了。放在这里做备忘吧。

 
  1. /* 
  2. allegro5+c 
  3. 2012/11/1 
  4. ymc 
  5. */ 
  6.  
  7. #include <stdio.h> 
  8. #include <allegro5/allegro.h> 
  9. #include <allegro5/allegro_primitives.h> 
  10. #include <allegro5/allegro_native_dialog.h> 
  11. #include <allegro5/allegro_font.h> 
  12. #include <allegro5/allegro_ttf.h> 
  13.  
  14. #define MAX 8 
  15. #define B_SIZE 100 
  16. #define B_BORDER 20 
  17. int N=4; 
  18. int width; 
  19. int height; 
  20. int x_border=100; 
  21. int y_border=100; 
  22. const char *helptxt[3]={
    "Move:up,down,left,right","Flip:space,enter","Exit:Esc"}; 
  23. bool redraw; 
  24. bool doexit; 
  25. bool win; 
  26. bool map[MAX][MAX]; 
  27. ALLEGRO_DISPLAY * display = NULL; 
  28. ALLEGRO_EVENT_QUEUE *event_queue = NULL; 
  29. ALLEGRO_FONT *font = NULL; 
  30. ALLEGRO_DISPLAY_MODE disp_data; 
  31. ALLEGRO_COLOR fd_color,bk_color,bd_color,cur_color; 
  32. int cur_x,cur_y; 
  33. int dir[4][2]={
    {1,0},{-1,0},{0,1},{0,-1}}; 
  34. void data_init() 
  35.     if(N<MAX) 
  36.         N++; 
  37.     else 
  38.         N=5; 
  39.     for(int i=0;i<N;i++) 
  40.         for(int j=0;j<N;j++) 
  41.             map[i][j]=false
  42. //    map[0][0]=map[1][0]=map[0][1]=false; 
  43.  
  44.     cur_x=cur_y=0; 
  45.     redraw = false
  46.     doexit = false
  47.     win = false
  48.     width = disp_data.width; 
  49.     height = disp_data.height; 
  50.     x_border = (width-N*B_SIZE-(N-1)*(B_BORDER))/2; 
  51.     y_border = (height-N*B_SIZE-(N-1)*(B_BORDER))/2; 
  52. void game_init() 
  53.     al_init(); 
  54.     al_init_primitives_addon(); 
  55.     al_install_keyboard(); 
  56.     al_init_font_addon(); 
  57.     al_init_ttf_addon(); 
  58.     //al_get_display_mode(al_get_num_display_modes() - 1, &disp_data); 
  59.     font = al_load_ttf_font("FreeSerif.ttf",36,0 ); 
  60.     al_get_display_mode(0, &disp_data); 
  61.     al_set_new_display_flags(ALLEGRO_FULLSCREEN); 
  62.     display = al_create_display(disp_data.width, disp_data.height); 
  63.  
  64.     event_queue = al_create_event_queue(); 
  65.     al_register_event_source(event_queue, al_get_display_event_source(display)); 
  66.     al_register_event_source(event_queue, al_get_keyboard_event_source()); 
  67.  
  68.     bk_color = al_map_rgb(220,220,220); 
  69.     fd_color = al_map_rgb(255,255,255); 
  70.     bd_color = al_map_rgb(100,100,100); 
  71.     cur_color = al_map_rgb(0,0,255); 
  72.     al_clear_to_color(bk_color); 
  73.     data_init(); 
  74. void game_destroy() 
  75.     al_shutdown_primitives_addon(); 
  76.     al_destroy_display(display); 
  77.     al_destroy_event_queue(event_queue); 
  78. void drawblock(int i,int j,ALLEGRO_COLOR color,ALLEGRO_COLOR bdcolor) 
  79.     int x=x_border+i*(B_SIZE+B_BORDER); 
  80.     int y=y_border+j*(B_SIZE+B_BORDER); 
  81.     al_draw_filled_rectangle(x,y,x+B_SIZE,y+B_SIZE,color); 
  82.     al_draw_rectangle(x,y,x+B_SIZE,y+B_SIZE, bdcolor,3); 
  83. void draw_block(int i,int j) 
  84.     ALLEGRO_COLOR color,bdcolor; 
  85.     if(map[i][j]) 
  86.         color=fd_color; 
  87.     else 
  88.         color=bk_color; 
  89.     if(i == cur_x && j == cur_y) 
  90.         bdcolor = cur_color; 
  91.     else 
  92.         bdcolor = bd_color; 
  93.     drawblock(i,j,color,bdcolor); 
  94. void draw_screen() 
  95.     al_clear_to_color(bk_color); 
  96.     for(int i=0;i<N;i++) 
  97.         for(int j=0;j<N;j++) 
  98.             draw_block(i,j); 
  99.     al_draw_text(font, fd_color, x_border,y_border-8*B_BORDER,ALLEGRO_ALIGN_LEFT, helptxt[0]); 
  100.     al_draw_text(font, fd_color, x_border,y_border-6*B_BORDER,ALLEGRO_ALIGN_LEFT, helptxt[1]); 
  101.     al_draw_text(font, fd_color, x_border,y_border-4*B_BORDER,ALLEGRO_ALIGN_LEFT, helptxt[2]); 
  102.     al_flip_display(); 
  103. void flip(int x,int y) 
  104.     int x1,y1; 
  105.     map[x][y]=!map[x][y]; 
  106.     for(int k=0;k<4;k++) 
  107.     { 
  108.         x1=x+dir[k][0]; 
  109.         y1=y+dir[k][1]; 
  110.         if(x1>=0 && x1<N && y1>=0 && y1<N) 
  111.             map[x1][y1]=!map[x1][y1]; 
  112.     } 
  113.  
  114. bool check_game() 
  115.     for(int i=0;i<N;i++) 
  116.         for(int j=0;j<N;j++) 
  117.             if(map[i][j]==false
  118.                 return false
  119.     return true
  120.  
  121. short m_box(const char* message       = "Well Done!Play game again?"
  122.             const char* content_title = "Congratulate"
  123.             const char* title         = "Win"
  124.  
  125.     switch(al_show_native_message_box(al_get_current_display(), 
  126.                                      title, 
  127.                                      content_title, 
  128.                                      message, NULL, 
  129.                                      ALLEGRO_MESSAGEBOX_YES_NO)) 
  130.     { 
  131.         case 0: return 0; // 
  132.         case 1: return 1; //  ok or yes 
  133.         case 2: return 2; // no or cancel 
  134.     } 
  135.     return 0; 
  136. int main() 
  137.     ALLEGRO_EVENT ev; 
  138.     game_init(); 
  139.     draw_screen(); 
  140.     while(!doexit) 
  141.     { 
  142.          al_wait_for_event(event_queue,&ev); 
  143.          if(ev.type == ALLEGRO_EVENT_KEY_DOWN) 
  144.          { 
  145.              switch(ev.keyboard.keycode) 
  146.              { 
  147.                  case ALLEGRO_KEY_UP: 
  148.                     if(cur_y>0) 
  149.                     { 
  150.                         cur_y--; 
  151.                         redraw=true
  152.                     } 
  153.                     break
  154.                  case ALLEGRO_KEY_DOWN: 
  155.                     if(cur_y<N-1) 
  156.                     { 
  157.                         cur_y++; 
  158.                         redraw=true
  159.                     } 
  160.                     break
  161.                  case ALLEGRO_KEY_LEFT: 
  162.                     if(cur_x>0) 
  163.                     { 
  164.                         cur_x--; 
  165.                          redraw=true
  166.                     } 
  167.                     break
  168.                  case ALLEGRO_KEY_RIGHT: 
  169.                     if(cur_x<N-1) 
  170.                     { 
  171.                         cur_x++; 
  172.                          redraw=true
  173.                     } 
  174.                     break
  175.                  case ALLEGRO_KEY_SPACE: 
  176.                  case ALLEGRO_KEY_ENTER: 
  177.                     flip(cur_x,cur_y); 
  178.                     win = check_game(); 
  179.                     redraw=true
  180.                     break
  181.                  case ALLEGRO_KEY_ESCAPE: 
  182.                     doexit=true
  183.                     break
  184.              } 
  185.          } 
  186.         if(!doexit && redraw && al_is_event_queue_empty(event_queue)) 
  187.         { 
  188.              redraw = false
  189.              draw_screen(); 
  190.         } 
  191.         if(win) 
  192.         { 
  193.             win=false
  194.             switch(m_box()) 
  195.             { 
  196.                 case 2://no ,exit game 
  197.                     doexit=true
  198.                     break
  199.                 case 1://yes,continue to play game 
  200.                     data_init(); 
  201.                     redraw=true
  202.                     break
  203.             } 
  204.         } 
  205.     } 
  206.     game_destroy(); 
  207.  
  208.     return 0;