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();
}
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();
}
To copy to clipboard, switch view to plain text mode
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:
{
// 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()
}
}
}
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()
}
}
}
To copy to clipboard, switch view to plain text mode
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.
Bookmarks