2

Just before finishing a game of Breakout, the game start to move very slow and the paddle behaves weirdly. The game works fine for the first 40 bricks. What causes this?

This is my code:

GObject object = detectCollision(window, ball);

if (object != NULL)
{
    // bounce on paddle
    if (object == paddle)
    {
        if (y > 0)
            y = -y;
    }

    // bounce on bricks
    else if (strcmp(getType(object), "GRect") == 0)
    {
        if (y < 0)
        {
            y = -y;
            removeGWindow(window, object);

            // convert i from int to string
            sprintf(s, "%i", counter);
            setLabel(label, s);
            counter++;
        }
    }
}
1
  • have you been able to solve your problem? If so, please mark any useful answer (if any) as "accepted", so other students with similar issues can benefit from your already answered question.
    – abelinux
    Commented Dec 27, 2014 at 16:16

2 Answers 2

3

Just for the record, I had the same problem of breakout getting "laggy" after running (in the appliance) for a few seconds. Staff's implementation would run properly, so it wasn't strictly a hardware (or a VM) issue.

I found this useful answer in /r/cs50, that, basically, states that the pause() statement is mandatory for the program to run properly.

As @delipity (and tutor @Gary) explains, the pause() statement is needed because, on a lower level, the OS (which is actually administering CPU computations) is trying to solve multitasking by allocating some CPU cycles to each loaded program (the OS itself, services / daemons, the VM player, the guest OS, gedit...).

If you don't pause, you're going to try to consume every single CPU cycle for your breakout, which the OS will not allow you to do. So, after a few seconds of doing that, it will, forcibly, start to skimp cycles at you and shuffle them among other running programs.

If you had solved ball movement with a small value in velocity without any pause, what will happen, in practice, is that you'll have a ball that will move at a very slow rate and a certain pause, imposed by the OS. Which you'll perceive as laggines


I also gained some improvement in the moves of my paddle by applying the "advice" given here: allow some events during program execution to bypass the pause statement.

1
  • Thank you. I had the same problem. I've read the articles you linked to. I think it's because of my computer, because, when the ball goes faster or slower when it moves across the scoreboard, my computer fan runs much louder. Commented Apr 11, 2015 at 18:45
-1

You know that

if(object !== NULL)

{
     // The object can be a brick or paddle

    if(object == paddle)
    {
        /*change the velocity in the y-direction
          When the ball hits the paddle, you know that before hitting, 
          the velocity of the ball was negative bcoz it's coming down.
          Hence, you don't need "if y>0 in your code"
        */
    }
    else if (strcmp(getType(object),"GRect") == 0)
    {
        //change the velocity in the y-direction again 
        velocity_y = -velocity_y;

        //remove the brick from the window
        removeGWindow (window, object);

        //increase points and decrease bricks
        points++;
        bricks--;

        //update scoreboard
        updateScoreboard(window, label, points);
    }
}
2
  • I just did what you suggest me to do it, the game is working better, but still, about 15 bricks before the end of the game, the game behave weird, the paddle doesn't follow the cursor properly.
    – julio
    Commented Sep 1, 2014 at 19:06
  • Do you have something else in your code like if(bricks<15)...that may be causing it to act weird only when there are few number of bricks left?
    – hunter
    Commented Sep 1, 2014 at 19:43

You must log in to answer this question.

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