Add basic platform
This commit is contained in:
parent
77bb46e7fd
commit
252f467332
4 changed files with 58 additions and 0 deletions
|
@ -1,10 +1,13 @@
|
||||||
#include "mainwindow.h"
|
#include "mainwindow.h"
|
||||||
#include "./ui_mainwindow.h"
|
#include "./ui_mainwindow.h"
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
|
MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
|
||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
|
|
||||||
|
platform = std::make_shared<Platform>(this);
|
||||||
|
|
||||||
auto ball = std::make_shared<Ball>(this);
|
auto ball = std::make_shared<Ball>(this);
|
||||||
connect(ball.get(), &Ball::deleted, this, &MainWindow::onDeleteBall);
|
connect(ball.get(), &Ball::deleted, this, &MainWindow::onDeleteBall);
|
||||||
balls.append(ball);
|
balls.append(ball);
|
||||||
|
@ -25,6 +28,8 @@ void MainWindow::paintEvent(QPaintEvent*) {
|
||||||
for (auto ball : balls) {
|
for (auto ball : balls) {
|
||||||
ball->draw(painter);
|
ball->draw(painter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
platform->draw(painter);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::onDeleteBall(Ball* ball) {
|
void MainWindow::onDeleteBall(Ball* ball) {
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
#define MAINWINDOW_H
|
#define MAINWINDOW_H
|
||||||
|
|
||||||
#include "ball.h"
|
#include "ball.h"
|
||||||
|
#include "platform.h"
|
||||||
#include <QMainWindow>
|
#include <QMainWindow>
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
|
@ -26,5 +27,6 @@ class MainWindow : public QMainWindow {
|
||||||
private:
|
private:
|
||||||
Ui::MainWindow* ui;
|
Ui::MainWindow* ui;
|
||||||
QList<std::shared_ptr<Ball>> balls;
|
QList<std::shared_ptr<Ball>> balls;
|
||||||
|
std::shared_ptr<Platform> platform;
|
||||||
};
|
};
|
||||||
#endif // MAINWINDOW_H
|
#endif // MAINWINDOW_H
|
||||||
|
|
26
src/platform.cpp
Normal file
26
src/platform.cpp
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
#include "platform.h"
|
||||||
|
#include <QDebug>
|
||||||
|
|
||||||
|
#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();
|
||||||
|
}
|
25
src/platform.h
Normal file
25
src/platform.h
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
#ifndef PLATFORM_H
|
||||||
|
#define PLATFORM_H
|
||||||
|
|
||||||
|
#include <QPainter>
|
||||||
|
#include <QTimer>
|
||||||
|
#include <QWidget>
|
||||||
|
|
||||||
|
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
|
Loading…
Add table
Reference in a new issue