mirror of
https://github.com/BreizhHardware/Poulpes-de-l-Espace-La-derniere-ligne-de-Defense.git
synced 2026-01-18 16:27:20 +01:00
Add player and enemy (player not displayed due to a bug need to be fixed later) (enemy spawn not tested) player movement currently bugged (input not detected)
This commit is contained in:
@@ -23,6 +23,12 @@ add_executable(Poulpes_de_l_espace_La_derniere_ligne_de_defense ${sourceCode}
|
||||
src/Rules.cpp
|
||||
src/Rules.h
|
||||
src/Leaderboard.cpp
|
||||
src/Leaderboard.h)
|
||||
src/Leaderboard.h
|
||||
src/Mob.cpp
|
||||
src/Mob.h
|
||||
src/Enemy.cpp
|
||||
src/Enemy.h
|
||||
src/Player.cpp
|
||||
src/Player.h)
|
||||
|
||||
target_link_libraries(Poulpes_de_l_espace_La_derniere_ligne_de_defense PRIVATE Qt6::Widgets Qt6::Sql)
|
||||
|
||||
11
src/Enemy.cpp
Normal file
11
src/Enemy.cpp
Normal file
@@ -0,0 +1,11 @@
|
||||
//
|
||||
// Created by breizhhardware on 26/04/24.
|
||||
//
|
||||
|
||||
#include "Enemy.h"
|
||||
|
||||
Enemy::Enemy(int health, int shield, int damage, float regenerationRate, int speed, std::string avatarPath, int x, int y, int coinDrop, int weight)
|
||||
: Mob(health, shield, damage, regenerationRate, speed, avatarPath, x, y) {
|
||||
this->coinDrop = coinDrop;
|
||||
this->weight = weight;
|
||||
}
|
||||
18
src/Enemy.h
Normal file
18
src/Enemy.h
Normal file
@@ -0,0 +1,18 @@
|
||||
//
|
||||
// Created by breizhhardware on 26/04/24.
|
||||
//
|
||||
|
||||
#ifndef POULPES_DE_L_ESPACE_LA_DERNIERE_LIGNE_DE_DEFENSE_ENEMY_H
|
||||
#define POULPES_DE_L_ESPACE_LA_DERNIERE_LIGNE_DE_DEFENSE_ENEMY_H
|
||||
#include "Mob.h"
|
||||
|
||||
class Enemy : public Mob
|
||||
{
|
||||
public:
|
||||
Enemy(int health, int shield, int damage, float regenerationRate, int speed, std::string avatarPath, int x, int y, int coinDrop, int weight);
|
||||
int coinDrop;
|
||||
int weight;
|
||||
};
|
||||
|
||||
|
||||
#endif //POULPES_DE_L_ESPACE_LA_DERNIERE_LIGNE_DE_DEFENSE_ENEMY_H
|
||||
39
src/Game.cpp
39
src/Game.cpp
@@ -9,31 +9,48 @@
|
||||
Game::Game(){
|
||||
userGold = 0;
|
||||
waveNumber = 0;
|
||||
this->setFocusPolicy(Qt::StrongFocus);
|
||||
player = new Player(100, 0, 10, 10, 1, "", 0, 0);
|
||||
}
|
||||
|
||||
void Game::start() {
|
||||
// Create the map
|
||||
gameMap.generateMap(25, 14);
|
||||
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;
|
||||
|
||||
// Set the player position to the end tile
|
||||
Tile* endTile = gameMap.getEndTile();
|
||||
player->setPosition(endTile);
|
||||
|
||||
// Start the game timer
|
||||
gameTimer.start(1000);
|
||||
std::cout << "Game timer started" << std::endl;
|
||||
player->getPlayerPosition();
|
||||
}
|
||||
|
||||
void Game::show() {
|
||||
// Show the map
|
||||
gameMap.show();
|
||||
std::cout << "Map shown" << std::endl;
|
||||
|
||||
auto* view = new QGraphicsView(&gameMap);
|
||||
view->show();
|
||||
void Game::keyPressEvent(QKeyEvent *event) {
|
||||
switch (event->key()) {
|
||||
case Qt::Key_Left:
|
||||
player->setPosition(player->getX() - 1, player->getY());
|
||||
player->getPlayerPosition();
|
||||
break;
|
||||
case Qt::Key_Right:
|
||||
player->setPosition(player->getX() + 1, player->getY());
|
||||
player->getPlayerPosition();
|
||||
break;
|
||||
case Qt::Key_Up:
|
||||
player->setPosition(player->getX(), player->getY() - 1);
|
||||
player->getPlayerPosition();
|
||||
break;
|
||||
case Qt::Key_Down:
|
||||
player->setPosition(player->getX(), player->getY() + 1);
|
||||
player->getPlayerPosition();
|
||||
break;
|
||||
default:
|
||||
QGraphicsView::keyPressEvent(event);
|
||||
}
|
||||
}
|
||||
14
src/Game.h
14
src/Game.h
@@ -5,20 +5,28 @@
|
||||
#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 <QGraphicsScene>
|
||||
#include <QGraphicsView>
|
||||
#include <QKeyEvent>
|
||||
#include "Map.h"
|
||||
#include "Player.h"
|
||||
|
||||
class Game
|
||||
class Game : public QGraphicsView
|
||||
{
|
||||
public:
|
||||
Game();
|
||||
void start();
|
||||
void show();
|
||||
Map gameMap;
|
||||
void setPlayerPosition(int x, int y);
|
||||
|
||||
private:
|
||||
int userGold;
|
||||
QTimer gameTimer;
|
||||
Map gameMap;
|
||||
int waveNumber;
|
||||
Player* player;
|
||||
|
||||
protected:
|
||||
void keyPressEvent(QKeyEvent* event) override;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -20,7 +20,17 @@ MainWindow::~MainWindow(){
|
||||
|
||||
void MainWindow::slot_aboutMenu(){
|
||||
QMessageBox msgBox;
|
||||
msgBox.setText("A small QT/C++ projet...");
|
||||
msgBox.setText("Poulpes de l'espace: La dernière ligne de défense<br><br>"
|
||||
"Version 1.0<br><br>"
|
||||
"A game by BreizhHardware and Amalizzy<br><br>"
|
||||
"2024<br><br>"
|
||||
"This game is a tower defense game where you have to defend your base from the incoming waves of enemies.<br><br>"
|
||||
"This game was made for the C++ course at the ISEN Nantes.<br><br>"
|
||||
"The ships design is mainly inspired by the game Star Citizen.<br><br>"
|
||||
"The game is open source and can be found on GitHub at the following link:<br>"
|
||||
"<a href='https://github.com/BreizhHardware/Poulpes-de-l-Espace-La-derniere-ligne-de-Defense'>https://github.com/BreizhHardware/Poulpes-de-l-Espace-La-derniere-ligne-de-Defense</a><br><br>"
|
||||
"Enjoy the game!");
|
||||
msgBox.setTextFormat(Qt::RichText); // this is needed to interpret the text as HTML
|
||||
msgBox.setModal(true); // on souhaite que la fenetre soit modale i.e qu'on ne puisse plus cliquer ailleurs
|
||||
msgBox.exec();
|
||||
}
|
||||
22
src/Map.cpp
22
src/Map.cpp
@@ -81,4 +81,26 @@ void Map::show() {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Tile* Map::getEndTile() {
|
||||
for (int i = 0; i < tiles.size(); i++){
|
||||
for (int j = 0; j < tiles[i].size(); j++){
|
||||
if (tiles[i][j]->getType() == Tile::End){
|
||||
return tiles[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Tile* Map::getStartTile() {
|
||||
for (int i = 0; i < tiles.size(); i++){
|
||||
for (int j = 0; j < tiles[i].size(); j++){
|
||||
if (tiles[i][j]->getType() == Tile::Start){
|
||||
return tiles[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
@@ -16,6 +16,10 @@ public:
|
||||
|
||||
void show();
|
||||
|
||||
Tile* getEndTile();
|
||||
Tile* getStartTile();
|
||||
|
||||
|
||||
private:
|
||||
QVector<QVector<Tile*>> tiles;
|
||||
};
|
||||
|
||||
39
src/Menu.cpp
39
src/Menu.cpp
@@ -11,7 +11,9 @@
|
||||
#include <iostream>
|
||||
|
||||
Menu::Menu(QWidget *parent) : QWidget(parent) {
|
||||
game = nullptr;
|
||||
auto* layout = new QVBoxLayout(this);
|
||||
view = new QGraphicsView(this);
|
||||
|
||||
playButton = new QPushButton("Play", this);
|
||||
connect(playButton, &QPushButton::clicked, this, &Menu::onPlayButtonClicked);
|
||||
@@ -28,23 +30,39 @@ Menu::Menu(QWidget *parent) : QWidget(parent) {
|
||||
quitButton = new QPushButton("Quit", this);
|
||||
connect(quitButton, &QPushButton::clicked, this, &Menu::onQuitButtonClicked);
|
||||
layout->addWidget(quitButton);
|
||||
|
||||
this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
||||
}
|
||||
|
||||
void Menu::removeButtons() {
|
||||
playButton->hide();
|
||||
rulesButton->hide();
|
||||
leaderboardButton->hide();
|
||||
quitButton->hide();
|
||||
}
|
||||
|
||||
void Menu::onPlayButtonClicked() {
|
||||
// Remove buttons
|
||||
removeButtons();
|
||||
// Set the view size
|
||||
view->setFixedSize(1280, 720);
|
||||
view->show();
|
||||
// Create a new game
|
||||
auto* game = new Game();
|
||||
game = new Game();
|
||||
game->start();
|
||||
|
||||
// Hide the menu
|
||||
hide();
|
||||
|
||||
// Show the game
|
||||
game->show();
|
||||
view->setScene(&game->gameMap);
|
||||
view->setFocus(); // Set focus to the QGraphicsView
|
||||
}
|
||||
|
||||
void Menu::onRulesButtonClicked() {
|
||||
removeButtons();
|
||||
auto* rules = new Rules();
|
||||
rules->show();
|
||||
view->setFixedSize(1280, 720);
|
||||
view->show();
|
||||
view->setScene(rules);
|
||||
QObject::connect(rules, &Rules::returnToMenuSignal, this, &Menu::showMenu);
|
||||
}
|
||||
|
||||
void Menu::onLeaderboardButtonClicked() {
|
||||
@@ -56,3 +74,12 @@ void Menu::onQuitButtonClicked() {
|
||||
QApplication::quit();
|
||||
}
|
||||
|
||||
void Menu::showMenu() {
|
||||
view->hide();
|
||||
this->setVisible(true);
|
||||
this->raise();
|
||||
playButton->show();
|
||||
rulesButton->show();
|
||||
leaderboardButton->show();
|
||||
quitButton->show();
|
||||
}
|
||||
@@ -4,8 +4,10 @@
|
||||
|
||||
#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 "Game.h"
|
||||
#include <QWidget>
|
||||
#include <QPushButton>
|
||||
#include <QGraphicsView>
|
||||
|
||||
class Menu : public QWidget {
|
||||
Q_OBJECT
|
||||
@@ -13,6 +15,9 @@ class Menu : public QWidget {
|
||||
public:
|
||||
Menu(QWidget* parent = nullptr);
|
||||
|
||||
public slots:
|
||||
void showMenu();
|
||||
|
||||
private slots:
|
||||
void onPlayButtonClicked();
|
||||
void onRulesButtonClicked();
|
||||
@@ -24,6 +29,9 @@ private:
|
||||
QPushButton* rulesButton;
|
||||
QPushButton* leaderboardButton;
|
||||
QPushButton* quitButton;
|
||||
QGraphicsView* view;
|
||||
Game* game;
|
||||
void removeButtons();
|
||||
};
|
||||
|
||||
|
||||
|
||||
16
src/Mob.cpp
Normal file
16
src/Mob.cpp
Normal file
@@ -0,0 +1,16 @@
|
||||
//
|
||||
// Created by breizhhardware on 26/04/24.
|
||||
//
|
||||
|
||||
#include "Mob.h"
|
||||
|
||||
Mob::Mob(int health, int shield, int damage, float regenerationRate, int speed, std::string avatarPath, int x, int y){
|
||||
this->health = health;
|
||||
this->shield = shield;
|
||||
this->damage = damage;
|
||||
this->regenerationRate = regenerationRate;
|
||||
this->speed = speed;
|
||||
this->avatarPath = avatarPath;
|
||||
this->x = x;
|
||||
this->y = y;
|
||||
}
|
||||
26
src/Mob.h
Normal file
26
src/Mob.h
Normal file
@@ -0,0 +1,26 @@
|
||||
//
|
||||
// Created by breizhhardware on 26/04/24.
|
||||
//
|
||||
|
||||
#ifndef POULPES_DE_L_ESPACE_LA_DERNIERE_LIGNE_DE_DEFENSE_MOB_H
|
||||
#define POULPES_DE_L_ESPACE_LA_DERNIERE_LIGNE_DE_DEFENSE_MOB_H
|
||||
#include <string>
|
||||
|
||||
class Mob {
|
||||
protected:
|
||||
int health;
|
||||
int shield;
|
||||
int damage;
|
||||
float regenerationRate;
|
||||
int speed;
|
||||
std::string avatarPath;
|
||||
int x;
|
||||
int y;
|
||||
|
||||
public:
|
||||
Mob(int health, int shield, int damage, float regenerationRate, int speed, std::string avatarPath, int x, int y);
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif //POULPES_DE_L_ESPACE_LA_DERNIERE_LIGNE_DE_DEFENSE_MOB_H
|
||||
41
src/Player.cpp
Normal file
41
src/Player.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
//
|
||||
// Created by breizhhardware on 26/04/24.
|
||||
//
|
||||
|
||||
#include "Player.h"
|
||||
#include <iostream>
|
||||
|
||||
Player::Player(int health, int shield, int damage, float regenerationRate, int speed, std::string avatarPath, int x, int y)
|
||||
: Mob(health, shield, damage, regenerationRate, speed, avatarPath, x, y) {
|
||||
this->x = x;
|
||||
this->y = y;
|
||||
graphics = new QGraphicsEllipseItem(x, y, 10, 10);
|
||||
graphics->setBrush(QBrush(Qt::blue));
|
||||
}
|
||||
void Player::setPosition(Tile* tile) {
|
||||
this->x = tile->gridX();
|
||||
this->y = tile->gridY();
|
||||
graphics->setRect(x * 50, y * 50, 50, 50);
|
||||
}
|
||||
|
||||
void Player::setPosition(int x, int y) {
|
||||
this->x = x;
|
||||
this->y = y;
|
||||
graphics->setRect(x * 50, y * 50, 50, 50);
|
||||
}
|
||||
|
||||
void Player::getPlayerPosition() {
|
||||
std::cout << "Player position: x: " << x << " y: " << y << std::endl;
|
||||
}
|
||||
|
||||
QGraphicsEllipseItem* Player::getGraphics() {
|
||||
return graphics;
|
||||
}
|
||||
|
||||
int Player::getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
int Player::getY() {
|
||||
return y;
|
||||
}
|
||||
28
src/Player.h
Normal file
28
src/Player.h
Normal file
@@ -0,0 +1,28 @@
|
||||
//
|
||||
// Created by breizhhardware on 26/04/24.
|
||||
//
|
||||
|
||||
#ifndef POULPES_DE_L_ESPACE_LA_DERNIERE_LIGNE_DE_DEFENSE_PLAYER_H
|
||||
#define POULPES_DE_L_ESPACE_LA_DERNIERE_LIGNE_DE_DEFENSE_PLAYER_H
|
||||
#include "Mob.h"
|
||||
#include "Tile.h"
|
||||
#include <QGraphicsEllipseItem>
|
||||
#include <QBrush>
|
||||
|
||||
class Player : public Mob
|
||||
{
|
||||
public:
|
||||
Player(int health, int shield, int damage, float regenerationRate, int speed, std::string avatarPath, int x, int y);
|
||||
void setPosition(Tile* tile);
|
||||
void setPosition(int x, int y);
|
||||
void getPlayerPosition();
|
||||
int getX();
|
||||
int getY();
|
||||
QGraphicsEllipseItem* getGraphics();
|
||||
|
||||
private:
|
||||
QGraphicsEllipseItem* graphics;
|
||||
};
|
||||
|
||||
|
||||
#endif //POULPES_DE_L_ESPACE_LA_DERNIERE_LIGNE_DE_DEFENSE_PLAYER_H
|
||||
@@ -4,16 +4,22 @@
|
||||
|
||||
#include "Rules.h"
|
||||
#include <QVBoxLayout>
|
||||
#include <QLayout>
|
||||
#include <iostream>
|
||||
|
||||
Rules::Rules(QWidget *parent) : QWidget(parent) {
|
||||
QVBoxLayout* layout = new QVBoxLayout(this);
|
||||
Rules::Rules(QObject* parent) : QGraphicsScene(parent) {
|
||||
auto* rulesText = new QGraphicsTextItem();
|
||||
rulesText->setPlainText("Rules are simple:\n - You have to defend your base from the incoming waves of enemies.\n - You can place towers on the map to help you defend.\n - You can also move your personnal ship to help defend using the arrow key.\n - Good luck!");
|
||||
addItem(rulesText);
|
||||
// Add the return to menu button
|
||||
auto* returnButton = new QPushButton("Return to menu");
|
||||
QGraphicsProxyWidget* proxy = this->addWidget(returnButton);
|
||||
proxy->setPos(0, rulesText->boundingRect().height());
|
||||
// Connect the return to menu button to the returnToMenu slot
|
||||
connect(returnButton, &QPushButton::clicked, this, &Rules::returnToMenu);
|
||||
}
|
||||
|
||||
void Rules::returnToMenu() {
|
||||
emit returnToMenuSignal();
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
18
src/Rules.h
18
src/Rules.h
@@ -5,17 +5,25 @@
|
||||
#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>
|
||||
#include <QGraphicsTextItem>
|
||||
#include <QGraphicsScene>
|
||||
#include <QPushButton>
|
||||
#include <QGraphicsProxyWidget>
|
||||
|
||||
class Rules : public QWidget
|
||||
class Rules : public QGraphicsScene
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
Rules(QWidget* parent = nullptr);
|
||||
Rules(QObject* parent = nullptr);
|
||||
|
||||
private:
|
||||
QLabel* rulesLabel;
|
||||
QGraphicsTextItem* rulesLabel;
|
||||
|
||||
private slots:
|
||||
void returnToMenu();
|
||||
|
||||
signals:
|
||||
void returnToMenuSignal();
|
||||
};
|
||||
|
||||
|
||||
#endif //POULPES_DE_L_ESPACE_LA_DERNIERE_LIGNE_DE_DEFENSE_RULES_H
|
||||
|
||||
@@ -27,4 +27,12 @@ Tile::Tile(Tile::Type type, QGraphicsItem *parent) : QGraphicsRectItem(parent),
|
||||
|
||||
Tile::Type Tile::getType() const {
|
||||
return type;
|
||||
}
|
||||
|
||||
int Tile::gridX() {
|
||||
return rect().x() / 50;
|
||||
}
|
||||
|
||||
int Tile::gridY() {
|
||||
return rect().y() / 50;
|
||||
}
|
||||
@@ -15,6 +15,9 @@ public:
|
||||
|
||||
Type getType() const;
|
||||
|
||||
int gridX();
|
||||
int gridY();
|
||||
|
||||
private:
|
||||
Type type;
|
||||
};
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
#include <QApplication>
|
||||
#include <QObject>
|
||||
#include "MainWindow.h"
|
||||
#include "Menu.h"
|
||||
#include "Rules.h"
|
||||
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
QApplication application(argc, argv);
|
||||
|
||||
Reference in New Issue
Block a user