Please help me - I want change my code that in code were classes, constructors and destructors but I don't know where must be for example: BITMAP *board = NULL; set_color_depth(32) and other things in my code - could you change my code that it includes classes, constructors and destructors ?? Please help
It is my board:
http://img407.imageshack.us/img407/2996/boardnj8.jpg
It is my pawn:
http://img409.imageshack.us/img409/5185/pawnyi0.jpg
It is my code:
1 | |
2 | |
3 | #include<iostream> |
4 | #include<ctime> |
5 | #include <allegro.h> |
6 | |
7 | using namespace std; |
8 | |
9 | |
10 | int main() |
11 | { |
12 | BITMAP *board = NULL; |
13 | BITMAP *pawn = NULL; |
14 | BITMAP *buffer = NULL; |
15 | allegro_init(); |
16 | install_keyboard(); |
17 | set_color_depth(32); |
18 | set_gfx_mode(GFX_AUTODETECT,1024,768,0,0); |
19 | buffer=create_bitmap(SCREEN_W,SCREEN_H); |
20 | |
21 | |
22 | board = load_bitmap("board.bmp",NULL); |
23 | pawn = load_bitmap("pawn.bmp",NULL); |
24 | |
25 | |
26 | int tab1[2][10]={ |
27 | {100,220,370,500,700,700,500,350,220,100}, |
28 | {100,100,100,100,100,340,340,340,340,340}, |
29 | }; |
30 | |
31 | int tab2[10]={0,1,2,3,4,5,6,7,8,9}; |
32 | |
33 | int *index=&tab2[0]; |
34 | int *zero=&tab2[0]; |
35 | int *nine=&tab2[9]; |
36 | int module; |
37 | int eyes; |
38 | srand (time(0)); |
39 | |
40 | |
41 | while(!key[KEY_ESC]) |
42 | { |
43 | eyes = 1+rand()%3; |
44 | clear_to_color(buffer,makecol(255,255,255)); |
45 | blit(board,buffer,0,0,0,0,board->w,board->h); |
46 | blit(pawn,buffer,0,0,tab1[0][*index],tab1[1][*index],pawn->w,pawn->h); |
47 | textprintf_ex(buffer, font, 267, 600, makecol(0, 0, 0), -1, "You throw: %d", eyes); |
48 | textprintf_ex(buffer, font, 267, 620, makecol(0, 0, 0), -1, "-> - left, <- - right"); |
49 | textprintf_ex(buffer, font, 267, 640, makecol(0, 0, 0), -1, "ESC - the end of the game"); |
50 | blit(buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H); |
51 | readkey(); |
52 | |
53 | |
54 | |
55 | direction: |
56 | if (key[KEY_LEFT]) |
57 | { |
58 | if(*index==0||*index==1||*index==2||*index==3||*index==4) |
59 | { |
60 | if ((*index-eyes)>=*zero) |
61 | { |
62 | index=index-eyes; |
63 | } |
64 | else |
65 | { |
66 | |
67 | index=nine-(eyes-*index-1); |
68 | } |
69 | |
70 | } |
71 | else if(*index==5||*index==6||*index==7||*index==8||*index==9) |
72 | |
73 | { |
74 | if ((*index+eyes)<=*nine) |
75 | { |
76 | index=index+eyes; |
77 | } |
78 | else |
79 | { |
80 | module=eyes-(*nine-*index); |
81 | if (module<0) |
82 | { |
83 | module=-module; |
84 | } |
85 | index=zero+module-1; |
86 | } |
87 | |
88 | } |
89 | } |
90 | |
91 | |
92 | |
93 | |
94 | else if (key[KEY_RIGHT]) |
95 | { |
96 | if(*index==0||*index==1||*index==2||*index==3||*index==4) |
97 | { |
98 | index=index+eyes; |
99 | |
100 | } |
101 | else |
102 | { |
103 | index=index-eyes; |
104 | } |
105 | |
106 | |
107 | } |
108 | |
109 | |
110 | |
111 | else |
112 | { |
113 | if (!key[KEY_ESC]) |
114 | { |
115 | goto direction; |
116 | } |
117 | |
118 | } |
119 | |
120 | |
121 | |
122 | } |
123 | |
124 | destroy_bitmap(board); |
125 | destroy_bitmap(pawn); |
126 | destroy_bitmap(buffer); |
127 | allegro_exit(); |
128 | return 0; |
129 | } |
130 | END_OF_MAIN() |
MERRY CHRISTMAS!!!. HO! HO! HO!.
marry christmas hehehe But is this answer for my problem ?
But is this answer for my problem ?
No. I'm guessing that Albin Engström is hinting that this may be Christmas time, but we're not feeling benevolent enough to do all the coding for you. We can help, but firstly you really need to pick up a C++ book, or check out the on-line tutorials, and try to work it out for yourself. If you get stuck, then maybe we can help you.
But in c++ books don't write where should be:
allegro_init();
install_keyboard();
set_color_depth(32);
set_gfx_mode(GFX_AUTODETECT,1024,768,0,0);
buffer=create_bitmap(SCREEN_W,SCREEN_H);
BITMAP *board = NULL;
BITMAP *pawn = NULL;
BITMAP *buffer = NULL;
in classes or in main ??
1 | #include<iostream> |
2 | #include<ctime> |
3 | #include <allegro.h> |
4 | using namespace std; |
5 | |
6 | |
7 | class boardboard |
8 | { |
9 | public: |
10 | allegro_init(); |
11 | install_keyboard(); |
12 | set_color_depth(32); |
13 | set_gfx_mode(GFX_AUTODETECT,1024,768,0,0); |
14 | buffer=create_bitmap(SCREEN_W,SCREEN_H); |
15 | BITMAP *board = NULL; |
16 | BITMAP *pawn = NULL; |
17 | BITMAP *buffer = NULL; |
18 | board = load_bitmap("board.bmp",NULL); |
19 | pawn = load_bitmap("pawn.bmp",NULL); |
20 | |
21 | int tab1[2][10]={ |
22 | {100,220,370,500,700,700,500,350,220,100}, |
23 | {100,100,100,100,100,340,340,340,340,340}, |
24 | }; |
25 | int tab2[10]={0,1,2,3,4,5,6,7,8,9}; |
26 | |
27 | int *index=&tab2[0]; |
28 | int *zero=&tab2[0]; |
29 | int *nine=&tab2[9]; |
30 | int module; |
31 | int eyes; |
32 | srand (time(0)); |
33 | |
34 | |
35 | while(!key[KEY_ESC]) |
36 | { |
37 | eyes = 1+rand()%3; |
38 | clear_to_color(buffer,makecol(255,255,255)); |
39 | blit(board,buffer,0,0,0,0,board->w,board->h); |
40 | blit(pawn,buffer,0,0,tab1[0][*index],tab1[1][*index],pawn->w,pawn->h); |
41 | textprintf_ex(buffer, font, 267, 600, makecol(0, 0, 0), -1, "You throw: %d", eyes); |
42 | textprintf_ex(buffer, font, 267, 620, makecol(0, 0, 0), -1, "-> - left, <- - right"); |
43 | textprintf_ex(buffer, font, 267, 640, makecol(0, 0, 0), -1, "ESC - the end of the game"); |
44 | blit(buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H); |
45 | readkey(); |
46 | |
47 | |
48 | |
49 | direction: |
50 | if (key[KEY_LEFT]) |
51 | { |
52 | if(*index==0||*index==1||*index==2||*index==3||*index==4) |
53 | { |
54 | if ((*index-eyes)>=*zero) |
55 | { |
56 | index=index-eyes; |
57 | } |
58 | else |
59 | { |
60 | |
61 | index=nine-(eyes-*index-1); |
62 | } |
63 | |
64 | } |
65 | else if(*index==5||*index==6||*index==7||*index==8||*index==9) |
66 | |
67 | { |
68 | if ((*index+eyes)<=*nine) |
69 | { |
70 | index=index+eyes; |
71 | } |
72 | else |
73 | { |
74 | module=eyes-(*nine-*index); |
75 | if (module<0) |
76 | { |
77 | module=-module; |
78 | } |
79 | index=zero+module-1; |
80 | } |
81 | |
82 | } |
83 | } |
84 | |
85 | |
86 | |
87 | |
88 | else if (key[KEY_RIGHT]) |
89 | { |
90 | if(*index==0||*index==1||*index==2||*index==3||*index==4) |
91 | { |
92 | index=index+eyes; |
93 | |
94 | } |
95 | else |
96 | { |
97 | index=index-eyes; |
98 | } |
99 | |
100 | |
101 | } |
102 | |
103 | |
104 | |
105 | else |
106 | { |
107 | if (!key[KEY_ESC]) |
108 | { |
109 | goto direction; |
110 | } |
111 | |
112 | } |
113 | |
114 | |
115 | |
116 | } |
117 | |
118 | destroy_bitmap(board); |
119 | destroy_bitmap(pawn); |
120 | destroy_bitmap(buffer); |
121 | allegro_exit(); |
122 | }; |
123 | |
124 | |
125 | |
126 | |
127 | |
128 | |
129 | int main() |
130 | { |
131 | boardboard.something; |
132 | |
133 | |
134 | return 0; |
135 | } |
136 | END_OF_MAIN() |
For the initialization (allegro_init(), etc.), that should be in the main function.
Classes (in my opinion) should just give the main function bitmaps to draw, or even just their position. Although after you initialize allegro in the main function, classes can use allegro functions.
I hope this answers your question. (And I hope that my answer is valid. )
I change my code but something is wrong - what is wrong
??
1 | |
2 | #include<iostream> |
3 | #include<ctime> |
4 | #include <allegro.h> |
5 | using namespace std; |
6 | |
7 | |
8 | class boardboard |
9 | { |
10 | private: |
11 | BITMAP *board; |
12 | BITMAP *pawn; |
13 | BITMAP *buffer; |
14 | |
15 | public: |
16 | |
17 | int tab1[2][10]={ |
18 | {100,220,370,500,700,700,500,350,220,100}, |
19 | {100,100,100,100,100,340,340,340,340,340}, |
20 | }; |
21 | int tab2[10]={0,1,2,3,4,5,6,7,8,9}; |
22 | |
23 | boardboard (BITMAP a, BITMAP b, BITMAP c) //constructor |
24 | { |
25 | *board=a; |
26 | *board=b; |
27 | *board=c; |
28 | |
29 | } |
30 | |
31 | |
32 | int *index=&tab2[0]; |
33 | int *zero=&tab2[0]; |
34 | int *nine=&tab2[9]; |
35 | int module; |
36 | int eyes; |
37 | srand (time(0)); |
38 | |
39 | while(!key[KEY_ESC]) |
40 | { |
41 | eyes = 1+rand()%3; |
42 | clear_to_color(buffer,makecol(255,255,255)); |
43 | blit(board,buffer,0,0,0,0,board->w,board->h); |
44 | blit(pawn,buffer,0,0,tab1[0][*index],tab1[1][*index],pawn->w,pawn->h); |
45 | textprintf_ex(buffer, font, 267, 600, makecol(0, 0, 0), -1, "You throw: %d", eyes); |
46 | textprintf_ex(buffer, font, 267, 620, makecol(0, 0, 0), -1, "-> - left, <- - right"); |
47 | textprintf_ex(buffer, font, 267, 640, makecol(0, 0, 0), -1, "ESC - the end of the game"); |
48 | blit(buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H); |
49 | readkey(); |
50 | |
51 | |
52 | |
53 | direction: |
54 | if (key[KEY_LEFT]) |
55 | { |
56 | if(*index==0||*index==1||*index==2||*index==3||*index==4) |
57 | { |
58 | if ((*index-eyes)>=*zero) |
59 | { |
60 | index=index-eyes; |
61 | } |
62 | else |
63 | { |
64 | |
65 | index=nine-(eyes-*index-1); |
66 | } |
67 | |
68 | } |
69 | else if(*index==5||*index==6||*index==7||*index==8||*index==9) |
70 | |
71 | { |
72 | if ((*index+eyes)<=*nine) |
73 | { |
74 | index=index+eyes; |
75 | } |
76 | else |
77 | { |
78 | module=eyes-(*nine-*index); |
79 | if (module<0) |
80 | { |
81 | module=-module; |
82 | } |
83 | index=zero+module-1; |
84 | } |
85 | |
86 | } |
87 | } |
88 | |
89 | |
90 | |
91 | |
92 | else if (key[KEY_RIGHT]) |
93 | { |
94 | if(*index==0||*index==1||*index==2||*index==3||*index==4) |
95 | { |
96 | index=index+eyes; |
97 | |
98 | } |
99 | else |
100 | { |
101 | index=index-eyes; |
102 | } |
103 | |
104 | |
105 | } |
106 | |
107 | |
108 | |
109 | else |
110 | { |
111 | if (!key[KEY_ESC]) |
112 | { |
113 | goto direction; |
114 | } |
115 | |
116 | } |
117 | |
118 | } |
119 | |
120 | |
121 | ~boardboard(a, b, c) //destructor |
122 | { |
123 | destroy_bitmap(board); |
124 | destroy_bitmap(pawn); |
125 | destroy_bitmap(buffer); |
126 | |
127 | } |
128 | |
129 | }; |
130 | |
131 | |
132 | |
133 | |
134 | |
135 | |
136 | int main() |
137 | { |
138 | |
139 | allegro_init(); |
140 | install_keyboard(); |
141 | set_color_depth(32); |
142 | set_gfx_mode(GFX_AUTODETECT,1024,768,0,0); |
143 | buffer=create_bitmap(SCREEN_W,SCREEN_H); |
144 | |
145 | |
146 | boardboard something ("board.bmp","pawn.bmp",NULL); //constructor |
147 | something.boardboard::~boardboard(BITMAP a, BITMAP b, BITMAP c); //destructor |
148 | |
149 | allegro_exit(); |
150 | return 0; |
151 | } |
152 | END_OF_MAIN() |
are you kidding?
[EDIT]
but to name one thing which is totally wrong:
don't use goto
also, you can't just write code into a class without putting it into a method. I think you should inform yourself about the concept of object-orientation before trying to use classes.
I change my code but something is wrong
Sorry, I left my psychic powers behind somewhere. Please explain what you mean by "something is wrong."
We will not do your homework for you, either.
but to name one thing which is totally wrong:
don't use goto
There are a few things like that in the code that are bad, and he was told about them last time he posted about his programs not working.
so I have questions:
1. in the class is this must be private or maybe something else ??:
??
2.
3.
constructor in the main ????- what is wrong ??: boardboard something ("board.bmp","pawn.bmp",NULL); //constructor
4. destructor in the class - what is wrong :
~boardboard(a, b, c) //destructor { destroy_bitmap(board); destroy_bitmap(pawn); destroy_bitmap(buffer); }
5. destructor in main - what is wrong :
something.boardboard::~boardboard(BITMAP a, BITMAP b, BITMAP c); //destructor
in the class is this must be private or maybe something else ??
No, they don't have to be private, but they should be.
constructor in the class should be ??:
Well for one thing, you're assingning the board pointer three different values consecutively, so the first two lines do nothing. Secondly, it should be:
board=&a;
4. destructor in the class - what is wrong
Well, you haven't given any type information about the arguments that the function takes. I don't know what C++ defults too if you omit this information. ints? Your destructor doesn't need to take any arguments anyway (I don't even think destructors can take arguments).
5. destructor in main - what is wrong
You do not call the destructor yourself. These are called automatically when you delete the object.
edit: had accidentally put "*board=&a"
Okay, I'm just going to be blunt as hell.
You have no idea what the hell you are talking about. You don't know what classes or objects are, what they represent, or how to use them. And you're trying to shoehorn a paradigm that you don't understand onto existing code that you probably don't understand either.
GET A C++ BOOK. FOLLOW IT. START FROM THE BOTTOM.
Then come back when you have questions that make sense.
May I suggest this C++ book?
I learned from it, it has stuff about about classes and basic programming practices, such as the standard library. I highly suggest it.
Steve head hurt >_<
Steve smash steve go boom boom steve no likey
LennyLen thank you - you are very helpful - Can I give you some points for help ?
I read c++ books and now I want use my knowledge but it is now the most complicated programm which I want write so I want that you help me - do some of you have really to be such boorish ?
Your advices very help me understand classes, constructors, destructors and ALLEGRO - really .
I change my code - could you now what in this code is wrong ??
1 | #include<iostream> |
2 | #include<ctime> |
3 | #include <allegro.h> |
4 | using namespace std; |
5 | |
6 | |
7 | class boardboard |
8 | { |
9 | private: |
10 | BITMAP *board; |
11 | BITMAP *pawn; |
12 | BITMAP *buffer; |
13 | |
14 | public: |
15 | |
16 | boardboard (BITMAP a, BITMAP b, BITMAP c) //constructor |
17 | { |
18 | *board=&a; |
19 | *board=&b; |
20 | *board=&c; |
21 | |
22 | } |
23 | |
24 | |
25 | ~boardboard() //destructor |
26 | { |
27 | destroy_bitmap(board); |
28 | destroy_bitmap(pawn); |
29 | destroy_bitmap(buffer); |
30 | |
31 | } |
32 | |
33 | |
34 | |
35 | |
36 | void function () |
37 | { |
38 | int tab1[2][10]= { |
39 | {100,220,370,500,700,700,500,350,220,100}, |
40 | {100,100,100,100,100,340,340,340,340,340}, |
41 | }; |
42 | int tab2[10]={0,1,2,3,4,5,6,7,8,9}; |
43 | |
44 | |
45 | |
46 | |
47 | int *index=&tab2[0]; |
48 | int *zero=&tab2[0]; |
49 | int *nine=&tab2[9]; |
50 | int module; |
51 | int eyes; |
52 | srand (time(0)); |
53 | |
54 | while(!key[KEY_ESC]) |
55 | { |
56 | eyes = 1+rand()%3; |
57 | clear_to_color(buffer,makecol(255,255,255)); |
58 | blit(board,buffer,0,0,0,0,board->w,board->h); |
59 | blit(pawn,buffer,0,0,tab1[0][*index],tab1[1][*index],pawn->w,pawn->h); |
60 | textprintf_ex(buffer, font, 267, 600, makecol(0, 0, 0), -1, "You throw: %d", eyes); |
61 | textprintf_ex(buffer, font, 267, 620, makecol(0, 0, 0), -1, "-> - left, <- - right"); |
62 | textprintf_ex(buffer, font, 267, 640, makecol(0, 0, 0), -1, "ESC - the end of the game"); |
63 | blit(buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H); |
64 | readkey(); |
65 | |
66 | |
67 | |
68 | direction: |
69 | if (key[KEY_LEFT]) |
70 | { |
71 | if(*index==0||*index==1||*index==2||*index==3||*index==4) |
72 | { |
73 | if ((*index-eyes)>=*zero) |
74 | { |
75 | index=index-eyes; |
76 | } |
77 | else |
78 | { |
79 | |
80 | index=nine-(eyes-*index-1); |
81 | } |
82 | |
83 | } |
84 | else if(*index==5||*index==6||*index==7||*index==8||*index==9) |
85 | |
86 | { |
87 | if ((*index+eyes)<=*nine) |
88 | { |
89 | index=index+eyes; |
90 | } |
91 | else |
92 | { |
93 | module=eyes-(*nine-*index); |
94 | if (module<0) |
95 | { |
96 | module=-module; |
97 | } |
98 | index=zero+module-1; |
99 | } |
100 | |
101 | } |
102 | } |
103 | |
104 | |
105 | |
106 | |
107 | else if (key[KEY_RIGHT]) |
108 | { |
109 | if(*index==0||*index==1||*index==2||*index==3||*index==4) |
110 | { |
111 | index=index+eyes; |
112 | |
113 | } |
114 | else |
115 | { |
116 | index=index-eyes; |
117 | } |
118 | |
119 | |
120 | } |
121 | |
122 | |
123 | |
124 | else |
125 | { |
126 | if (!key[KEY_ESC]) |
127 | { |
128 | goto direction; |
129 | } |
130 | |
131 | } |
132 | |
133 | } |
134 | |
135 | |
136 | |
137 | } |
138 | }; |
139 | |
140 | |
141 | |
142 | |
143 | |
144 | |
145 | int main() |
146 | { |
147 | |
148 | allegro_init(); |
149 | install_keyboard(); |
150 | set_color_depth(32); |
151 | set_gfx_mode(GFX_AUTODETECT,1024,768,0,0); |
152 | buffer=create_bitmap(SCREEN_W,SCREEN_H); |
153 | |
154 | |
155 | boardboard something ("board.bmp","pawn.bmp",NULL); //constructor |
156 | something.boardboard::~boardboard(); //destructor |
157 | |
158 | allegro_exit(); |
159 | return 0; |
160 | } |
161 | END_OF_MAIN() |
If you could please post the errors, that would be good. I, for one, do not like to take the time to compile your code. I did with yours though, just for the heck of it. [Edit:] Yes, I did just contradict myself.
I spotted one problem: You did not declare your "buffer" bitmap before trying to set it to a value.
The other problems I cannot figure out, because I have no clue what boardboard is, and what the point of most of the functions are.
[EDIT2:]
I just noticed something... you are trying to use strings as bitmaps when creating a "boardboard". This cannot be done. If you want to load a bitmap, you would want to use this.
[EDIT3:] After more attempts to fix your code, I gave up.
Personally, I would re-write my code and use comments more often. It seems your code is very disorganized, and you don't even use the game loop function in your main function. (At least I don't think so.)
It's slightly amusing but probably more depressing, thats what it is!
kosmitek:
1) You should probably put the class declaration in its own header (.H) file and the implementation in a .CPP file:
something.h:
something.cpp:
1 | #include <iostream> |
2 | #include <ctime> |
3 | #include <allegro.h> |
4 | |
5 | using namespace std; |
6 | |
7 | |
8 | boardboard::boardboard(BITMAP a, BITMAP b, BITMAP c) |
9 | { |
10 | *board = &a; |
11 | *board = &b; |
12 | *board = &c; |
13 | } |
14 | |
15 | |
16 | boardboard::~boardboard() |
17 | { |
18 | destroy_bitmap(board); |
19 | destroy_bitmap(pawn); |
20 | destroy_bitmap(buffer); |
21 | } |
22 | |
23 | |
24 | void boardboard::function(void) |
25 | { |
26 | int tab1[2][10] = |
27 | { |
28 | {100, 220, 370, 500, 700, 700, 500, 350, 220, 100}, |
29 | {100, 100, 100, 100, 100, 340, 340, 340, 340, 340}, |
30 | }; |
31 | |
32 | int tab2[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; |
33 | |
34 | int *index = &tab2[0]; |
35 | int *zero = &tab2[0]; |
36 | int *nine = &tab2[9]; |
37 | int module; |
38 | int eyes; |
39 | |
40 | srand (time(0)); |
41 | |
42 | while (!key[KEY_ESC]) |
43 | { |
44 | eyes = 1 + rand() % 3; |
45 | clear_to_color(buffer, makecol(255, 255, 255)); |
46 | |
47 | blit(board, buffer, 0, 0, 0, 0, board->w, board->h); |
48 | blit(pawn, buffer, 0, 0, tab1[0][*index], tab1[1][*index], pawn->w, pawn->h); |
49 | |
50 | textprintf_ex(buffer, font, 267, 600, makecol(0, 0, 0), -1, "You throw: %d", eyes); |
51 | textprintf_ex(buffer, font, 267, 620, makecol(0, 0, 0), -1, "-> - left, <- - right"); |
52 | textprintf_ex(buffer, font, 267, 640, makecol(0, 0, 0), -1, "ESC - the end of the game"); |
53 | |
54 | blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H); |
55 | readkey(); |
56 | } |
57 | |
58 | :direction // for GOTO statement |
59 | |
60 | if (key[KEY_LEFT]) |
61 | { |
62 | if (*index >= 0 && *index <= 4) |
63 | { |
64 | if ((*index - eyes) >= *zero) |
65 | { |
66 | index -= eyes; |
67 | } |
68 | else |
69 | { |
70 | index = nine - (eyes - *index - 1); |
71 | } |
72 | } |
73 | else if (*index >= 5 && *index <= 9) |
74 | { |
75 | if ((*index + eyes) <= *nine) |
76 | { |
77 | index += eyes; |
78 | } |
79 | else |
80 | { |
81 | module = eyes - (*nine - *index); |
82 | |
83 | if (module < 0) |
84 | { |
85 | module = -module; |
86 | } |
87 | |
88 | index = zero + module - 1; |
89 | } |
90 | } |
91 | } |
92 | else if (key[KEY_RIGHT]) |
93 | { |
94 | if (*index >= 0 && *index <= 4) |
95 | { |
96 | index += eyes; |
97 | } |
98 | else |
99 | { |
100 | index -= eyes; |
101 | } |
102 | } |
103 | else |
104 | { |
105 | if (!key[KEY_ESC]) |
106 | { |
107 | goto direction; |
108 | } |
109 | } |
110 | } |
111 | |
112 | |
113 | int main() |
114 | { |
115 | allegro_init(); |
116 | install_keyboard(); |
117 | set_color_depth(32); |
118 | set_gfx_mode(GFX_AUTODETECT, 1024, 768, 0, 0); |
119 | buffer = create_bitmap(SCREEN_W, SCREEN_H); |
120 | |
121 | boardboard something ("board.bmp", "pawn.bmp", NULL); //constructor |
122 | something.boardboard::~boardboard(); //destructor |
123 | |
124 | allegro_exit(); |
125 | return 0; |
126 | } |
127 | |
128 | END_OF_MAIN() |
2) You should put your movement TEST routines in a regular function, not a class. You should keep your movement LOGIC routines in the class:
That would also take care of the need for the GOTO statement in there: it would be processed fine without ever needing that.
3) As Durnus said, you are trying to pass strings: boardboard something ("board.bmp", "pawn.bmp", NULL); into a function that is expecting a BITMAP pointer: boardboard(BITMAP a, BITMAP b, BITMAP c) (which should be <b>boardboard(BITMAP *a, BITMAP *b, BITMAP c), by the way.
You would want to change either:
1 | // Example 1: |
2 | |
3 | PALETTE pal; // Global |
4 | |
5 | int main() |
6 | { |
7 | ... |
8 | BITMAP *board; |
9 | BITMAP *pawn; |
10 | BITMAP *other; |
11 | |
12 | boardboard something(board, pawn, NULL); |
13 | ... |
14 | } |
15 | |
16 | boardboard::boardboard(BITMAP *a, BITMAP *b, BITMAP *c) |
17 | { |
18 | load_bitmap(a, pal); |
19 | load_bitmap(b, pal); |
20 | load_bitmap(c, pal); |
21 | } |
...or...
1 | // Example 2: |
2 | int main() |
3 | { |
4 | ... |
5 | boardboard something ("board.bmp", "pawn.bmp", ""); |
6 | } |
7 | |
8 | boardboard::boardboard(char *a, char *b, char *c) |
9 | { |
10 | if (a != NULL) |
11 | load_bitmap(a, pal); |
12 | if (b != NULL) |
13 | load_bitmap(b, pal); |
14 | if (c != NULL) |
15 | load_bitmap(c, pal); |
16 | |
17 | ... |
18 | } |
4) I'm not entirely certain what you're doing with all those pointers (tab1 and tab2), but you could DEFINITELY be able to simplify those, or get rid of them entirely (why have an array with the VALUES 0..9, instead of just using "0..9" as numbers?).
We went through this last thread... I don't have any idea what C++ book you read but you gotta find something better. I hope it's not a Sams Teach Yourself C++ in 24hr books either.
Look, the very fact that you're trying to impose an object-oriented design approach onto something that isn't designed that way in the first place is the clearest indicator we could possibly get that you just don't understand object-oriented programming. Get a different book, read it slowly, and follow the exercises if there are any.
May I suggest my previously suggested C++ book?
May I suggest this C++ book?
I learned from it, it has stuff about about classes and basic programming practices, such as the standard library. I highly suggest it.
As I have said, it doesn't include just classes, but basic C++ programming. (No offence intended with the basic stuff, but it is a very good book to learn classes from.
And as X-G said, you are trying to make classes for something that doesn't need them.
I hope it's not a Sams Teach Yourself C++ in 24hr books either.
Why, what's wrong with them? It's what got me started. And no-one's died yet
Ok, so my games never work, the PC resets with every recompile, and every time I get to the point where I can execute my program I need to re-install the windows. But otherwise it's fine.
Durnus - my programm shouldn't use classes, constructor and destructor ?? Why ??
And why I shouldn't use "goto" ??
I change my code
programme and it is compiling and gun, but is it in code everything okey ?
1 | |
2 | |
3 | #include<iostream> |
4 | #include<ctime> |
5 | #include <allegro.h> |
6 | using namespace std; |
7 | |
8 | |
9 | |
10 | |
11 | class boardboard |
12 | { |
13 | private: |
14 | BITMAP *board; |
15 | BITMAP *pawn; |
16 | BITMAP *buffer; |
17 | |
18 | public: |
19 | boardboard (BITMAP *a, BITMAP *b, BITMAP *c) //constructor |
20 | { |
21 | board=a; |
22 | pawn=b; |
23 | buffer=c; |
24 | function (a, b, c); |
25 | } |
26 | |
27 | |
28 | |
29 | ~boardboard() //destructor |
30 | { |
31 | destroy_bitmap; |
32 | |
33 | } |
34 | |
35 | |
36 | void function (BITMAP *a, BITMAP *b, BITMAP *c) |
37 | { |
38 | |
39 | c=create_bitmap(SCREEN_W,SCREEN_H); |
40 | a = load_bitmap("board.bmp",NULL); |
41 | b = load_bitmap("pawn.bmp",NULL); |
42 | int tab1[2][10]= { |
43 | {100,220,370,500,700,700,500,350,220,100}, |
44 | {100,100,100,100,100,340,340,340,340,340}, |
45 | }; |
46 | int tab2[10]={0,1,2,3,4,5,6,7,8,9}; |
47 | |
48 | int *index=&tab2[0]; |
49 | int *zero=&tab2[0]; |
50 | int *nine=&tab2[9]; |
51 | int module; |
52 | int eyes; |
53 | srand (time(0)); |
54 | |
55 | |
56 | while(!key[KEY_ESC]) |
57 | { |
58 | eyes = 1+rand()%3; |
59 | clear_to_color(c,makecol(255,255,255)); |
60 | blit(a,c,0,0,0,0,a->w,a->h); |
61 | blit(b,c,0,0,tab1[0][*index],tab1[1][*index],b->w,b->h); |
62 | textprintf_ex(c, font, 267, 600, makecol(0, 0, 0), -1, "You throw: %d", eyes); |
63 | textprintf_ex(c, font, 267, 620, makecol(0, 0, 0), -1, "-> - left, <- - right"); |
64 | textprintf_ex(c, font, 267, 640, makecol(0, 0, 0), -1, "ESC - the end of the game"); |
65 | blit(c,screen,0,0,0,0,SCREEN_W,SCREEN_H); |
66 | readkey(); |
67 | |
68 | |
69 | |
70 | direction: |
71 | if (key[KEY_LEFT]) |
72 | { |
73 | if(*index==0||*index==1||*index==2||*index==3||*index==4) |
74 | { |
75 | if ((*index-eyes)>=*zero) |
76 | { |
77 | index=index-eyes; |
78 | } |
79 | else |
80 | { |
81 | |
82 | index=nine-(eyes-*index-1); |
83 | } |
84 | |
85 | } |
86 | else if(*index==5||*index==6||*index==7||*index==8||*index==9) |
87 | |
88 | { |
89 | if ((*index+eyes)<=*nine) |
90 | { |
91 | index=index+eyes; |
92 | } |
93 | else |
94 | { |
95 | module=eyes-(*nine-*index); |
96 | if (module<0) |
97 | { |
98 | module=-module; |
99 | } |
100 | index=zero+module-1; |
101 | } |
102 | |
103 | } |
104 | } |
105 | |
106 | |
107 | |
108 | |
109 | else if (key[KEY_RIGHT]) |
110 | { |
111 | if(*index==0||*index==1||*index==2||*index==3||*index==4) |
112 | { |
113 | index=index+eyes; |
114 | |
115 | } |
116 | else |
117 | { |
118 | index=index-eyes; |
119 | } |
120 | |
121 | |
122 | } |
123 | |
124 | |
125 | |
126 | else |
127 | { |
128 | if (!key[KEY_ESC]) |
129 | { |
130 | goto direction; |
131 | } |
132 | |
133 | } |
134 | |
135 | } |
136 | |
137 | |
138 | |
139 | } |
140 | }; |
141 | |
142 | |
143 | |
144 | |
145 | |
146 | |
147 | |
148 | |
149 | |
150 | |
151 | |
152 | int main() |
153 | { |
154 | |
155 | allegro_init(); |
156 | install_keyboard(); |
157 | set_color_depth(32); |
158 | set_gfx_mode(GFX_AUTODETECT,1024,768,0,0); |
159 | |
160 | |
161 | boardboard bb(NULL, NULL, NULL); |
162 | |
163 | bb.boardboard::~boardboard(); |
164 | allegro_exit(); |
165 | return 0; |
166 | } |
167 | END_OF_MAIN() |
everything okay ??
PS. thanks TeamTerradactyl for your advices
This is a joke right? This is not even closely valid C++- - code! Did you ever try to compile it? Even if it would, by skimming through it, I can say it would crash like hell... Listen to people's advises or at least post the freaking compiler-errors!!
you shouldn't use classes, constructors and destructors because it seems you have no idea how to use them.
you shouldn't use goto because it's dated, makes your code hard to read, and there's is nothing which you can do with goto, which can't be done without goto in a more elegant way.
if you really read a c++-book (which i doubt), you should have come across a thing called while-loop. you could replace your goto with this statement.
also, passing BITMAPs to your function-function (by the way: nice naming), is senseless in your case, but passing NULL-pointers seems like even worse.
there is just so much wrong with your code, it's hard to make a start.
But what is wrong in this code:
(IN attachment you have all my program)
1 | #include<iostream> |
2 | #include<ctime> |
3 | #include <allegro.h> |
4 | using namespace std; |
5 | |
6 | |
7 | |
8 | |
9 | class boardboard |
10 | { |
11 | private: |
12 | BITMAP *board; |
13 | BITMAP *pawn; |
14 | BITMAP *buffer; |
15 | |
16 | public: |
17 | boardboard (BITMAP *a, BITMAP *b, BITMAP *c) //constructor |
18 | { |
19 | board=a; |
20 | pawn=b; |
21 | buffer=c; |
22 | function (a, b, c); |
23 | } |
24 | |
25 | |
26 | |
27 | ~boardboard() //destructor |
28 | { |
29 | destroy_bitmap(board); |
30 | destroy_bitmap(pawn); |
31 | destroy_bitmap(buffer); |
32 | |
33 | } |
34 | |
35 | |
36 | void function (BITMAP *a, BITMAP *b, BITMAP *c) |
37 | { |
38 | |
39 | c=create_bitmap(SCREEN_W,SCREEN_H); |
40 | a = load_bitmap("board.bmp",NULL); |
41 | b = load_bitmap("pawn.bmp",NULL); |
42 | int tab1[2][10]= { |
43 | {100,220,370,500,700,700,500,350,220,100}, |
44 | {100,100,100,100,100,340,340,340,340,340}, |
45 | }; |
46 | int tab2[10]={0,1,2,3,4,5,6,7,8,9}; |
47 | |
48 | int *index=&tab2[0]; |
49 | int *zero=&tab2[0]; |
50 | int *nine=&tab2[9]; |
51 | int module; |
52 | int eyes; |
53 | srand (time(0)); |
54 | |
55 | |
56 | while(!key[KEY_ESC]) |
57 | { |
58 | eyes = 1+rand()%3; |
59 | clear_to_color(c,makecol(255,255,255)); |
60 | blit(a,c,0,0,0,0,a->w,a->h); |
61 | blit(b,c,0,0,tab1[0][*index],tab1[1][*index],b->w,b->h); |
62 | textprintf_ex(c, font, 267, 600, makecol(0, 0, 0), -1, "You throw: %d", eyes); |
63 | textprintf_ex(c, font, 267, 620, makecol(0, 0, 0), -1, "-> - left, <- - right"); |
64 | textprintf_ex(c, font, 267, 640, makecol(0, 0, 0), -1, "ESC - the end of the game"); |
65 | blit(c,screen,0,0,0,0,SCREEN_W,SCREEN_H); |
66 | readkey(); |
67 | |
68 | |
69 | |
70 | direction: |
71 | if (key[KEY_LEFT]) |
72 | { |
73 | if(*index==0||*index==1||*index==2||*index==3||*index==4) |
74 | { |
75 | if ((*index-eyes)>=*zero) |
76 | { |
77 | index=index-eyes; |
78 | } |
79 | else |
80 | { |
81 | |
82 | index=nine-(eyes-*index-1); |
83 | } |
84 | |
85 | } |
86 | else if(*index==5||*index==6||*index==7||*index==8||*index==9) |
87 | |
88 | { |
89 | if ((*index+eyes)<=*nine) |
90 | { |
91 | index=index+eyes; |
92 | } |
93 | else |
94 | { |
95 | module=eyes-(*nine-*index); |
96 | if (module<0) |
97 | { |
98 | module=-module; |
99 | } |
100 | index=zero+module-1; |
101 | } |
102 | |
103 | } |
104 | } |
105 | |
106 | |
107 | |
108 | |
109 | else if (key[KEY_RIGHT]) |
110 | { |
111 | if(*index==0||*index==1||*index==2||*index==3||*index==4) |
112 | { |
113 | index=index+eyes; |
114 | |
115 | } |
116 | else |
117 | { |
118 | index=index-eyes; |
119 | } |
120 | |
121 | |
122 | } |
123 | |
124 | |
125 | |
126 | else |
127 | { |
128 | if (!key[KEY_ESC]) |
129 | { |
130 | goto direction; |
131 | } |
132 | |
133 | } |
134 | |
135 | } |
136 | |
137 | |
138 | |
139 | } |
140 | }; |
141 | |
142 | |
143 | |
144 | |
145 | |
146 | |
147 | |
148 | |
149 | |
150 | |
151 | |
152 | int main() |
153 | { |
154 | |
155 | allegro_init(); |
156 | install_keyboard(); |
157 | set_color_depth(32); |
158 | set_gfx_mode(GFX_AUTODETECT,1024,768,0,0); |
159 | |
160 | |
161 | boardboard bb(NULL, NULL, NULL); |
162 | |
163 | bb.boardboard::~boardboard(); |
164 | allegro_exit(); |
165 | return 0; |
166 | } |
167 | END_OF_MAIN() |
This programm guns very good so what is wrong
imaxcs you said:
"I can say it would crash like hell"
but it doesn't crah
All right, guys, let's just stop taking this thread seriously. Either it's a troll, or so blithely ignorant as to be beyond our help. We've already given the only correct response: Get a C++ book and follow it from the beginning to the end.
X-G I am not troll
I use your advices and thanks some of you my programm gun but what is in this programm wrong (without "goto") ??
X-G if you don't want help me you didn't write.
X-G my programm is almost good and you can only criticize me - you do not want even to say what is bad despite that my programme great run.
And "goto" must be because if somebody will press other button than: "->; <-" then program go to "direction" - and player will have press "->; <-" - the number of eyes will not change, if I use "continue" then player also will have press "->; <-; Esc" but the number of eyes will change - and I don't want that the number of eyes change until player press "->" or "<-".
kosmitek: Attached is a cleaned-up version of your code.
Since you really don't need to use a class/struct for this program, I have removed them. I also added a bunch of comments so you could read the changes I've made, or else what's going on in the program.
Thank you for posting your source code so I could see what your game was supposed to do.
You may want to convert your .BMP images into .PCX or another format: The filesize will be a LOT smaller (the BMP images are uncompressed, while PCX are).
I also changed this to WINDOWED mode instead of full-screen (my computer crashes a lot whenever it's in full-screen, for some reason), so if you need to change it back, you can do so.
I hope this helps.
I think that if you WANTED to use a class/struct, the best thing to do would be to have something that has a lot of different data: your "pawn" could be an animated tile, and so the pawn could be a class, or if you have several different "boards", you could make each of those with a separate class (like, one could have a wood background with the "tiles" a few inches apart, and another could have a stone background with the "tiles" in more of a circular pattern and different coordinates, etc.).
What you're doing right now doesn't really need a class, since that looks like you're trying to use your class as a substitute for the main body of your game.
Anyway, hope this helps (and others can take a look at the code and make sure I didn't leak a bunch of memory )
This site should be enough.
TeamTerradactyl thank you very much for your help and comments in the code.
I wanted use class, constructor and destructor because I want develop my game and for example: "destroy_bitmap" - it was ideal to use destructor etc.
But if I want to develop my game for example that if player will stand up on same field he will get money - so should I use now class, constructor and destructor in this code (because I want that my final version of game using classes, constructors, destructors etc.) ?
kosmitek,
Let's say that that little "pink pawn" that you had in your code was going to become a nice little animated sprite (like a swirling "rock" or a "chess piece" that occasionally moves or whatever).
+---+---+---+---+---+---+ | X | | X | | | | +---+---+---+---+---+---+ | | | | X | | | +---+---+---+---+---+---+
Say you had three pawns on the screen at once. Declaring the pawn as a class would allow each of the three pawns to have a different look, have different x/y coordinates, be facing different directions, etc. For those, it would be fine to use a class, since each pawn would need to keep track of similar data (direction facing, coordinates, etc) but all the data itself would be different (the top two are facing down, the bottom is facing up, so each would have a different "direction").
As an example of a usage of classes, my games have several bad guys/monsters. Each bad guy/monster keeps track of its current location, the direction is is trying to go, and its health. If I attack one of those monsters, its HP goes down slightly. When the monster dies, I don't need to keep track of that monster anymore and "delete" it. When it gets "deleted", the class destructor frees up all the data it was storing (like the BITMAP it was using, its location, etc.). In that case, it would be useful to have a class.
So kosmitek, if you could describe for us, what do you want to use a "class" for in your game? If you have only 1 pawn and 1 "game board", I don't think you would need to use a class.
I especially love how you all seem to think that "classes" is some kind of feature you "add to your code". THAT'S NOT HOW IT WORKS! Object-oriented programming is a design paradigm that you base your entire architecture on. This is why I'm sure you don't understand OOP.
TeamTerradactyl you are very very very helpful - thank you a lot.
So I want do this game:
Player throws bone and pawn moves about some number of fields on left or right ( player decides), every time when pawn stands up on field will be drawn event, for example: "you get 100 dollars" or "you lose 50% of your weapons", player can stand up on some field which is shop - there he can buy weapons but if he stand up on field which is shop and then he throw 2 eyes then he lose all his weapons. After 20 movements he will fight with monster so he must have many weapons, if he has too little weapons he will die.
So it is my plan of game - where I can apply here OOP ?
Kosmetek: Just out of curiosity how old are you?
kosmitek:
This sounds slightly similar to Monopoly, where each "field" has a different attribute (like "collect 200 dollars" or "You owe [some person] $50", etc.). If this is the case, then I would suggest making the "field" a class:
1 | class Field |
2 | { |
3 | public: |
4 | Field(const char *filename); |
5 | ~Field(); |
6 | |
7 | void setAttribute(const Attribute &newAttribute); |
8 | Attribute getAttribute(void); |
9 | |
10 | void draw(BITMAP *target, const int &x, const int &y, const float &rotate); |
11 | private: |
12 | BITMAP *pPicture; |
13 | Attribute pTheAttribute; // <-- You would have to define the "Attribute" class yourself |
14 | }; |
15 | |
16 | Field::Field(const char *filename) |
17 | { |
18 | pPicture = load_bitmap(filename, NULL); |
19 | } |
20 | |
21 | Field::~Field() |
22 | { |
23 | destroy_bitmap(pPicture); |
24 | pPicture = NULL; |
25 | } |
26 | |
27 | void Field::setAttribute(const Attribute &newAttribute) |
28 | { |
29 | pTheAttribute = newAttribute; |
30 | } |
31 | |
32 | Attribute Field::getAttribute(void) |
33 | { |
34 | return pTheAttribute; |
35 | } |
36 | |
37 | void draw(BITMAP *target, const int &x, const int &y, const float &rotate = 0.0) |
38 | { |
39 | if (rotate != 0.0) |
40 | rotate_sprite(target, pPicture, x, y, rotate); |
41 | else |
42 | draw_sprite(target, pPicture, x, y); |
43 | } |
Then, each "field" you land on can have its own attributes and pictures. This would make sense, since you would use several "field" objects where you would really only use a handful of "pawn" objects.
You would need to define several different "Attributes" that this class would be able to use: one would be a "shop" where you buy stuff, another would be a "thief" where you lose 50% of your weapons, etc. How you implement your "Attributes" would be up to you. But I hope this helps answer your question.
What a strange game, throwing bones and eyes...
Steve:
Bones are dice
Eyes (snake eyes): double 1's
ups - sorry - bones = dice
TeamTerradactyl thank you a lot - you must be very good programmer.
Your idea is too complicated for me. I would like to use in my game some simple class, constructor, destruktor and heirdom but I have no idea - maybe I should do two monsters and when player kill one monster then he heirdom his strength and then he fight with second monster but where can I use here simply class, constructor and destructor..... - class "monster" ??
kosmitek: Yes, the "monster" would be an excellent place to use a class. Another would be the "player" because you need to keep track of the player's position, health, strength, etc.
TeamTerradactyl has a heart of.. gold, i really want to see you change, kosmitek.
(note: It's Christmas)
To kosmitek:
'heirdom'? you probably mean inheritance...
It has nothing to do with "the player inherits the monster's money"
In the case of your game, one possible use of inheritance could be if monsters stay on the board:
"The player and the (visible) monsters ARE pawns on the board: They are represented by a BITMAP, and they are positioned on one of the cells. If these two informations (image and position) are "stored" in the parent class (ex: Pawn), you can code a function Pawn::draw() to draw them on the screen, this single function will work well for a Player or a Monster - anything that inherits Pawn.
This is only an example, certainly not THE solution for what you want to do. People here have much experience coding 2D objects moving on a background (I've just described 40% of the games on a.cc), but board games are much, much more difficult to design and code, and frankly there's no evident OO design there. If you've never DESIGNED something in object-oriented design, a board game is the worst choice to start...You will never complete the game, and you will not even learn why your design does not make your game go in the right direction.
If you want to progress in your game, structured programming is the way to go...until at least you completely understand how to use Allegro for a game, with some code for all initializations, a game loop, some code for logic, some other code for display, etc... and no memory messup.
I guess even 23yrold3yrold started out like Kosmitek... there may be hope
Only if he starts taking the only good advice in this thread and reads a damn book and starts from the beginning, and drops the notions he has ingrained in his head right now. He needs to start over, not be further confused and fed misinformation.
:>
Thank you for you advices, especially thank TeamTerradactyl. I do not thank "X-G", he only discouraged me and he write nothing helpful.
I have fast change my code as you say. Now I have simply class, late I will add here that if player stand on some field then something will done, I will also add class monster etc.
But can you say me that my fragments of code touch OOP are okey ? And why my destructor isn't okey ?
1 | #include<iostream> |
2 | #include<ctime> |
3 | #include <allegro.h> |
4 | using namespace std; |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | class player |
11 | { |
12 | private: |
13 | int money; |
14 | int health; |
15 | int weapon; |
16 | int power; |
17 | |
18 | |
19 | public: |
20 | player(int a, int b, int c, int d) |
21 | { |
22 | money=a; |
23 | health=b; |
24 | weapon=c; |
25 | power=d; |
26 | } |
27 | |
28 | int change_money(int x) |
29 | { |
30 | money=money+x; |
31 | return money; |
32 | } |
33 | |
34 | int change_health(int x) |
35 | { |
36 | health=health+x; |
37 | return health; |
38 | } |
39 | |
40 | |
41 | int change_weapon(int x) |
42 | { |
43 | weapon=weapon+x; |
44 | return weapon; |
45 | } |
46 | |
47 | |
48 | int change_power(int x) |
49 | { |
50 | power=power+x; |
51 | return power; |
52 | } |
53 | |
54 | // ~player(); // destructor - something is wrong ?? |
55 | |
56 | }; |
57 | |
58 | |
59 | |
60 | |
61 | |
62 | void start_game(BITMAP *board, BITMAP *pawn, BITMAP *buffer) |
63 | { |
64 | |
65 | int tab1[2][10]={ |
66 | {100,220,370,500,700,700,500,350,220,100}, |
67 | {100,100,100,100,100,340,340,340,340,340}, |
68 | }; |
69 | |
70 | int tab2[10]={0,1,2,3,4,5,6,7,8,9}; |
71 | |
72 | int *index=&tab2[0]; |
73 | int *zero=&tab2[0]; |
74 | int *nine=&tab2[9]; |
75 | int module; |
76 | int eyes; |
77 | srand (time(0)); |
78 | int i=21; |
79 | int y=1; |
80 | |
81 | player game(99, 99, 99, 99); |
82 | |
83 | while(!key[KEY_ESC]&&i!=0) |
84 | { |
85 | i--; |
86 | eyes = 1+rand()%3; |
87 | clear_to_color(buffer,makecol(255,255,255)); |
88 | blit(board,buffer,0,0,0,0,board->w,board->h); |
89 | blit(pawn,buffer,0,0,tab1[0][*index],tab1[1][*index],pawn->w,pawn->h); |
90 | textprintf_ex(buffer, font, 267, 580, makecol(0, 0, 0), -1, "You have: %d money, %d health, %d weapon and %d power.", game.change_money(y), game.change_money(y), game.change_weapon(y), game.change_power(y)); |
91 | textprintf_ex(buffer, font, 267, 600, makecol(0, 0, 0), -1, "You throw: %d, it left yo %d movements", eyes, i); |
92 | textprintf_ex(buffer, font, 267, 620, makecol(0, 0, 0), -1, "-> - left, <- - right"); |
93 | textprintf_ex(buffer, font, 267, 640, makecol(0, 0, 0), -1, "ESC - the end of the game"); |
94 | blit(buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H); |
95 | |
96 | |
97 | |
98 | |
99 | direction: |
100 | readkey(); |
101 | if (key[KEY_LEFT]) |
102 | { |
103 | if(*index>=0&&*index<=4) |
104 | { |
105 | if ((*index-eyes)>=*zero) |
106 | { |
107 | index=index-eyes; |
108 | } |
109 | else |
110 | { |
111 | |
112 | index=nine-(eyes-*index-1); |
113 | } |
114 | |
115 | } |
116 | else if(*index>=5&&*index<=9) |
117 | |
118 | { |
119 | if ((*index+eyes)<=*nine) |
120 | { |
121 | index=index+eyes; |
122 | } |
123 | else |
124 | { |
125 | module=eyes-(*nine-*index); |
126 | if (module<0) |
127 | { |
128 | module=-module; |
129 | } |
130 | index=zero+module-1; |
131 | } |
132 | |
133 | } |
134 | } |
135 | |
136 | |
137 | |
138 | |
139 | else if (key[KEY_RIGHT]) |
140 | { |
141 | if(*index>=0&&*index<=4) |
142 | { |
143 | index=index+eyes; |
144 | |
145 | } |
146 | else |
147 | { |
148 | index=index-eyes; |
149 | } |
150 | |
151 | |
152 | } |
153 | |
154 | |
155 | |
156 | else |
157 | { |
158 | if (!key[KEY_ESC]) |
159 | { |
160 | goto direction; |
161 | } |
162 | |
163 | } |
164 | |
165 | |
166 | |
167 | } |
168 | |
169 | //game.~player(); // destructor - something is wrong |
170 | |
171 | } |
172 | |
173 | |
174 | |
175 | |
176 | |
177 | |
178 | int main() |
179 | { |
180 | BITMAP *board = NULL; |
181 | BITMAP *pawn = NULL; |
182 | BITMAP *buffer = NULL; |
183 | allegro_init(); |
184 | install_keyboard(); |
185 | set_color_depth(32); |
186 | set_gfx_mode(GFX_AUTODETECT,1024,768,0,0); |
187 | buffer=create_bitmap(SCREEN_W,SCREEN_H); |
188 | |
189 | |
190 | board = load_bitmap("board.bmp",NULL); |
191 | pawn = load_bitmap("pawn.bmp",NULL); |
192 | |
193 | start_game(board, pawn, buffer); |
194 | |
195 | |
196 | destroy_bitmap(board); |
197 | destroy_bitmap(pawn); |
198 | destroy_bitmap(buffer); |
199 | allegro_exit(); |
200 | return 0; |
201 | } |
202 | END_OF_MAIN() |
And why my destructor isn't okey ?
That's only a declaration for your destructor. You either need to define the function elsewhere, or change the line to this:
~player() {}
As for X-G's comments, while he may be abrupt, he is correct. You really do need to get a better understanding of what you are doing. If you're learning from a book, and it has exercises, do them.
He is also correct in that you have not listened to advice that will make your code more readable to others.
I know I must read good book C++ and I will change my code according to your advices but later.
~player() {} - now I have declaration and definition of destructor in the class but I have one more question - I have also something wrong in activation of destructor in my function (second green text near the end of my code):
//game.~player(); // activation destructor - something is wrong
- what is wrong ?
PS. Thanks LennyLen.
DON'T CALL THE FRIGGIN DESTRUCTOR MANUALLY... there is a construct called "delete" use it.
delete game - it is bad and I think that we use "delete" when we allocate memory dynamically........
We are here to help you, we keep giving you the correct advice and yet you still keep to your own predisposed ideas about C and C++, I try to help, do what you will.
Ok if you declare your class as Game game(); then you won't have to call delete, if you delcare it as Game *game = new Game(); then you have to use delete. In your case since you are not using new then you won't have to worry about calling the destructor at all, that will be done automatically once the class goes out of scope.
kosmitek:
If at all possible, I would suggest using the code I uploaded here for you earlier. I'm not able to easily read your code, and I'm not entirely sure what's going on -- especially since I don't think you need things like:
int tab2[10]={0,1,2,3,4,5,6,7,8,9};
If you wanted an index of 0..4, and 5..9, just use literal numbers and not an array that point to "0..4, 5..9".
That being said, Steve Terry was correct: You never call the destructor itself:
game.~player();
You would do one of two things:
1) Nothing. When you hit the closing "}" for the main() loop, the object's "game.~player()" is called automatically. (This is nice, because you NEVER have to clean up your own code... it is all done automatically for you! )
2) If you have an array of these, and you did something like this:
player *thePlayer; thePlayer = new player(99, 99, 99, 99); ... delete thePlayer; // <-- You want to call DELETE, which calls the "~player()" destructor for you
Your "player" class IS correctly using classes, though: good job.
The only thing I would like to suggest, coming from a programmer's standpoint: It would be a little better if you were to use header files and keep your class declaration there, and then implement your code in a .CPP file. By doing this, it makes your code portable. If you don't do this, and someday you (or someone else) ever wants to use your class again, they're stuck with YOUR code... they can't simply change the parts they want (like, instead of setting money = a; health = b;, they wanted to use money = b; health = a;, etc.).
Also, with the code I posted for you earlier, the "goto" was removed: this was necessary, because otherwise, your program took up 100% of my CPU and was very "jerky". Having it in a function like I provided allowed the program to take up only 1%, and there was no "jerkiness".
You could VERY easily put your "player" class into the code I provided: the way that you rewrote this code (above) looks like you implemented "player" very smoothly, and it shouldn't be a problem.
<knitpick>Also, your code is harder to read because it's not indented consistently. You may want to give it a pass through either GNU indent or, my preference, aStyle (which is better for indenting/formatting C++).</knitpick>
<knitpick>
You never call the destructor itself
...unless using placement new.
</knitpick>
However, as willing to help as I am, I'm with X-G on that one. kosmitek: I highly suggest you stop here, restart and progress slowly with the guidance of a (few) book(s). The bare amount of new information for you is plainly overwhelming and, if anything, almost certainly only confuses you.
As previously said, OOP is not throwing a few keywords like "class" in, it's a way of thinking. After having grasped the concepts, you can go on and make use of the syntactic sugar that C++ provides over C.
Steve Terry and TeamTerradactyl thanks for your advice - so I won't declare destructor.
Only one question - now I have 1 object: player game(99, 99, 99, 99); and 4 components private:
1 | class player |
2 | { |
3 | private: |
4 | int money; |
5 | int health; |
6 | int weapon; |
7 | int power; |
8 | |
9 | |
10 | public: |
11 | player(int a, int b, int c, int d) |
12 | { |
13 | money=a; |
14 | health=b; |
15 | weapon=c; |
16 | power=d; |
17 | } |
18 | |
19 | int change_money(int x) |
20 | { |
21 | money=money+x; |
22 | return money; |
23 | } |
24 | |
25 | int change_health(int x) |
26 | { |
27 | health=health+x; |
28 | return health; |
29 | } |
30 | |
31 | |
32 | int change_weapon(int x) |
33 | { |
34 | weapon=weapon+x; |
35 | return weapon; |
36 | } |
37 | |
38 | |
39 | int change_power(int x) |
40 | { |
41 | power=power+x; |
42 | return power; |
43 | } |
Mayby better solution will be 4 objects
player a(99); player b(99); player c(99); player d(99);
and 1 component private:
1 | class player |
2 | { |
3 | private: |
4 | int something; |
5 | |
6 | |
7 | |
8 | public: |
9 | player(int x) |
10 | { |
11 | something=x; |
12 | |
13 | } |
14 | |
15 | int change_something(int y) |
16 | { |
17 | something=something+y; |
18 | return something; |
19 | } |
Which solution is more OOP ??
PS. TeamTerradactyl I don't understand all your sentences so tomorrow I will use english dictionary
The second example defeats the entire purpose of data structures (both in C and C++), which is to keep related data together.
Instead of just having one player object, which contains everything you need in one place, you now have four player objects, each of which is storing a something, that you have to keep track of yourself.
Neither example is inherently more OOP, but the second one is just wrong.
<knitpick>
... nitpick. Oh, the delicious irony!
<nitpick>
<knitpick>
... Oh, the delicious irony!
</nitpick>
X-G: Sorry; I'm too used to sewing and picking out "knitting" stitches (hence, "knit" and "pick").
Thank you LennyLen.
The most help me: TeamTerradactyl. Also help me: LennyLen and Steve Terry.
Can I give points for help me on this forum ?
Can I give points for help me on this forum ?
Yes, but only if you classified the thread as "A question or problem with a specific answer" when you created it.
Just because you don't like the answer doesn't mean it's not the correct/most helpful one.
X-G: "Just because you don't like the answer doesn't mean it's not the correct/most helpful one."
Indeed, Although i must say that i know the feeling you get when people nag your ideas, i have acted bullheaded many times on this forum, but as i'm not an experienced programmer i don't ignore what people say and i certainly don't get angry when people say i'm way of the road. You should listen more to what people say kosmitek, information, suggestions and criticism is never a bad thing.