From a5d465c533cc15d5b2a29d59a07322f093dee5b6 Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Mon, 24 Mar 2025 15:29:15 +0100 Subject: [PATCH] Spawn balls on a timer --- src/ball.cpp | 7 +++++-- src/ball.h | 3 ++- src/mainwindow.cpp | 40 ++++++++++++++++++++++++++++++++++------ src/mainwindow.h | 9 ++++++++- 4 files changed, 49 insertions(+), 10 deletions(-) diff --git a/src/ball.cpp b/src/ball.cpp index 39d69c3..dceafca 100644 --- a/src/ball.cpp +++ b/src/ball.cpp @@ -43,7 +43,7 @@ void Ball::timerEvent(QTimerEvent*) { } // Bottom collision (only if no longer spawning) if (!m_isSpawning && m_y > rct.height() - RADIUS) { - emit deleted(this); + emit fellThrough(this); stop(); m_parent->update(); return; @@ -51,7 +51,10 @@ void Ball::timerEvent(QTimerEvent*) { // Platform collision (only if no longer spawning) if (!m_isSpawning && m_platform->rect().intersects(QRect(m_x, m_y, RADIUS * 2.0, RADIUS * 2.0))) { - m_ky *= -1; + emit caught(this); + stop(); + m_parent->update(); + return; } m_x += m_kx; diff --git a/src/ball.h b/src/ball.h index 7fd3dd3..fafcb41 100644 --- a/src/ball.h +++ b/src/ball.h @@ -15,7 +15,8 @@ class Ball : public QTimer { void draw(QPainter& painter); signals: - void deleted(Ball* ball); + void fellThrough(Ball* ball); + void caught(Ball* ball); protected: virtual void timerEvent(QTimerEvent*) override; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 727ca61..ecb9468 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -4,14 +4,18 @@ #include #include -MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWindow), leftArrowPressed{false}, rightArrowPressed{false} { +#define INIT_SPAWN_SPEED 1500.0 +#define SPAWN_SPEED_INCREASE 100.0 +#define MIN_SPAWN_SPEED 100.0 + +MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWindow), leftArrowPressed{false}, rightArrowPressed{false}, m_spawnInterval{INIT_SPAWN_SPEED} { ui->setupUi(this); - platform = std::make_shared(this); + connect(&m_spawnTimer, &QTimer::timeout, this, &MainWindow::onBallSpawn); + m_spawnTimer.start(m_spawnInterval); - auto ball = std::make_shared(this, platform); - connect(ball.get(), &Ball::deleted, this, &MainWindow::onDeleteBall); - balls.append(ball); + platform = std::make_shared(this); + spawnBall(); } MainWindow::~MainWindow() { @@ -33,7 +37,7 @@ void MainWindow::paintEvent(QPaintEvent*) { platform->draw(painter); } -void MainWindow::onDeleteBall(Ball* ball) { +void MainWindow::deleteBall(Ball* ball) { // Remove the ball from the list by checking the raw pointer // This will also free it (smart ptrs). for (auto it = balls.begin(); it != balls.end(); ++it) { @@ -45,6 +49,30 @@ void MainWindow::onDeleteBall(Ball* ball) { } } +void MainWindow::spawnBall() { + auto ball = std::make_shared(this, platform); + connect(ball.get(), &Ball::fellThrough, this, &MainWindow::ballFell); + connect(ball.get(), &Ball::caught, this, &MainWindow::ballCaught); + balls.append(ball); +} + +void MainWindow::ballFell(Ball* ball) { + deleteBall(ball); + // TODO: Game over +} + +void MainWindow::ballCaught(Ball* ball) { + m_spawnInterval -= SPAWN_SPEED_INCREASE; + if (m_spawnInterval < MIN_SPAWN_SPEED) + m_spawnInterval = MIN_SPAWN_SPEED; + deleteBall(ball); +} + +void MainWindow::onBallSpawn() { + spawnBall(); + m_spawnTimer.start(m_spawnInterval); +} + void MainWindow::keyPressEvent(QKeyEvent* event) { switch (event->key()) { case Qt::Key_Left: leftArrowPressed = true; break; diff --git a/src/mainwindow.h b/src/mainwindow.h index 3da5baa..af451c7 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -25,15 +25,22 @@ class MainWindow : public QMainWindow { bool rightArrowPressed; private slots: - void onDeleteBall(Ball* ball); // Slot to handle the ball deletion + void ballFell(Ball* ball); + void ballCaught(Ball* ball); + void onBallSpawn(); protected: virtual void paintEvent(QPaintEvent*) override; + void deleteBall(Ball* ball); + void spawnBall(); + void keyPressEvent(QKeyEvent* event) override; void keyReleaseEvent(QKeyEvent* event) override; private: + float m_spawnInterval; + QTimer m_spawnTimer; Ui::MainWindow* ui; QList> balls; std::shared_ptr platform;