Simple x's and O's game. If 1-9 keys are pressed, draw an X or an O at the right place on the board:
The code is below, the changePlayer() function just changes the player (not included) and i use board[0] to hold which player it is.
When I run the program board is displayed correctly but X's and O's aren't drawn when i press a key.
I have tried to draw everything to the buffer then draw the buffer to the screen but wont work. If I dont use a buffer and just draw everything straight away to screen - it works perfectly!
I'm sure I'm missing something simple :S Here's the code:
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();
Some general pointers given that Allegro 4 is really old and doesn't always work well on modern OS's:
If you're just starting, use Allegro 5
Always use set_color_depth(desktop_color_depth())
for maximum compatibility.
Use GFX_GDI
for maximum compatibility. (Obviously not the fastest driver, but for simple games, it doesn't matter.)
Don't use acquire_screen()
and release_screen()
For your particular problem, there's nothing much to say because the code you posted apparently works for you. If you are using a buffer, of course you need to be sure to blit it to the screen on every frame.