Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Help with class

Credits go to kikabo for helping out!
This thread is locked; no one can reply to it. rss feed Print
Help with class
Money
Member #6,730
December 2005
avatar

hey, when i draw the first paddle object it shows, but it doens't when i do the second

-startClassic.cpp-

1#include <allegro.h>
2#include "startGame.h"
3#include "main.h"
4#include "paddle.h"
5//#include "ball.h"
6 
7 
8 
9 
10Paddle *p1 = new Paddle();
11Paddle *p2 = new Paddle();
12 
13 
14 
15 
16void startClassic(void)
17{
18 
19 
20 
21 
22 
23 
24 while(!key[KEY_ESC])
25 {
26 clear_bitmap(buffer);
27 blit(title,buffer,0,0,0,0,640,480);
28 textout_ex(buffer,font,"Music: On/Off",500,20,DARK_BLUE,0);
29 rect(buffer,5,5,5,475,BLUE);//left wall
30 rect(buffer,635,5,635,475,BLUE);//right wall
31 rect(buffer,5,475,635,475,BLUE);//bottom wall
32 rect(buffer,5,5,635,5,BLUE);//top wall
33 rect(buffer,(SCREEN_W/2),5,(SCREEN_W/2),475,BLUE);//screen divider
34 p1->setCoords(30,30);
35 p1->drawPaddle(pBit);
36 p1->getInput();
37 p2->setCoords(50,50);
38 p2->drawPaddle(pBit);
39 
40 
41 
42 blit(buffer,screen,0,0,0,0,640,480);
43 }
44 
45 
46}

-paddle.h--

1#include <allegro.h>
2#include "main.h"
3 
4 
5 
6 
7class Paddle
8{
9 public:
10 
11 Paddle(){};
12 ~Paddle();
13 
14 
15 void drawPaddle(BITMAP *b);
16 void movePaddle_up(void);
17 void movePaddle_down(void);
18 int get_y(void);
19 void getInput(void);
20 void setCoords(int temp_x, int temp_y);
21 void set_y_coord(int y_temp){ pos_y = y_temp;}
22 void drawPaddle(BITMAP *b,int temp_pos_y);
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 private:
33 int pos_x;
34 int pos_y;
35 
36 
37 
38};
39 
40void Paddle::drawPaddle(BITMAP *b)
41{
42 masked_blit(b,buffer,0,0,pos_x,pos_y,640,480);
43}
44 
45void Paddle::movePaddle_up(void)
46{
47 if(true)
48 {
49 
50 clear_bitmap(buffer);
51 blit(title,buffer,0,0,0,0,640,480);
52 drawPaddle(pBit,get_y()-5);
53 rect(buffer,5,5,5,475,BLUE);//left wall
54 rect(buffer,635,5,635,475,BLUE);//right wall
55 rect(buffer,5,475,635,475,BLUE);//bottom wall
56 rect(buffer,5,5,635,5,BLUE);//top wall
57 rect(buffer,(SCREEN_W/2),5,(SCREEN_W/2),475,BLUE);//screen divider
58 draw_sprite(buffer, pBit, pos_x, pos_y);
59 blit(buffer,screen,0,0,0,0,640,480);
60 
61 
62 }
63 
64}
65 
66void Paddle::movePaddle_down(void)
67{
68 if(true)
69 {
70 
71 clear_bitmap(buffer);
72 blit(title,buffer,0,0,0,0,640,480);
73 drawPaddle(pBit,(get_y())+5);
74 rect(buffer,5,5,5,475,BLUE);//left wall
75 rect(buffer,635,5,635,475,BLUE);//right wall
76 rect(buffer,5,475,635,475,BLUE);//bottom wall
77 rect(buffer,5,5,635,5,BLUE);//top wall
78 rect(buffer,(SCREEN_W/2),5,(SCREEN_W/2),475,BLUE);//screen divider
79 draw_sprite(buffer, pBit, pos_x, pos_y);
80 blit(buffer,screen,0,0,0,0,640,480);
81 
82 }
83 
84 
85}
86 
87int Paddle::get_y()
88{
89 return pos_y;
90}
91 
92void Paddle::getInput(void)
93{
94 while(!key[KEY_ESC])
95 {
96 while(key[KEY_DOWN])
97 {
98 Paddle::movePaddle_down();
99 }
100 while(key[KEY_UP])
101 {
102 Paddle::movePaddle_up();
103 }
104 
105 
106 
107 
108 
109 
110 blit(buffer,screen,0,0,0,0,640,480);
111 }
112}
113 
114void Paddle::setCoords(int temp_x,int temp_y)
115{
116 pos_x = temp_x;
117 pos_y = temp_y;
118}
119 
120void Paddle::drawPaddle(BITMAP *b,int temp_pos_y)
121{
122 pos_y = temp_pos_y;
123 masked_blit(b,buffer,0,0,pos_x,pos_y,640,480);
124}

kikabo
Member #3,679
July 2003
avatar

take this out of you Paddle::getInput class

 blit(buffer,screen,0,0,0,0,640,480);

Also, it's strange to have this between draw functions

            p1->setCoords(30,30);
            p1->drawPaddle(pBit);
            p1->getInput();
            p2->setCoords(50,50);
            p2->drawPaddle(pBit);

Generally start by grouping all logic functions, then draw, then blit

edit:
Ah, looking at the rest of it, you need to take all drawing out of your input routines (keep the logic separate by just updating your x_pos, y_pos variables), then have one draw routine at the end.

Money
Member #6,730
December 2005
avatar

uhh, i have to have that blit funciton in, other wise it won't show the main game area unless i press enter

[edit]
i go tit, i had to change the while loop in getINput to an if statement, thanks :)

kikabo
Member #3,679
July 2003
avatar

Well you already have a blit at the end of your main loop

1 p1->setCoords(30,30); // moved initialization
2 p2->setCoords(50,50);
3 
4 while(!key[KEY_ESC])
5 {
6 // logic
7 
8 p1->getInput();
9 p2->getInput(); // added
10 
11 // drawing
12 
13 clear_bitmap(buffer);
14 blit(title,buffer,0,0,0,0,640,480);
15 textout_ex(buffer,font,"Music: On/Off",500,20,DARK_BLUE,0);
16 rect(buffer,5,5,5,475,BLUE);//left wall
17 rect(buffer,635,5,635,475,BLUE);//right wall
18 rect(buffer,5,475,635,475,BLUE);//bottom wall
19 rect(buffer,5,5,635,5,BLUE);//top wall
20 rect(buffer,(SCREEN_W/2),5,(SCREEN_W/2),475,BLUE);//screen divider
21 
22 p1->drawPaddle(pBit);
23 p2->drawPaddle(pBit);
24 
25 blit(buffer,screen,0,0,0,0,640,480);
26 }

soon you will need to look at timing so that it will run at the same speed on all computers but first you need to separate logic, then have a search on these forums for timing

edit: I'm too slow for this, I've seen your reply now :)
oops, yes, they should be ifs

Money
Member #6,730
December 2005
avatar

gah, man something happened, this is my new code, i had to have a while instead of an if statement ingetInput because if there was an if, it would jsut move down on input then return back to position once you let go of key

//startClassic.cpp

1#include <allegro.h>
2#include "startGame.h"
3#include "main.h"
4#include "paddle.h"
5//#include "ball.h"
6 
7 
8 
9 
10Paddle *p1 = new Paddle();
11Paddle *p2 = new Paddle();
12 
13 
14 
15 
16void startClassic(void)
17{
18 
19 
20 
21 
22 
23 
24 while(!key[KEY_ESC])
25 {
26 clear_bitmap(buffer);
27 blit(title,buffer,0,0,0,0,640,480);
28 /*while(key[KEY_B]){pauseGame();}*/
29 rect(buffer,5,5,5,475,BLUE);//left wall
30 rect(buffer,635,5,635,475,BLUE);//right wall
31 rect(buffer,5,475,635,475,BLUE);//bottom wall
32 rect(buffer,5,5,635,5,BLUE);//top wall
33 rect(buffer,(SCREEN_W/2),5,(SCREEN_W/2),475,BLUE);//screen divider
34 p1->setCoords(8,25);
35 p1->setBitmap(paddle1);
36 p1->drawPaddle();
37 p1->getInput();
38 p2->setCoords(50,50);
39 p2->setBitmap(paddle2);
40 p2->drawPaddle();
41 p2->getInput();
42 
43 
44 
45 
46 blit(buffer,screen,0,0,0,0,640,480);
47 }
48 
49delete(p1);
50delete(p2);
51}

//paddle.h

