0

私は c++ で Qt を使用して、画面上で物を動かすことを伴うプラント z ゾンビをコーディングしています。そのために、advance() 関数に接続した QTimer を使用しています。

オブジェクトを移動させることはできますが (qDebug を使用して座標を確認します)、画面を更新するか、ウィンドウを切り替えない限り (たとえば、Alt タブを 2 回押すか、Windows キー ボタンをクリックします)、画像が物理的に変化することはありません。

これは私のクラスの一例です。ここでは、エンドウ豆を画面上で動かそうとしています。

#ifndef BULLETS_H
#define BULLETS_H

#include <QPoint>
#include <QGraphicsScene>
#include <QGraphicsPixmapItem>
#include <QGraphicsItem>
#include <QRectF>
#include <QPainter>
#include <QVector>
#include <QImage>
#include <QWidget>
//#include "mainwindow.h"


class bullets : public QGraphicsItem
{
public:
    QRectF boundingRect() const {return QRectF(this->x,0, 20,20);}
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);

    bullets(int xPlant, int yPlant, QImage bullet);
    ~bullets() {}

    void advance(int phase); //Moves the bullet across the screen

    int x;
    int y;

    QGraphicsPixmapItem *bulletsPixMap;
    QImage pea_picture;

    int velocity;
};

#endif 

対応する cpp ファイルとともに

#include "bullets.h"

#include <QDebug>
#include <QPainter>

int counter = 0;

bullets::bullets(int xPlant, int yPlant,QImage bullet)
{
    velocity = 10;
    x = xPlant;
    y = yPlant;
    pea_picture = bullet;
}

void bullets::advance(int phase)
{
    if (!phase)
        return;
    x += (velocity * 0.5);
    //  setPos(x,y);
}

void bullets::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    painter->drawPixmap(this->x,this->y,QPixmap::fromImage(pea_picture));

    /*
    QBrush b(QColor(0,252,0));
    painter->setBrush(b);
    painter->drawEllipse(boundingRect());
    */
}
4

0 に答える 0