Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » how to make a game menu

Credits go to Edgar Reynaldo for helping out!
This thread is locked; no one can reply to it. rss feed Print
how to make a game menu
Vieira
Member #16,926
December 2018

Hello, I want to create a menu for my game and I have some questions.
First I wanted the mouse to move over the word, it would change color, but when it changes color if I take the mouse out of the word it does not return its original color (white)
The way I did it does not appear very accurate because almost any movement of the mouse causes the word to change color.
maybe I'm putting the wrong update line, I've already put it in several different places of the program, if you can clarify where to put it would also be great.
Another question is: will it be necessary to create different windows for when I click, for example, to start the game or to change its audio settings?
How could a menu be made? I am new at programming with the library.

my code:

#SelectExpand
1 2#include <allegro5/allegro.h> 3#include <allegro5/allegro_native_dialog.h> 4#include <allegro5/allegro_image.h> 5#include <allegro5/allegro_font.h> 6#include <allegro5/allegro_ttf.h> 7#include <allegro5/allegro_primitives.h> 8 9 /* Variáveis globais*/ 10const int x=1280,y=720; 11bool fim= false; 12int pos_x=100; 13int pos_y=100; 14 15enum teclas {direita,esquerda}; 16bool teclas[] = {false,false}; 17 18int main(void) 19{ 20 /* Inicializamos a biblioteca*/ 21 al_init(); 22 23 ALLEGRO_EVENT_QUEUE* fila_eventos = NULL; 24 25 /* Variável representando a janela principal*/ 26 ALLEGRO_DISPLAY *janela = NULL; 27 ALLEGRO_BITMAP *imagem; 28 29 /* Inicialização Add-on*/ 30 al_init_font_addon(); 31 al_init_ttf_addon(); 32 al_init_image_addon(); 33 al_install_keyboard(); 34 al_install_mouse(); 35 36 /* Para deixar em FullScreen*/ 37 ALLEGRO_MONITOR_INFO info; 38 39 int res_x_comp, res_y_comp; 40 al_get_monitor_info(0,&info); 41 res_x_comp = info.x2 - info.x1; 42 res_y_comp = info.y2 - info.y1; 43 al_set_new_display_flags(ALLEGRO_FULLSCREEN_WINDOW); 44 45 /* Criamos a nossa janela*/ 46 janela = al_create_display(res_x_comp,res_y_comp); 47 48 /* Adaptamos a resolução do jogo ao FullScreen*/ 49 50 float red_x = res_x_comp / (float) x; 51 float red_y = res_y_comp / (float) y; 52 53 ALLEGRO_TRANSFORM transformar; 54 al_identity_transform(&transformar); 55 al_scale_transform(&transformar, red_x,red_y); 56 al_use_transform(&transformar); 57 58 /* Escrita*/ 59 ALLEGRO_FONT *mainfont = al_load_font("fonte.ttf",160,NULL); 60 ALLEGRO_FONT *mainfontmenor = al_load_font("fonte.ttf",80,NULL); 61 62 /* Carrega a imagem*/ 63 imagem = al_load_bitmap("90_minutos_menu.png"); 64 65 /* Desenha a imagem na tela*/ 66 al_draw_bitmap(imagem, 0, 0, 0); 67 68 /* Desenha o título do jogo na tela*/ 69 al_draw_text(mainfont,al_map_rgb(250,0,0),580,100,NULL,"90 MiNuToS"); 70 71 /*Desenha opções do jogo na tela*/ 72 al_draw_text(mainfontmenor,al_map_rgb(250,250,250),780,280,NULL,"InIcIaR"); 73 al_draw_text(mainfontmenor,al_map_rgb(250,250,250),795,360,NULL,"SoNs"); 74 75 /*fila de eventos*/ 76 77 fila_eventos = al_create_event_queue(); 78al_init_primitives_addon(); 79 80 /*Fontes de evento*/ 81 82 al_register_event_source(fila_eventos,al_get_keyboard_event_source()); 83 al_register_event_source(fila_eventos,al_get_mouse_event_source()); 84 85 86 /*loop principal*/ 87 88 while (!fim) { 89 90 /* Atualiza a tela*/ 91 al_flip_display(); 92 93 /* Segura a execução por 90 minutos*/ 94 95 //DESCOBRIR COMO FAZER// 96 97 ALLEGRO_EVENT ev; 98 99 al_wait_for_event(fila_eventos, &ev); 100 101 //Movimentação 102 103 if(ev.type == ALLEGRO_EVENT_KEY_DOWN) 104 { 105 if (ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE) 106 { 107 fim= true; 108 } 109 110 switch(ev.keyboard.keycode) 111 { 112 case ALLEGRO_KEY_RIGHT: 113 teclas[direita] = true; 114 break; 115 case ALLEGRO_KEY_LEFT: 116 teclas[esquerda] = true; 117 break; 118 case ALLEGRO_KEY_A: 119 teclas[esquerda] = true; 120 break; 121 case ALLEGRO_KEY_D: 122 teclas[direita] = true; 123 break; 124 } 125 126 } 127 else if(ev.type == ALLEGRO_EVENT_KEY_UP) 128{ 129 switch(ev.keyboard.keycode) 130 { 131 case ALLEGRO_KEY_RIGHT: 132 teclas[direita] =false; 133 break; 134 case ALLEGRO_KEY_LEFT: 135 teclas[esquerda] =false; 136 break; 137 case ALLEGRO_KEY_A: 138 teclas[esquerda] =false; 139 break; 140 case ALLEGRO_KEY_D: 141 teclas[direita] =false; 142 break; 143 } 144} 145else if (ev.type == ALLEGRO_EVENT_MOUSE_AXES) 146 147{ 148 if(ev.mouse.y= 280,ev.mouse.x=780) 149 { 150 al_draw_text(mainfontmenor,al_map_rgb(250,0,0),780,280,NULL,"InIcIaR"); 151 } 152 else if (ev.mouse.y= 280,ev.mouse.x=780) 153 154 { al_draw_text(mainfontmenor,al_map_rgb(250,250,250),780,280,NULL,"InIcIaR"); 155 al_flip_display(); 156} 157 pos_x += teclas[direita]*40; 158 pos_x -= teclas[esquerda]*40; 159 160 } 161 } 162 163 /* Finaliza a janela*/ 164 al_destroy_display(janela); 165 al_destroy_font(mainfont); 166 al_destroy_display(imagem); 167 al_destroy_event_queue(fila_eventos); 168 169 return 0; 170}

Frank Drebin
Member #2,987
December 2002
avatar

Vieira,

please use code-tags (see Formatting Help) if you are posting code to make it more readable.

If I understand correctly you try to make a game menu that changes color based on the mouse input. This can be achieved without creating different windows. Also, you don't need to include/use the "allegro_native_dialog" for this.

Take a look at that line:

if(ev.mouse.y= 280,ev.mouse.x=780)

Here, you are doing an assignment rather than a comparison. I think what you want to do is:

if(ev.mouse.y==280,ev.mouse.x==780)

Also, you probably need to make the comparison like this:

if ((ev.mouse.y==280) && (ev.mouse.x==780))

Then there are two if-statements that are exactly the same so the second if-statement will never be checked.

else if (ev.type == ALLEGRO_EVENT_MOUSE_AXES)

{
if(ev.mouse.y= 280,ev.mouse.x=780)
{
al_draw_text(mainfontmenor,al_map_rgb(250,0,0),780,280,NULL,"InIcIaR");
}
else if (ev.mouse.y= 280,ev.mouse.x=780)

{ al_draw_text(mainfontmenor,al_map_rgb(250,250,250),780,280,NULL,"InIcIaR");
al_flip_display();
}

I think what you wanted to to here is something like this:

else if (ev.type == ALLEGRO_EVENT_MOUSE_AXES)

{
if ((ev.mouse.y==280) && (ev.mouse.x==780))
{
al_draw_text(mainfontmenor,al_map_rgb(250,0,0),780,280,NULL,"InIcIaR");
}
else

{ al_draw_text(mainfontmenor,al_map_rgb(250,250,250),780,280,NULL,"InIcIaR");
al_flip_display();
}

See if that helps ...

Vieira
Member #16,926
December 2018

Hello, Thanks for the trouble!
It worked in parts, however when I added logic (and) it did not work, so I just chose to make use of the comma.
However, the word only turns red at a specific point on the screen, I think it would be better to do in an area that covers the word, I thought of making less equal to xxx, but I do not know if this is the best way to do ???

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Games are just data and instructions. If you think in terms of those two things, then you can program anything.

First, the data - the word position and area.

Second, the instruction - if the mouse is over the word area, display in red, else display in white.

Now just code it.

The word has a rectangle. The mouse has a position. You can check if the mouse is inside the rectangle. Then you can decide whether to display red or white text.

Neil Roy
Member #2,229
April 2002
avatar

In my own Deluxe Pacman 2 game, I simply check each button and compare the rectangular area it occupies with the mouse position. If the mouse isn't over it, I draw a blue button, if it is over it, I draw a red button, if you click and are over it, I draw a red button that appears depressed. I created three button graphics for each one. It's really simple to code. I have menu button routines for adding new buttons and storing their location for checks later on.

---
“I love you too.” - last words of Wanda Roy

Vieira
Member #16,926
December 2018

:D Thanks,
I understood that the words occupy the dimensions, after that, it was really easier to understand how it does

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Go to: