PDA

View Full Version : This program runs, but does not work properly



rezas1000
14th September 2014, 15:33
Hello,This program runs, but does not work properly.I copied the codes from this link (http://zetcode.com/gui/qt4/breakoutgame/) and Because I have Qt 5.2.1,I changed FALSE to false and TRUE to true.Also I used from the Q_UNUSED for prevent warning.However,the program runs but does not work properly and does not work the game and Only opens a window. Thanks in advance.
10630

d_stranz
14th September 2014, 18:16
Did you also copy the PNG files for the images of the paddle, brick, and ball?


image.load( "xxx.png" );

These statements in the paddle, ball, and brick constructors are probably failing and as a result the images are not valid. There is nothing for the painter to paint in the paintEvent().

If you did add the images, are they in a location where the EXE can find them at run-time to load them properly? The program is expecting these to be in the same directory as the exe. A better solution would be to add the images as resources to your project, and then load them using a resource URL. See the Qt Resource System documentation.

rezas1000
14th September 2014, 20:54
Did you also copy the PNG files for the images of the paddle, brick, and ball?
No.why?I didn't see the PNG files in the link.

If you did add the images, are they in a location where the EXE can find them at run-time to load them properly?
No.only I copied the codes from the link and I didn't see nothing else in the link.what do you mean?Can you explain more? thanks

d_stranz
15th September 2014, 02:46
As I said, the Paddle, Brick, and Ball classes each have a QImage member variable called "image". In the constructor for each class, the image is loaded from a PNG file, like this (from the Ball Class):



Ball::Ball()
{

xdir = 1;
ydir = -1;

image.load("ball.png"); // << This code, right here, tries to load a PNG file called "ball.png"

rect = image.rect();
resetState();

}

If the file cannot be found, then there is no image loaded. In that case, the image member variables are not valid, and when passed to the painter for drawing on the screen, nothing happens:



void Breakout::paintEvent(QPaintEvent *event)
{
QPainter painter(this);

// If / else code omitted because this is
// the only code that matters in this case:

else {
painter.drawImage(ball->getRect(),
ball->getImage()); // << ball->getImage() returns an invalid image
painter.drawImage(paddle->getRect(),
paddle->getImage()); // << so does paddle->getImage()

for (int i=0; i<30; i++) {
if (!bricks[i]->isDestroyed())
painter.drawImage(bricks[i]->getRect(),
bricks[i]->getImage()); // << and bricks[i]->getImage()
}
}
}

So, you can't just blindly copy code from somewhere without studying it to see what it is trying to do. If the web site where you found that code has a download link that will let you download the complete project (in other words, not just a copy and paste), then that download probably contains the image files you need.