Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <allegro.h>
- #include <stdio.h>
- #include <stdlib.h>
- // enum ValoresCelula { Vazia = 0, Parede = 1, Premio = 2, Robo = 3 };
- enum ValoresCelula { Vazia, Parede, Premio };
- enum Direcao { Norte, Sul, Leste, Oeste };
- int corCelula[4];
- void init();
- void deinit();
- struct Robo {
- int x, y;
- enum Direcao d;
- };
- void constroiRobo( struct Robo* r, int xInicial, int yInicial ) {
- r->x = xInicial;
- r->y = yInicial;
- r->d = Oeste;
- }
- struct Tabuleiro {
- int x, y;
- int tamanho_celula;
- int numero_celulas;
- int **celula;
- };
- struct Jogo {
- struct Tabuleiro tab;
- struct Robo joe;
- };
- void constroiTabuleiro( struct Tabuleiro* tab, int x, int y ) {
- int i, j;
- tab->x = x;
- tab->y = y;
- tab->tamanho_celula = 35;
- tab->numero_celulas = 8;
- tab->celula = (int **) malloc( tab->numero_celulas * sizeof( int* ) );
- for( i = 0; i < tab->numero_celulas; i++ )
- tab->celula[i] = (int *) malloc( tab->numero_celulas * sizeof( int ) );
- for( i = 0; i < tab->numero_celulas; i++ )
- for( j = 0; j < tab->numero_celulas; j++ )
- tab->celula[i][j] = Vazia;
- tab->celula[4][5] = Premio;
- //tab->celula[2][2] = Parede;
- //tab->celula[2][2] = Parede;
- tab->celula[2][3] = Parede;
- tab->celula[2][4] = Parede;
- tab->celula[2][5] = Parede;
- }
- void constroiJogo( struct Jogo* j ) {
- constroiTabuleiro( &j->tab, 50, 10);
- constroiRobo( &j->joe, 0, 0);
- }
- void desenhaRobo( BITMAP *bmp, struct Jogo *j ) {
- int x = j->tab.x + j->joe.x * j->tab.tamanho_celula;
- int y = j->tab.y + j->joe.y * j->tab.tamanho_celula;
- int tamanho = j->tab.tamanho_celula;
- switch( j->joe.d ) {
- case Norte:
- rectfill( bmp, x + 3, y + 15,
- x + tamanho - 3, y + tamanho - 10,
- makecol( 255, 255, 0 ) );
- rectfill( bmp, x + 15, y + 15,
- x + tamanho - 15, y + 5,
- makecol( 255, 255, 0 ) );
- break;
- case Sul:
- rectfill( bmp, x + 3, y + tamanho - 15,
- x + tamanho - 3, y + 10,
- makecol( 255, 255, 0 ) );
- rectfill( bmp, x + 15, y + tamanho - 15,
- x + tamanho - 15, y + tamanho - 5,
- makecol( 255, 255, 0 ) );
- break;
- case Oeste:
- rectfill( bmp, x + 15, y + 3,
- x + tamanho - 10, y + tamanho - 3,
- makecol( 255, 255, 0 ) );
- rectfill( bmp, x + 15, y + 15,
- x + 5, y + tamanho - 15,
- makecol( 255, 255, 0 ) );
- break;
- case Leste:
- rectfill( bmp, x + tamanho - 15, y + 3,
- x + 10, y + tamanho - 3,
- makecol( 255, 255, 0 ) );
- rectfill( bmp, x + tamanho - 15, y + 15,
- x + tamanho - 5, y + tamanho - 15,
- makecol( 255, 255, 0 ) );
- break;
- }
- }
- void desenhaTabuleiro( BITMAP *bmp, struct Tabuleiro* tab ) {
- int i, j;
- for( i = 0; i <= tab->numero_celulas; i++ )
- line( bmp, tab->x + i * tab->tamanho_celula,
- tab->y,
- tab->x + i * tab->tamanho_celula,
- tab->y + tab->numero_celulas * tab->tamanho_celula,
- makecol( 0, 0, 0 ) );
- for( i = 0; i <= tab->numero_celulas; i++ )
- line( bmp, tab->x,
- tab->y + i * tab->tamanho_celula,
- tab->x + tab->numero_celulas * tab->tamanho_celula,
- tab->y + i * tab->tamanho_celula,
- makecol( 0, 0, 0 ) );
- for( i = 0; i < tab->numero_celulas; i++ )
- for( j = 0; j < tab->numero_celulas; j++ )
- rectfill( bmp, tab->x + i * tab->tamanho_celula + 1,
- tab->y + j * tab->tamanho_celula + 1,
- tab->x + (i+1) * tab->tamanho_celula - 1,
- tab->y + (j+1) * tab->tamanho_celula - 1,
- corCelula[ tab->celula[i][j] ] );
- }
- int main() {
- struct Jogo jogo;
- init();
- constroiJogo( &jogo );
- show_mouse(screen);
- rectfill( screen, 0, 0, 639, 319, makecol( 128, 128, 128 ) );
- desenhaTabuleiro( screen, &jogo.tab );
- desenhaRobo( screen, &jogo );
- while (!key[KEY_ESC]) {
- BITMAP *parede;
- parede = load_bitmap ("parede.bmp", NULL);
- blit(parede, screen, 0, 0, 51, 11, 34, 34);
- }
- deinit();
- return 0;
- }
- END_OF_MAIN()
- void init() {
- int depth, res;
- allegro_init();
- depth = desktop_color_depth();
- if (depth == 0) depth = 32;
- set_color_depth(depth);
- res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 320, 0, 0);
- if (res != 0) {
- allegro_message(allegro_error);
- exit(-1);
- }
- install_timer();
- install_keyboard();
- install_mouse();
- /* add other initializations here */
- corCelula[Vazia] = makecol( 160, 160, 160 );
- corCelula[Parede] = makecol( 90, 100, 90 );
- corCelula[Premio] = makecol( 0, 0, 160 );
- }
- void deinit() {
- clear_keybuf();
- /* add other deinitializations here */
- }
Add Comment
Please, Sign In to add comment