Basic ball
This commit is contained in:
parent
224f68c1b6
commit
87e2a0e9aa
5 changed files with 121 additions and 13 deletions
|
@ -35,6 +35,7 @@ else()
|
||||||
else()
|
else()
|
||||||
add_executable(${PROJECT_NAME}
|
add_executable(${PROJECT_NAME}
|
||||||
${PROJECT_SOURCES}
|
${PROJECT_SOURCES}
|
||||||
|
src/ball.h src/ball.cpp
|
||||||
)
|
)
|
||||||
endif()
|
endif()
|
||||||
endif()
|
endif()
|
||||||
|
|
68
src/ball.cpp
Normal file
68
src/ball.cpp
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
#include "ball.h"
|
||||||
|
#include <QRect>
|
||||||
|
#include <QWidget>
|
||||||
|
#include <random>
|
||||||
|
|
||||||
|
#define RADIUS 15
|
||||||
|
#define TICK_SPEED 10
|
||||||
|
#define MAX_SPEED 5.0
|
||||||
|
#define GRAVITY 0.981
|
||||||
|
|
||||||
|
double getRandomDouble(double maxValue);
|
||||||
|
|
||||||
|
Ball::Ball(QWidget* parent) : m_parent{parent}, m_isSpawning{true} {
|
||||||
|
QRect rct = parent->rect();
|
||||||
|
|
||||||
|
m_x = rand() % rct.width(); // random x pos
|
||||||
|
m_y = rct.height() + 5; // start below the screen
|
||||||
|
|
||||||
|
m_kx = getRandomDouble(MAX_SPEED); // random horizontal speed
|
||||||
|
m_ky = -MAX_SPEED;
|
||||||
|
|
||||||
|
start(TICK_SPEED);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Ball::draw(QPainter& painter) {
|
||||||
|
painter.drawEllipse(m_x, m_y, RADIUS * 2.0, RADIUS * 2.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Ball::timerEvent(QTimerEvent*) {
|
||||||
|
if (m_ky < -MAX_SPEED)
|
||||||
|
m_ky = std::max(m_ky - GRAVITY, -MAX_SPEED);
|
||||||
|
|
||||||
|
// Horizontal wall collision
|
||||||
|
QRect rct = m_parent->rect();
|
||||||
|
if (m_x < RADIUS || m_x > rct.width() - RADIUS) {
|
||||||
|
m_kx *= -1;
|
||||||
|
}
|
||||||
|
// Ceiling collision
|
||||||
|
if (m_y < RADIUS) {
|
||||||
|
m_ky *= -1;
|
||||||
|
}
|
||||||
|
// Bottom collision (only if no longer spawning)
|
||||||
|
if (!m_isSpawning && m_y > rct.height() - RADIUS) {
|
||||||
|
// TODO: Destroy ball
|
||||||
|
}
|
||||||
|
|
||||||
|
m_x += m_kx;
|
||||||
|
m_y += m_ky;
|
||||||
|
|
||||||
|
// Once we get into the window, we're no longer "spawning"
|
||||||
|
if (m_isSpawning && m_y < rct.width() - RADIUS)
|
||||||
|
m_isSpawning = false;
|
||||||
|
|
||||||
|
// Update the window
|
||||||
|
m_parent->update();
|
||||||
|
}
|
||||||
|
|
||||||
|
double getRandomDouble(double maxValue) {
|
||||||
|
// Create a random device and a random engine
|
||||||
|
std::random_device rd;
|
||||||
|
std::mt19937 gen(rd());
|
||||||
|
|
||||||
|
// Define the range from 0 to maxValue
|
||||||
|
std::uniform_real_distribution<> dis(0.0, maxValue);
|
||||||
|
|
||||||
|
// Return the random value
|
||||||
|
return dis(gen);
|
||||||
|
}
|
25
src/ball.h
Normal file
25
src/ball.h
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
#ifndef BALL_H
|
||||||
|
#define BALL_H
|
||||||
|
|
||||||
|
#include <QPainter>
|
||||||
|
#include <QTimer>
|
||||||
|
|
||||||
|
class Ball : public QTimer {
|
||||||
|
public:
|
||||||
|
Ball(QWidget* parent);
|
||||||
|
|
||||||
|
void draw(QPainter& painter);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual void timerEvent(QTimerEvent*) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
QWidget* m_parent;
|
||||||
|
double m_x;
|
||||||
|
double m_y;
|
||||||
|
double m_kx;
|
||||||
|
double m_ky;
|
||||||
|
bool m_isSpawning;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // BALL_H
|
|
@ -1,14 +1,24 @@
|
||||||
#include "mainwindow.h"
|
#include "mainwindow.h"
|
||||||
#include "./ui_mainwindow.h"
|
#include "./ui_mainwindow.h"
|
||||||
|
|
||||||
MainWindow::MainWindow(QWidget *parent)
|
MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
|
||||||
: QMainWindow(parent)
|
|
||||||
, ui(new Ui::MainWindow)
|
|
||||||
{
|
|
||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
|
balls.append(std::make_shared<Ball>(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
MainWindow::~MainWindow()
|
MainWindow::~MainWindow() {
|
||||||
{
|
|
||||||
delete ui;
|
delete ui;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MainWindow::paintEvent(QPaintEvent*) {
|
||||||
|
QPainter painter(this);
|
||||||
|
|
||||||
|
painter.fillRect(rect(), Qt::darkGray);
|
||||||
|
|
||||||
|
painter.setBrush(QBrush(Qt::white));
|
||||||
|
painter.setPen(Qt::black);
|
||||||
|
|
||||||
|
for (auto ball : balls) {
|
||||||
|
ball->draw(painter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#ifndef MAINWINDOW_H
|
#ifndef MAINWINDOW_H
|
||||||
#define MAINWINDOW_H
|
#define MAINWINDOW_H
|
||||||
|
|
||||||
|
#include "ball.h"
|
||||||
#include <QMainWindow>
|
#include <QMainWindow>
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
|
@ -9,15 +10,18 @@ class MainWindow;
|
||||||
}
|
}
|
||||||
QT_END_NAMESPACE
|
QT_END_NAMESPACE
|
||||||
|
|
||||||
class MainWindow : public QMainWindow
|
class MainWindow : public QMainWindow {
|
||||||
{
|
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
MainWindow(QWidget* parent = nullptr);
|
MainWindow(QWidget* parent = nullptr);
|
||||||
~MainWindow();
|
~MainWindow();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual void paintEvent(QPaintEvent*) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Ui::MainWindow* ui;
|
Ui::MainWindow* ui;
|
||||||
|
QList<std::shared_ptr<Ball>> balls;
|
||||||
};
|
};
|
||||||
#endif // MAINWINDOW_H
|
#endif // MAINWINDOW_H
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue