Allegro.cc - Online Community
Post Reply

Allegro.cc Forums » Programming Questions » Screen is cleared and a new one appears Allegro5 in C?

rss feed Print
Screen is cleared and a new one appears Allegro5 in C?
mayna azevedo
Member #20,077
May 2021

HELLO!!!

I'm making a very simple game in C with allegro5, and I'm trying to trigger a screen change by clicking on a certain key. For example, if you press ENTER, the entire screen is cleared and a new one appears, with a new message. Does anyone know how to tell me what I do wrong? Pressing ENTER does disappear the music aand the text, but the new one don't appear in the display.

There is no error message and the document compiles, but it does not do what I want.

Here is the code:

#SelectExpand
1#include <stdio.h> 2#include <stdlib.h> 3#include <allegro5/allegro5.h> 4#include <allegro5/allegro_font.h> 5#include <allegro5/allegro_image.h> 6#include <allegro5/allegro_primitives.h> // permite colocar formas simples, linhas, pontos, etc. (formas primitivas) 7#include <allegro5/allegro_audio.h> // biblioteca de som 8#include <allegro5/allegro_acodec.h> 9 10void must_init(bool test, const char *description){ 11 if(test) return; 12 13 printf("couldn't initialize %s\n", description); 14 exit(1); 15} 16 17int main(){ 18 must_init(al_init(), "allegro"); // auxilia o must_init(), evitar de colocar instruções a mais par averificar erros. 19 must_init(al_install_keyboard(), "keyboard"); 20 must_init(al_install_mouse(), "mouse"); // instalando o mouse 21 22 ALLEGRO_TIMER* timer = al_create_timer(1.0 / 30.0); 23 must_init(timer, "timer"); 24 25 ALLEGRO_EVENT_QUEUE* queue = al_create_event_queue(); 26 must_init(queue, "queue"); 27 28 // Permite que as formas e linhas tenham maior definição, deixar mais suave 29 al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 1, ALLEGRO_SUGGEST); // imagem suave 30 al_set_new_display_option(ALLEGRO_SAMPLES, 8, ALLEGRO_SUGGEST); // imagem suave 31 al_set_new_bitmap_flags(ALLEGRO_MIN_LINEAR | ALLEGRO_MAG_LINEAR); // imagem suave 32 33 ALLEGRO_DISPLAY* disp = al_create_display(1200, 800); 34 must_init(disp, "display"); 35 36 ALLEGRO_FONT* font = al_create_builtin_font(); 37 must_init(font, "font"); 38 39 ALLEGRO_FONT* font1 = al_create_builtin_font(); 40 must_init(font1, "font1"); 41 42 must_init(al_init_image_addon(), "image addon"); 43 ALLEGRO_BITMAP* dino_apresentacao = al_load_bitmap("dino_apresentacao.png"); 44 must_init(dino_apresentacao, "dino_apresentacao"); 45 46 must_init(al_init_primitives_addon(), "primitives"); // Para inserir linhas, formas e pontos no código 47 48 al_register_event_source(queue, al_get_keyboard_event_source()); 49 al_register_event_source(queue, al_get_display_event_source(disp)); 50 al_register_event_source(queue, al_get_timer_event_source(timer)); 51 al_register_event_source(queue, al_get_mouse_event_source()); // adicionado para o mouse funcionar 52 53 // adicionando o audio ao jogo 54 must_init(al_install_audio(), "audio"); 55 must_init(al_init_acodec_addon(), "audio codecs"); 56 must_init(al_reserve_samples(16), "reserve samples"); 57 58 ALLEGRO_SAMPLE* elephant = al_load_sample("elephant.wav"); // audio // verificar a extenção, não funciona em mp3 59 must_init(elephant, "elephant"); 60 61 // para música longa 62 ALLEGRO_AUDIO_STREAM* music = al_load_audio_stream("music.opus", 2, 2048); // os números são referentes as áreas da memoria onde a musica sera armazenada. não mudar 63 must_init(music, "music"); 64 al_set_audio_stream_playmode(music, ALLEGRO_PLAYMODE_LOOP); // música em loop 65 al_attach_audio_stream_to_mixer(music, al_get_default_mixer()); // linha para o som sair, senão tem sai 66 67 bool done = false; 68 bool redraw = true; 69 ALLEGRO_EVENT event; 70 71 float x, y; 72 x = 100; 73 y = 100; 74 75 #define KEY_SEEN 1 76 #define KEY_RELEASED 2 77 78 unsigned char key[ALLEGRO_KEY_MAX]; 79 memset(key, 0, sizeof(key)); // tem que iniciar zerado 80 al_hide_mouse_cursor(disp); // faz com que o mouse do computador suma 81 82 al_start_timer(timer); 83 while(1){ 84 al_wait_for_event(queue, &event); 85 86 switch(event.type) // movimento do teclado 87 { 88 case ALLEGRO_EVENT_TIMER: 89 // once again, no game logic. fishy? maybe. 90 redraw = true; 91 break; 92 93 case ALLEGRO_EVENT_KEY_CHAR: // usar na criação dos menus de navegação 94 if(event.keyboard.keycode == ALLEGRO_KEY_ENTER) 95 { 96 al_destroy_audio_stream(music); 97 //al_destroy_display(disp); 98 //al_destroy_event_queue(queue); 99 al_destroy_font(font); 100 al_clear_to_color(al_map_rgb(0,0,0)); 101 al_draw_text(font1, al_map_rgb(255, 255, 255), 200, 750, 0, "APERTEI ENTER"); 102 //al_flip_display() 103 if(redraw && al_is_event_queue_empty(queue)) 104 { 105 //al_get_new_display_refresh_rate(); 106 al_clear_to_color(al_map_rgb(0, 0, 0)); 107 al_draw_text(font1, al_map_rgb(255, 255, 255), 200, 750, 0, "APERTEI ENTER"); 108 al_flip_display(); 109 110 redraw = false; 111 } 112 } 113 if(event.keyboard.keycode == ALLEGRO_KEY_F) 114 al_draw_text(font, al_map_rgb(255, 255, 255), 430, 750, 0, "@ TODOS DIREITOS RESERVADOS AO DINO PESCOÇUDO @"); 115 116 if(event.keyboard.keycode != ALLEGRO_KEY_ESCAPE) 117 break; 118 119 case ALLEGRO_EVENT_DISPLAY_CLOSE: 120 done = true; 121 break; 122 } 123 124 if(done) 125 break; 126 127 if(redraw && al_is_event_queue_empty(queue)) 128 { 129 al_clear_to_color(al_map_rgb(0, 0, 0)); 130 al_draw_text(font, al_map_rgb(255, 255, 255), 200, 450, 0, "TESTE"); 131 al_flip_display(); 132 133 redraw = false; 134 } 135 136 137 } 138 139 al_destroy_bitmap(dino_apresentacao); 140 al_destroy_sample(elephant); 141 al_destroy_audio_stream(music); 142 al_destroy_font(font); 143 al_destroy_display(disp); 144 al_destroy_timer(timer); 145 al_destroy_event_queue(queue); 146 147 return 0; 148}

Can someone help me?

Dizzy Egg
Member #10,824
March 2009
avatar

There is quite a lot wrong here; unfortunately I don't have your resources (images/sounds/music) so I can't run it myself.

You should really be running a state system, rather than having 2 places where you try and draw.

This is a very basic example, based on your code. Here the logic is seperated from the drawing. The drawing only happens once.

You need to loop these steps:

get event
workout logic
draw screen

Here is working code to get you started:

#SelectExpand
1#include <stdio.h> 2#include <stdlib.h> 3#include <allegro5/allegro5.h> 4#include <allegro5/allegro_font.h> 5#include <allegro5/allegro_image.h> 6#include <allegro5/allegro_primitives.h> // permite colocar formas simples, linhas, pontos, etc. (formas primitivas) 7 8char text[255] = "TESTE"; 9 10void must_init(bool test, const char *description){ 11 if(test) return; 12 13 printf("couldn't initialize %s\n", description); 14 exit(1); 15} 16 17int main(){ 18 must_init(al_init(), "allegro"); // auxilia o must_init(), evitar de colocar instruções a mais par averificar erros. 19 must_init(al_install_keyboard(), "keyboard"); 20 must_init(al_install_mouse(), "mouse"); // instalando o mouse 21 22 ALLEGRO_TIMER* timer = al_create_timer(1.0 / 30.0); 23 must_init(timer, "timer"); 24 25 ALLEGRO_EVENT_QUEUE* queue = al_create_event_queue(); 26 must_init(queue, "queue"); 27 28 // Permite que as formas e linhas tenham maior definição, deixar mais suave 29 al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 1, ALLEGRO_SUGGEST); // imagem suave 30 al_set_new_display_option(ALLEGRO_SAMPLES, 8, ALLEGRO_SUGGEST); // imagem suave 31 al_set_new_bitmap_flags(ALLEGRO_MIN_LINEAR | ALLEGRO_MAG_LINEAR); // imagem suave 32 33 ALLEGRO_DISPLAY* disp = al_create_display(1200, 800); 34 must_init(disp, "display"); 35 36 ALLEGRO_FONT* font = al_create_builtin_font(); 37 must_init(font, "font"); 38 39 ALLEGRO_FONT* font1 = al_create_builtin_font(); 40 must_init(font1, "font1"); 41 42 must_init(al_init_image_addon(), "image addon"); 43 44 must_init(al_init_primitives_addon(), "primitives"); // Para inserir linhas, formas e pontos no código 45 46 al_start_timer(timer); 47 al_register_event_source(queue, al_get_keyboard_event_source()); 48 al_register_event_source(queue, al_get_display_event_source(disp)); 49 al_register_event_source(queue, al_get_timer_event_source(timer)); 50 al_register_event_source(queue, al_get_mouse_event_source()); // adicionado para o mouse funcionar 51 52 // adicionando o audio ao jogo 53 54 55 // para música longa 56 57 bool done = false; 58 bool redraw = false; 59 ALLEGRO_EVENT event; 60 61 float x, y; 62 x = 200; 63 y = 450; 64 65 #define KEY_SEEN 1 66 #define KEY_RELEASED 2 67 68 unsigned char key[ALLEGRO_KEY_MAX]; 69 memset(key, 0, sizeof(key)); 70 al_hide_mouse_cursor(disp); 71 72 73 74 while(1){ 75 al_wait_for_event(queue, &event); 76 77 switch(event.type) 78 { 79 case ALLEGRO_EVENT_TIMER: 80 { 81 redraw = true; 82 break; 83 } 84 85 case ALLEGRO_EVENT_KEY_CHAR: 86 { 87 if(event.keyboard.keycode == ALLEGRO_KEY_ENTER) 88 { 89 sprintf(text, "%s", "APERTEI ENTER"); 90 x = 200; 91 y = 750; 92 } 93 else if(event.keyboard.keycode == ALLEGRO_KEY_F) 94 { 95 sprintf(text, "%s", "@ TODOS DIREITOS RESERVADOS AO DINO PESCOÇUDO @"); 96 x = 430; 97 y = 750; 98 } 99 100 if(event.keyboard.keycode != ALLEGRO_KEY_ESCAPE) 101 { 102 break; 103 } 104 } 105 106 case ALLEGRO_EVENT_DISPLAY_CLOSE: 107 { 108 done = true; 109 break; 110 } 111 } 112 113 if(done) 114 break; 115 116 if(redraw && al_is_event_queue_empty(queue)) 117 { 118 al_clear_to_color(al_map_rgb(0, 0, 0)); 119 al_draw_text(font, al_map_rgb(255, 255, 255), x, y, 0, text); 120 al_flip_display(); 121 122 redraw = false; 123 } 124 125 126 } 127 128 al_destroy_font(font); 129 al_destroy_display(disp); 130 al_destroy_timer(timer); 131 al_destroy_event_queue(queue); 132 133 return 0; 134}

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

