创建单独的计时器以创建速度和球

发布于 2025-01-17 10:53:33 字数 3854 浏览 3 评论 0原文

我正在努力下降的球应用程序, 关于应用程序:球不断定期下降,并且有一个篮子抓住球。如果被抓住,那么我们就会赢得一点点,然后丧生。

对于此应用程序,我使用QT和CPP。 我创建了一个级别的精灵,并具有有关球(x和y)和速度(DX和DY)的初始位置的规格。 我正在使用一个计时器来实现球的速度,但是我需要定期进行另一个计时器来创建球。谁能帮助我如何定期实施球?

sprite.h
--------
#ifndef SPRITE_H
#define SPRITE_H

#include <QTimer>
#include <QPainter>
#include <QWidget>

/**
 * Header class for Sprite
 */
class Sprite : public QTimer
{
public:
    Sprite(QWidget *parent);
    //draw() to draw a sprite
    void draw(QPainter &painter);
protected:
    //This timerEvent will be called after certain time prescribed.
    virtual void timerEvent(QTimerEvent *e) override;

    int x;//position of sprite in x-direction.
    int y;//position of sprite in y-direction.
    int dx;//difference in x-direction position.
    int dy;//difference in y-direction position.
    int x1;//position of the basket in x-direction
    int y1;//position of the basket in y-direction.
    QWidget *parent;//parent class for all widgets.

};
#endif // SPRITE_H


sprite.cpp
-----------
#include "sprite.h"
#include <QDebug>

Sprite::Sprite(QWidget *parent):parent(parent)
{
    QRect rct = parent->rect();
    x = rand() % rct.width();//randomly initialize the x-position for the sprite.
    y=rct.height()*0.05;//start position for the sprite is about 5% after the top of the menu bar.

    dx = rand() % 10;//the speed is randomly set in x-direction.

//    dy = rand() % 10;//the speed is randomly set in y-direction.
    dy = 4;

    x1=rct.width()/2;
    y1 = rct.height()-80;

    start(10);

}

void Sprite::draw(QPainter &painter)
{
    qDebug() <<"Sprite::draw() called";
    painter.drawEllipse(x, y, 15, 15);//ball
    painter.drawRect( x1, y1, 80, 30);//basket
}


void Sprite::timerEvent(QTimerEvent *)
{
    qDebug("timerEvent called");
    QRect rct = parent->rect();
    if ( x > rct.width() || x < 0)
        dx *= -1;
    if ( y > rct.height() || y < 0){}

    x += dx;
    y += dy;
    parent->update();
}

mainwindow.cpp
-----------------
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QKeyEvent>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    qDebug() <<"mainWindow constructor";

      QTimer timer;
    QObject::connect(&timer, &QTimer::timeout, this,&MainWindow::onTimer);
    timer.start(1000);
}

MainWindow::~MainWindow()
{
    qDebug() <<"mainWindow destructor";
    delete ui;
}
void MainWindow::onTimer()
{
    std::cout << "Tick!-----------------------------------------------------------------------------" << std::endl;

}
  


void MainWindow::paintEvent(QPaintEvent *)
{
    qDebug() <<"painEvent() called";
    QPainter painter(this);

    painter.fillRect(rect(), QBrush(QColor(Qt::white)));

    painter.setPen(Qt::black);
    painter.setBrush(QBrush(QColor(Qt::darkBlue)));
    emit draw(painter);
}
    
MainWindow.h
-----------
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "sprite.h"
#include <balls.h>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow

{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
signals:
    void draw(QPainter &painter);
    void move(int nDirection);

protected:
    //This event handler captures all the paint events request to repaint all or part of the widget.
    void paintEvent(QPaintEvent* event)override;
    
public slots:
    void onTimer();

private slots:
    void on_actionStart_triggered();

    void on_actionStop_triggered();

private:
    Ui::MainWindow *ui;
    Sprite *sprite;
    };
#endif // MAINWINDOW_H

我如何定期创建球,任何sudo代码都会有所帮助。

也欢迎任何更好的实施方法。

I am working on Falling ball application,
About the application: the balls keep on falling at regular intervals, and there is a basket that catches the balls. If caught then we earn a point if not then lose one life.

For this application, I am using qt and CPP.
I have created one class Sprite with specifications regarding the initial position of the ball(x and y) and speed (dx and dy).
I am using one timer for the speed of the ball, but I need to another timer for ball creation at regular intervals. Could anyone help me with how to implement the ball creation at regular intervals?

sprite.h
--------
#ifndef SPRITE_H
#define SPRITE_H

#include <QTimer>
#include <QPainter>
#include <QWidget>

/**
 * Header class for Sprite
 */
class Sprite : public QTimer
{
public:
    Sprite(QWidget *parent);
    //draw() to draw a sprite
    void draw(QPainter &painter);
protected:
    //This timerEvent will be called after certain time prescribed.
    virtual void timerEvent(QTimerEvent *e) override;

    int x;//position of sprite in x-direction.
    int y;//position of sprite in y-direction.
    int dx;//difference in x-direction position.
    int dy;//difference in y-direction position.
    int x1;//position of the basket in x-direction
    int y1;//position of the basket in y-direction.
    QWidget *parent;//parent class for all widgets.

};
#endif // SPRITE_H


sprite.cpp
-----------
#include "sprite.h"
#include <QDebug>

Sprite::Sprite(QWidget *parent):parent(parent)
{
    QRect rct = parent->rect();
    x = rand() % rct.width();//randomly initialize the x-position for the sprite.
    y=rct.height()*0.05;//start position for the sprite is about 5% after the top of the menu bar.

    dx = rand() % 10;//the speed is randomly set in x-direction.

//    dy = rand() % 10;//the speed is randomly set in y-direction.
    dy = 4;

    x1=rct.width()/2;
    y1 = rct.height()-80;

    start(10);

}

void Sprite::draw(QPainter &painter)
{
    qDebug() <<"Sprite::draw() called";
    painter.drawEllipse(x, y, 15, 15);//ball
    painter.drawRect( x1, y1, 80, 30);//basket
}


void Sprite::timerEvent(QTimerEvent *)
{
    qDebug("timerEvent called");
    QRect rct = parent->rect();
    if ( x > rct.width() || x < 0)
        dx *= -1;
    if ( y > rct.height() || y < 0){}

    x += dx;
    y += dy;
    parent->update();
}

mainwindow.cpp
-----------------
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QKeyEvent>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    qDebug() <<"mainWindow constructor";

      QTimer timer;
    QObject::connect(&timer, &QTimer::timeout, this,&MainWindow::onTimer);
    timer.start(1000);
}

MainWindow::~MainWindow()
{
    qDebug() <<"mainWindow destructor";
    delete ui;
}
void MainWindow::onTimer()
{
    std::cout << "Tick!-----------------------------------------------------------------------------" << std::endl;

}
  


void MainWindow::paintEvent(QPaintEvent *)
{
    qDebug() <<"painEvent() called";
    QPainter painter(this);

    painter.fillRect(rect(), QBrush(QColor(Qt::white)));

    painter.setPen(Qt::black);
    painter.setBrush(QBrush(QColor(Qt::darkBlue)));
    emit draw(painter);
}
    
MainWindow.h
-----------
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "sprite.h"
#include <balls.h>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow

{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
signals:
    void draw(QPainter &painter);
    void move(int nDirection);

protected:
    //This event handler captures all the paint events request to repaint all or part of the widget.
    void paintEvent(QPaintEvent* event)override;
    
public slots:
    void onTimer();

private slots:
    void on_actionStart_triggered();

    void on_actionStop_triggered();

private:
    Ui::MainWindow *ui;
    Sprite *sprite;
    };
#endif // MAINWINDOW_H

How can I create balls at regular intervals, any sudo code will help.

Or any better way to implement this is also welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雪化雨蝶 2025-01-24 10:53:33

调用 qobject :: startTimer 将同时运行两个计时器:

int timerId1 = startTimer(2000);
int timerId2 = startTimer(10);

void MyObject::timerEvent(QTimerEvent *event)
{
    if (event->timerId() == timerId1)
        ; // 2000ms timer is fired
     else if (event->timerId() == timerId2)
        ; // 10ms timer is fired
}

qt文档此用例的示例。

另一种选择是使用两个 qtimer对象,对象,对象并将一个插槽连接到每个qtimer对象的超时信号。

Calling QObject::startTimer twice, will run two timers simultaneously:

int timerId1 = startTimer(2000);
int timerId2 = startTimer(10);

void MyObject::timerEvent(QTimerEvent *event)
{
    if (event->timerId() == timerId1)
        ; // 2000ms timer is fired
     else if (event->timerId() == timerId2)
        ; // 10ms timer is fired
}

Also the Qt documentation itself contains a clear example of this use case.

An alternative is to use two QTimer objects, and connect a slot to the timeout signal of each QTimer object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文