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;
    }
}
William Hemsworth commented: Code tags on first post. +10

sorry found a mistake in my code.
line 56 should read box.y += yVel; instead
of box.y -= yVel; .

I was just testing something while making this thread and didnt realise when I copied the code over.
The same problem still persists.

So, are you trying to make the puk bounce when it hits the top or the bottom? If so, simply invert the Y-Velocity like this:

if ( box.y < 0 ) {
  yVel *= -1;
  box.y = 0;
}

else if ( box.y > curLvlHeight - PUK_HEIGHT ) {
  yVel *= -1;
  box.y = curLvlHeight - PUK_HEIGHT;
}

Hope this helps.

Hey, thanks for your help...it works!!!!!!!

Thank you, yeh basically I just wanted the puk bounce back whenever it hit the top or bottom but my code seemed to only allow the puk to bounce off the top or bottom once. But thanks to you, the puk can now do multiple bounces. And with that the game is finished. It was the last bug that I was trying to fix.

Cheers for that.

commented: :] +10
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.