mayna azevedo
Member #20,077
May 2021

WOW, it did work!

Can you answer another question? How do I make an if() in another if() work? Example: After press ENTER, it questions if a person want option A or B. How can I do this? I try to create a new if, but it did not work...

#SelectExpand
1#include <stdio.h> 2#include <stdlib.h> 3#include <allegro5/allegro5.h> 4#include <allegro5/allegro_font.h> 5#include <allegro5/allegro_image.h> 6#include <allegro5/allegro_primitives.h> // permite colocar formas simples, linhas, pontos, etc. (formas primitivas) 7 8char text[255] = "TESTE"; 9 10void must_init(bool test, const char *description){ 11 if(test) return; 12 13 printf("couldn't initialize %s\n", description); 14 exit(1); 15} 16 17int main(){ 18 must_init(al_init(), "allegro"); // auxilia o must_init(), evitar de colocar instruções a mais par averificar erros. 19 must_init(al_install_keyboard(), "keyboard"); 20 must_init(al_install_mouse(), "mouse"); // instalando o mouse 21 22 ALLEGRO_TIMER* timer = al_create_timer(1.0 / 30.0); 23 must_init(timer, "timer"); 24 25 ALLEGRO_EVENT_QUEUE* queue = al_create_event_queue(); 26 must_init(queue, "queue"); 27 28 // Permite que as formas e linhas tenham maior definição, deixar mais suave 29 al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 1, ALLEGRO_SUGGEST); // imagem suave 30 al_set_new_display_option(ALLEGRO_SAMPLES, 8, ALLEGRO_SUGGEST); // imagem suave 31 al_set_new_bitmap_flags(ALLEGRO_MIN_LINEAR | ALLEGRO_MAG_LINEAR); // imagem suave 32 33 ALLEGRO_DISPLAY* disp = al_create_display(1200, 800); 34 must_init(disp, "display"); 35 36 ALLEGRO_FONT* font = al_create_builtin_font(); 37 must_init(font, "font"); 38 39 ALLEGRO_FONT* font1 = al_create_builtin_font(); 40 must_init(font1, "font1"); 41 42 must_init(al_init_image_addon(), "image addon"); 43 44 must_init(al_init_primitives_addon(), "primitives"); // Para inserir linhas, formas e pontos no código 45 46 al_start_timer(timer); 47 al_register_event_source(queue, al_get_keyboard_event_source()); 48 al_register_event_source(queue, al_get_display_event_source(disp)); 49 al_register_event_source(queue, al_get_timer_event_source(timer)); 50 al_register_event_source(queue, al_get_mouse_event_source()); // adicionado para o mouse funcionar 51 52 // adicionando o audio ao jogo 53 54 55 // para música longa 56 57 bool done = false; 58 bool redraw = false; 59 ALLEGRO_EVENT event; 60 61 float x, y; 62 x = 200; 63 y = 450; 64 65 #define KEY_SEEN 1 66 #define KEY_RELEASED 2 67 68 unsigned char key[ALLEGRO_KEY_MAX]; 69 memset(key, 0, sizeof(key)); 70 al_hide_mouse_cursor(disp); 71 72 73 74 while(1){ 75 al_wait_for_event(queue, &event); 76 77 switch(event.type) 78 { 79 case ALLEGRO_EVENT_TIMER: 80 { 81 redraw = true; 82 break; 83 } 84 85 case ALLEGRO_EVENT_KEY_CHAR: 86 { 87 if(event.keyboard.keycode == ALLEGRO_KEY_ENTER) 88 { 89 sprintf(text, "%s", "APERTEI ENTER"); 90 x = 200; 91 y = 750; 92 93 if(event.keyboard.keycode == ALLEGRO_KEY_A) 94 { 95 sprintf(text, "%s", "PRESS A"); 96 x = 200; 97 y = 750; 98 99 if(event.keyboard.keycode == ALLEGRO_KEY_B) 100 { 101 sprintf(text, "%s", "PRESS B"); 102 x = 200; 103 y = 750; 104 } 105 } 106 } 107 else if(event.keyboard.keycode == ALLEGRO_KEY_F) 108 { 109 sprintf(text, "%s", "@ TODOS DIREITOS RESERVADOS AO DINO PESCOÇUDO @"); 110 x = 430; 111 y = 750; 112 } 113 114 if(event.keyboard.keycode != ALLEGRO_KEY_ESCAPE) 115 { 116 break; 117 } 118 } 119 120 case ALLEGRO_EVENT_DISPLAY_CLOSE: 121 { 122 done = true; 123 break; 124 } 125 } 126 127 if(done) 128 break; 129 130 if(redraw && al_is_event_queue_empty(queue)) 131 { 132 al_clear_to_color(al_map_rgb(0, 0, 0)); 133 al_draw_text(font, al_map_rgb(255, 255, 255), x, y, 0, text); 134 al_flip_display(); 135 136 redraw = false; 137 } 138 139 140 } 141 142 al_destroy_font(font); 143 al_destroy_display(disp); 144 al_destroy_timer(timer); 145 al_destroy_event_queue(queue); 146 147 return 0; 148}

Many thanks

Dizzy Egg
Member #10,824
March 2009
avatar

This is why we need a "state"; we need to know what state the game is in, because we only want to drawn once in our loop.

We create different states, and when we press keys, we check our state to see what should happen.

In this code, we check the key pressed, but we also check the state, to see what we should do.

Now you can press enter if the state is "STATE_START". Then you can press A or B.

You can also press backspace; if you have only pressed enter, we will go back to the start; if you have pressed enter, then pressed A or B, we will go back to "STATE_PRESSED_ENTER". We can then press backspace again, to go back to start, or we can press A or B again.

It is important to know what state our game is in, then we can make decisions in our logic. If we do not know what state the game is in, we cannot make decisions:

#SelectExpand
1#include <stdio.h> 2#include <stdlib.h> 3#include <allegro5/allegro5.h> 4#include <allegro5/allegro_font.h> 5#include <allegro5/allegro_image.h> 6#include <allegro5/allegro_primitives.h> // permite colocar formas simples, linhas, pontos, etc. (formas primitivas) 7 8//HERE ARE THE GAME STATES, WE NEED TO KNOW WHAT "STATE" THE GAME IS IN... 9#define STATE_START 1 10#define STATE_PRESSED_ENTER 2 11#define STATE_PRESSED_A_OR_B 3 12 13int gameState = STATE_START; //HERE IS OUR STARTING STATE, WE CAN CHANGE THIS TO USE IN OUR "IF" 14 15 16char text[255] = "TESTE"; 17 18void must_init(bool test, const char *description){ 19 if(test) return; 20 21 printf("couldn't initialize %s\n", description); 22 exit(1); 23} 24 25int main(){ 26 must_init(al_init(), "allegro"); // auxilia o must_init(), evitar de colocar instruções a mais par averificar erros. 27 must_init(al_install_keyboard(), "keyboard"); 28 must_init(al_install_mouse(), "mouse"); // instalando o mouse 29 30 ALLEGRO_TIMER* timer = al_create_timer(1.0 / 30.0); 31 must_init(timer, "timer"); 32 33 ALLEGRO_EVENT_QUEUE* queue = al_create_event_queue(); 34 must_init(queue, "queue"); 35 36 // Permite que as formas e linhas tenham maior definição, deixar mais suave 37 al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 1, ALLEGRO_SUGGEST); // imagem suave 38 al_set_new_display_option(ALLEGRO_SAMPLES, 8, ALLEGRO_SUGGEST); // imagem suave 39 al_set_new_bitmap_flags(ALLEGRO_MIN_LINEAR | ALLEGRO_MAG_LINEAR); // imagem suave 40 41 ALLEGRO_DISPLAY* disp = al_create_display(1200, 800); 42 must_init(disp, "display"); 43 44 ALLEGRO_FONT* font = al_create_builtin_font(); 45 must_init(font, "font"); 46 47 ALLEGRO_FONT* font1 = al_create_builtin_font(); 48 must_init(font1, "font1"); 49 50 must_init(al_init_image_addon(), "image addon"); 51 52 must_init(al_init_primitives_addon(), "primitives"); // Para inserir linhas, formas e pontos no código 53 54 al_start_timer(timer); 55 al_register_event_source(queue, al_get_keyboard_event_source()); 56 al_register_event_source(queue, al_get_display_event_source(disp)); 57 al_register_event_source(queue, al_get_timer_event_source(timer)); 58 al_register_event_source(queue, al_get_mouse_event_source()); // adicionado para o mouse funcionar 59 60 // adicionando o audio ao jogo 61 62 63 // para música longa 64 65 bool done = false; 66 bool redraw = false; 67 ALLEGRO_EVENT event; 68 69 float x, y; 70 x = 200; 71 y = 450; 72 73 #define KEY_SEEN 1 74 #define KEY_RELEASED 2 75 76 unsigned char key[ALLEGRO_KEY_MAX]; 77 memset(key, 0, sizeof(key)); 78 al_hide_mouse_cursor(disp); 79 80 81 82 while(1){ 83 al_wait_for_event(queue, &event); 84 85 switch(event.type) 86 { 87 case ALLEGRO_EVENT_TIMER: 88 { 89 redraw = true; 90 break; 91 } 92 93 case ALLEGRO_EVENT_KEY_CHAR: 94 { 95 if(event.keyboard.keycode == ALLEGRO_KEY_ENTER && gameState == STATE_START) //IF WE PRESS ENTER WHEN STATE IS "START".... 96 { 97 sprintf(text, "%s", "APERTEI ENTER"); 98 x = 200; 99 y = 750; 100 gameState = STATE_PRESSED_ENTER; //...NOW WE CHANGE STATE TO "PRESSED ENTER" 101 102 } 103 104 else if(event.keyboard.keycode == ALLEGRO_KEY_A && gameState == STATE_PRESSED_ENTER) //WE MUST HAVE PRESSED ENTER, BECAUSE NOW STATE IS "PRESSED_ENTER"... 105 { 106 sprintf(text, "%s", "PRESS A"); 107 x = 200; 108 y = 750; 109 gameState = STATE_PRESSED_A_OR_B; //...NOW WE CHANGE STATE TO "PRESSED A OR B" 110 } 111 112 else if(event.keyboard.keycode == ALLEGRO_KEY_B && gameState == STATE_PRESSED_ENTER) //WE MUST HAVE PRESSED ENTER, BECAUSE NOW STATE IS "PRESSED ENTER"... 113 { 114 sprintf(text, "%s", "PRESS B"); 115 x = 200; 116 y = 750; 117 gameState = STATE_PRESSED_A_OR_B; //...NOW WE CHANGE STATE TO "PRESSED A OR B" 118 } 119 120 //HERE WE USE A SINLE KEY (BACKSPACE), AND THEN USE IF/ELSE TO DO DIFFERENT THINGS, DEPENDING ON THE STATE... 121 else if(event.keyboard.keycode == ALLEGRO_KEY_BACKSPACE) 122 { 123 if(gameState == STATE_PRESSED_ENTER) //IF WE HAVE ONLY PRESSED ENTER... 124 { 125 sprintf(text, "%s", "TESTE"); 126 x = 200; 127 y = 450; 128 gameState = STATE_START; //...WE CHANGE GO BACK TO STATE "START" 129 } 130 131 else if(gameState == STATE_PRESSED_A_OR_B) //ELSE IF WE HAVE PRESSED A OR B... 132 { 133 sprintf(text, "%s", "APERTEI ENTER"); 134 x = 200; 135 y = 750; 136 gameState = STATE_PRESSED_ENTER; //...WE CHANGE GO BACK TO STATE "PRESSED ENTER" 137 } 138 } 139 else if(event.keyboard.keycode == ALLEGRO_KEY_F) 140 { 141 sprintf(text, "%s", "@ TODOS DIREITOS RESERVADOS AO DINO PESCOÇUDO @"); 142 x = 430; 143 y = 750; 144 } 145 146 if(event.keyboard.keycode != ALLEGRO_KEY_ESCAPE) 147 { 148 break; 149 } 150 } 151 152 case ALLEGRO_EVENT_DISPLAY_CLOSE: 153 { 154 done = true; 155 break; 156 } 157 } 158 159 if(done) 160 break; 161 162 if(redraw && al_is_event_queue_empty(queue)) 163 { 164 al_clear_to_color(al_map_rgb(0, 0, 0)); 165 al_draw_text(font, al_map_rgb(255, 255, 255), x, y, 0, text); 166 al_flip_display(); 167 168 redraw = false; 169 } 170 171 172 } 173 174 al_destroy_font(font); 175 al_destroy_display(disp); 176 al_destroy_timer(timer); 177 al_destroy_event_queue(queue); 178 179 return 0; 180}

REMEMBER...we loop every time, so we cannot check 2 key presses in a single pass; we go:

get event
work out logic
draw

So we need to get each event, every loop. This is why we need a state; we need to know what happened last loop, so we can make a decision on the next loop...

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

mayna azevedo
Member #20,077
May 2021

Hello. I don't know if I really understood ???. Actually, not kkkkkk. Thinking that I will always have an if () inside another if (), do I have to create decision levels with STATE_PRESSED?

I did below how it would look, more or less, the structure of the game. But as predicted, it is not working.

The order is: Start with ENTER, and the user chooses between A or B. If you choose A, inside you will have another choice, from A or B, and it will go on.

I'm sorry for the questions, but I don't understand anything about this library :'(.

#SelectExpand
1#include <stdio.h> 2#include <stdlib.h> 3#include <allegro5/allegro5.h> 4#include <allegro5/allegro_font.h> 5#include <allegro5/allegro_image.h> 6#include <allegro5/allegro_primitives.h> // permite colocar formas simples, linhas, pontos, etc. (formas primitivas) 7 8//HERE ARE THE GAME STATES, WE NEED TO KNOW WHAT "STATE" THE GAME IS IN... 9#define STATE_START 1 10#define STATE_PRESSED_ENTER 2 11#define STATE_PRESSED_A_OR_B 3 12#define STATE_PRESSED_FASE_4 4 13 14int gameState = STATE_START; //HERE IS OUR STARTING STATE, WE CAN CHANGE THIS TO USE IN OUR "IF" 15 16 17char text[255] = "PRESS ENTER TO START THE GAME"; 18 19void must_init(bool test, const char *description){ 20 if(test) return; 21 22 printf("couldn't initialize %s\n", description); 23 exit(1); 24} 25 26int main(){ 27 must_init(al_init(), "allegro"); // auxilia o must_init(), evitar de colocar instruções a mais par averificar erros. 28 must_init(al_install_keyboard(), "keyboard"); 29 must_init(al_install_mouse(), "mouse"); // instalando o mouse 30 31 ALLEGRO_TIMER* timer = al_create_timer(1.0 / 30.0); 32 must_init(timer, "timer"); 33 34 ALLEGRO_EVENT_QUEUE* queue = al_create_event_queue(); 35 must_init(queue, "queue"); 36 37 // Permite que as formas e linhas tenham maior definição, deixar mais suave 38 al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 1, ALLEGRO_SUGGEST); // imagem suave 39 al_set_new_display_option(ALLEGRO_SAMPLES, 8, ALLEGRO_SUGGEST); // imagem suave 40 al_set_new_bitmap_flags(ALLEGRO_MIN_LINEAR | ALLEGRO_MAG_LINEAR); // imagem suave 41 42 ALLEGRO_DISPLAY* disp = al_create_display(1200, 800); 43 must_init(disp, "display"); 44 45 ALLEGRO_FONT* font = al_create_builtin_font(); 46 must_init(font, "font"); 47 48 ALLEGRO_FONT* font1 = al_create_builtin_font(); 49 must_init(font1, "font1"); 50 51 must_init(al_init_image_addon(), "image addon"); 52 53 must_init(al_init_primitives_addon(), "primitives"); // Para inserir linhas, formas e pontos no código 54 55 al_start_timer(timer); 56 al_register_event_source(queue, al_get_keyboard_event_source()); 57 al_register_event_source(queue, al_get_display_event_source(disp)); 58 al_register_event_source(queue, al_get_timer_event_source(timer)); 59 al_register_event_source(queue, al_get_mouse_event_source()); // adicionado para o mouse funcionar 60 61 // adicionando o audio ao jogo 62 63 64 // para música longa 65 66 bool done = false; 67 bool redraw = false; 68 ALLEGRO_EVENT event; 69 70 float x, y; 71 x = 200; 72 y = 450; 73 74 #define KEY_SEEN 1 75 #define KEY_RELEASED 2 76 77 unsigned char key[ALLEGRO_KEY_MAX]; 78 memset(key, 0, sizeof(key)); 79 al_hide_mouse_cursor(disp); 80 81 82 83 while(1){ 84 al_wait_for_event(queue, &event); 85 86 switch(event.type) 87 { 88 case ALLEGRO_EVENT_TIMER: 89 { 90 redraw = true; 91 break; 92 } 93 94 case ALLEGRO_EVENT_KEY_CHAR: 95 { 96 if(event.keyboard.keycode == ALLEGRO_KEY_ENTER && gameState == STATE_START) //IF WE PRESS ENTER WHEN STATE IS "START".... 97 { 98 sprintf(text, "%s", "1º FASE: CHOOSE TO DO ACCTION A OR B"); 99 x = 200; 100 y = 750; 101 gameState = STATE_PRESSED_ENTER; //...NOW WE CHANGE STATE TO "PRESSED ENTER" 102 103 if(event.keyboard.keycode == ALLEGRO_KEY_A && gameState == STATE_PRESSED_ENTER) //WE MUST HAVE PRESSED ENTER, BECAUSE NOW STATE IS "PRESSED_ENTER"... 104 { 105 sprintf(text, "%s", "PRESS A 1º decision, choose A OR B: "); 106 x = 200; 107 y = 750; 108 gameState = STATE_PRESSED_A_OR_B; //...NOW WE CHANGE STATE TO "PRESSED A OR B" 109 110 if(event.keyboard.keycode == ALLEGRO_KEY_A && gameState == STATE_PRESSED_A_OR_B) //WE MUST HAVE PRESSED ENTER, BECAUSE NOW STATE IS "PRESSED_ENTER"... 111 { 112 sprintf(text, "%s", "PRESS A, 2º DECISION, choose A OR B:"); 113 x = 200; 114 y = 750; 115 gameState = STATE_PRESSED_FASE_4; //...NOW WE CHANGE STATE TO "PRESSED A OR B" 116 } else if(event.keyboard.keycode == ALLEGRO_KEY_B && gameState == STATE_PRESSED_A_OR_B) //WE MUST HAVE PRESSED ENTER, BECAUSE NOW STATE IS "PRESSED_ENTER"... 117 { 118 sprintf(text, "%s", "PRESS B, 2º DECISION, choose A OR B:"); 119 x = 200; 120 y = 750; 121 gameState = STATE_PRESSED_FASE_4; //...NOW WE CHANGE STATE TO "PRESSED A OR B" 122 } 123 }else if(event.keyboard.keycode == ALLEGRO_KEY_B && gameState == STATE_PRESSED_ENTER) //WE MUST HAVE PRESSED ENTER, BECAUSE NOW STATE IS "PRESSED ENTER"... 124 { 125 sprintf(text, "%s", "PRESS B, 1º DECISION"); 126 x = 200; 127 y = 750; 128 gameState = STATE_PRESSED_A_OR_B; //...NOW WE CHANGE STATE TO "PRESSED A OR B" 129 130 if(event.keyboard.keycode == ALLEGRO_KEY_A && gameState == STATE_PRESSED_A_OR_B) //WE MUST HAVE PRESSED ENTER, BECAUSE NOW STATE IS "PRESSED_ENTER"... 131 { 132 sprintf(text, "%s", "PRESS A, 2º DECISION, choose A OR B:"); 133 x = 200; 134 y = 750; 135 gameState = STATE_PRESSED_FASE_4; //...NOW WE CHANGE STATE TO "PRESSED A OR B" 136 } 137 } 138 } 139 140 if(event.keyboard.keycode != ALLEGRO_KEY_ESCAPE) 141 { 142 break; 143 } 144 } 145 146 case ALLEGRO_EVENT_DISPLAY_CLOSE: 147 { 148 done = true; 149 break; 150 } 151 } 152 153 if(done) 154 break; 155 156 if(redraw && al_is_event_queue_empty(queue)) 157 { 158 al_clear_to_color(al_map_rgb(0, 0, 0)); 159 al_draw_text(font, al_map_rgb(255, 255, 255), x, y, 0, text); 160 al_flip_display(); 161 162 redraw = false; 163 } 164 165 166 } 167 168 al_destroy_font(font); 169 al_destroy_display(disp); 170 al_destroy_timer(timer); 171 al_destroy_event_queue(queue); 172 173 return 0; 174}

