diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 518b95a..b27d935 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -2,8 +2,9 @@ #include "./ui_mainwindow.h" #include #include +#include -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(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; + } +} diff --git a/src/mainwindow.h b/src/mainwindow.h index 08e4493..3da5baa 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -3,7 +3,10 @@ #include "ball.h" #include "platform.h" +#include #include +#include +#include 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> balls; diff --git a/src/platform.cpp b/src/platform.cpp index b2f90eb..5037a03 100644 --- a/src/platform.cpp +++ b/src/platform.cpp @@ -1,5 +1,7 @@ #include "platform.h" +#include "mainwindow.h" #include +#include #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; } diff --git a/src/platform.h b/src/platform.h index 33e947a..e2ccdd4 100644 --- a/src/platform.h +++ b/src/platform.h @@ -5,11 +5,16 @@ #include #include +// 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; };