GO - #1
Conversation
danillo19
left a comment
There was a problem hiding this comment.
Проверь все на утечки памяти, пули че то никак не удаляются. Давай еще добавим еще одни тип Вражеского Танка камикадзе, который при встрече с игроком взрывается и наносит урон
| static constexpr qreal kSpeed = 30.0; | ||
| static constexpr qreal kWidth = 32.0; | ||
| static constexpr qreal kHeight = 32.0; |
There was a problem hiding this comment.
что значит тут префикс k? Почему static переменные, а не тупо const или вообще private? Тут инкапсуляция немного хромает
| // Легкий | ||
| class LightEnemy : public EnemyTank { | ||
| public: | ||
| explicit LightEnemy(qreal x, qreal y); | ||
| qreal GetSpeed() const override { return 1.5; } | ||
| protected: | ||
| void UpdatePixmap() override; | ||
| }; |
| qreal GetSpeed() const override { return 0.8; } | ||
| protected: | ||
| void UpdatePixmap() override; | ||
| QList<Bullet*> Fire(Direction dir) override; |
There was a problem hiding this comment.
Почему сырой указатель? А не от Qt или стандартный умный?
| class Tank; | ||
| class Wall; | ||
| class BrickWall; | ||
| class Bullet; | ||
| class EnemyTank; | ||
| class Bonus; |
| void UpdateBonuses(); | ||
|
|
||
| bool IsCollidingWithSolidWall(const QRectF& rect) const; | ||
| BrickWall* IsCollidingWithBrickWall(const QRectF& rect) const; |
There was a problem hiding this comment.
Is предполагает возврат true/false, такой метод надо называть FindCollidingBrickWall
| switch (m_direction) { | ||
| case Direction::Up: | ||
| painter.drawLine(center, center, center, center - kTankGunLength); | ||
| break; | ||
| case Direction::Down: | ||
| painter.drawLine(center, center, center, center + kTankGunLength); | ||
| break; | ||
| case Direction::Left: | ||
| painter.drawLine(center, center, center - kTankGunLength, center); | ||
| break; | ||
| case Direction::Right: | ||
| painter.drawLine(center, center, center + kTankGunLength, center); | ||
| break; | ||
| } |
There was a problem hiding this comment.
давай часть сложных свитчей перепишем на map + lambda или std::function. Суть в чем:
- Делаешь мапу/мапы
std::unordered_map<Direction, std::function<...>> painterHandlers;
painterHandlers.put(Direction::Up, [](...) {painter.drawLine(center, center, center, center - kTankGunLength);}) // кладем лямбду
...
- Используем:
auto hander = painterHandlers.find(direction)
if handler != painterHandlers.end() {
handler(...);
...
}
| painter.setPen(Qt::NoPen); | ||
| painter.setBrush(Qt::black); | ||
| const struct { int x; int y; } rivets[] = { | ||
| {4, 4}, {12, 4}, {4, 12}, {12, 12}, | ||
| {20, 4}, {28, 4}, {20, 12}, {28, 12}, | ||
| {4, 20}, {12, 20}, {4, 28}, {12, 28}, | ||
| {20, 20},{28, 20},{20, 28}, {28, 28} | ||
| }; | ||
| for (const auto& r : rivets) { | ||
| painter.drawEllipse(r.x - kRivetRadius, r.y - kRivetRadius, | ||
| 2 * kRivetRadius, 2 * kRivetRadius); | ||
| } |
There was a problem hiding this comment.
вынеси в метод по типу InitRivets и тд
|
|
||
| private: | ||
|
|
||
| void keyPressEvent(QKeyEvent* event) override; |
There was a problem hiding this comment.
gamescane, enter , выстрел и тд
| private: | ||
|
|
||
| void keyPressEvent(QKeyEvent* event) override; | ||
| void keyReleaseEvent(QKeyEvent* event) override; |
There was a problem hiding this comment.
вынеси в отдельный контроллер это. Обычно че делают, преобразуют евенты Qt в свои модельные события, по типу GoUp, DirectionUp, PlayerClick и тд. Контроллер это обрабатывает и меняет модель, т.е GameModel
| void Update(); | ||
| void Move(); | ||
| auto GetFutureRect(Direction dir) const -> QRectF; | ||
| auto GetDirection() const -> Direction; | ||
|
|
||
|
|
||
| private: | ||
| void UpdatePixmap(); | ||
|
|
||
| Direction m_direction { Direction::Up }; | ||
| static constexpr qreal kSpeed = 2.0; |
There was a problem hiding this comment.
Танк и модель и вью, себя и рисует и танцует, надо разделить либо вынести в GameScene его отрисовку. В Tank должна быть сущность, по типу скорости, здоровья, урона, брони, логи урона и тд. Отрисовка не его задача.
danillo19
left a comment
There was a problem hiding this comment.
Так и не увидел удаления пуль, если не согласен, пиши в ответах на комменты мои, порешаем
|
|
||
| #include "GlobalConstants.h" | ||
|
|
||
| Bullet::Bullet(qreal x, qreal y, Direction direction, BulletOwner owner) |
There was a problem hiding this comment.
Скинь замер памяти мне, либо ссылку на доку или скрин дебага объектов, не вижу удаления пуль.
| } | ||
|
|
||
| void EnemyTank::DrawGun(QPainter& painter, int center, int gunLength) const { | ||
| using Handler = std::function<void(QPainter&, int, int)>; |
There was a problem hiding this comment.
лайк, но обычно это если переиспользуется switch в нескольких местах, но ок
| Bullet* GameModel::AddBullet(std::unique_ptr<Bullet> b) { bullets.push_back(std::move(b)); return bullets.back().get(); } | ||
| std::vector<Bullet*> GameModel::GetBullets() const { std::vector<Bullet*> out; out.reserve(bullets.size()); for (const auto& p: bullets) out.push_back(p.get()); return out; } | ||
| void GameModel::RemoveBullet(Bullet* b) { auto it = std::find_if(bullets.begin(), bullets.end(), [b](const std::unique_ptr<Bullet>& p){ return p.get() == b; }); if (it != bullets.end()) bullets.erase(it); } | ||
| void GameModel::ClearBullets() { bullets.clear(); } | ||
| size_t GameModel::GetBulletsCount() const { return bullets.size(); } | ||
| Bullet* GameModel::GetBulletAt(size_t i) const { return bullets[i].get(); } | ||
|
|
||
| Bullet* GameModel::AddEnemyBullet(std::unique_ptr<Bullet> b) { enemyBullets.push_back(std::move(b)); return enemyBullets.back().get(); } | ||
| std::vector<Bullet*> GameModel::GetEnemyBullets() const { std::vector<Bullet*> out; out.reserve(enemyBullets.size()); for (const auto& p: enemyBullets) out.push_back(p.get()); return out; } |
| int type = QRandomGenerator::global()->bounded(0, 4); | ||
| std::unique_ptr<EnemyTank> uptr; | ||
| switch (type) { | ||
| case 0: |
| if (m_model) m_model->ModifyScore(delta); | ||
| } | ||
|
|
||
| void GameScane::keyPressEvent(QKeyEvent* event) { |
| m_livesText->setPlainText(QString("Lives: %1").arg(lives)); | ||
| m_livesText->setDefaultTextColor(Qt::white); | ||
| m_livesText->setFont(QFont("Arial", 16, QFont::Bold)); | ||
| m_livesText->setPos(5, 5); |
| if (dir == Direction::Up || dir == Direction::Down) { | ||
| qreal bx1 = pos.x() + GetWidth() / 2 - 8; | ||
| qreal bx2 = pos.x() + GetWidth() / 2 + 8; | ||
| qreal by = (dir == Direction::Up) ? pos.y() - 3 : pos.y() + GetHeight() - 3; |
There was a problem hiding this comment.
под такое коммент надо, магия какая то)
No description provided.