How to draw a custom shaped element?

95 Views Asked by At

I'm currently working in an app which is essentially a pong game. The game features two paddles currently in rectangular format to ease collision detection against the ball(currently square).

I'm using a "QRect" element as paddle since it provides the ".intersect" method, making it easy to check for collisions. My implementation for a rectangular paddle is as follows:

Paddle::Paddle(int initial_x, int initial_y) {

  QImage image.load(":images/paddle.png");
  QRect rect = image.rect();
  resetState(initial_x, initial_y);
}

I'm trying to draw an arch like paddle and hitbox, similar to what the code below provides:

QRectF rectangle(10.0, 20.0, 80.0, 60.0);
int startAngle = 30 * 16;
int spanAngle = 120 * 16;

QPainter painter(this);
painter.drawChord(rectangle, startAngle, spanAngle);

The only problem with the code above is it only works inside a paintEvent function and that won't work for me.

paddle.h

#ifndef PADDLE_H

#pragma once

#include <QImage>
#include <QRect>

class Paddle {

  public:
    Paddle(int, int);
    ~Paddle();

  public:
    void resetState(int, int);
    void move();
    void setDx(int);
    void setDy(int);
    QRect getRect();
    QImage & getImage();

  private:
    QImage image;
    QRect rect;
    int dx;
    int dy;
    static const int INITIAL_X1 = 70;
    static const int INITIAL_Y1 = 350;
};

#define PADDLE_H

#endif // PADDLE_H

paddle.cpp

#include <iostream>
#include "paddle.h"

Paddle::Paddle(int initial_x, int initial_y) {

  dy = 0;
  image.load(":images/paddle.png");
  rect = image.rect();
  resetState(initial_x, initial_y);
}

Paddle::~Paddle() {

 std::cout << ("Paddle deleted") << std::endl;
}

void Paddle::setDy(int y) {
  dy = y;
}

void Paddle::move() {

    int x = rect.x();
    int y = rect.top() + dy;

    rect.moveTo(x, y);
}

void Paddle::resetState(int initial_x, int initial_y) {

  rect.moveTo(initial_x, initial_y);
}

QRect Paddle::getRect() {

  return rect;
}

QImage & Paddle::getImage() {

  return image;
}

mainGame.h

#ifndef BREAKOUT_H

#pragma once

#include <QWidget>
#include <QKeyEvent>
#include <QFrame>
#include "ball.h"
#include "brick.h"
#include "paddle.h"

class Breakout : public QFrame {
    Q_OBJECT

  public:
    Breakout(QWidget *parent = 0);
    ~Breakout();

signals:
    void leftScoreChanged(int leftScore);
    void rightScoreChanged(int rightScore);
    void ballLost(int ballsLeft);

  protected:
    void paintEvent(QPaintEvent *);
    void timerEvent(QTimerEvent *);
    void keyPressEvent(QKeyEvent *);
    void keyReleaseEvent(QKeyEvent *);
    void drawObjects(QPainter *);
    void finishGame(QPainter *, QString);
    void moveObjects();

    void startGame();
    void pauseGame();
    void stopGame();
    void victory();
    void validateScoreChange(int);
    void checkCollision();

  private:
    int x;
    int timerId;
    int ballsLeft;
    int leftScore;
    int rightScore;
    static const int N_OF_BRICKS = 30;
    static const int DELAY = 5;

    static const int TOP_EDGE = 0;
    static const int LEFT_EDGE = 0;
    static const int BOTTOM_EDGE = 700;
    static const int RIGHT_EDGE = 1200;

    Ball *ball;
    Paddle *leftPaddle;
    Paddle *rightPaddle;
    Brick *bricks[N_OF_BRICKS];
    bool gameOver;
    bool gameWon;
    bool gameStarted;
    bool paused;
};

#define BREAKOUT_H

#endif // BREAKOUT_H

Breakout::paintEvent()

void Breakout::paintEvent(QPaintEvent *e) {

      Q_UNUSED(e);

      QPainter painter(this);

      if (gameOver) {

        finishGame(&painter, "Game lost");

      } else if(gameWon) {

        finishGame(&painter, "Victory");
      }
      else {

        drawObjects(&painter);
      }

      QWidget::paintEvent(e);
    }
1

There are 1 best solutions below

3
On BEST ANSWER

From what I understand you do not want to load the QImage from a .png file but you want to draw it, if so you can use QPainter to create the image as I show below:

Paddle::Paddle(int initial_x, int initial_y) {

    // draw image
    QRectF rectangle(10.0, 20.0, 80.0, 60.0);
    int startAngle = 30 * 16;
    int spanAngle = 120 * 16;
    image = QImage(QSize(100, 100), QImage::Format_ARGB32);
    image.fill(Qt::transparent);
    QPainter painter(&image);
    painter.drawChord(rectangle, startAngle, spanAngle);
    painter.end();

    dy = 0;
    rect = image.rect();
    resetState(initial_x, initial_y);
}