2 Replies - 1132 Views - Last Post: 18 June 2012 - 10:18 AM Rate Topic: -----

#1 computerguy3100  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 04-June 12

Stop Pong Ball from Getting Stuck

Posted 18 June 2012 - 03:03 AM

Hello Everyone, I have written a nice pong game in C++ using the Nokia QT Creator in WIndows 7. The game compiles and starts up with two paddles and a ball, however, the ball just flies up to the top right and gets stuck. I will not post the entire code for the game since it is four pages long, but I believe that the problem must be occurring somewhere here:

#include <QDebug>
#include <QKeyEvent>

#include "pong.hpp"

#define WIDTH 400
#define HEIGHT (WIDTH/4*3)
#define PADDLE (WIDTH/15)



Pong::Pong()
{
        this->setSceneRect(0, 0, WIDTH, HEIGHT);
        this->p1 = new QGraphicsRectItem(0, 0,
                                         PADDLE,
                                         PADDLE*3);
        this->p2 = new QGraphicsRectItem(0, 0,
                                         PADDLE,
                                         PADDLE*3);

        this->p1->setPos(PADDLE, (HEIGHT-PADDLE*3)/2);
        this->p2->setPos(WIDTH-PADDLE*2, (HEIGHT-PADDLE*3)/2);
        this->p1->setBrush(QBrush(Qt::red));
        this->p2->setBrush(QBrush(Qt::black));

        /* ball */
        this->ball = new QGraphicsEllipseItem(0, 0, PADDLE, PADDLE);
        this->ball->setPos((WIDTH-PADDLE)/2, (HEIGHT-PADDLE)/2);
        this->ball->setBrush(QBrush(Qt::blue));

        this->dx = -1;
        this->dy = -1;

        QTimeLine *timer = new QTimeLine(5000);
        timer->setFrameRange(0, 100);
        this->addItem(ball);
        this->addItem(p1);
        this->addItem(p2);
        timer->setLoopCount(10000);
        timer->start();
        connect(timer,
                SIGNAL(frameChanged(int)),
                this,
                SLOT(value_changed(int)));
}

void
Pong::value_changed(int)
{
        /* do we collide ? */
        if (ball->x()>=PADDLE*2 && ball->x()<=WIDTH-PADDLE*2) {
                if (ball->collidesWithItem(p1) || ball->collidesWithItem(p2)) {
                        dx = -dx;
                } else if (ball->y()<=0 || ball->y()>=HEIGHT-PADDLE) {
                        dy = -dy;
                }
                this->ball->moveBy(PADDLE*dx*0.175, PADDLE*dy*0.175);

        }
}

void
Pong::keyPressEvent(QKeyEvent *e)
{
        int x = 0;
        int x2 = 0;
        switch(e->key()) {
        case (Qt::Key_Up):
                x = -1;
                break;
        case (Qt::Key_Down):
                x = 1;
                break;
        case (Qt::Key_A):
                x2 = -1;
                break;
        case (Qt::Key_Q):
                x2 = 1;
                break;
        default:
                x = 0;
                x2 = 0;
                break;
        }

        /*      if(p1->y() >= PADDLE*2 && */
        /*      p1->y() + PADDLE*3 <= HEIGHT) */
        /* TODO : edge detection */
        this->p1->moveBy(0, PADDLE*x);
        this->p2->moveBy(0, PADDLE*x2);
}




Any suggestions would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Stop Pong Ball from Getting Stuck

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Stop Pong Ball from Getting Stuck

Posted 18 June 2012 - 09:15 AM

Do you know how to debug? That will tell you exactly what the problem is. :)
Was This Post Helpful? 0
  • +
  • -

#3 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 841
  • View blog
  • Posts: 2,472
  • Joined: 20-March 10

Re: Stop Pong Ball from Getting Stuck

Posted 18 June 2012 - 10:18 AM



void Pong::value_changed(int)
{
        /* do we collide ? */
        if (ball->x()>=PADDLE*2 && ball->x()<=WIDTH-PADDLE*2)
       {
                if (ball->collidesWithItem(p1) || ball->collidesWithItem(p2)) {
                        dx = -dx;
                } else if (ball->y()<=0 || ball->y()>=HEIGHT-PADDLE) {
                        dy = -dy;
                }
                this->ball->moveBy(PADDLE*dx*0.175, PADDLE*dy*0.175);

        }
}







If I'm being honest this section doesn't make a lot of sense to me
basically in Pong if the ball is less than 0 on the x coordinate its a point scored.

Where is your point scored bit?

Also if the x coordinate is more than WIDTH then it's a point scored.

Where is your point scored bit?

If the y coordinate is less than 0 then reverse the direction of y (it has bounced off the side)

where do you do that ?

If the y coordinate is more than Height then reverse the direction of y (it has bounced off the side)

where do you do that ?

You do seem to handle the what if it hits a paddle scenario sort of..

It's just I don't think it's right.

In Pong if the ball hits a paddle you should change the x direction first and foremost to send it spinning in the opposite direction of the screen.

But I also like to see some change in y depending on what part of the paddle it has hit maybe a slowing down of y maybe a speeding up of y, depending on the movement (up or down) of the paddle a complete direction change of y coupled with perhaps a slight slowing down of y.

It makes for a more interesting Pong game..

Have a think on that and tell me what you think...

All the Best.

Snoopy.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1