Start of the game (tiles don't show maybe a kde neon issues due to theme will be check on windows)

This commit is contained in:
2024-04-26 12:28:47 +02:00
parent 960f5c7a87
commit 76f3caf9ae
17 changed files with 486 additions and 54 deletions

View File

@@ -1,50 +1,28 @@
cmake_minimum_required(VERSION 3.25)
project(Poulpes_de_l_Espace_La_derniere_ligne_de_Defense)
cmake_minimum_required(VERSION 3.18)
project(Poulpes_de_l_espace_La_derniere_ligne_de_defense LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
find_package(Qt6 REQUIRED COMPONENTS Widgets Sql)
find_package(Qt6 COMPONENTS
Core
Gui
Widgets
REQUIRED)
file(GLOB_RECURSE sourceCode src/*.cpp src/*.h)
add_executable(Poulpes_de_l_Espace_La_derniere_ligne_de_Defense main.cpp)
target_link_libraries(Poulpes_de_l_Espace_La_derniere_ligne_de_Defense
Qt::Core
Qt::Gui
Qt::Widgets
)
qt_standard_project_setup()
if (WIN32 AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
set(DEBUG_SUFFIX)
if (MSVC AND CMAKE_BUILD_TYPE MATCHES "Debug")
set(DEBUG_SUFFIX "d")
endif ()
set(QT_INSTALL_PATH "${CMAKE_PREFIX_PATH}")
if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
endif ()
endif ()
if (EXISTS "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll")
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory
"$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
"${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll"
"$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
endif ()
foreach (QT_LIB Core Gui Widgets)
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
"${QT_INSTALL_PATH}/bin/Qt6${QT_LIB}${DEBUG_SUFFIX}.dll"
"$<TARGET_FILE_DIR:${PROJECT_NAME}>")
endforeach (QT_LIB)
endif ()
add_executable(Poulpes_de_l_espace_La_derniere_ligne_de_defense ${sourceCode}
src/Tile.cpp
src/Tile.h
src/Map.cpp
src/Map.h
src/Menu.cpp
src/Menu.h
src/Game.cpp
src/Game.h
src/Rules.cpp
src/Rules.h
src/Leaderboard.cpp
src/Leaderboard.h)
target_link_libraries(Poulpes_de_l_espace_La_derniere_ligne_de_defense PRIVATE Qt6::Widgets Qt6::Sql)

View File

@@ -1,11 +0,0 @@
#include <QApplication>
#include <QPushButton>
int main(int argc, char* argv[]) {
QApplication a(argc, argv);
QApplication::setApplicationName("Poules de l'espace: La dernière ligne de défense");
QPushButton button("Hello world!", nullptr);
button.resize(200, 100);
button.show();
return QApplication::exec();
}

35
src/Game.cpp Normal file
View File

@@ -0,0 +1,35 @@
//
// Created by breizhhardware on 26/04/24.
//
#include "Game.h"
#include <iostream>
Game::Game(){
userGold = 0;
waveNumber = 0;
}
void Game::start() {
// Create the map
gameMap.generateMap(1280, 720);
std::cout << "Map generated" << std::endl;
// Set the user gold
userGold = 100;
std::cout << "User gold set" << std::endl;
// Set the wave number
waveNumber = 1;
std::cout << "Wave number set" << std::endl;
// Start the game timer
gameTimer.start(1000);
std::cout << "Game timer started" << std::endl;
}
void Game::show() {
// Show the map
gameMap.show();
std::cout << "Map shown" << std::endl;
}

25
src/Game.h Normal file
View File

@@ -0,0 +1,25 @@
//
// Created by breizhhardware on 26/04/24.
//
#ifndef POULPES_DE_L_ESPACE_LA_DERNIERE_LIGNE_DE_DEFENSE_GAME_H
#define POULPES_DE_L_ESPACE_LA_DERNIERE_LIGNE_DE_DEFENSE_GAME_H
#include <QTimer>
#include "Map.h"
class Game
{
public:
Game();
void start();
void show();
private:
int userGold;
QTimer gameTimer;
Map gameMap;
int waveNumber;
};
#endif //POULPES_DE_L_ESPACE_LA_DERNIERE_LIGNE_DE_DEFENSE_GAME_H

29
src/Leaderboard.cpp Normal file
View File

@@ -0,0 +1,29 @@
//
// Created by breizhhardware on 26/04/24.
//
#include "Leaderboard.h"
#include <QVBoxLayout>
#include <QSqlDatabase>
Leaderboard::Leaderboard(QWidget *parent) : QWidget(parent) {
QVBoxLayout* layout = new QVBoxLayout(this);
leaderboardTable = new QTableView(this);
layout->addWidget(leaderboardTable);
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("leaderboard.db");
if (!db.open()) {
qDebug() << "Error: unable to open database";
}
leaderboardModel = new QSqlTableModel(this, db);
leaderboardModel->setTable("leaderboard");
// Sort by number of waves completed in descending order
leaderboardModel->setSort(2, Qt::DescendingOrder);
leaderboardModel->select();
leaderboardTable->setModel(leaderboardModel);
}

23
src/Leaderboard.h Normal file
View File

@@ -0,0 +1,23 @@
//
// Created by breizhhardware on 26/04/24.
//
#ifndef POULPES_DE_L_ESPACE_LA_DERNIERE_LIGNE_DE_DEFENSE_LEADERBOARD_H
#define POULPES_DE_L_ESPACE_LA_DERNIERE_LIGNE_DE_DEFENSE_LEADERBOARD_H
#include <QWidget>
#include <QTableView>
#include <QtSql/QSqlTableModel>
class Leaderboard : public QWidget
{
Q_OBJECT
public:
Leaderboard(QWidget* parent = nullptr);
private:
QTableView* leaderboardTable;
QSqlTableModel* leaderboardModel;
};
#endif //POULPES_DE_L_ESPACE_LA_DERNIERE_LIGNE_DE_DEFENSE_LEADERBOARD_H

26
src/MainWindow.cpp Normal file
View File

@@ -0,0 +1,26 @@
#include "MainWindow.h"
#include "Menu.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
Menu* menu = new Menu(this);
this->setCentralWidget(menu);
this->setWindowTitle("Poulpes de l'espace: La dernière ligne de défense");
this->resize(1280, 720);
helpMenu = menuBar()->addMenu(tr("&Help"));
QAction* actionHelp = new QAction(tr("&About"), this);
connect(actionHelp, SIGNAL(triggered()), this, SLOT(slot_aboutMenu()));
helpMenu->addAction(actionHelp);
}
MainWindow::~MainWindow(){
}
void MainWindow::slot_aboutMenu(){
QMessageBox msgBox;
msgBox.setText("A small QT/C++ projet...");
msgBox.setModal(true); // on souhaite que la fenetre soit modale i.e qu'on ne puisse plus cliquer ailleurs
msgBox.exec();
}

29
src/MainWindow.h Normal file
View File

@@ -0,0 +1,29 @@
#ifndef CPP_QT_TPMINIPROJET_MAINWINDOW_H
#define CPP_QT_TPMINIPROJET_MAINWINDOW_H
#include <QMainWindow>
#include <QGraphicsView>
#include <QMenu>
#include <QMenuBar>
#include <QAction>
#include <QMessageBox>
#include "Game.h"
class MainWindow : public QMainWindow {
Q_OBJECT
private :
QGraphicsView* mainView;
QMenu* helpMenu;
public:
MainWindow(QWidget* parent = nullptr);
virtual ~MainWindow();
public slots:
void slot_aboutMenu();
};
#endif //CPP_QT_TPMINIPROJET_MAINWINDOW_H

84
src/Map.cpp Normal file
View File

@@ -0,0 +1,84 @@
//
// Created by breizhhardware on 26/04/24.
//
#include "Map.h"
Map::Map(QObject *parent) : QGraphicsScene(parent) {
}
void Map::generateMap(int width, int height) {
tiles = QVector<QVector<Tile*>>(height, QVector<Tile*>(width));
int x = 0, y = height - 1;
// Length of the path
int length = 0;
// Create a new Start tile
Tile* startTile = new Tile(Tile::Start);
startTile->setRect(x * 50, y * 50, 50, 50);
tiles[y][x] = startTile;
addItem(startTile);
length++;
while (length < 100){
// Create a new Road tile
Tile* tile = new Tile(Tile::Road);
tile->setRect(x * 50, y * 50, 50, 50);
tiles[y][x] = tile;
addItem(tile);
length++;
// Move horizontally to the right edge
while (x < width -1 && length < 100){
x++;
Tile* tile = new Tile(Tile::Road);
tile->setRect(x * 50, y * 50, 50, 50);
tiles[y][x] = tile;
addItem(tile);
length++;
}
// Move vertically to the top edge
while (y > 0 && length < 100){
y--;
Tile* tile = new Tile(Tile::Road);
tile->setRect(x * 50, y * 50, 50, 50);
tiles[y][x] = tile;
addItem(tile);
length++;
}
// Create a new End tile
Tile* endTile = new Tile(Tile::End);
endTile->setRect(x * 50, y * 50, 50, 50);
tiles[y][x] = endTile;
addItem(endTile);
// Fill the rest of the map with Other tiles
for (int i = 0; i < height; i++){
for (int j = 0; j < width; j++){
if (tiles[i][j] == nullptr){
Tile* tile = new Tile(Tile::Other);
tile->setRect(j * 50, i * 50, 50, 50);
tiles[i][j] = tile;
addItem(tile);
}
}
}
}
}
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();
}
}
}
}

24
src/Map.h Normal file
View File

@@ -0,0 +1,24 @@
//
// Created by breizhhardware on 26/04/24.
//
#ifndef POULPES_DE_L_ESPACE_LA_DERNIERE_LIGNE_DE_DEFENSE_MAP_H
#define POULPES_DE_L_ESPACE_LA_DERNIERE_LIGNE_DE_DEFENSE_MAP_H
#include "Tile.h"
#include <QGraphicsScene>
class Map : public QGraphicsScene
{
public:
Map(QObject* parent = nullptr);
void generateMap(int width, int height);
void show();
private:
QVector<QVector<Tile*>> tiles;
};
#endif //POULPES_DE_L_ESPACE_LA_DERNIERE_LIGNE_DE_DEFENSE_MAP_H

58
src/Menu.cpp Normal file
View File

@@ -0,0 +1,58 @@
//
// Created by breizhhardware on 26/04/24.
//
#include "Menu.h"
#include "Game.h"
#include "Rules.h"
#include "Leaderboard.h"
#include <QVBoxLayout>
#include <QApplication>
#include <iostream>
Menu::Menu(QWidget *parent) : QWidget(parent) {
auto* layout = new QVBoxLayout(this);
playButton = new QPushButton("Play", this);
connect(playButton, &QPushButton::clicked, this, &Menu::onPlayButtonClicked);
layout->addWidget(playButton);
rulesButton = new QPushButton("Rules", this);
connect(rulesButton, &QPushButton::clicked, this, &Menu::onRulesButtonClicked);
layout->addWidget(rulesButton);
leaderboardButton = new QPushButton("Leaderboard", this);
connect(leaderboardButton, &QPushButton::clicked, this, &Menu::onLeaderboardButtonClicked);
layout->addWidget(leaderboardButton);
quitButton = new QPushButton("Quit", this);
connect(quitButton, &QPushButton::clicked, this, &Menu::onQuitButtonClicked);
layout->addWidget(quitButton);
}
void Menu::onPlayButtonClicked() {
// Create a new game
auto* game = new Game();
game->start();
// Hide the menu
hide();
// Show the game
game->show();
}
void Menu::onRulesButtonClicked() {
auto* rules = new Rules();
rules->show();
}
void Menu::onLeaderboardButtonClicked() {
auto* leaderboard = new Leaderboard();
leaderboard->show();
}
void Menu::onQuitButtonClicked() {
QApplication::quit();
}

30
src/Menu.h Normal file
View File

@@ -0,0 +1,30 @@
//
// Created by breizhhardware on 26/04/24.
//
#ifndef POULPES_DE_L_ESPACE_LA_DERNIERE_LIGNE_DE_DEFENSE_MENU_H
#define POULPES_DE_L_ESPACE_LA_DERNIERE_LIGNE_DE_DEFENSE_MENU_H
#include <QWidget>
#include <QPushButton>
class Menu : public QWidget {
Q_OBJECT
public:
Menu(QWidget* parent = nullptr);
private slots:
void onPlayButtonClicked();
void onRulesButtonClicked();
void onLeaderboardButtonClicked();
void onQuitButtonClicked();
private:
QPushButton* playButton;
QPushButton* rulesButton;
QPushButton* leaderboardButton;
QPushButton* quitButton;
};
#endif //POULPES_DE_L_ESPACE_LA_DERNIERE_LIGNE_DE_DEFENSE_MENU_H

19
src/Rules.cpp Normal file
View File

@@ -0,0 +1,19 @@
//
// Created by breizhhardware on 26/04/24.
//
#include "Rules.h"
#include <QVBoxLayout>
Rules::Rules(QWidget *parent) : QWidget(parent) {
QVBoxLayout* layout = new QVBoxLayout(this);
rulesLabel = new QLabel("Rules:\n"
"1. Place towers on the map\n"
"2. Click on the start button\n"
"3. Watch the enemies go through the map\n"
"4. If an enemy reaches the end, you lose\n"
"5. If you kill all the enemies, you win\n"
"6. Have fun!", this);
layout->addWidget(rulesLabel);
}

21
src/Rules.h Normal file
View File

@@ -0,0 +1,21 @@
//
// Created by breizhhardware on 26/04/24.
//
#ifndef POULPES_DE_L_ESPACE_LA_DERNIERE_LIGNE_DE_DEFENSE_RULES_H
#define POULPES_DE_L_ESPACE_LA_DERNIERE_LIGNE_DE_DEFENSE_RULES_H
#include <QWidget>
#include <QLabel>
class Rules : public QWidget
{
Q_OBJECT
public:
Rules(QWidget* parent = nullptr);
private:
QLabel* rulesLabel;
};
#endif //POULPES_DE_L_ESPACE_LA_DERNIERE_LIGNE_DE_DEFENSE_RULES_H

30
src/Tile.cpp Normal file
View File

@@ -0,0 +1,30 @@
//
// Created by breizhhardware on 26/04/24.
//
#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;
}
}
Tile::Type Tile::getType() const {
return type;
}

23
src/Tile.h Normal file
View File

@@ -0,0 +1,23 @@
//
// Created by breizhhardware on 26/04/24.
//
#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>
class Tile : public QGraphicsRectItem
{
public:
enum Type { Road, Start, End, Tower, Other };
Tile(Type type, QGraphicsItem* parent = nullptr);
Type getType() const;
private:
Type type;
};
#endif //POULPES_DE_L_ESPACE_LA_DERNIERE_LIGNE_DE_DEFENSE_TILE_H

9
src/main.cpp Normal file
View File

@@ -0,0 +1,9 @@
#include <QApplication>
#include "MainWindow.h"
int main(int argc, char *argv[]) {
QApplication application(argc, argv);
MainWindow mainWindow;
mainWindow.show();
return application.exec();
}