Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » if somebody press Esc he go to another function

This thread is locked; no one can reply to it. rss feed Print
if somebody press Esc he go to another function
Evert
Member #794
November 2000
avatar

Quote:

nooooooo - your idea with readkey()>>8 is bad

No, your implementation is. I'm not going to spoon-feed you, but I will give you one more hint (even if Richard already gave the exact same advice above): don't declare it as a global variable; you want to keep calling readkey, don't you?
Think before you type.

Aside, I realise this is probably a reflection of your English not being very good, but I will tell you this: I have been programming for more than fifteen years, and programming in C with Allegro for nine years. I do not take kindly to a novice user getting angry and calling code examples I give "bad" because he fails to implement it properly. I seriously suggest that you invest some time on that attitude.

Quote:

I readed but it isn't simply when I can't good english........

Feel free to ask any question related to the manual. Feel free todirectly ask the friendly Polish regulars for advice is Polish if you need to.

a b
Member #8,092
December 2006

1. This is a bit uncomfortable declaring in every function this - do you judge ?

2. And when I want that programme will stop as long as I will press ANY(WHATEVER) key then I use only: "readkey()" yes ??

2. I change:

  readkey();
        while (!key[KEY_ESC]&&!key[KEY_ENTER])
        {
          readkey();
        }

on that:

 int k = readkey()>>8;  
        while (k!=KEY_ESC&&k!=KEY_ENTER)
        {
          k = readkey()>>8; 
        }

okay ??

Richard Phipps
Member #1,632
November 2001
avatar

Quote:

2. I change:

readkey();
while (!key[KEY_ESC]&&!key[KEY_ENTER])
{
readkey();
}

on that:

int k = readkey()>>8;
while (k!=KEY_ESC&&k!=KEY_ENTER)
{
k = readkey()>>8;
}

Please think about what we have said, and what you have read in the manual. That will answer your question.. Ask some polish people here if the english is too difficult.

Evert
Member #794
November 2000
avatar

Another hint: you only need to make a very small change to the readkey() call in your original code.

X-G
Member #856
December 2000
avatar

Quote:

Oh how right he was.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Kauhiz
Member #4,798
July 2004

Quote:

1. This is a bit uncomfortable declaring in every function this - do you judge ?

I'm not quite sure what you're question is, but technically if you wanted k to be a global variable, you could declare int k; globally and the do k = readkey()>>8; when you need to (albeit I don't see any point in making it into a global variable, and using k as a name for a global variable is a horrible idea).

---
It's Ridge Racer! RIIIIIDGE RAAAAACER!

a b
Member #8,092
December 2006

Evert: "you only need to make a very small change to the readkey() call in your original code." - what do you mean ??

Evert
Member #794
November 2000
avatar

I'm not going to explain more than that. I cannot say more without writing down the actual code, and I want you to do that yourself.
Read what has been written in this thread and try to understand it and think about it.

a b
Member #8,092
December 2006

int k = readkey()>>8;
while (k!=KEY_ESC&&k!=KEY_ENTER)
{
k = readkey()>>8;
}

this code runs very very well - so....... ? ? ?

Evert
Member #794
November 2000
avatar

I'm completely lost now.
If the code you have now works, what is your question?

EDIT: even if your English is not very good, you can explain a bit better what you mean.

LennyLen
Member #5,313
December 2004
avatar

Quote:

If the code you have now works, what is your question?

He's asking you if he changed the right part.

Evert
Member #794
November 2000
avatar

I don't know. Last I knew, he wanted to do something like the pseudo-code I posted here.
Why he would need a while loop at all is beyond me, because I don't think I understand what he's asking for.

LennyLen
Member #5,313
December 2004
avatar

Quote:

Why he would need a while loop at all is beyond me, because I don't think I understand what he's asking for.

It's simple. He has a menu. He wants the UP and DOWN keys to change the current selection, and ENTER to accept it. [I'm assuming] ESC exits the menu.

a b
Member #8,092
December 2006

yes, yes LennyLen :)

But now I must give question - is it possible because I don't know: Now I have 6 functions, during the game (not in menu but in game) player will press keys many times - and I must write every time when player should press key if this key isn't ESC because if it is ESC then game should exit. Is it possible write as a global that if player press ESC game exit and I can't check it in each place when player should press some key for example: "enter" or "->" or another ??

LennyLen
Member #5,313
December 2004
avatar

Quote:

Is it possible write as a global that if player press ESC game exit and I can't check it in each place when player should press some key for example: "enter" or "->" or another ??

Richard Phipps already answered that question.

Kauhiz
Member #4,798
July 2004

Do it like this:

bool gameRunning = true;
...
int main()
{
  //Initialization stuff goes here
  while(gameRunning)
  {
    //Main part of your game

    if(k == KEY_ESC) gameRunning = false;
  }
  //De-init stuff goes here
  return 0;
}

That is if you want the game to exit when ESC is pressed.

---
It's Ridge Racer! RIIIIIDGE RAAAAACER!

a b
Member #8,092
December 2006

Kauhiz I made that but when I press ESC - nothing do:

1#include <iostream>
2#include <allegro.h>
3using namespace std;
4 
5BITMAP *game = NULL;
6BITMAP *bufor = NULL;
7BITMAP *menu = NULL;
8BITMAP *autors=NULL;
9BITMAP *instructions = NULL;
10BITMAP *frame = NULL;
11 
12 
13int go=1;
14int table[4]={215,295,392,550};
15int index=0;
16 
17void meni();
18void autor();
19void instruction();
20void start();
21void end();
22 
23 
24bool gameRunning = true;
25 
26 
27 
28 
29int main()
30{
31allegro_init();
32 install_keyboard();
33 set_color_depth(32);
34 set_gfx_mode(GFX_AUTODETECT,1024,768,0,0);
35 bufor=create_bitmap(SCREEN_W,SCREEN_H);
36 game = load_bitmap("game.bmp",NULL);
37 menu = load_bitmap("menu.bmp",NULL);
38 autors = load_bitmap("autors.bmp",NULL);
39 instructions = load_bitmap("instructions.bmp",NULL);
40 frame = load_bitmap("frame.bmp",NULL);
41 
42int k;
43while(gameRunning)
44{
45meni();
46k = readkey()>>8;
47if(k == KEY_ESC) gameRunning = false;
48}
49
50end();
51return 0;
52}
53END_OF_MAIN()
54 
55 
56 
57 
58void meni()
59{
60int k;
61 while(go!=0)
62{
63clear_to_color(bufor,makecol(255,255,255));
64blit(menu,bufor,0,0,0,0,menu->w,menu->h);
65masked_blit(frame,bufor, 0,0, 80,table[index],frame->w,frame->h);
66blit(bufor,screen,0,0,0,0,SCREEN_W,SCREEN_H);
67k= readkey()>>8;
68 if ((k==KEY_DOWN)&&index!=3)
69 {
70
71 index++;
72 }
73 else if ((k==KEY_DOWN)&&index==3)
74 {
75
76 index=0;
77 }
78 else if ((k==KEY_UP)&&index!=0)
79 {
80
81 index--;
82 }
83 else if ((k==KEY_UP)&&index==0)
84 {
85
86 index=3;
87 }
88 else if((k==KEY_ENTER)&&index==0){start();}
89 else if((k==KEY_ENTER)&&index==1){instruction();}
90 else if((k==KEY_ENTER)&&index==2){autor();}
91 
92}
93}
94 
95 
96 
97void start()
98{
99 clear_to_color(bufor,makecol(255,255,255));
100 blit(game,bufor,0,0,0,0,game->w,game->h);
101 blit(bufor,screen,0,0,0,0,SCREEN_W,SCREEN_H);
102 readkey();
103
104}
105 
106 
107void instruction()
108{
109 clear_to_color(bufor,makecol(255,255,255));
110 blit(instructions,bufor,0,0,0,0,instructions->w,instructions->h);
111 blit(bufor,screen,0,0,0,0,SCREEN_W,SCREEN_H);
112 readkey();
113}
114 
115 
116void autor()
117{
118 clear_to_color(bufor,makecol(255,255,255));
119 blit(autors,bufor,0,0,0,0,autors->w,autors->h);
120 blit(bufor,screen,0,0,0,0,SCREEN_W,SCREEN_H);
121 readkey();
122}
123 
124 
125 
126 void end()
127{
128 destroy_bitmap(game);
129 destroy_bitmap(menu);
130 destroy_bitmap(frame);
131 destroy_bitmap(instructions);
132 destroy_bitmap(autors);
133 destroy_bitmap(bufor);
134 allegro_exit();
135}

Kauhiz
Member #4,798
July 2004

Did you even look at the code you wrote!? Obviously it's not going to quit, because you have while(go!=0) in your meni() function, and go isn't set to anything inside that loop ::). So you have an infinite loop, and it never gets to the point where it'd check to see if it should quit...

This is basic stuff that has nothing to do with allegro! If you expect to get any help on this forum in the future you have to at least TRY to solve your problems yourself, because otherwise people will get frustrated and just give up! Just like Evert said, you don't need a while-loop there! Do you just hit some random keys on your keyboard and then compile to see if it works?

---
It's Ridge Racer! RIIIIIDGE RAAAAACER!

a b
Member #8,092
December 2006

I try made what you said and I change my code ...... Firstly it was:

1#include <iostream>
2#include <allegro.h>
3using namespace std;
4 
5BITMAP *game = NULL;
6BITMAP *bufor = NULL;
7BITMAP *menu = NULL;
8BITMAP *autors=NULL;
9BITMAP *instructions = NULL;
10BITMAP *frame = NULL;
11 
12 
13int go=1;
14int table[4]={215,295,392,550};
15int index=0;
16 
17int meni();
18void autor();
19void instruction();
20void start();
21void end();
22 
23 
24 
25 
26 
27 
28 
29int main()
30{
31allegro_init();
32 install_keyboard();
33 set_color_depth(32);
34 set_gfx_mode(GFX_AUTODETECT,1024,768,0,0);
35 bufor=create_bitmap(SCREEN_W,SCREEN_H);
36 game = load_bitmap("game.bmp",NULL);
37 menu = load_bitmap("menu.bmp",NULL);
38 autors = load_bitmap("autors.bmp",NULL);
39 instructions = load_bitmap("instructions.bmp",NULL);
40 frame = load_bitmap("frame.bmp",NULL);
41if (meni()==0)
42 { return 0; }
43
44end();
45return 0;
46}
47END_OF_MAIN()
48 
49 
50 
51 
52int meni()
53{
54int k;
55 while(go!=0)
56{
57k = readkey()>>8;
58clear_to_color(bufor,makecol(255,255,255));
59blit(menu,bufor,0,0,0,0,menu->w,menu->h);
60masked_blit(frame,bufor, 0,0, 80,table[index],frame->w,frame->h);
61blit(bufor,screen,0,0,0,0,SCREEN_W,SCREEN_H);
62 
63 
64 if ((k==KEY_DOWN)&&index!=3)
65 {
66
67 index++;
68 }
69 else if ((k==KEY_DOWN)&&index==3)
70 {
71
72 index=0;
73 }
74 else if ((k==KEY_UP)&&index!=0)
75 {
76
77 index--;
78 }
79 else if ((k==KEY_UP)&&index==0)
80 {
81
82 index=3;
83 }
84 else if((k==KEY_ENTER)&&index==3){return 0;}
85 else if((k==KEY_ENTER)&&index==0){start();}
86 else if((k==KEY_ENTER)&&index==1){instruction();}
87 else if((k==KEY_ENTER)&&index==2){autor();}
88 
89}
90}
91 
92 
93 
94void start()
95{
96 clear_to_color(bufor,makecol(255,255,255));
97 blit(game,bufor,0,0,0,0,game->w,game->h);
98 blit(bufor,screen,0,0,0,0,SCREEN_W,SCREEN_H);
99
100 while ((k!=KEY_ESC)&&(k!=KEY_ENTER))
101 {
102 readkey();
103 }
104SEEING(); // it is define in second code
105}
106 
107 
108void instruction()
109{
110 clear_to_color(bufor,makecol(255,255,255));
111 blit(instructions,bufor,0,0,0,0,instructions->w,instructions->h);
112 blit(bufor,screen,0,0,0,0,SCREEN_W,SCREEN_H);
113 readkey();
114}
115 
116 
117void autor()
118{
119 clear_to_color(bufor,makecol(255,255,255));
120 blit(autors,bufor,0,0,0,0,autors->w,autors->h);
121 blit(bufor,screen,0,0,0,0,SCREEN_W,SCREEN_H);
122 readkey();
123}
124 
125 
126 
127 void end()
128{
129 destroy_bitmap(game);
130 destroy_bitmap(menu);
131 destroy_bitmap(frame);
132 destroy_bitmap(instructions);
133 destroy_bitmap(autors);
134 destroy_bitmap(bufor);
135 allegro_exit();
136}

This loop is infinite on purpose. It must be infinite (only when somebody is in index 3 and press enter will be exit and now I want also make that if somebody press ESC program will be exit ) so just therefore I ask is it possible that if somebody press ESC program will exit. This program is also example because I have bigger program and more functions, for example I have function (I will must change there readkey() and key[]):

1void SEEING()
2{
3.....................................
4 readkey();
5 while (!key[KEY_ENTER]&&!key[KEY_ESC])
6 {
7 readkey();
8 }
9 something();
10
11}
12 
13 
14void something()
15{
16 if(!key[KEY_ESC])
17 {
18
19 .........................................
20 readkey();
21 }
22}

Just therefore I ask if it possible that that ESC was pressed then the end of programme

Kauhiz
Member #4,798
July 2004

:o Did you read my post? Did you understand it? Do you even understand your own code!? This is why everyone else gave up on you (it seems) and I'm starting to feel I'm wasting my time here too...

Quote:

This loop is infinite on purpose. It must be infinite

No, wrong! You DO NOT want to have an infinite loop in your code! Secondly, if you can make the program exit when ENTER is pressed, why can't you make it exit when ESC is pressed? There's absolutely no difference!! And BTW, returning when ENTER is pressed isn't even the best way to do that. Replace this:if((k==KEY_ENTER)&&index==3){return 0;} with this:if((k == KEY_ENTER) && index == 3) || k == KEY_ESC) go = 0;
If you can't figure that out by yourself, I quit. I suggest you get a book on C++ and read Phipps' game creation articles, or something. And THINK before you write code (and ask questions)!!

---
It's Ridge Racer! RIIIIIDGE RAAAAACER!

X-G
Member #856
December 2000
avatar

Wow, you guys really are a bunch of incurable masochists, aren't you?

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Kauhiz
Member #4,798
July 2004

I know, it's bad :(

---
It's Ridge Racer! RIIIIIDGE RAAAAACER!

Richard Phipps
Member #1,632
November 2001
avatar

a b
Member #8,092
December 2006

no, no, no - this code is only example, in my real code in main after function "meni()" is another function and if I do as you say function after "meni()" in main will be run - and I don't want this.
I want that during the game after menu if somebody press "ESC" program will be exit.

Richard Phipps
Member #1,632
November 2001
avatar



Go to: