Set png for the tile instead of QBrush

# To do:
- Upgrade des tours
- Equilibrage
- Dasagne
- Easter egg
- I'm a teapot
This commit is contained in:
2024-05-01 17:29:39 +02:00
parent 890fd479e1
commit a6d40558ec
10 changed files with 34 additions and 45 deletions

BIN
ressources/end.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 591 B

BIN
ressources/other.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 594 B

BIN
ressources/road.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 595 B

BIN
ressources/start.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 595 B

BIN
ressources/tower.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 595 B

View File

@@ -21,8 +21,9 @@ void Map::generateMap(const int width, const int height) {
// Create a new Start tile
Tile* startTile = new Tile(Tile::Start);
startTile->setRect(x * 50, y * 50, 50, 50);
startTile->getGraphics()->setPos(x * 50, y * 50);
tiles[y][x] = startTile;
addItem(startTile);
addItem(startTile->getGraphics());
length++;
while (length < pathLength){
@@ -43,16 +44,18 @@ void Map::generateMap(const int width, const int height) {
// Create a new Road tile
Tile* tile = new Tile(Tile::Road);
tile->setRect(x * 50, y * 50, 50, 50);
tile->getGraphics()->setPos(x * 50, y * 50);
tiles[y][x] = tile;
addItem(tile);
addItem(tile->getGraphics());
length++;
}
// Create a new End tile
Tile* endTile = new Tile(Tile::End);
endTile->setRect(x * 50, y * 50, 50, 50);
endTile->getGraphics()->setPos(x * 50, y * 50);
tiles[y][x] = endTile;
addItem(endTile);
addItem(endTile->getGraphics());
// Fill the rest of the map with Other tiles
for (int i = 0; i < height; i++){
@@ -60,8 +63,9 @@ void Map::generateMap(const int width, const int height) {
if (tiles[i][j] == nullptr){
Tile* tile = new Tile(Tile::Other);
tile->setRect(j * 50, i * 50, 50, 50);
tile->getGraphics()->setPos(j * 50, i * 50);
tiles[i][j] = tile;
addItem(tile);
addItem(tile->getGraphics());
}
}
}
@@ -70,22 +74,6 @@ void Map::generateMap(const int width, const int height) {
this->height = height;
}
void Map::show() {
// Display the map in the main window
setSceneRect(0, 0, 1280, 720);
setBackgroundBrush(QBrush(Qt::black));
// Show the tiles
for (const auto& row : tiles){
for (Tile* tile : row){
if (tile != nullptr){
tile->show();
}
}
}
}
Tile* Map::getEndTile() {
for (int i = 0; i < tiles.size(); i++){
for (int j = 0; j < tiles[i].size(); j++){

View File

@@ -12,7 +12,6 @@ class Map : public QGraphicsScene
public:
Map(QObject* parent = nullptr);
void generateMap(int width, int height);
void show();
Tile* getEndTile();
Tile* getStartTile();
QGraphicsScene scene;

View File

@@ -3,26 +3,9 @@
//
#include "Tile.h"
#include <QBrush>
Tile::Tile(Tile::Type type, QGraphicsItem *parent) : QGraphicsRectItem(parent), type(type) {
switch (type) {
case Road:
setBrush(QBrush(Qt::gray));
break;
case Start:
setBrush(QBrush(Qt::green));
break;
case End:
setBrush(QBrush(Qt::red));
break;
case Tower:
setBrush(QBrush(Qt::blue));
break;
case Other:
setBrush(QBrush(Qt::yellow));
break;
}
setType(type);
}
Tile::Type Tile::getType() const {
@@ -43,21 +26,35 @@ bool Tile::isPath() {
void Tile::setType(Tile::Type type) {
this->type = type;
QPixmap pixmap;
switch (type) {
case Road:
setBrush(QBrush(Qt::gray));
pixmap = QPixmap(QString::fromStdString("../ressources/road.png"));
break;
case Start:
setBrush(QBrush(Qt::green));
pixmap = QPixmap(QString::fromStdString("../ressources/start.png"));
break;
case End:
setBrush(QBrush(Qt::red));
pixmap = QPixmap(QString::fromStdString("../ressources/end.png"));
break;
case Tower:
setBrush(QBrush(Qt::blue));
pixmap = QPixmap(QString::fromStdString("../ressources/tower.png"));
break;
case Other:
setBrush(QBrush(Qt::yellow));
pixmap = QPixmap(QString::fromStdString("../ressources/other.png"));
break;
}
if (pixmap.isNull()) {
return;
} else {
graphics = new QGraphicsPixmapItem();
QPixmap scaledPixmap = pixmap.scaled(50, 50, Qt::KeepAspectRatio, Qt::SmoothTransformation); // Scale the pixmap to 50x50 pixels
graphics->setPixmap(scaledPixmap);
graphics->setPos(x() * 50, y() * 50);
graphics->setZValue(1);
}
}
QGraphicsPixmapItem* Tile::getGraphics() const {
return graphics;
}

View File

@@ -5,6 +5,9 @@
#ifndef POULPES_DE_L_ESPACE_LA_DERNIERE_LIGNE_DE_DEFENSE_TILE_H
#define POULPES_DE_L_ESPACE_LA_DERNIERE_LIGNE_DE_DEFENSE_TILE_H
#include <QGraphicsRectItem>
#include <QBrush>
#include <QGraphicsPixmapItem>
#include <QPixmap>
class Tile : public QGraphicsRectItem
{
@@ -16,9 +19,11 @@ public:
int gridY();
bool isPath();
void setType(Type type);
QGraphicsPixmapItem* getGraphics() const;
private:
Type type;
QGraphicsPixmapItem* graphics;
};

View File

@@ -67,7 +67,7 @@ LaserTower::LaserTower(QPointF position, Game& game) : Tower(10, 1, 10, 1, 50, p
QPixmap scaledPixmap = pixmap.scaled(50, 50, Qt::KeepAspectRatio, Qt::SmoothTransformation); // Scale the pixmap to 50x50 pixels
graphics->setPixmap(scaledPixmap);
graphics->setPos(x * 50, y * 50);
graphics->setZValue(1);
graphics->setZValue(2);
}
}