Thank you for your help.

Dizzy Egg
Member #10,824
March 2009
avatar

You need to remember that your game is looping, with one event at a time. The reason your code does not work is because you are trying to decide on 2 things in one event.

When a key is pressed, only 1 key is pressed. You can't say:

IF key enter, IF key A....

You need to think in terms of a single pass each loop. This is why you need a state. You need to check a single key each time.

The only way your example would work would be if the user pressed ENTER and A at the same time...

Please keep asking, I like helping, and no, this is not easy, but you can only get better, and when you do understand it, you will be much happier ;D

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

mayna azevedo
Member #20,077
May 2021

Okay, I think I'm starting to understand. Can you explain more about this part of STATE? I understood (I think) how to use it for one case after another, as in the code below. But what I do when an if () generates another if (), for decision?

I'm thinking in normal logic of C: the story will change according to each choice, which will generate several different ends, but I'm not able to think how to do this.

I have to create the states, correct? Just like you did below
#define STATE_START 1
#define STATE_PRESSED_ENTER 2
#define STATE_PRESSED_A_OR_B 3

What do the numbers at the end of the line indicate?

For several stories, would you have to do a different state? Example:

#define STATE_HISTORY_1 4
#define STATE_HISTORY_2 5
...?

Thank you so much for your help, I don't know what I would do without it.

#SelectExpand
1#include <stdio.h> 2#include <stdlib.h> 3#include <allegro5/allegro5.h> 4#include <allegro5/allegro_font.h> 5#include <allegro5/allegro_image.h> 6#include <allegro5/allegro_primitives.h> // permite colocar formas simples, linhas, pontos, etc. (formas primitivas) 7 8//HERE ARE THE GAME STATES, WE NEED TO KNOW WHAT "STATE" THE GAME IS IN... 9#define STATE_START 1 10#define STATE_PRESSED_ENTER 2 11#define STATE_PRESSED_A_OR_B 3 12 13int gameState = STATE_START; //HERE IS OUR STARTING STATE, WE CAN CHANGE THIS TO USE IN OUR "IF" 14 15 16char text[255] = "PRESS ENTER"; 17 18void must_init(bool test, const char *description){ 19 if(test) return; 20 21 printf("couldn't initialize %s\n", description); 22 exit(1); 23} 24 25int main(){ 26 must_init(al_init(), "allegro"); // auxilia o must_init(), evitar de colocar instruções a mais par averificar erros. 27 must_init(al_install_keyboard(), "keyboard"); 28 must_init(al_install_mouse(), "mouse"); // instalando o mouse 29 30 ALLEGRO_TIMER* timer = al_create_timer(1.0 / 30.0); 31 must_init(timer, "timer"); 32 33 ALLEGRO_EVENT_QUEUE* queue = al_create_event_queue(); 34 must_init(queue, "queue"); 35 36 // Permite que as formas e linhas tenham maior definição, deixar mais suave 37 al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 1, ALLEGRO_SUGGEST); // imagem suave 38 al_set_new_display_option(ALLEGRO_SAMPLES, 8, ALLEGRO_SUGGEST); // imagem suave 39 al_set_new_bitmap_flags(ALLEGRO_MIN_LINEAR | ALLEGRO_MAG_LINEAR); // imagem suave 40 41 ALLEGRO_DISPLAY* disp = al_create_display(1200, 800); 42 must_init(disp, "display"); 43 44 ALLEGRO_FONT* font = al_create_builtin_font(); 45 must_init(font, "font"); 46 47 ALLEGRO_FONT* font1 = al_create_builtin_font(); 48 must_init(font1, "font1"); 49 50 must_init(al_init_image_addon(), "image addon"); 51 52 must_init(al_init_primitives_addon(), "primitives"); // Para inserir linhas, formas e pontos no código 53 54 al_start_timer(timer); 55 al_register_event_source(queue, al_get_keyboard_event_source()); 56 al_register_event_source(queue, al_get_display_event_source(disp)); 57 al_register_event_source(queue, al_get_timer_event_source(timer)); 58 al_register_event_source(queue, al_get_mouse_event_source()); // adicionado para o mouse funcionar 59 60 // adicionando o audio ao jogo 61 62 63 // para música longa 64 65 bool done = false; 66 bool redraw = false; 67 ALLEGRO_EVENT event; 68 69 float x, y; 70 x = 200; 71 y = 450; 72 73 #define KEY_SEEN 1 74 #define KEY_RELEASED 2 75 76 unsigned char key[ALLEGRO_KEY_MAX]; 77 memset(key, 0, sizeof(key)); 78 al_hide_mouse_cursor(disp); 79 80 81 82 while(1){ 83 al_wait_for_event(queue, &event); 84 85 switch(event.type) 86 { 87 case ALLEGRO_EVENT_TIMER: 88 { 89 redraw = true; 90 break; 91 } 92 93 case ALLEGRO_EVENT_KEY_CHAR: 94 { 95 if(event.keyboard.keycode == ALLEGRO_KEY_ENTER && gameState == STATE_START) //IF WE PRESS ENTER WHEN STATE IS "START".... 96 { 97 sprintf(text, "%s", "YOU PRESS ENTER. \nCHOOSE A OR B 1º: "); 98 x = 200; 99 y = 750; 100 gameState = STATE_PRESSED_ENTER; //...NOW WE CHANGE STATE TO "PRESSED ENTER" 101 102 } 103 104 else if(event.keyboard.keycode == ALLEGRO_KEY_A && gameState == STATE_PRESSED_ENTER) //WE MUST HAVE PRESSED ENTER, BECAUSE NOW STATE IS "PRESSED_ENTER"... 105 { 106 sprintf(text, "%s", "YOU PRESS A, CHOOSE A OR B 2º"); 107 x = 200; 108 y = 750; 109 gameState = STATE_PRESSED_A_OR_B; //...NOW WE CHANGE STATE TO "PRESSED A OR B" 110 } 111 112 else if(event.keyboard.keycode == ALLEGRO_KEY_B && gameState == STATE_PRESSED_ENTER) //WE MUST HAVE PRESSED ENTER, BECAUSE NOW STATE IS "PRESSED ENTER"... 113 { 114 sprintf(text, "%s", "YOU PRESS B, CHOOSE A OR B 2º"); 115 x = 200; 116 y = 750; 117 gameState = STATE_PRESSED_A_OR_B; //...NOW WE CHANGE STATE TO "PRESSED A OR B" 118 } 119 120 else if(event.keyboard.keycode == ALLEGRO_KEY_A && gameState == STATE_PRESSED_A_OR_B) //WE MUST HAVE PRESSED ENTER, BECAUSE NOW STATE IS "PRESSED_ENTER"... 121 { 122 sprintf(text, "%s", "YOU PRESS A, CHOOSE A OR B 3º"); 123 x = 200; 124 y = 750; 125 gameState = STATE_PRESSED_A_OR_B; //...NOW WE CHANGE STATE TO "PRESSED A OR B" 126 } 127 128 else if(event.keyboard.keycode == ALLEGRO_KEY_B && gameState == STATE_PRESSED_A_OR_B) //WE MUST HAVE PRESSED ENTER, BECAUSE NOW STATE IS "PRESSED ENTER"... 129 { 130 sprintf(text, "%s", "YOU PRESS B, CHOOSE A OR B 3º"); 131 x = 200; 132 y = 750; 133 gameState = STATE_PRESSED_A_OR_B; //...NOW WE CHANGE STATE TO "PRESSED A OR B" 134 } 135 136 else if(event.keyboard.keycode == ALLEGRO_KEY_A && gameState == STATE_PRESSED_A_OR_B) //WE MUST HAVE PRESSED ENTER, BECAUSE NOW STATE IS "PRESSED_ENTER"... 137 { 138 sprintf(text, "%s", "YOU PRESS A, CHOOSE A OR B 4º"); 139 x = 200; 140 y = 750; 141 gameState = STATE_PRESSED_A_OR_B; //...NOW WE CHANGE STATE TO "PRESSED A OR B" 142 } 143 144 else if(event.keyboard.keycode == ALLEGRO_KEY_B && gameState == STATE_PRESSED_A_OR_B) //WE MUST HAVE PRESSED ENTER, BECAUSE NOW STATE IS "PRESSED ENTER"... 145 { 146 sprintf(text, "%s", "YOU PRESS B, CHOOSE A OR B 4º"); 147 x = 200; 148 y = 750; 149 gameState = STATE_PRESSED_A_OR_B; //...NOW WE CHANGE STATE TO "PRESSED A OR B" 150 } 151 152 //HERE WE USE A SINLE KEY (BACKSPACE), AND THEN USE IF/ELSE TO DO DIFFERENT THINGS, DEPENDING ON THE STATE... 153 else if(event.keyboard.keycode == ALLEGRO_KEY_BACKSPACE) 154 { 155 if(gameState == STATE_PRESSED_ENTER) //IF WE HAVE ONLY PRESSED ENTER... 156 { 157 sprintf(text, "%s", "TESTE"); 158 x = 200; 159 y = 450; 160 gameState = STATE_START; //...WE CHANGE GO BACK TO STATE "START" 161 } 162 163 else if(gameState == STATE_PRESSED_A_OR_B) //ELSE IF WE HAVE PRESSED A OR B... 164 { 165 sprintf(text, "%s", "APERTEI ENTER"); 166 x = 200; 167 y = 750; 168 gameState = STATE_PRESSED_ENTER; //...WE CHANGE GO BACK TO STATE "PRESSED ENTER" 169 } 170 } 171 else if(event.keyboard.keycode == ALLEGRO_KEY_F) 172 { 173 sprintf(text, "%s", "@ TODOS DIREITOS RESERVADOS AO DINO PESCOÇUDO @"); 174 x = 430; 175 y = 750; 176 } 177 178 if(event.keyboard.keycode != ALLEGRO_KEY_ESCAPE) 179 { 180 break; 181 } 182 } 183 184 case ALLEGRO_EVENT_DISPLAY_CLOSE: 185 { 186 done = true; 187 break; 188 } 189 } 190 191 if(done) 192 break; 193 194 if(redraw && al_is_event_queue_empty(queue)) 195 { 196 al_clear_to_color(al_map_rgb(0, 0, 0)); 197 al_draw_text(font, al_map_rgb(255, 255, 255), x, y, 0, text); 198 al_flip_display(); 199 200 redraw = false; 201 } 202 203 204 } 205 206 al_destroy_font(font); 207 al_destroy_display(disp); 208 al_destroy_timer(timer); 209 al_destroy_event_queue(queue); 210 211 return 0; 212}

Dizzy Egg
Member #10,824
March 2009
avatar

Yes, for different stories, you need a state for each story. The numbers at the end are just an integer...

...the thing to remember is, the event/logic/draw happens 30 times every second! We have to constantly "loop", but the user may not press a key for 2 minutes, so we need to check each loop, and we need a "state" to know what to do.

What you will want to do, as your game grows, is to create "functions", so you seperate the code into small chunks. That way you can manage it.

You are thinking that, if I press one key then IF......no, not that way with games programming. Remember, the game is looping 30 times every second! Very fast. If we only did one thing at a time, it would be:

Draw Screen
Do nothing until user presses a key
Decide on key pressed
Work out logic on key pressed
Draw screen
Do nothing until user presses a key
Decide on key pressed
Work out logic on key pressed
Draw screen

This is NOT what you want to do; games work by looping all the time. We loop and loop and loop. If no key is pressed, we draw screen. If another thing happens (user pressed key) we can work out logic. The loop is happening 30/60/120/240 times per second! Don't think that you do one thing, then one thing, then one thing....you have to get used to loop...so as I said, our logic is:

get event
logic
draw screen

over and over and over again! Not:

Draw Screen
Do nothing until user presses a key
Decide on key pressed
Work out logic on key pressed
Draw screen
Do nothing until user presses a key
Decide on key pressed
Work out logic on key pressed
Draw Screen
Do nothing until user presses a key
Decide on key pressed
Work out logic on key pressed
Draw screen
Do nothing until user presses a key
Decide on key pressed
Work out logic on key pressed
Draw Screen
Do nothing until user presses a key
Decide on key pressed
Work out logic on key pressed
Draw screen
Do nothing until user presses a key
Decide on key pressed
Work out logic on key pressed
Draw screen

That is not the way! You have to loop, every loop (30 frames a second, or 60 times a second, or 240 times a second) and before you draw, work out logic...

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

mayna azevedo
Member #20,077
May 2021

Hi.

Can you explain this part better to me?

#SelectExpand
1 //HERE WE USE A SINLE KEY (BACKSPACE), AND THEN USE IF/ELSE TO DO DIFFERENT THINGS, DEPENDING ON THE STATE... 2 else if(event.keyboard.keycode == ALLEGRO_KEY_BACKSPACE) 3 { 4 if(gameState == STATE_PRESSED_GAME_OVER) //IF WE HAVE ONLY PRESSED ENTER... 5 { 6 sprintf(text, "%s", "TESTE"); 7 x = 100; 8 y = 400; 9 gameState = STATE_START; //...WE CHANGE GO BACK TO STATE "START" 10 } 11 12 else if(gameState == STATE_PRESSED_PULA_BURACO) //ELSE IF WE HAVE PRESSED A OR B... 13 { 14 sprintf(text, "%s", "APERTEI ENTER"); 15 x = 100; 16 y = 400; 17 gameState = STATE_PRESSED_BURACO; //...WE CHANGE GO BACK TO STATE "PRESSED ENTER" 18 } 19 }

This is what I've done so far, and it's finally working!

#SelectExpand
2#include <stdio.h> 3#include <stdlib.h> 4#include <allegro5/allegro5.h> 5#include <allegro5/allegro_font.h> 6#include <allegro5/allegro_image.h> 7#include <allegro5/allegro_primitives.h> // permite colocar formas simples, linhas, pontos, etc. (formas primitivas) 8 9//HERE ARE THE GAME STATES, WE NEED TO KNOW WHAT "STATE" THE GAME IS IN... 10#define STATE_START 1 11#define STATE_PRESSED_BURACO 2 12#define STATE_PRESSED_PULA_BURACO 3 13#define STATE_PRESSED_CAI_BURACO 4 14#define STATE_PRESSED_MACHUCADO_DESCANSA_OU_CORRE 5 15#define STATE_PRESSED_SONO_DESCANSA_OU_CORRE 6 16#define STATE_PRESSED_MACHUCADO_CORRE 7 17#define STATE_PRESSED_MACHUCADO_DESCANSA 8 18#define STATE_PRESSED_GAME_OVER 9 19#define STATE_PRESSED_SONO_CORRE 10 20#define STATE_PRESSED_SONO_DESCANSA 11 21 22int gameState = STATE_START; //HERE IS OUR STARTING STATE, WE CAN CHANGE THIS TO USE IN OUR "IF" 23 24 25char text[255] = "PRESSIONE ENTER PARA COMEÇAR O JOGO"; 26char text1[255] = " "; 27 28void must_init(bool test, const char *description){ 29 if(test) return; 30 31 printf("couldn't initialize %s\n", description); 32 exit(1); 33} 34 35int main(){ 36 must_init(al_init(), "allegro"); // auxilia o must_init(), evitar de colocar instruções a mais par averificar erros. 37 must_init(al_install_keyboard(), "keyboard"); 38 must_init(al_install_mouse(), "mouse"); // instalando o mouse 39 40 ALLEGRO_TIMER* timer = al_create_timer(1.0 / 30.0); 41 must_init(timer, "timer"); 42 43 ALLEGRO_EVENT_QUEUE* queue = al_create_event_queue(); 44 must_init(queue, "queue"); 45 46 // Permite que as formas e linhas tenham maior definição, deixar mais suave 47 al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 1, ALLEGRO_SUGGEST); // imagem suave 48 al_set_new_display_option(ALLEGRO_SAMPLES, 8, ALLEGRO_SUGGEST); // imagem suave 49 al_set_new_bitmap_flags(ALLEGRO_MIN_LINEAR | ALLEGRO_MAG_LINEAR); // imagem suave 50 51 ALLEGRO_DISPLAY* disp = al_create_display(1200, 800); 52 must_init(disp, "display"); 53 54 ALLEGRO_FONT* font = al_create_builtin_font(); 55 must_init(font, "font"); 56 57 must_init(al_init_image_addon(), "image addon"); 58 59 must_init(al_init_primitives_addon(), "primitives"); // Para inserir linhas, formas e pontos no código 60 61 al_start_timer(timer); 62 al_register_event_source(queue, al_get_keyboard_event_source()); 63 al_register_event_source(queue, al_get_display_event_source(disp)); 64 al_register_event_source(queue, al_get_timer_event_source(timer)); 65 al_register_event_source(queue, al_get_mouse_event_source()); // adicionado para o mouse funcionar 66 67 // adicionando o audio ao jogo 68 69 70 // para música longa 71 72 bool done = false; 73 bool redraw = false; 74 ALLEGRO_EVENT event; 75 76 float x, y; 77 x = 100; 78 y = 400; 79 80 #define KEY_SEEN 1 81 #define KEY_RELEASED 2 82 83 unsigned char key[ALLEGRO_KEY_MAX]; 84 memset(key, 0, sizeof(key)); 85 al_hide_mouse_cursor(disp); 86 87 88 89 while(1){ 90 al_wait_for_event(queue, &event); 91 92 switch(event.type) 93 { 94 case ALLEGRO_EVENT_TIMER: 95 { 96 redraw = true; 97 break; 98 } 99 100 case ALLEGRO_EVENT_KEY_CHAR: 101 { 102 if(event.keyboard.keycode == ALLEGRO_KEY_ENTER && gameState == STATE_START) //IF WE PRESS ENTER WHEN STATE IS "START".... 103 { 104 sprintf(text, "%s", "INICIO DO JOGO. DINO VÊ UM BURACO: PULA (A) OU NÃO (B): "); 105 x = 100; 106 y = 400; 107 gameState = STATE_PRESSED_BURACO; //...NOW WE CHANGE STATE TO "PRESSED ENTER" 108 109 } 110 // PULA BURACO 111 else if(event.keyboard.keycode == ALLEGRO_KEY_A && gameState == STATE_PRESSED_BURACO) 112 { 113 sprintf(text, "%s", "DINO PULOU O BURACO, MAS MACHUCOU A PATA. DINO DEVE CONTINUAR CORRENDO (A), MESMO MACHUCADO, OU COMER UMA PLANTA MEDICINAL (B)?"); 114 x = 100; 115 y = 400; 116 gameState = STATE_PRESSED_PULA_BURACO; 117 } 118 119 // DINO PULA BURACO E CORRE 120 else if(event.keyboard.keycode == ALLEGRO_KEY_A && gameState == STATE_PRESSED_PULA_BURACO) 121 { 122 sprintf(text, "%s", "DINO CONTINUOU CORRENDO, MESMO MACHUCADO, MAS SUA VELOCIDADE É BAIXA. DINO DEVE CONTINUAR CORRENDO MACHUCADO (A) OU DESCANSAR (B)?"); 123 x = 100; 124 y = 100; 125 gameState = STATE_PRESSED_MACHUCADO_DESCANSA_OU_CORRE; 126 } 127 128 else if(event.keyboard.keycode == ALLEGRO_KEY_A && gameState == STATE_PRESSED_MACHUCADO_DESCANSA_OU_CORRE) 129 { 130 sprintf(text, "%s", "DINO CONTINUOU CORRENDO, MESMO MACHUCADO, MAS SUA VELOCIDADE É BAIXA. ASSIM, O METEORO ESTÁ CADA VEZ MAIS PRÓXIMO."); 131 sprintf(text1, "%s", "DINO DEVE CONTINUAR CORRENDO DO METEORO (A) OU PROCURAR ABRIGO (B)?"); 132 x = 100; 133 y = 400; 134 gameState = STATE_PRESSED_MACHUCADO_CORRE; 135 } 136 137 else if(event.keyboard.keycode == ALLEGRO_KEY_B && gameState == STATE_PRESSED_MACHUCADO_DESCANSA_OU_CORRE) 138 { 139 sprintf(text, "%s", "DINO PAROU PARA DESCANSAR, MAS PERDEU MUITO TEMPO E O METEORO ESTÁ PRÓXIMO."); 140 sprintf(text1, "%s", "DINO DEVE CORRER DO METEORO (A) OU PROCURAR ABRIGO (B)?"); 141 x = 100; 142 y = 400; 143 gameState = STATE_PRESSED_MACHUCADO_DESCANSA; 144 } 145 146 else if(event.keyboard.keycode == ALLEGRO_KEY_A && gameState == STATE_PRESSED_MACHUCADO_CORRE) 147 { 148 sprintf(text, "%s", "VOCÊ MATOU O DINO!."); 149 sprintf(text1, "%s", "GAME OVER"); 150 x = 100; 151 y = 400; 152 gameState = STATE_PRESSED_GAME_OVER; 153 } 154 155 else if(event.keyboard.keycode == ALLEGRO_KEY_B && gameState == STATE_PRESSED_MACHUCADO_CORRE) 156 { 157 sprintf(text, "%s", "VOCÊ MATOU O DINO!."); 158 sprintf(text1, "%s", "GAME OVER"); 159 x = 100; 160 y = 400; 161 gameState = STATE_PRESSED_GAME_OVER; 162 } 163 164 else if(event.keyboard.keycode == ALLEGRO_KEY_A && gameState == STATE_PRESSED_MACHUCADO_DESCANSA) 165 { 166 sprintf(text, "%s", "VOCÊ MATOU O DINO!."); 167 sprintf(text1, "%s", "GAME OVER"); 168 x = 100; 169 y = 400; 170 gameState = STATE_PRESSED_GAME_OVER; 171 } 172 173 else if(event.keyboard.keycode == ALLEGRO_KEY_B && gameState == STATE_PRESSED_MACHUCADO_DESCANSA) 174 { 175 sprintf(text, "%s", "VOCÊ MATOU O DINO!."); 176 sprintf(text1, "%s", "GAME OVER"); 177 x = 100; 178 y = 400; 179 gameState = STATE_PRESSED_GAME_OVER; 180 } 181 182 //DINO PULA O BURACO E COME A PLANTA 183 else if(event.keyboard.keycode == ALLEGRO_KEY_B && gameState == STATE_PRESSED_PULA_BURACO) 184 { 185 sprintf(text, "%s", "DINO PAROU PARA COMER UMA PLANTA, E A DOR PASSOU. ENTRETANTO, A PLANTA DEIXOU DINO FICOU COM SONO."); 186 sprintf(text1, "%s", "DINO DEVE COMEÇAR A CORRER (A) OU PARAR PARA DESCANSAR (B)?"); 187 x = 100; 188 y = 400; 189 gameState = STATE_PRESSED_SONO_DESCANSA_OU_CORRE; 190 } 191 192 else if(event.keyboard.keycode == ALLEGRO_KEY_A && gameState == STATE_PRESSED_SONO_DESCANSA_OU_CORRE) 193 { 194 sprintf(text, "%s", "DINO COMEÇAR A CORRER MESMO SONOLENTO, E FICA BEM DEVAGAR. ASSIM, PERDEU MUITO TEMPO E O COMETA ESTÁ PRÓXIMO"); 195 sprintf(text1, "%s", "DINO DEVE CONTINUAR CORRENDO DO METEORO (A) OU PROCURAR ABRIGO (B)?"); 196 x = 100; 197 y = 400; 198 gameState = STATE_PRESSED_SONO_CORRE; 199 } 200 201 else if(event.keyboard.keycode == ALLEGRO_KEY_B && gameState == STATE_PRESSED_SONO_DESCANSA_OU_CORRE) 202 { 203 sprintf(text, "%s", "DINO PAROU PARA DESCANSAR, MAS PERDEU MUITO TEMPO. O METEORO ESTÁ PRÓXIMO."); 204 sprintf(text1, "%s", "DINO DEVE CONTINUAR CORRENDO DO METEORO (A) OU PROCURAR ABRIGO (B)?"); 205 x = 100; 206 y = 400; 207 gameState = STATE_PRESSED_SONO_DESCANSA; 208 } 209 210 else if(event.keyboard.keycode == ALLEGRO_KEY_A && gameState == STATE_PRESSED_SONO_CORRE) 211 { 212 sprintf(text, "%s", "VOCÊ MATOU O DINO!."); 213 sprintf(text1, "%s", "GAME OVER"); 214 x = 100; 215 y = 400; 216 gameState = STATE_PRESSED_GAME_OVER; 217 } 218 219 else if(event.keyboard.keycode == ALLEGRO_KEY_B && gameState == STATE_PRESSED_SONO_CORRE) 220 { 221 sprintf(text, "%s", "VOCÊ MATOU O DINO!."); 222 sprintf(text1, "%s", "GAME OVER"); 223 x = 100; 224 y = 400; 225 gameState = STATE_PRESSED_GAME_OVER; 226 } 227 228 else if(event.keyboard.keycode == ALLEGRO_KEY_A && gameState == STATE_PRESSED_SONO_DESCANSA) 229 { 230 sprintf(text, "%s", "VOCÊ MATOU O DINO!."); 231 sprintf(text1, "%s", "GAME OVER"); 232 x = 100; 233 y = 400; 234 gameState = STATE_PRESSED_GAME_OVER; 235 } 236 237 else if(event.keyboard.keycode == ALLEGRO_KEY_B && gameState == STATE_PRESSED_SONO_DESCANSA) 238 { 239 sprintf(text, "%s", "VOCÊ MATOU O DINO!."); 240 sprintf(text1, "%s", "GAME OVER"); 241 x = 100; 242 y = 400; 243 gameState = STATE_PRESSED_GAME_OVER; 244 } 245 246 247 // CAI NO BURACO 248 else if(event.keyboard.keycode == ALLEGRO_KEY_B && gameState == STATE_PRESSED_BURACO) 249 { 250 sprintf(text, "%s", "DINO NÃO PULOU, E CAIU NO BURACO. DINO DEVE PEDIR AJUDA (A) OU SAIR DO BURACO CAVANDO (B)?"); 251 x = 100; 252 y = 400; 253 gameState = STATE_PRESSED_CAI_BURACO; 254 } 255 256 //HERE WE USE A SINLE KEY (BACKSPACE), AND THEN USE IF/ELSE TO DO DIFFERENT THINGS, DEPENDING ON THE STATE... 257 else if(event.keyboard.keycode == ALLEGRO_KEY_BACKSPACE) 258 { 259 if(gameState == STATE_PRESSED_GAME_OVER) //IF WE HAVE ONLY PRESSED ENTER... 260 { 261 sprintf(text, "%s", "TESTE"); 262 x = 100; 263 y = 400; 264 gameState = STATE_START; //...WE CHANGE GO BACK TO STATE "START" 265 } 266 267 else if(gameState == STATE_PRESSED_PULA_BURACO) //ELSE IF WE HAVE PRESSED A OR B... 268 { 269 sprintf(text, "%s", "APERTEI ENTER"); 270 x = 100; 271 y = 400; 272 gameState = STATE_PRESSED_BURACO; //...WE CHANGE GO BACK TO STATE "PRESSED ENTER" 273 } 274 } 275 276 if(event.keyboard.keycode != ALLEGRO_KEY_ESCAPE) 277 { 278 break; 279 } 280 } 281 282 case ALLEGRO_EVENT_DISPLAY_CLOSE: 283 { 284 done = true; 285 break; 286 } 287 } 288 289 if(done) 290 break; 291 292 if(redraw && al_is_event_queue_empty(queue)) 293 { 294 al_clear_to_color(al_map_rgb(0, 0, 0)); 295 al_draw_text(font, al_map_rgb(255, 255, 255), x, y, 0, text); 296 al_draw_text(font, al_map_rgb(255, 255, 255), 500, 500, 0, text1); 297 al_flip_display(); 298 299 redraw = false; 300 } 301 302 } 303 304 al_destroy_font(font); 305 al_destroy_display(disp); 306 al_destroy_timer(timer); 307 al_destroy_event_queue(queue); 308 309 return 0; 310}

