Add logic for moving the platform
This commit is contained in:
parent
252f467332
commit
9b458a1eec
4 changed files with 43 additions and 4 deletions
|
@ -2,8 +2,9 @@
|
|||
#include "./ui_mainwindow.h"
|
||||
#include <QDebug>
|
||||
#include <memory>
|
||||
#include <qnamespace.h>
|
||||
|
||||
MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
|
||||
MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWindow), leftArrowPressed{false}, rightArrowPressed{false} {
|
||||
ui->setupUi(this);
|
||||
|
||||
platform = std::make_shared<Platform>(this);
|
||||
|
@ -43,3 +44,17 @@ void MainWindow::onDeleteBall(Ball* ball) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::keyPressEvent(QKeyEvent* event) {
|
||||
switch (event->key()) {
|
||||
case Qt::Key_Left: leftArrowPressed = true; break;
|
||||
case Qt::Key_Right: rightArrowPressed = true; break;
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::keyReleaseEvent(QKeyEvent* event) {
|
||||
switch (event->key()) {
|
||||
case Qt::Key_Left: leftArrowPressed = false; break;
|
||||
case Qt::Key_Right: rightArrowPressed = false; break;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,10 @@
|
|||
|
||||
#include "ball.h"
|
||||
#include "platform.h"
|
||||
#include <QKeyEvent>
|
||||
#include <QMainWindow>
|
||||
#include <QWidget>
|
||||
#include <qevent.h>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
namespace Ui {
|
||||
|
@ -18,12 +21,18 @@ class MainWindow : public QMainWindow {
|
|||
MainWindow(QWidget* parent = nullptr);
|
||||
~MainWindow();
|
||||
|
||||
bool leftArrowPressed;
|
||||
bool rightArrowPressed;
|
||||
|
||||
private slots:
|
||||
void onDeleteBall(Ball* ball); // Slot to handle the ball deletion
|
||||
|
||||
protected:
|
||||
virtual void paintEvent(QPaintEvent*) override;
|
||||
|
||||
void keyPressEvent(QKeyEvent* event) override;
|
||||
void keyReleaseEvent(QKeyEvent* event) override;
|
||||
|
||||
private:
|
||||
Ui::MainWindow* ui;
|
||||
QList<std::shared_ptr<Ball>> balls;
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
#include "platform.h"
|
||||
#include "mainwindow.h"
|
||||
#include <QDebug>
|
||||
#include <qapplication.h>
|
||||
|
||||
#define TICK_SPEED 15
|
||||
#define PLATFORM_WIDTH 100
|
||||
|
@ -7,7 +9,7 @@
|
|||
#define PLATFORM_MARGIN_BOTTOM 80
|
||||
#define SPEED 5.0
|
||||
|
||||
Platform::Platform(QWidget* parent) : m_parent{parent} {
|
||||
Platform::Platform(MainWindow* parent) : m_parent{parent} {
|
||||
QRect rct = parent->rect();
|
||||
|
||||
m_x = rand() % (rct.width() - PLATFORM_WIDTH);
|
||||
|
@ -23,4 +25,12 @@ void Platform::draw(QPainter& painter) {
|
|||
|
||||
void Platform::timerEvent(QTimerEvent*) {
|
||||
m_parent->update();
|
||||
|
||||
int direction = 0;
|
||||
if (m_parent->leftArrowPressed && !m_parent->rightArrowPressed)
|
||||
direction = -1;
|
||||
else if (m_parent->rightArrowPressed && !m_parent->leftArrowPressed)
|
||||
direction = 1;
|
||||
|
||||
m_x += direction * SPEED;
|
||||
}
|
||||
|
|
|
@ -5,11 +5,16 @@
|
|||
#include <QTimer>
|
||||
#include <QWidget>
|
||||
|
||||
// Forward declaration, avoiding circular import of mainwindow.h
|
||||
// (this probably isn't the best way to handle this, but I wasn't
|
||||
// sure how else to do it.)
|
||||
class MainWindow;
|
||||
|
||||
class Platform : public QTimer {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
Platform(QWidget* parent);
|
||||
Platform(MainWindow* parent);
|
||||
|
||||
void draw(QPainter& painter);
|
||||
|
||||
|
@ -17,7 +22,7 @@ class Platform : public QTimer {
|
|||
virtual void timerEvent(QTimerEvent*) override;
|
||||
|
||||
private:
|
||||
QWidget* m_parent;
|
||||
MainWindow* m_parent;
|
||||
double m_x;
|
||||
double m_y;
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue