0

I'm trying to get the ball to collide with all of the walls of the window. My current code works for the first 3 seconds of gameplay but then the ball will jiggle through a wall. I'll show you the code I currently have and any help in the right direction would be greatly appreciated.

// balls movement
#define b_move drand48()

// balls y velocity
double y_velocity = 2.0;

// balls x velocity
double x_velocity = 2.0;

int main(void)

while(lives > 0 && bricks > 0)
{
    // ball movement up and down
    move(ball, b_move, y_velocity);

    // collision for top and bottom of window  
    if (getY(ball) + getHeight(ball) >= getHeight(window))
    {
        y_velocity = -y_velocity;
    }

    else if (getY(ball) <= 0)
    {
        y_velocity = -y_velocity;
    }

    // ball movement left and right
    move(ball, x_velocity, b_move);

    //collision for left and right of window
    if (getX(ball) + getWidth(ball) >= getWidth(window))
    {
        x_velocity = -x_velocity;
    }

    else if (getX(ball) <= 0)
    {
        x_velocity = -x_velocity;
    }

    pause(10);
}

I just can't see what I need to change :(

0

I fixed my problem by getting rid of "b_move" and changing drand48() to actual ints. I also combined my if and if else statements into a single if statement by using "||"

| improve this answer | |
  • Glad you figured it out yourself! That's great, and so is answering your own question! But could you please accept your own answer so that the question gets removed from the unanswered question pool? Otherwise it just becomes an abandoned question. Let's keep up on forum housekeeping. thanks. ;-) – Cliff B May 26 '15 at 19:57
  • Thanks, I've already tried but it says I can only accept my own answer after 2 days. So if in 2 days I remember to do it then I will! – brutalitea May 26 '15 at 20:01
  • cool. I forgot about that two day restriction. – Cliff B May 26 '15 at 20:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .