From 252f4673326b6b46fe6c8a42568eb8910d48fea0 Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Tue, 18 Mar 2025 14:32:07 +0100 Subject: [PATCH] Add basic platform --- src/mainwindow.cpp | 5 +++++ src/mainwindow.h | 2 ++ src/platform.cpp | 26 ++++++++++++++++++++++++++ src/platform.h | 25 +++++++++++++++++++++++++ 4 files changed, 58 insertions(+) create mode 100644 src/platform.cpp create mode 100644 src/platform.h diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 2e2fe7b..518b95a 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -1,10 +1,13 @@ #include "mainwindow.h" #include "./ui_mainwindow.h" #include +#include MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); + platform = std::make_shared(this); + auto ball = std::make_shared(this); connect(ball.get(), &Ball::deleted, this, &MainWindow::onDeleteBall); balls.append(ball); @@ -25,6 +28,8 @@ void MainWindow::paintEvent(QPaintEvent*) { for (auto ball : balls) { ball->draw(painter); } + + platform->draw(painter); } void MainWindow::onDeleteBall(Ball* ball) { diff --git a/src/mainwindow.h b/src/mainwindow.h index 9f66533..08e4493 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -2,6 +2,7 @@ #define MAINWINDOW_H #include "ball.h" +#include "platform.h" #include QT_BEGIN_NAMESPACE @@ -26,5 +27,6 @@ class MainWindow : public QMainWindow { private: Ui::MainWindow* ui; QList> balls; + std::shared_ptr platform; }; #endif // MAINWINDOW_H diff --git a/src/platform.cpp b/src/platform.cpp new file mode 100644 index 0000000..b2f90eb --- /dev/null +++ b/src/platform.cpp @@ -0,0 +1,26 @@ +#include "platform.h" +#include + +#define TICK_SPEED 15 +#define PLATFORM_WIDTH 100 +#define PLATFORM_HEIGHT 20 +#define PLATFORM_MARGIN_BOTTOM 80 +#define SPEED 5.0 + +Platform::Platform(QWidget* parent) : m_parent{parent} { + QRect rct = parent->rect(); + + m_x = rand() % (rct.width() - PLATFORM_WIDTH); + m_y = rct.height() - (PLATFORM_HEIGHT + PLATFORM_MARGIN_BOTTOM); + + start(TICK_SPEED); +} + +void Platform::draw(QPainter& painter) { + QRect rct(m_x, m_y, PLATFORM_WIDTH, PLATFORM_HEIGHT); + painter.drawRect(rct); +} + +void Platform::timerEvent(QTimerEvent*) { + m_parent->update(); +} diff --git a/src/platform.h b/src/platform.h new file mode 100644 index 0000000..33e947a --- /dev/null +++ b/src/platform.h @@ -0,0 +1,25 @@ +#ifndef PLATFORM_H +#define PLATFORM_H + +#include +#include +#include + +class Platform : public QTimer { + Q_OBJECT + + public: + Platform(QWidget* parent); + + void draw(QPainter& painter); + + protected: + virtual void timerEvent(QTimerEvent*) override; + + private: + QWidget* m_parent; + double m_x; + double m_y; +}; + +#endif // PLATFORM_H