简单的 x 和 O 游戏。如果按下 1-9 键,在棋盘的正确位置画一个 X 或一个 O:
7|8|9
4|5|6
1|2|3
代码如下,changePlayer() 函数只是更改播放器(不包括在内),我使用 board[0] 来保存它是哪个播放器。
当我运行程序板时,程序板显示正确,但按一个键时没有绘制 X 和 O。
我试图将所有内容绘制到缓冲区,然后将缓冲区绘制到屏幕但无法正常工作。如果我不使用缓冲区并直接将所有内容绘制到屏幕上 - 它可以完美运行!
我确定我遗漏了一些简单的东西:S 这是代码:
int main()
{
int winner = 0;
BITMAP *xSprite;
BITMAP *oSprite;
BITMAP *newSprite;
int board[10];
int i = 0;
for(i=0; i<10; i++)
{
board[i] = 0;
}
allegro_init();
install_keyboard();
set_color_depth(16);
set_gfx_mode( GFX_AUTODETECT, 1920, 1080, 0, 0);
xSprite = load_bitmap( "x.bmp", NULL);
oSprite = load_bitmap( "o.bmp", NULL);
BITMAP *buffer = create_bitmap(1920, 1080);
//draw black background
rectfill(buffer,0,0,1920,1080,makecol(0,0,0));
//draw white grid
line( buffer, 200, 0, 200, 480, makecol( 255, 255, 255));
line( buffer, 400, 0, 400, 480, makecol( 255, 255, 255));
line( buffer, 0, 150, 680, 150, makecol( 255, 255, 255));
line( buffer, 0, 300, 680, 300, makecol( 255, 255, 255));
acquire_screen();
blit(buffer, screen, 0, 0, 0, 0, 1920, 1080);
clear_bitmap(buffer);
release_screen();
while(!key[KEY_ESC])
{
clear_keybuf();
if (board[0] == 0)
newSprite = xSprite;
else
newSprite = oSprite;
acquire_screen();
if(key[KEY_7] && board[7] == 0) {draw_sprite( screen, newSprite, 0, 0); board[7] = 1+board[0]; board[0] = changePlayer(board[0]);}
if(key[KEY_8] && board[8] == 0) {draw_sprite( screen, newSprite, 200, 0); board[8] = 1+board[0]; board[0] = changePlayer(board[0]);}
if(key[KEY_9] && board[9] == 0) {draw_sprite( screen, newSprite, 400, 0); board[9] = 1+board[0]; board[0] = changePlayer(board[0]);}
if(key[KEY_4] && board[4] == 0) {draw_sprite( screen, newSprite, 0, 150); board[4] = 1+board[0]; board[0] = changePlayer(board[0]);}
if(key[KEY_5] && board[5] == 0) {draw_sprite( screen, newSprite, 200, 150); board[5] = 1+board[0]; board[0] = changePlayer(board[0]);}
if(key[KEY_6] && board[6] == 0) {draw_sprite( screen, newSprite, 400, 150); board[6] = 1+board[0]; board[0] = changePlayer(board[0]);}
if(key[KEY_1] && board[1] == 0) {draw_sprite( screen, newSprite, 0, 300); board[1] = 1+board[0]; board[0] = changePlayer(board[0]);}
if(key[KEY_2] && board[2] == 0) {draw_sprite( screen, newSprite, 200, 300); board[2] = 1+board[0]; board[0] = changePlayer(board[0]);}
if(key[KEY_3] && board[3] == 0) {draw_sprite( screen, newSprite, 400, 300); board[3] = 1+board[0]; board[0] = changePlayer(board[0]);}
release_screen();
}
return 0;
}
END_OF_MAIN();