This is my first post on these forums and I am starting to learn how to program.
I have tried to write pong from scratch using the Allegro game engine and MSVS C++ 6.0. Although I am writing it from scratch, I am using the example code on cppgameprogramming.com as a look-to if I have any problems. So far I have drawn the 2nd paddle and am trying to make it move. I have the if statements for the up and down key that increment the y variable for the paddle down and up respectively. What I dont understand is that when I press the key up it draws a line the same thickness of the paddle to the top of the screen. Same happens with the down key except the line goes to the bottom of the screen. What could be causing this? Could it be that in the example code there are also temp x and y variables that are equal to the x and y variables? These draw the same paddle. If this is the case could someone please explain what is the purpose of drawing the same thing twice. Any help would be appreciated :).
Here is my move_paddle2 function:
void move_paddle2(){
acquire_screen();
rectfill( screen, pad2x, pad2y, pad2x-5 , pad2y+100, makecol( 0, 0, 255)); //draw paddle2
release_screen();
if (key[KEY_UP] && pad2y>0)
{
--pad2y;
}//end if
else if (key[KEY_DOWN] && pad2y>0)
{
++pad2y;
}//end if
}//end move_paddle2
Temp x and temp y problems
I'm not sure if I fully understand, but it sounds like you are seeing the accumulation of several frames. Are you clearing the screen between draw calls? If not, the paddles will act more like pens, as you describe.
I would suggest some code to repair the problem, but I'm not sure if your render loop is double-buffered.
Admiral
I would suggest some code to repair the problem, but I'm not sure if your render loop is double-buffered.
Admiral
No it is not double buffered. Problem is, I don't know what that is. Does the temp x and y have something to do with it?
Quote:Original post by Dmitry 151
No it is not double buffered. Problem is, I don't know what that is. Does the temp x and y have something to do with it?
I can't help with the problem, because I'm not familiar with Allegro.
However, double buffering avoids graphical artefacts such as 'tearing' by not drawing directly to the screen. Instead, you draw to a 'backbuffer'. When it is time to draw a new frame, the backbuffer is blitted (as one) to the front buffer (the screen). You then draw a new backbuffer.
Hi,
This is the code from the example, which would suggest the second part of your IF statement is wrong.
Also the KEY check is before any drawing.
I would think YOUR code should be :
Also you are missing the 'First' rectfill statement, which looking at the original source, suggests it is drawing a black rectangle, to cover/blank out the old one as you move, and the 'Second' rectfill is drawing the NEW paddle on the screen.
[Edited by - darren_mfuk on April 8, 2007 11:07:07 AM]
This is the code from the example, which would suggest the second part of your IF statement is wrong.
Also the KEY check is before any drawing.
[source lang = "cpp"]void p2Move(){ p2_tempY = p2_y; if( key[KEY_UP] && p2_y > 0){ --p2_y; } else if( key[KEY_DOWN] && p2_y < 420){ ++p2_y; } acquire_screen(); rectfill( buffer, p2_tempX, p2_tempY, p2_tempX + 10, p2_tempY + 60, makecol ( 0, 0, 0)); rectfill( buffer, p2_x, p2_y, p2_x + 10, p2_y + 60, makecol ( 0, 0, 255)); release_screen(); }
I would think YOUR code should be :
void move_paddle2(){ if (key[KEY_UP] && pad2y > 0) { --pad2y; }//end if else if (key[KEY_DOWN] && pad2y < 420) { ++pad2y; }//end if acquire_screen(); rectfill( screen, pad2x, pad2y, pad2x + 5 , pad2y + 100, makecol( 0, 0, 255)); //draw paddle2 release_screen();}//end move_paddle2
Also you are missing the 'First' rectfill statement, which looking at the original source, suggests it is drawing a black rectangle, to cover/blank out the old one as you move, and the 'Second' rectfill is drawing the NEW paddle on the screen.
[Edited by - darren_mfuk on April 8, 2007 11:07:07 AM]
I have changed the if statement it now reads as follows:
void move_paddle1(){
pad1y=pad1tempy;
if (key[KEY_W] && pad1y>0) // if the W key is pressed, move paddle 1 up
{
--pad1y;
}//end if
else if (key[KEY_S] && pad1y<420) // if the s key is pressed, move paddle 1 down
{
++pad1y;
}//end if
acquire_screen();
rectfill(buffer, pad1tempx, pad1tempy, pad1x-5,pad1tempy+60, makecol(0,0,0)); //draw paddle1
rectfill(buffer, pad1x, pad1y, pad1x-5,pad1y+60, makecol(0,0,255)); //draw paddle 1
release_screen();
}
I have created an empty bitmap called buffer as in the tutorial. My main statement is this:
int main(){
allegro_init();
install_keyboard();
set_color_depth(16);
set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);
buffer=create_bitmap(640,480);//makes buffer the size of the screen
setUpGame(); //sets up game
while( !key[KEY_ESC]){//include game fuctions
clear_to_color(buffer,makecol(0,0,0));
move_paddle1();
move_paddle2();
}//end while
return 0;
}//end main
I have gotten the paddles to clear themselves without the double buffer. The flickered a lot so I decided to try double buffering. Now the problem is that the program draws the paddles fine but is not responding to input. What could be wrong? move_paddle2() is basically the same as move_paddle1().
void move_paddle1(){
pad1y=pad1tempy;
if (key[KEY_W] && pad1y>0) // if the W key is pressed, move paddle 1 up
{
--pad1y;
}//end if
else if (key[KEY_S] && pad1y<420) // if the s key is pressed, move paddle 1 down
{
++pad1y;
}//end if
acquire_screen();
rectfill(buffer, pad1tempx, pad1tempy, pad1x-5,pad1tempy+60, makecol(0,0,0)); //draw paddle1
rectfill(buffer, pad1x, pad1y, pad1x-5,pad1y+60, makecol(0,0,255)); //draw paddle 1
release_screen();
}
I have created an empty bitmap called buffer as in the tutorial. My main statement is this:
int main(){
allegro_init();
install_keyboard();
set_color_depth(16);
set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);
buffer=create_bitmap(640,480);//makes buffer the size of the screen
setUpGame(); //sets up game
while( !key[KEY_ESC]){//include game fuctions
clear_to_color(buffer,makecol(0,0,0));
move_paddle1();
move_paddle2();
}//end while
return 0;
}//end main
I have gotten the paddles to clear themselves without the double buffer. The flickered a lot so I decided to try double buffering. Now the problem is that the program draws the paddles fine but is not responding to input. What could be wrong? move_paddle2() is basically the same as move_paddle1().
Quote:Original post by Dmitry 151
Now the problem is that the program draws the paddles fine but is not responding to input. What could be wrong?
The first instruction of this function resets pad1y to the effectively constant pad1tempy. Subsequent changes to pad1y are destroyed next time the function iterates, so it never really changes.
Here's a new draft, with a little effort to reduce flicker while stationary.
void move_paddle1(){ pad1tempy = pad1y; bool has_moved = false; if (key[KEY_W] && pad1y > 0) { // If the W key is pressed, move paddle 1 up --pad1y; has_moved = true; } else if (key[KEY_S] && pad1y < 420) { // If the s key is pressed, move paddle 1 down ++pad1y; has_moved = true; } // Now pad1tempy hold the old y coordinate // and pad1y contains the current coordinate acquire_screen(); if (has_moved) { // This is a slight flicker-reducing measure // The paddle will only be cleared if it has moved rectfill(buffer, pad1tempx, pad1tempy, pad1tempx - 5, pad1tempy + 60, makecol(0,0,0)); // Clear paddle 1 } rectfill(buffer, pad1x, pad1y, pad1x-5,pad1y+60, makecol(0,0,255)); // Draw paddle 1 release_screen();}
Normally, I'd go into detail about why your naming conventions and use of global variables are verging on criminal, but I'll refrain for the moment. This is the beginners' forum, after [smile].
Admiral
Thank you all very much for your help. Especially darren_mfuk because you helped me understand why in the world anyone would want to draw a black rectangle no one could see. Turns out I did the doulbe buffer wrong. When I drew the buffer I drew it outside the while(!key[KEY_ESC]) loop. This meant that when the x and y coordinates were updated the screen itself was not. All I did was add a draw_bitmap(screen, buffer, 0, 0) command to one of the move paddles.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement