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
Kauhiz
Member #4,798
July 2004

Oh, so you asked a question and didn't bother to post the ACTUAL CODE YOU WERE HAVING PROBLEMS WITH! Great! That's just... great! Why the hell do you even have a function after meni(), if you want to quit straight after meni() anyway? >:(

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

a b
Member #8,092
December 2006

I have 462 lines of code now, I can't show everything because I have it in POLISH (names and comments are polish), but I now shorten my program and change in this program names and comments to english - for example:
As you see in this program player must press many times the keys (in functions: meni, start, kaukiz) and in each time I must check is key which he press is ESC or not - if the key is ESC some functions won't be make - is it checking in many functions every time when player must press key is necessary ? Can not this simplify ?
And my code has also another wrong thing - if player during the game press ESC in my program (now) he go to menu - I don't want also this - I want that if player press during the game ESC the game will exit. Is it possible do that ?

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 
13 
14 
15void meni();
16void autor();
17void instruction();
18void start();
19void end();
20void kaukiz();
21void something();
22 
23 
24 
25 
26 
27int main()
28{
29allegro_init();
30 install_keyboard();
31 set_color_depth(32);
32 set_gfx_mode(GFX_AUTODETECT,1024,768,0,0);
33 bufor=create_bitmap(SCREEN_W,SCREEN_H);
34 game = load_bitmap("game.bmp",NULL);
35 menu = load_bitmap("menu.bmp",NULL);
36 autors = load_bitmap("autors.bmp",NULL);
37 instructions = load_bitmap("instructions.bmp",NULL);
38 frame = load_bitmap("frame.bmp",NULL);
39 
40
41meni();
42 
43end();
44return 0;
45}
46END_OF_MAIN()
47 
48 
49 
50 
51 
52 
53 
54void meni()
55{
56int go=1;
57int table[4]={215,295,392,550};
58int index=0;
59int k;
60 while(go!=0)
61{
62clear_to_color(bufor,makecol(255,255,255));
63blit(menu,bufor,0,0,0,0,menu->w,menu->h);
64masked_blit(frame,bufor, 0,0, 80,table[index],frame->w,frame->h);
65blit(bufor,screen,0,0,0,0,SCREEN_W,SCREEN_H);
66k= readkey()>>8;
67 if ((k==KEY_DOWN)&&index!=3)
68 {
69
70 index++;
71 }
72 else if ((k==KEY_DOWN)&&index==3)
73 {
74
75 index=0;
76 }
77 else if ((k==KEY_UP)&&index!=0)
78 {
79
80 index--;
81 }
82 else if ((k==KEY_UP)&&index==0)
83 {
84
85 index=3;
86 }
87 else if((k==KEY_ENTER)&&index==0){start();}
88 else if((k==KEY_ENTER)&&index==1){instruction();}
89 else if((k==KEY_ENTER)&&index==2){autor();}
90 else if((k==KEY_ENTER)&&index==3){break;}
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 while (!key[KEY_ESC]&&!key[KEY_ENTER]) //pressing keys as long as you press ESC or Enter
104 {
105 readkey();
106 }
107 kaukiz(); // for you kaukiz :)
108
109}
110 
111 
112 
113void kaukiz()
114{
115if(!key[KEY_ESC]) //I must check if somebody don't press ESC in last pressing key
116 {
117 //here is something
118 readkey();
119 something();
120 }
121}
122 
123 
124void something()
125{
126if(!key[KEY_ESC]) //I must check if somebody don't press ESC in last pressing key
127 {
128 //here is something
129
130 }
131}
132 
133 
134 
135void instruction()
136{
137 clear_to_color(bufor,makecol(255,255,255));
138 blit(instructions,bufor,0,0,0,0,instructions->w,instructions->h);
139 blit(bufor,screen,0,0,0,0,SCREEN_W,SCREEN_H);
140 readkey();
141}
142 
143 
144void autor()
145{
146 clear_to_color(bufor,makecol(255,255,255));
147 blit(autors,bufor,0,0,0,0,autors->w,autors->h);
148 blit(bufor,screen,0,0,0,0,SCREEN_W,SCREEN_H);
149 readkey();
150}
151 
152 
153 
154 void end()
155{
156 destroy_bitmap(game);
157 destroy_bitmap(menu);
158 destroy_bitmap(frame);
159 destroy_bitmap(instructions);
160 destroy_bitmap(autors);
161 destroy_bitmap(bufor);
162 allegro_exit();
163}

tobing
Member #5,213
November 2004
avatar

Post the complete code. No matter what language. People that are willing to help would look into that code, regardless of the language. Try to help those people that are willing to help you! Write comments in english if you can, and use english names for functions and variables, if you can.

Kauhiz
Member #4,798
July 2004

What you want to do is use states for your game, just like LennyLen told you to do in post #10 of this topic (he even posted code!).

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

LennyLen
Member #5,313
December 2004
avatar

Quote:

What you want to do is use states for your game, just like LennyLen told you to do in post #10 of this topic (he even posted code!).

I didn't test the code, I just wrote it straight into the reply box, so there's one problem with it. At the moment, it uses if (key[KEY_ENTER])), when it should use readkey() >> 8.

Richard Phipps
Member #1,632
November 2001
avatar

ARRRRRRRRRGGGGGGGGGGGGHHHHHHHHHHH!!!!!!

LennyLen
Member #5,313
December 2004
avatar

Don't worry Richard, it'll all be okay. After all...

a b said:

I know c++ very well

X-G
Member #856
December 2000
avatar

You all realize I'm going to be smug about this all week, right?

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

LennyLen
Member #5,313
December 2004
avatar

Quote:

You all realize I'm going to be smug about this all week, right?

And how does that differ from any other week? :P

Kauhiz
Member #4,798
July 2004

LennyLen said:

Don't worry Richard, it'll all be okay. After all...
[quote a b]
I know c++ very well

</quote>Where'd he say that? Are you sure he didn't just forget the [sarcasm] tags or something?

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

Evert
Member #794
November 2000
avatar

a b
Member #8,092
December 2006

Now I have 1016 lines of code and I can't insert my code here because:
"Your post of 162.82KB is too long. The maximum size is 64K." - eh............. so I can't give all my code - it is only part of my code :/

I have 1016 lines of code - and it is my code (I have this code in 5 files so I insert here everything) - please tell me what should I do if I want that during the game when I press ESC game exit. So could you help me ?

DELETE ;D;D;D;D

hexagon
Member #8,207
January 2007
avatar

Now, that's what I call some ugly code.

When posting alot of files like that, you should atleast put a;
// name_of_file.h/cpp ex:
// klasy_deklaracje.h between every file so that one can see which one it is.

EDIT:

Quote:

[..]I can't show everything because I have it in POLISH (names and comments are polish)[..]

The only comments I can find in that code are;

int tab1[2][10]={                                             
                  {272,489,664,882,762,912,682,471,437,211},    // <- "x"
                  {120,175,102,114,262,360,354,421,311,354},    // <- "y"  
                  };

and that, IIRC, is not Polish.

tobing
Member #5,213
November 2004
avatar

You better make a zip-File or rar-File with your (complete) code in it and attach that to a posting here.

Kauhiz
Member #4,798
July 2004

No. That does it, I quit. I'm not going to go through 400+ lines of code because you can't bother to think about the advice you are given. You have been given dozens of good answers. But basically, what you do is you copy & paste the code you're given and do zero implementation. And then you expect it to work? Boy, I wonder why your problem hasn't been solved ???. Please, develop some basic problem solving skills, and the ability of independent thought. Then start programming. You'll find that you'll have less problems and people will be more inclined to help you.

Don't expect anyone to write your code for you. There's hardly a difference between posting your entire source and asking someone to write it from scratch, except that I'd actually prefer writing it from scratch to going through that mess of uncommented code. Why the hell does every variable name have to have like 3 Zs in it? Damn, Polish sucks :P (well, to be fair, Finnish isn't that much better, but at least we don't get to use our quirky spelling anywhere). IMO, any words that have a C, X and a Z in them should be outlawed.

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

a b
Member #8,092
December 2006

Kaukiz Wrrrrrrr >:( I alone write this code and copy and paste code from my 5 files here so you have:

#include <iostream>
#include <ctime>
#include <allegro.h>
#include "klasy_deklaracje.h"
#include "funkcje_deklaracje.h"
using namespace std;
..............

and after that you have:

..................
#include "klasy_deklaracje.h"
#include <iostream>
#include "funkcje_deklaracje.h"
using namespace std;

etc. - because it is code from 5 files - I could this more nicely cut and paste here (in 5 detached code) but I thought that you understood......... eh........ Programme certainly run !!! Kaukiz what this is for you 1000 the line of code :) hehehehe I add daily about 100 line of code, my game is unceasingly change. In fact anybody does not answer what should I change in the code if I want that after press ESC game will exit.

Richard Phipps
Member #1,632
November 2001
avatar

Evert
Member #794
November 2000
avatar

a b, has anyone ever told you to read http://alleg.sourceforge.net/stabledocs/en/help.html ? It's specifically for when you have a problem with Allegro, but much of the advice given is generally valid, and you routinely breach a lot of it. I very strongly suggest that you read it and try to ask questions based on that.

a b
Member #8,092
December 2006

Evert but in my game haven't mistakes - I only want to know is it better way that if somebody press ESC game will exit - I will be very grateful if you say me mayby on this exmaple - because it is more readable for you - is it better way then in below function: kaukiz() and something() ??

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

tobing
Member #5,213
November 2004
avatar

This guy is really hopeless. I give up.

Kauhiz
Member #4,798
July 2004

Quote:

I alone write this code and blah blah blah...

I think you missunderstood what I said. What I meant was that people post example code of how to do what you want. And you just throw that into your source, and it doesn't work since it's just an example, and you have to implement it properly.

Quote:

In fact anybody does not answer what should I change in the code if I want that after press ESC game will exit.

In fact I did. In fact every question you have asked so far has been answered and example code has been supplied. It just seems to me that you think that if you ask the same question again and again, eventually someone's going to write the whole thing for you. Either that or we're getting Punk'd.

Edit: Oh, BTW, kudos on constantly and consistently misspelling my name.

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

X-G
Member #856
December 2000
avatar

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

a b
Member #8,092
December 2006

Eh......... I don't want that you write it for me !!!! I now deleting code of my game - and now staying only my examples so you can't write code for me. In function kaukiz() and something() I must check if somebody don't press ESC - could this write better ?

Kauhiz
Member #4,798
July 2004

I said:

In fact every question you have asked so far has been answered and example code has been supplied.

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

hexagon
Member #8,207
January 2007
avatar

Quote:

could this write better ?

Yes, you could write it a lot better. (If that is what you mean)

Honestly, I would suggest you to buy a book regarding C/C++ and read it (and understand it),
before even thinking about game programming.



Go to: