Hi there,
This is my first post and I am quite new to developing games.
I have only made tic tac toe and currently making a pong clone,
using c++ and SDL.
I seem to be having a problem with the animation of my ball (or puk as I call it). The animation works fine...except for when it has a second encounter with either the top or bottom of the screen. I have coded so that when the puk encounters the y=0 axis(top) it bounces back on to the screen and also similar code for when it encounters with the bottom of the screen.
The problem I am having however is that if a second encounter with the top or bottom of the screen occurs instead of bouncing back the puk simply vanishes and a point goes to the appropriate player. I have tried to enclose that part of the code in a loop but it just froze the application.
I would appreciate any thoughts/suggestions on the matter. I have enclosed the function from my puk class that deals with the puks motion below.
Thanks!
void Puk::move(){
//Move the Puk left or right********************************
box.x += xVel;
//If the Puk went too far to the left reset puk at player 2 position
if(box.x < 0){
box.x -= xVel;
init((curLvlWidth - 6*PUK_WIDTH), (curLvlHeight - PUK_HEIGHT)/2, curLvlWidth, curLvlHeight);
xVel = 0;
yVel = 0;
status = PUK_LEFT;
score_p2++;
//re-initialise npc bat
npc_bat.init( (curLvlWidth- 4*BAT_WIDTH), (curLvlHeight - BAT_HEIGHT)/2, curLvlWidth, curLvlHeight);
npc_bat.set_vel(0, 0);
}
//If the Puk went too far to the right reset puk at player 1 position
if( (box.x + PUK_WIDTH) > curLvlWidth ){
box.x -= xVel;
init(6*PUK_WIDTH, (curLvlHeight - PUK_HEIGHT)/2, curLvlWidth, curLvlHeight);
xVel = 0;
yVel = 0;
status = PUK_RIGHT;
score_p1++;
//re-initialise npc bat
npc_bat.init( (curLvlWidth- 4*BAT_WIDTH), (curLvlHeight - BAT_HEIGHT)/2, curLvlWidth, curLvlHeight);
npc_bat.set_vel(0, 0);
}
//Checks if collision with player 2 bat occured and if true, then bounce puk back
if(Check_Collision(npc_bat, my_puk) == true){
Mix_PlayChannel( -1, collision, 0 );
box.x -= xVel;
xVel = 0;
yVel = random_yvel + npc_bat.get_yvel() ;
xVel -= PUK_HEIGHT;
box.x += xVel;
status = PUK_LEFT;
}
//Checks if collision with player 1 bat occured and if true, then bounce puk back
if(Check_Collision(player1_bat, my_puk) == true ){
Mix_PlayChannel( -1, collision, 0 );
box.x -= xVel;
xVel = 0;
yVel = random_yvel + player1_bat.get_yvel() ;
xVel += PUK_HEIGHT;
box.x += xVel;
//box.y += yVel;
status = PUK_RIGHT;
}
//Move the Puk up or down*********************************
box.y -= yVel;
//If the Puk went too far up or down
if(box.y < 0){
//move back
yVel -= PUK_HEIGHT;
box.y -= yVel;
}
if((box.y + PUK_HEIGHT) > curLvlHeight){
//move back
yVel += PUK_HEIGHT;
box.y -= yVel;
}
}