1#include <allegro.h>
2#include "main.h"
3 
4 
5 
6 
7class Paddle
8{
9 public:
10 
11 Paddle(){};
12 ~Paddle(){};
13 
14 
15 
16 void drawPaddle(void);
17 void movePaddle_up(void);
18 void movePaddle_down(void);
19 int get_y(void);
20 void getInput(void);
21 void setCoords(int temp_x, int temp_y);
22 void set_y_coord(int y_temp){ pos_y = y_temp;}
23 void drawPaddle(BITMAP *b,int temp_pos_y);
24 void setBitmap(BITMAP *temp_bit);
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 private:
38 BITMAP *pBit; //our access bitmap
39 int pos_x;
40 int pos_y;
41 
42 
43 
44};
45 
46void Paddle::drawPaddle()
47{
48 masked_blit(pBit,buffer,0,0,pos_x,pos_y,640,480);
49}
50 
51void Paddle::movePaddle_up(void)
52{
53 if(true)
54 {
55 
56 clear_bitmap(buffer);
57 blit(title,buffer,0,0,0,0,640,480);
58 drawPaddle(pBit,get_y()-5);
59 rect(buffer,5,5,5,475,BLUE);//left wall
60 rect(buffer,635,5,635,475,BLUE);//right wall
61 rect(buffer,5,475,635,475,BLUE);//bottom wall
62 rect(buffer,5,5,635,5,BLUE);//top wall
63 rect(buffer,(SCREEN_W/2),5,(SCREEN_W/2),475,BLUE);//screen divider
64 draw_sprite(buffer, pBit, pos_x, pos_y);
65 blit(buffer,screen,0,0,0,0,640,480);
66 
67 
68 }
69 
70}
71 
72void Paddle::movePaddle_down(void)
73{
74 if(true)
75 {
76 
77 clear_bitmap(buffer);
78 blit(title,buffer,0,0,0,0,640,480);
79 drawPaddle(pBit,(get_y())+5);
80 if(pos_y == 375){drawPaddle(pBit,200);}
81 rect(buffer,5,5,5,475,BLUE);//left wall
82 rect(buffer,635,5,635,475,BLUE);//right wall
83 rect(buffer,5,475,635,475,BLUE);//bottom wall
84 rect(buffer,5,5,635,5,BLUE);//top wall
85 rect(buffer,(SCREEN_W/2),5,(SCREEN_W/2),475,BLUE);//screen divider
86 draw_sprite(buffer, pBit, pos_x, pos_y);
87 blit(buffer,screen,0,0,0,0,640,480);
88 
89 }
90 
91 
92}
93 
94int Paddle::get_y()
95{
96 return pos_y;
97}
98 
99void Paddle::getInput(void)
100{
101 while(!key[KEY_ESC])
102 {
103 while(key[KEY_S])
104 {
105 Paddle::movePaddle_down();
106 }
107 while(key[KEY_W])
108 {
109 Paddle::movePaddle_up();
110 }
111 
112 
113 
114 blit(buffer,screen,0,0,0,0,640,480);
115 
116 }
117}
118 
119void Paddle::setCoords(int temp_x,int temp_y)
120{
121 pos_x = temp_x;
122 pos_y = temp_y;
123}
124 
125void Paddle::drawPaddle(BITMAP *b,int temp_pos_y)
126{
127 pos_y = temp_pos_y;
128 masked_blit(b,buffer,0,0,pos_x,pos_y,640,480);
129}
130void Paddle::setBitmap(BITMAP *temp_bit)
131{
132 pBit = temp_bit;
133}

i have if(pos_y == 375){drawPaddle(pBit,200);} in paddle.h works, but when i try if(pos_y == 375){drawPaddle(pBit,375);} that doesn't work, i tried setting the boundary for the paddle so if it tried to go past a point it couldn't but anything before like 315 or something like that works

oh by the way, i'll put the timer function in main.cpp

//main.cpp

1#include <allegro.h>
2#include "menu.h"
3#include "main.h"
4 
5 
6 
7 
8 
9 
10 
11BITMAP *buffer;
12BITMAP *title;
13BITMAP *htp;
14BITMAP *paddle1;
15BITMAP *paddle2;
16 
17 
18 
19 
20 
21int main(void)
22{
23 /*>>>>Initialization<<<<<<*/
24 allegro_init();
25 set_color_depth(24);
26 set_gfx_mode( GFX_AUTODETECT,640,480,0,0);
27 install_keyboard();
28 install_mouse();
29 
30 //load our resources
31 buffer = create_bitmap(640,480); //create our buffer to draw everything to
32 htp = load_bitmap("htp.bmp",NULL); //load how to play page
33 title= load_bitmap("stars.bmp",NULL);//load title background
34 paddle1= load_bitmap("paddle.bmp",NULL);
35 paddle2= load_bitmap("paddle.bmp",NULL);
36 
37 
38 
39 /*>>>Game Loop<<<*/
40 while(true)
41 {
42 displayMenu();
43 blit(buffer,screen,0,0,0,0,640,480);
44 }
45 
46destroy_bitmap(buffer);
47destroy_bitmap(title);
48destroy_bitmap(htp);
49destroy_bitmap(paddle1);
50destroy_bitmap(paddle2);
51return 0;
52}
53END_OF_MAIN()

but my main problem is that in my startClassic.cpp, it doesn't draw the second paddle at the specified position. please help

Audric
Member #907
January 2001

Quote:

i had to have a while instead of an if statement ingetInput because if there was an if, it would jsut move down on input then return back to position once you let go of key

That's the mother of bad reasons... Notice your game now stays stuck in p1.getInput(), until you press ESC: this is a mistake. And because you put drawing code in movePaddle*, which is a mistake as well, you can see and move ONE paddle...
You should really put in each method only the relevant code..
The draw method draws on buffer, the update method changes the coordinates (variables) depending on controls.

Thomas Fjellstrom
Member #476
June 2000
avatar

I have to point out that I spent hours with Money here in the #allegro IRC channel, as did several others on separate occasions, and nothing seems to get through to him, I even gave him a fully working basic prototype that I wanted him to look at, and learn from, and then explain to me how it worked, after that, he got all mad and said "I dont have to waste my time, blah blah blah", and at that point I'd had it, if he couldn't listen, and just got mad at me for trying to help him, I decided that that was the last time I'd ever off him help. All the other #allegro regulars already made that decision days before I did.

But hey, if you think you can help him, go for it. Though you're just wasting your time.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Go to: