如何绘制自定义形状元素?

How to draw a custom shaped element?

我目前正在开发一款本质上是乒乓球游戏的应用程序。该游戏具有两个当前为矩形格式的球拍,以简化对球(当前为方形)的碰撞检测。

我使用 "QRect" 元素作为桨,因为它提供了“.intersect”方法,可以轻松检查碰撞。 我对矩形桨的实现如下:

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

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

我正在尝试绘制一个类似桨和碰撞箱的拱门,类似于下面的代码提供的内容:

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);

上面代码的唯一问题是它只能在 paintEvent 函数中运行,对我来说不起作用。

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);
    }

据我了解,您不想从 .png 文件加载 QImage,但想绘制它,如果是这样,您可以使用 QPainter 创建图像,如我所示以下:

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);
}