Add player display, movement

This commit is contained in:
2024-04-29 10:22:27 +02:00
parent 550bb919ab
commit b9b61daf13
12 changed files with 76 additions and 28 deletions

9
.gitignore vendored
View File

@@ -30,3 +30,12 @@
*.exe
*.out
*.app
# .idea folder
.idea/
# build folder
build/
# cmake-build-debug folder
cmake-build-debug/

View File

@@ -5,7 +5,7 @@ set(CMAKE_CXX_STANDARD 17)
set(CMAKE_AUTOMOC ON)
find_package(Qt6 REQUIRED COMPONENTS Widgets Sql)
find_package(Qt6 REQUIRED COMPONENTS Widgets Sql Gui Core)
file(GLOB_RECURSE sourceCode src/*.cpp src/*.h)

BIN
ressources/player.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 KiB

View File

@@ -5,12 +5,15 @@
#include "Game.h"
#include <QGraphicsView>
#include <iostream>
#include <QDebug>
Game::Game(){
userGold = 0;
waveNumber = 0;
this->setFocusPolicy(Qt::StrongFocus);
player = new Player(100, 0, 10, 10, 1, "", 0, 0);
player = new Player(100, 0, 10, 10, 1, "../ressources/player.png", 0, 0, gameMap);
gameMap.addItem(player->getGraphics());
this->setScene(&gameMap);
}
void Game::start() {
@@ -29,26 +32,24 @@ void Game::start() {
// Start the game timer
gameTimer.start(1000);
player->getPlayerPosition();
// Set focus to the QGraphicsView
this->setFocus();
}
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);

View File

@@ -17,7 +17,6 @@ public:
Game();
void start();
Map gameMap;
void setPlayerPosition(int x, int y);
private:
int userGold;

View File

@@ -65,6 +65,9 @@ void Map::generateMap(const int width, const int height) {
}
}
}
this->width = width;
this->height = height;
}
@@ -103,4 +106,12 @@ Tile* Map::getStartTile() {
}
}
return nullptr;
}
int Map::getWidth() {
return width;
}
int Map::getHeight() {
return height;
}

View File

@@ -19,9 +19,15 @@ public:
Tile* getEndTile();
Tile* getStartTile();
QGraphicsScene scene;
int getWidth();
int getHeight();
private:
QVector<QVector<Tile*>> tiles;
int width;
int height;
};

View File

@@ -3,11 +3,6 @@
//
#include "Menu.h"
#include "Game.h"
#include "Rules.h"
#include "Leaderboard.h"
#include <QVBoxLayout>
#include <QApplication>
#include <iostream>
Menu::Menu(QWidget *parent) : QWidget(parent) {
@@ -54,6 +49,15 @@ void Menu::onPlayButtonClicked() {
// Show the game
view->setScene(&game->gameMap);
view->setFocus(); // Set focus to the QGraphicsView
// Get a pointer to the MainWindow
auto* mainWindow = qobject_cast<MainWindow*>(this->parentWidget());
// Set the Game object as the central widget of the MainWindow
if (mainWindow) {
mainWindow->setCentralWidget(game);
QTimer::singleShot(0, game, SLOT(setFocus()));
}
}
void Menu::onRulesButtonClicked() {

View File

@@ -5,6 +5,11 @@
#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 "Rules.h"
#include "Leaderboard.h"
#include "MainWindow.h"
#include <QVBoxLayout>
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QGraphicsView>

View File

@@ -5,30 +5,43 @@
#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) {
Player::Player(int health, int shield, int damage, float regenerationRate, int speed, const std::string& avatarPath, int x, int y, Map& gameMap)
: Mob(health, shield, damage, regenerationRate, speed, avatarPath, x, y), gameMap(gameMap) {
this->x = x;
this->y = y;
graphics = new QGraphicsEllipseItem(x, y, 10, 10);
graphics->setBrush(QBrush(Qt::blue));
QPixmap pixmap(QString::fromStdString(avatarPath));
if (pixmap.isNull()) {
std::cerr << "Failed to load image from path: " << avatarPath << std::endl;
} 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); // Set the Z-value to 1 to draw the player on top of the map tiles
}
}
void Player::setPosition(Tile* tile) {
this->x = tile->gridX();
this->y = tile->gridY();
graphics->setRect(x * 50, y * 50, 50, 50);
graphics->setPos(x * 50, y * 50);
}
void Player::setPosition(int x, int y) {
this->x = x;
this->y = y;
graphics->setRect(x * 50, y * 50, 50, 50);
int mapWidth = gameMap.getWidth();
int mapHeight = gameMap.getHeight();
if (x >= 0 && x < mapWidth && y >= 0 && y < mapHeight) {
this->x = x;
this->y = y;
graphics->setPos(x * 50, y * 50);
}
}
void Player::getPlayerPosition() {
std::cout << "Player position: x: " << x << " y: " << y << std::endl;
}
QGraphicsEllipseItem* Player::getGraphics() {
QGraphicsPixmapItem* Player::getGraphics() {
return graphics;
}

View File

@@ -6,22 +6,25 @@
#define POULPES_DE_L_ESPACE_LA_DERNIERE_LIGNE_DE_DEFENSE_PLAYER_H
#include "Mob.h"
#include "Tile.h"
#include "Map.h"
#include <QGraphicsEllipseItem>
#include <QGraphicsPixmapItem>
#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);
Player(int health, int shield, int damage, float regenerationRate, int speed, const std::string& avatarPath, int x, int y, Map& gameMap);
void setPosition(Tile* tile);
void setPosition(int x, int y);
void getPlayerPosition();
int getX();
int getY();
QGraphicsEllipseItem* getGraphics();
QGraphicsPixmapItem* getGraphics();
private:
QGraphicsEllipseItem* graphics;
QGraphicsPixmapItem* graphics;
Map& gameMap;
};

View File

@@ -1,8 +1,5 @@
#include <QApplication>
#include <QObject>
#include "MainWindow.h"
#include "Menu.h"
#include "Rules.h"
int main(int argc, char *argv[]) {