allegro 5ベースの白黒駒反転ゲーム
ずいぶん前にturbo c 2.0で書いた最初のゲームですが、今日はallegro 5の紹介を見て、
ubuntuでのcode::blocksでallegro 5を自分でコンパイルし、この小さなゲームを書き直しました.コードが短いので、コメントは書きません.ここに置いてメモしましょう.
ubuntuでのcode::blocksでallegro 5を自分でコンパイルし、この小さなゲームを書き直しました.コードが短いので、コメントは書きません.ここに置いてメモしましょう.
- /*
- allegro5+c
- 2012/11/1
- ymc
- */
-
- #include <stdio.h>
- #include <allegro5/allegro.h>
- #include <allegro5/allegro_primitives.h>
- #include <allegro5/allegro_native_dialog.h>
- #include <allegro5/allegro_font.h>
- #include <allegro5/allegro_ttf.h>
-
- #define MAX 8
- #define B_SIZE 100
- #define B_BORDER 20
- int N=4;
- int width;
- int height;
- int x_border=100;
- int y_border=100;
- const char *helptxt[3]={"Move:up,down,left,right","Flip:space,enter","Exit:Esc"};
- bool redraw;
- bool doexit;
- bool win;
- bool map[MAX][MAX];
- ALLEGRO_DISPLAY * display = NULL;
- ALLEGRO_EVENT_QUEUE *event_queue = NULL;
- ALLEGRO_FONT *font = NULL;
- ALLEGRO_DISPLAY_MODE disp_data;
- ALLEGRO_COLOR fd_color,bk_color,bd_color,cur_color;
- int cur_x,cur_y;
- int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
- void data_init()
- {
- if(N<MAX)
- N++;
- else
- N=5;
- for(int i=0;i<N;i++)
- for(int j=0;j<N;j++)
- map[i][j]=false;
- // map[0][0]=map[1][0]=map[0][1]=false;
-
- cur_x=cur_y=0;
- redraw = false;
- doexit = false;
- win = false;
- width = disp_data.width;
- height = disp_data.height;
- x_border = (width-N*B_SIZE-(N-1)*(B_BORDER))/2;
- y_border = (height-N*B_SIZE-(N-1)*(B_BORDER))/2;
- }
- void game_init()
- {
- al_init();
- al_init_primitives_addon();
- al_install_keyboard();
- al_init_font_addon();
- al_init_ttf_addon();
- //al_get_display_mode(al_get_num_display_modes() - 1, &disp_data);
- font = al_load_ttf_font("FreeSerif.ttf",36,0 );
- al_get_display_mode(0, &disp_data);
- al_set_new_display_flags(ALLEGRO_FULLSCREEN);
- display = al_create_display(disp_data.width, disp_data.height);
-
- event_queue = al_create_event_queue();
- al_register_event_source(event_queue, al_get_display_event_source(display));
- al_register_event_source(event_queue, al_get_keyboard_event_source());
-
- bk_color = al_map_rgb(220,220,220);
- fd_color = al_map_rgb(255,255,255);
- bd_color = al_map_rgb(100,100,100);
- cur_color = al_map_rgb(0,0,255);
- al_clear_to_color(bk_color);
- data_init();
- }
- void game_destroy()
- {
- al_shutdown_primitives_addon();
- al_destroy_display(display);
- al_destroy_event_queue(event_queue);
- }
- void drawblock(int i,int j,ALLEGRO_COLOR color,ALLEGRO_COLOR bdcolor)
- {
- int x=x_border+i*(B_SIZE+B_BORDER);
- int y=y_border+j*(B_SIZE+B_BORDER);
- al_draw_filled_rectangle(x,y,x+B_SIZE,y+B_SIZE,color);
- al_draw_rectangle(x,y,x+B_SIZE,y+B_SIZE, bdcolor,3);
- }
- void draw_block(int i,int j)
- {
- ALLEGRO_COLOR color,bdcolor;
- if(map[i][j])
- color=fd_color;
- else
- color=bk_color;
- if(i == cur_x && j == cur_y)
- bdcolor = cur_color;
- else
- bdcolor = bd_color;
- drawblock(i,j,color,bdcolor);
- }
- void draw_screen()
- {
- al_clear_to_color(bk_color);
- for(int i=0;i<N;i++)
- for(int j=0;j<N;j++)
- draw_block(i,j);
- al_draw_text(font, fd_color, x_border,y_border-8*B_BORDER,ALLEGRO_ALIGN_LEFT, helptxt[0]);
- al_draw_text(font, fd_color, x_border,y_border-6*B_BORDER,ALLEGRO_ALIGN_LEFT, helptxt[1]);
- al_draw_text(font, fd_color, x_border,y_border-4*B_BORDER,ALLEGRO_ALIGN_LEFT, helptxt[2]);
- al_flip_display();
- }
- void flip(int x,int y)
- {
- int x1,y1;
- map[x][y]=!map[x][y];
- for(int k=0;k<4;k++)
- {
- x1=x+dir[k][0];
- y1=y+dir[k][1];
- if(x1>=0 && x1<N && y1>=0 && y1<N)
- map[x1][y1]=!map[x1][y1];
- }
- }
-
- bool check_game()
- {
- for(int i=0;i<N;i++)
- for(int j=0;j<N;j++)
- if(map[i][j]==false)
- return false;
- return true;
- }
-
- short m_box(const char* message = "Well Done!Play game again?",
- const char* content_title = "Congratulate",
- const char* title = "Win")
- {
-
- switch(al_show_native_message_box(al_get_current_display(),
- title,
- content_title,
- message, NULL,
- ALLEGRO_MESSAGEBOX_YES_NO))
- {
- case 0: return 0; //
- case 1: return 1; // ok or yes
- case 2: return 2; // no or cancel
- }
- return 0;
- }
- int main()
- {
- ALLEGRO_EVENT ev;
- game_init();
- draw_screen();
- while(!doexit)
- {
- al_wait_for_event(event_queue,&ev);
- if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
- {
- switch(ev.keyboard.keycode)
- {
- case ALLEGRO_KEY_UP:
- if(cur_y>0)
- {
- cur_y--;
- redraw=true;
- }
- break;
- case ALLEGRO_KEY_DOWN:
- if(cur_y<N-1)
- {
- cur_y++;
- redraw=true;
- }
- break;
- case ALLEGRO_KEY_LEFT:
- if(cur_x>0)
- {
- cur_x--;
- redraw=true;
- }
- break;
- case ALLEGRO_KEY_RIGHT:
- if(cur_x<N-1)
- {
- cur_x++;
- redraw=true;
- }
- break;
- case ALLEGRO_KEY_SPACE:
- case ALLEGRO_KEY_ENTER:
- flip(cur_x,cur_y);
- win = check_game();
- redraw=true;
- break;
- case ALLEGRO_KEY_ESCAPE:
- doexit=true;
- break;
- }
- }
- if(!doexit && redraw && al_is_event_queue_empty(event_queue))
- {
- redraw = false;
- draw_screen();
- }
- if(win)
- {
- win=false;
- switch(m_box())
- {
- case 2://no ,exit game
- doexit=true;
- break;
- case 1://yes,continue to play game
- data_init();
- redraw=true;
- break;
- }
- }
- }
- game_destroy();
-
- return 0;
- }