I have two more questions: How can I change the style of the letters, make them bigger and how can I make the game return to the beginning of the game, as soon as it ends, letting the player play again.

Thank you soooooooooooooo much!!! ;D;)8-)

Dizzy Egg
Member #10,824
March 2009
avatar

Can you explain this part better to me?

Yes; what we are doing here is reducing the number of conditions. We want to make the code as small as possible, so rather than saying:

if(key == a && state = some_state_1)
else if(key == a && state == some_state_2)
else if(key == a && state == some_state_3)

we can wrap that up, as we don't want to check key==a every time. So, we can say:

if(key == a) //we only need to check key one time for multiple states
{
    if(state == some_state_1)
    else if(state == some_state_2)
    else if(state == some_state_3)
}

That will make our code easier to read, and easier to add new states when key==a.

Quote:

How can I change the style of the letters, make them bigger

For this you will need to use the font addon. You will need to include the ttf_font header (#include <allegro5/allegro_ttf.h>) and initialise it (al_init_ttf_addon()). You can then use:

al_load_font("filename.ttf", size, flags);

The second argument will let you choose the size. Then, when you draw your text, it will be larger, depending on the size you choose when you load.

Quote:

how can I make the game return to the beginning of the game, as soon as it ends, letting the player play again

Well, you don't want to wait for a key, but I guess you don't want to go there straight away, as you want to display a message first? So I would suggest, add a new timer, that you can start/stop. You can start it when it's game over, and then check it every frame - if it's counter goes up, you know it's passed and can then restart the game.

Here is working code for you. I have added the ttf_fonts (ttf file is arial unicode, I can't attach as it's too big, but you should have a unicode font on your computer), and made it larger. I have also added a new timer, gameOverTimer, that will display "GAME OVER" for 3 seconds and then restart the game.

I'll let you look at it, and then you can ask more if you need!

#SelectExpand
1#include <stdio.h> 2#include <stdlib.h> 3#include <allegro5/allegro5.h> 4#include <allegro5/allegro_font.h> 5#include <allegro5/allegro_image.h> 6#include <allegro5/allegro_primitives.h> // permite colocar formas simples, linhas, pontos, etc. (formas primitivas) 7#include <allegro5/allegro_ttf.h> //NEED FOR TTF FONTS 8 9//HERE ARE THE GAME STATES, WE NEED TO KNOW WHAT "STATE" THE GAME IS IN... 10#define STATE_START 1 11#define STATE_PRESSED_BURACO 2 12#define STATE_PRESSED_PULA_BURACO 3 13#define STATE_PRESSED_CAI_BURACO 4 14#define STATE_PRESSED_MACHUCADO_DESCANSA_OU_CORRE 5 15#define STATE_PRESSED_SONO_DESCANSA_OU_CORRE 6 16#define STATE_PRESSED_MACHUCADO_CORRE 7 17#define STATE_PRESSED_MACHUCADO_DESCANSA 8 18#define STATE_PRESSED_GAME_OVER 9 19#define STATE_PRESSED_SONO_CORRE 10 20#define STATE_PRESSED_SONO_DESCANSA 11 21 22int gameState = STATE_START; //HERE IS OUR STARTING STATE, WE CAN CHANGE THIS TO USE IN OUR "IF" 23 24 25char text[255] = "PRESSIONE ENTER PARA COMEÇAR O JOGO"; 26char text1[255] = " "; 27 28void must_init(bool test, const char *description){ 29 if(test) return; 30 31 printf("couldn't initialize %s\n", description); 32 exit(1); 33} 34 35int main(){ 36 must_init(al_init(), "allegro"); // auxilia o must_init(), evitar de colocar instruções a mais par averificar erros. 37 must_init(al_install_keyboard(), "keyboard"); 38 must_init(al_install_mouse(), "mouse"); // instalando o mouse 39 40 ALLEGRO_TIMER* timer = al_create_timer(1.0 / 30.0); 41 must_init(timer, "timer"); 42 43 ALLEGRO_TIMER* gameOverTimer = al_create_timer(3); //Game Over timer will last 3 seconds 44 must_init(gameOverTimer, "gameOverTimer"); 45 46 ALLEGRO_EVENT_QUEUE* queue = al_create_event_queue(); 47 must_init(queue, "queue"); 48 49 // Permite que as formas e linhas tenham maior definição, deixar mais suave 50 al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 1, ALLEGRO_SUGGEST); // imagem suave 51 al_set_new_display_option(ALLEGRO_SAMPLES, 8, ALLEGRO_SUGGEST); // imagem suave 52 al_set_new_bitmap_flags(ALLEGRO_MIN_LINEAR | ALLEGRO_MAG_LINEAR); // imagem suave 53 54 ALLEGRO_DISPLAY* disp = al_create_display(1200, 800); 55 must_init(disp, "display"); 56 57 must_init(al_init_image_addon(), "image addon"); 58 59 must_init(al_init_primitives_addon(), "primitives"); // Para inserir linhas, formas e pontos no código 60 61 must_init(al_init_font_addon(), "font"); //WE NEED TO INIT FONT FIRST! 62 must_init(al_init_ttf_addon(), "ttf font"); //THEN WE NEED TO INIT TTF FONTS 63 ALLEGRO_FONT* font = al_load_ttf_font("arialuni.ttf", 14, ALLEGRO_ALIGN_LEFT); //NOW WE CAN CHOOSE SIZE 64 65 al_start_timer(timer); 66 al_register_event_source(queue, al_get_keyboard_event_source()); 67 al_register_event_source(queue, al_get_display_event_source(disp)); 68 al_register_event_source(queue, al_get_timer_event_source(timer)); 69 al_register_event_source(queue, al_get_mouse_event_source()); // adicionado para o mouse funcionar 70 71 // adicionando o audio ao jogo 72 73 74 // para música longa 75 76 bool done = false; 77 bool redraw = false; 78 ALLEGRO_EVENT event; 79 80 float x, y; 81 x = 100; 82 y = 400; 83 84 #define KEY_SEEN 1 85 #define KEY_RELEASED 2 86 87 unsigned char key[ALLEGRO_KEY_MAX]; 88 memset(key, 0, sizeof(key)); 89 al_hide_mouse_cursor(disp); 90 91 92 93 while(1){ 94 al_wait_for_event(queue, &event); 95 96 switch(event.type) 97 { 98 case ALLEGRO_EVENT_TIMER: 99 { 100 redraw = true; 101 102 if(gameState == STATE_PRESSED_GAME_OVER && al_get_timer_count(gameOverTimer) > 0) //IF STATE IS GAME OVER AND TIMER HAS TICKED AT LEAST ONCE... 103 { 104 sprintf(text, "%s", "TESTE"); 105 x = 100; 106 y = 400; 107 gameState = STATE_START; //...WE CHANGE GO BACK TO STATE "START" 108 109 //RESET THE GAME OVER TIMER! 110 al_stop_timer(gameOverTimer); 111 al_set_timer_count(gameOverTimer, 0); 112 } 113 114 break; 115 } 116 117 case ALLEGRO_EVENT_KEY_CHAR: 118 { 119 if(event.keyboard.keycode == ALLEGRO_KEY_ENTER && gameState == STATE_START) //IF WE PRESS ENTER WHEN STATE IS "START".... 120 { 121 sprintf(text, "%s", "INICIO DO JOGO. DINO VĂ UM BURACO: PULA (A) OU NĂO (B): "); 122 x = 100; 123 y = 400; 124 gameState = STATE_PRESSED_BURACO; //...NOW WE CHANGE STATE TO "PRESSED ENTER" 125 126 } 127 // PULA BURACO 128 else if(event.keyboard.keycode == ALLEGRO_KEY_A && gameState == STATE_PRESSED_BURACO) 129 { 130 sprintf(text, "%s", "DINO PULOU O BURACO, MAS MACHUCOU A PATA. DINO DEVE CONTINUAR CORRENDO (A), MESMO MACHUCADO, OU COMER UMA PLANTA MEDICINAL (B)?"); 131 x = 10; 132 y = 400; 133 gameState = STATE_PRESSED_PULA_BURACO; 134 } 135 136 // DINO PULA BURACO E CORRE 137 else if(event.keyboard.keycode == ALLEGRO_KEY_A && gameState == STATE_PRESSED_PULA_BURACO) 138 { 139 sprintf(text, "%s", "DINO CONTINUOU CORRENDO, MESMO MACHUCADO, MAS SUA VELOCIDADE É BAIXA. DINO DEVE CONTINUAR CORRENDO MACHUCADO (A) OU DESCANSAR (B)?"); 140 x = 10; 141 y = 100; 142 gameState = STATE_PRESSED_MACHUCADO_DESCANSA_OU_CORRE; 143 } 144 145 else if(event.keyboard.keycode == ALLEGRO_KEY_A && gameState == STATE_PRESSED_MACHUCADO_DESCANSA_OU_CORRE) 146 { 147 sprintf(text, "%s", "DINO CONTINUOU CORRENDO, MESMO MACHUCADO, MAS SUA VELOCIDADE É BAIXA. ASSIM, O METEORO ESTÁ CADA VEZ MAIS PRÓXIMO."); 148 sprintf(text1, "%s", "DINO DEVE CONTINUAR CORRENDO DO METEORO (A) OU PROCURAR ABRIGO (B)?"); 149 x = 10; 150 y = 400; 151 gameState = STATE_PRESSED_MACHUCADO_CORRE; 152 } 153 154 else if(event.keyboard.keycode == ALLEGRO_KEY_B && gameState == STATE_PRESSED_MACHUCADO_DESCANSA_OU_CORRE) 155 { 156 sprintf(text, "%s", "DINO PAROU PARA DESCANSAR, MAS PERDEU MUITO TEMPO E O METEORO ESTÁ PRÓXIMO."); 157 sprintf(text1, "%s", "DINO DEVE CORRER DO METEORO (A) OU PROCURAR ABRIGO (B)?"); 158 x = 10; 159 y = 4000; 160 gameState = STATE_PRESSED_MACHUCADO_DESCANSA; 161 } 162 163 else if(event.keyboard.keycode == ALLEGRO_KEY_A && gameState == STATE_PRESSED_MACHUCADO_CORRE) 164 { 165 sprintf(text, "%s", "VOCÊ MATOU O DINO!."); 166 sprintf(text1, "%s", "GAME OVER"); 167 x = 100; 168 y = 400; 169 gameState = STATE_PRESSED_GAME_OVER; 170 al_start_timer(gameOverTimer); 171 } 172 173 else if(event.keyboard.keycode == ALLEGRO_KEY_B && gameState == STATE_PRESSED_MACHUCADO_CORRE) 174 { 175 sprintf(text, "%s", "VOCÊ MATOU O DINO!."); 176 sprintf(text1, "%s", "GAME OVER"); 177 x = 100; 178 y = 400; 179 gameState = STATE_PRESSED_GAME_OVER; 180 al_start_timer(gameOverTimer); 181 } 182 183 else if(event.keyboard.keycode == ALLEGRO_KEY_A && gameState == STATE_PRESSED_MACHUCADO_DESCANSA) 184 { 185 sprintf(text, "%s", "VOCÊ MATOU O DINO!."); 186 sprintf(text1, "%s", "GAME OVER"); 187 x = 100; 188 y = 400; 189 gameState = STATE_PRESSED_GAME_OVER; 190 al_start_timer(gameOverTimer); 191 } 192 193 else if(event.keyboard.keycode == ALLEGRO_KEY_B && gameState == STATE_PRESSED_MACHUCADO_DESCANSA) 194 { 195 sprintf(text, "%s", "VOCÊ MATOU O DINO!."); 196 sprintf(text1, "%s", "GAME OVER"); 197 x = 100; 198 y = 400; 199 gameState = STATE_PRESSED_GAME_OVER; 200 al_start_timer(gameOverTimer); 201 } 202 203 //DINO PULA O BURACO E COME A PLANTA 204 else if(event.keyboard.keycode == ALLEGRO_KEY_B && gameState == STATE_PRESSED_PULA_BURACO) 205 { 206 sprintf(text, "%s", "DINO PAROU PARA COMER UMA PLANTA, E A DOR PASSOU. ENTRETANTO, A PLANTA DEIXOU DINO FICOU COM SONO."); 207 sprintf(text1, "%s", "DINO DEVE COMEÇAR A CORRER (A) OU PARAR PARA DESCANSAR (B)?"); 208 x = 100; 209 y = 400; 210 gameState = STATE_PRESSED_SONO_DESCANSA_OU_CORRE; 211 } 212 213 else if(event.keyboard.keycode == ALLEGRO_KEY_A && gameState == STATE_PRESSED_SONO_DESCANSA_OU_CORRE) 214 { 215 sprintf(text, "%s", "DINO COMEÇAR A CORRER MESMO SONOLENTO, E FICA BEM DEVAGAR. ASSIM, PERDEU MUITO TEMPO E O COMETA ESTÁ PRÓXIMO"); 216 sprintf(text1, "%s", "DINO DEVE CONTINUAR CORRENDO DO METEORO (A) OU PROCURAR ABRIGO (B)?"); 217 x = 100; 218 y = 400; 219 gameState = STATE_PRESSED_SONO_CORRE; 220 } 221 222 else if(event.keyboard.keycode == ALLEGRO_KEY_B && gameState == STATE_PRESSED_SONO_DESCANSA_OU_CORRE) 223 { 224 sprintf(text, "%s", "DINO PAROU PARA DESCANSAR, MAS PERDEU MUITO TEMPO. O METEORO ESTÁ PRÓXIMO."); 225 sprintf(text1, "%s", "DINO DEVE CONTINUAR CORRENDO DO METEORO (A) OU PROCURAR ABRIGO (B)?"); 226 x = 100; 227 y = 400; 228 gameState = STATE_PRESSED_SONO_DESCANSA; 229 } 230 231 else if(event.keyboard.keycode == ALLEGRO_KEY_A && gameState == STATE_PRESSED_SONO_CORRE) 232 { 233 sprintf(text, "%s", "VOCÊ MATOU O DINO!."); 234 sprintf(text1, "%s", "GAME OVER"); 235 x = 100; 236 y = 400; 237 gameState = STATE_PRESSED_GAME_OVER; 238 al_start_timer(gameOverTimer); 239 } 240 241 else if(event.keyboard.keycode == ALLEGRO_KEY_B && gameState == STATE_PRESSED_SONO_CORRE) 242 { 243 sprintf(text, "%s", "VOCÊ MATOU O DINO!."); 244 sprintf(text1, "%s", "GAME OVER"); 245 x = 100; 246 y = 400; 247 gameState = STATE_PRESSED_GAME_OVER; 248 al_start_timer(gameOverTimer); 249 } 250 251 else if(event.keyboard.keycode == ALLEGRO_KEY_A && gameState == STATE_PRESSED_SONO_DESCANSA) 252 { 253 sprintf(text, "%s", "VOCÊ MATOU O DINO!."); 254 sprintf(text1, "%s", "GAME OVER"); 255 x = 100; 256 y = 400; 257 gameState = STATE_PRESSED_GAME_OVER; 258 al_start_timer(gameOverTimer); 259 } 260 261 else if(event.keyboard.keycode == ALLEGRO_KEY_B && gameState == STATE_PRESSED_SONO_DESCANSA) 262 { 263 sprintf(text, "%s", "VOCÊ MATOU O DINO!."); 264 sprintf(text1, "%s", "GAME OVER"); 265 x = 100; 266 y = 400; 267 gameState = STATE_PRESSED_GAME_OVER; 268 al_start_timer(gameOverTimer); 269 } 270 271 272 // CAI NO BURACO 273 else if(event.keyboard.keycode == ALLEGRO_KEY_B && gameState == STATE_PRESSED_BURACO) 274 { 275 sprintf(text, "%s", "DINO NÃO PULOU, E CAIU NO BURACO. DINO DEVE PEDIR AJUDA (A) OU SAIR DO BURACO CAVANDO (B)?"); 276 x = 100; 277 y = 400; 278 gameState = STATE_PRESSED_CAI_BURACO; 279 } 280 281 //HERE WE USE A SINLE KEY (BACKSPACE), AND THEN USE IF/ELSE TO DO DIFFERENT THINGS, DEPENDING ON THE STATE... 282 else if(event.keyboard.keycode == ALLEGRO_KEY_BACKSPACE) 283 { 284 if(gameState == STATE_PRESSED_PULA_BURACO) //ELSE IF WE HAVE PRESSED A OR B... 285 { 286 sprintf(text, "%s", "APERTEI ENTER"); 287 x = 100; 288 y = 400; 289 gameState = STATE_PRESSED_BURACO; //...WE CHANGE GO BACK TO STATE "PRESSED ENTER" 290 } 291 } 292 293 if(event.keyboard.keycode != ALLEGRO_KEY_ESCAPE) 294 { 295 break; 296 } 297 } 298 299 case ALLEGRO_EVENT_DISPLAY_CLOSE: 300 { 301 done = true; 302 break; 303 } 304 } 305 306 if(done) 307 break; 308 309 if(redraw && al_is_event_queue_empty(queue)) 310 { 311 al_clear_to_color(al_map_rgb(0, 0, 0)); 312 al_draw_text(font, al_map_rgb(255, 255, 255), x, y, 0, text); 313 314 if(gameState == STATE_PRESSED_GAME_OVER) 315 { 316 al_draw_text(font, al_map_rgb(255, 255, 255), 500, 500, 0, text1); 317 } 318 319 al_flip_display(); 320 321 redraw = false; 322 } 323 324 } 325 326 al_destroy_font(font); 327 al_destroy_display(disp); 328 al_destroy_timer(timer); 329 al_destroy_event_queue(queue); 330 331 return 0; 332}

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

If you want to get fancy, and a little more advanced, try looking at allegro's event queue's. You can emit custom events, and then check for them too.

You can even write "Scenes" instead of "States". Each scene would have its own state, and its own input and display routines. I use them all the time in my own games to transition from one state to another. But that way they are encapsulated in their own class and cannot be messed up by another state (usually).

Dizzy Egg
Member #10,824
March 2009
avatar

I agree; I also think it would be prudent to look into functions, start seperating out some of that code, make chunks easier to deal with, and keep that main loop nice and minimal.

We'll see how this goes, it's quite hard to learn Allegro/games programming alongside learning how to structure code etc.

But keep on keeping on, you'll get your first game out of it, and then you'll be unstoppable

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

mayna azevedo
Member #20,077
May 2021

Wow, so many thanks.

I will look with a special way this weekday. If a don't understand, I will be here again 8-)8-)8-)

Have a good week.

Post Reply
Go to: