Spawn balls on a timer

This commit is contained in:
ItsDrike 2025-03-24 15:29:15 +01:00
parent d4c0a3ca08
commit a5d465c533
Signed by: ItsDrike
GPG key ID: FA2745890B7048C0
4 changed files with 49 additions and 10 deletions

View file

@ -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;

View file

@ -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;

View file

@ -4,14 +4,18 @@
#include <memory>
#include <qnamespace.h>
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<Platform>(this);
connect(&m_spawnTimer, &QTimer::timeout, this, &MainWindow::onBallSpawn);
m_spawnTimer.start(m_spawnInterval);
auto ball = std::make_shared<Ball>(this, platform);
connect(ball.get(), &Ball::deleted, this, &MainWindow::onDeleteBall);
balls.append(ball);
platform = std::make_shared<Platform>(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<Ball>(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;

View file

@ -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<std::shared_ptr<Ball>> balls;
std::shared_ptr<Platform> platform;