From ca204d05af19545cba1e19fcd2e5652250c11550 Mon Sep 17 00:00:00 2001 From: UnEpicier Date: Tue, 30 Jan 2024 15:45:14 +0100 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=9A=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 72 ++++---- Space-Invader.sln | 62 +++---- Space-Invader.vcxproj | 318 +++++++++++++++++----------------- Space-Invader.vcxproj.filters | 106 ++++++------ Space-Invader.vcxproj.user | 10 +- include/Alien.h | 18 ++ include/GameManager.h | 68 ++++---- include/Laser.h | 43 +++-- include/Player.h | 50 +++--- include/Shield.h | 49 +++--- lib/Alien.cpp | 7 + lib/GameManager.cpp | 289 +++++++++++++++++------------- lib/Laser.cpp | 20 +-- lib/Player.cpp | 42 ++--- lib/Shield.cpp | 24 +-- main.cpp | 30 ++-- vcpkg.json | 18 +- 17 files changed, 658 insertions(+), 568 deletions(-) create mode 100644 include/Alien.h create mode 100644 lib/Alien.cpp diff --git a/.gitignore b/.gitignore index d213ce4..66fafe0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,37 +1,37 @@ -# Prerequisites -*.d - -# Compiled Object files -*.slo -*.lo -*.o -*.obj - -# Precompiled Headers -*.gch -*.pch - -# Compiled Dynamic libraries -*.so -*.dylib -*.dll - -# Fortran module files -*.mod -*.smod - -# Compiled Static libraries -*.lai -*.la -*.a -*.lib - -# Executables -*.exe -*.out -*.app - -# Visual Studio -.vs -vcpkg_installed +# Prerequisites +*.d + +# Compiled Object files +*.slo +*.lo +*.o +*.obj + +# Precompiled Headers +*.gch +*.pch + +# Compiled Dynamic libraries +*.so +*.dylib +*.dll + +# Fortran module files +*.mod +*.smod + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables +*.exe +*.out +*.app + +# Visual Studio +.vs +vcpkg_installed x64 \ No newline at end of file diff --git a/Space-Invader.sln b/Space-Invader.sln index fca078e..fb89525 100644 --- a/Space-Invader.sln +++ b/Space-Invader.sln @@ -1,31 +1,31 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.8.34330.188 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Space-Invader", "Space-Invader.vcxproj", "{77E81A7A-9E66-4C14-8C8C-2D046FEB7DE8}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|x64 = Debug|x64 - Debug|x86 = Debug|x86 - Release|x64 = Release|x64 - Release|x86 = Release|x86 - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {77E81A7A-9E66-4C14-8C8C-2D046FEB7DE8}.Debug|x64.ActiveCfg = Debug|x64 - {77E81A7A-9E66-4C14-8C8C-2D046FEB7DE8}.Debug|x64.Build.0 = Debug|x64 - {77E81A7A-9E66-4C14-8C8C-2D046FEB7DE8}.Debug|x86.ActiveCfg = Debug|Win32 - {77E81A7A-9E66-4C14-8C8C-2D046FEB7DE8}.Debug|x86.Build.0 = Debug|Win32 - {77E81A7A-9E66-4C14-8C8C-2D046FEB7DE8}.Release|x64.ActiveCfg = Release|x64 - {77E81A7A-9E66-4C14-8C8C-2D046FEB7DE8}.Release|x64.Build.0 = Release|x64 - {77E81A7A-9E66-4C14-8C8C-2D046FEB7DE8}.Release|x86.ActiveCfg = Release|Win32 - {77E81A7A-9E66-4C14-8C8C-2D046FEB7DE8}.Release|x86.Build.0 = Release|Win32 - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {FC394877-8CB5-406D-846E-C6337AFC7323} - EndGlobalSection -EndGlobal + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.8.34330.188 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Space-Invader", "Space-Invader.vcxproj", "{77E81A7A-9E66-4C14-8C8C-2D046FEB7DE8}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {77E81A7A-9E66-4C14-8C8C-2D046FEB7DE8}.Debug|x64.ActiveCfg = Debug|x64 + {77E81A7A-9E66-4C14-8C8C-2D046FEB7DE8}.Debug|x64.Build.0 = Debug|x64 + {77E81A7A-9E66-4C14-8C8C-2D046FEB7DE8}.Debug|x86.ActiveCfg = Debug|Win32 + {77E81A7A-9E66-4C14-8C8C-2D046FEB7DE8}.Debug|x86.Build.0 = Debug|Win32 + {77E81A7A-9E66-4C14-8C8C-2D046FEB7DE8}.Release|x64.ActiveCfg = Release|x64 + {77E81A7A-9E66-4C14-8C8C-2D046FEB7DE8}.Release|x64.Build.0 = Release|x64 + {77E81A7A-9E66-4C14-8C8C-2D046FEB7DE8}.Release|x86.ActiveCfg = Release|Win32 + {77E81A7A-9E66-4C14-8C8C-2D046FEB7DE8}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {FC394877-8CB5-406D-846E-C6337AFC7323} + EndGlobalSection +EndGlobal diff --git a/Space-Invader.vcxproj b/Space-Invader.vcxproj index 6d61d67..d3c8056 100644 --- a/Space-Invader.vcxproj +++ b/Space-Invader.vcxproj @@ -1,159 +1,161 @@ - - - - - Debug - Win32 - - - Release - Win32 - - - Debug - x64 - - - Release - x64 - - - - 17.0 - Win32Proj - {77e81a7a-9e66-4c14-8c8c-2d046feb7de8} - SpaceInvader - 10.0 - - - - Application - true - v143 - Unicode - - - Application - false - v143 - true - Unicode - - - Application - true - v143 - Unicode - - - Application - false - v143 - true - Unicode - - - - - - - - - - - - - - - - - - - - - true - - - true - - - true - - - - Level3 - true - WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - - - Console - true - - - - - Level3 - true - true - true - WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - - - Console - true - true - true - - - - - Level3 - true - _DEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - include;%(AdditionalIncludeDirectories) - - - Console - true - - - - - Level3 - true - true - true - NDEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - include;%(AdditionalIncludeDirectories) - - - Console - true - true - true - - - - - - - - - - - - - - - - - - - - - + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {77e81a7a-9e66-4c14-8c8c-2d046feb7de8} + SpaceInvader + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + true + + + true + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + include;%(AdditionalIncludeDirectories) + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + include;%(AdditionalIncludeDirectories) + + + Console + true + true + true + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Space-Invader.vcxproj.filters b/Space-Invader.vcxproj.filters index 76cad6b..f896f96 100644 --- a/Space-Invader.vcxproj.filters +++ b/Space-Invader.vcxproj.filters @@ -1,51 +1,57 @@ - - - - - {4FC737F1-C7A5-4376-A066-2A32D752A2FF} - cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx - - - {93995380-89BD-4b04-88EB-625FBE52EBFB} - h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd - - - {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} - rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms - - - - - - - - Fichiers sources - - - Fichiers sources - - - Fichiers sources - - - Fichiers sources - - - Fichiers sources - - - - - Fichiers d%27en-tête - - - Fichiers d%27en-tête - - - Fichiers d%27en-tête - - - Fichiers d%27en-tête - - + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + + + + Fichiers sources + + + Fichiers sources + + + Fichiers sources + + + Fichiers sources + + + Fichiers sources + + + Fichiers sources + + + + + Fichiers d%27en-tête + + + Fichiers d%27en-tête + + + Fichiers d%27en-tête + + + Fichiers d%27en-tête + + + Fichiers d%27en-tête + + \ No newline at end of file diff --git a/Space-Invader.vcxproj.user b/Space-Invader.vcxproj.user index 966b4ff..429333d 100644 --- a/Space-Invader.vcxproj.user +++ b/Space-Invader.vcxproj.user @@ -1,6 +1,6 @@ - - - - true - + + + + true + \ No newline at end of file diff --git a/include/Alien.h b/include/Alien.h new file mode 100644 index 0000000..201bc33 --- /dev/null +++ b/include/Alien.h @@ -0,0 +1,18 @@ +#pragma once + +#include +#include + +using namespace std; +using namespace sf; + +class Alien +{ +public: + Alien(Vector2f& position); + + RectangleShape& getShape() { return _shape; }; + +private: + RectangleShape _shape; +}; \ No newline at end of file diff --git a/include/GameManager.h b/include/GameManager.h index d67ed5d..705d499 100644 --- a/include/GameManager.h +++ b/include/GameManager.h @@ -1,32 +1,36 @@ -#pragma once - -#include -#include -#include "Player.h" -#include "Laser.h" -#include "Shield.h" - -using namespace std; -using namespace sf; - -class GameManager -{ -public: - GameManager(); - - void run(); - void handleInputs(); - void handleLasersCollision(); - -private: - Clock _clock; - - //RenderWindow _window{ VideoMode::getFullscreenModes()[0], "Space Invader", Style::Fullscreen }; - RenderWindow _window{ VideoMode(1024, 800), "Space Invader", Style::Close | Style::Titlebar}; - - Player _player = { _window.getSize() }; - - vector _lasers; - vector _shields; -}; - +#pragma once + +#include +#include +#include "Player.h" +#include "Laser.h" +#include "Shield.h" +#include "Alien.h" + +using namespace std; +using namespace sf; + +class GameManager +{ +public: + GameManager(); + + void run(); + void handleInputs(); + void handleLasersCollision(); + +private: + Clock _clock; + + const int alienScreenMargin = 50; + + //RenderWindow _window{ VideoMode::getFullscreenModes()[0], "Space Invader", Style::Fullscreen }; + RenderWindow _window{ VideoMode(1024, 800), "Space Invader", Style::Close | Style::Titlebar}; + + Player _player = { _window.getSize() }; + + vector _lasers; + vector _shields; + vector _aliens; +}; + diff --git a/include/Laser.h b/include/Laser.h index a25031c..5f98c5c 100644 --- a/include/Laser.h +++ b/include/Laser.h @@ -1,22 +1,21 @@ -#pragma once - -#include -#include - -using namespace std; -using namespace sf; - -class Laser -{ -public: - Laser() {}; - Laser(Window& window); - - RectangleShape& getShape() { return _laser; } - - void setInitialPosition(Vector2f position); - -private: - RectangleShape _laser; -}; - +#pragma once + +#include +#include + +using namespace std; +using namespace sf; + +class Laser +{ +public: + Laser(Window& window); + + RectangleShape& getShape() { return _laser; } + + void setInitialPosition(Vector2f position); + +private: + RectangleShape _laser; +}; + diff --git a/include/Player.h b/include/Player.h index 1d54e42..e429b22 100644 --- a/include/Player.h +++ b/include/Player.h @@ -1,25 +1,25 @@ -#pragma once - -#define _USE_MATH_DEFINES - -#include -#include -#include - -using namespace std; -using namespace sf; - -class Player -{ -public: - Player(Vector2u screenSize); - - CircleShape& getShape() { return _shape; }; - void move(int direction); - -private: - CircleShape _shape; - - int _screenWidth; -}; - +#pragma once + +#define _USE_MATH_DEFINES + +#include +#include +#include + +using namespace std; +using namespace sf; + +class Player +{ +public: + Player(Vector2u screenSize); + + CircleShape& getShape() { return _shape; }; + void move(int direction); + +private: + CircleShape _shape; + + int _screenWidth; +}; + diff --git a/include/Shield.h b/include/Shield.h index 861b3ec..d6359e4 100644 --- a/include/Shield.h +++ b/include/Shield.h @@ -1,26 +1,25 @@ -#pragma once - -#include -#include -#include - -using namespace sf; -using namespace std; - -class Shield -{ -public: - Shield() {}; - Shield(Window& window); - - void setPosition(Vector2f position); - - RectangleShape& getShape() { return _shape; }; - - int getLife() const { return _life; }; - void decreaseLife() { _life -= 1; }; - -private: - int _life = 10; - RectangleShape _shape; +#pragma once + +#include +#include +#include + +using namespace sf; +using namespace std; + +class Shield +{ +public: + Shield(Window& window); + + void setPosition(Vector2f position); + + RectangleShape& getShape() { return _shape; }; + + int getLife() const { return _life; }; + void decreaseLife() { _life -= 1; }; + +private: + int _life = 10; + RectangleShape _shape; }; \ No newline at end of file diff --git a/lib/Alien.cpp b/lib/Alien.cpp new file mode 100644 index 0000000..05d02e0 --- /dev/null +++ b/lib/Alien.cpp @@ -0,0 +1,7 @@ +#include "Alien.h" + +Alien::Alien(Vector2f& position) { + _shape = RectangleShape(Vector2f(100, 50)); + _shape.setFillColor(Color::Red); + _shape.setPosition(Vector2f(position.x - (_shape.getScale().x / 2.0), position.y)); +} \ No newline at end of file diff --git a/lib/GameManager.cpp b/lib/GameManager.cpp index 3b3f95d..9fad352 100644 --- a/lib/GameManager.cpp +++ b/lib/GameManager.cpp @@ -1,118 +1,173 @@ -#include "GameManager.h"; - -GameManager::GameManager() { - _window.setVerticalSyncEnabled(true); - - for (int i = 0; i < 4; i++) { - Shield* shield = new Shield(_window); - shield->setPosition(Vector2f(_window.getSize().x / 4.25 * i, _window.getSize().y * 0.9)); - - _shields.push_back(shield); - } -} - -void GameManager::run() { - while (_window.isOpen()) { - - Event event; - while (_window.pollEvent(event)) { - if (event.type == Event::Closed) { - _window.close(); - } - } - - // Handle user's inputs - handleInputs(); - - _window.clear(Color::Black); - - // Draw everything - _window.draw(_player.getShape()); - - for (auto shield : _shields) { - _window.draw(shield->getShape()); - } - - for (vector::iterator it = _lasers.begin(); it < _lasers.end();) { - _window.draw((*it)->getShape()); - - (*it)->getShape().move(0, -5); - - if ((*it)->getShape().getPosition().y < -(*it)->getShape().getScale().y) { - delete* it; - it = _lasers.erase(it); - continue; - } - - ++it; - } - - handleLasersCollision(); - - _window.display(); - } -} - -void GameManager::handleInputs() { - // Exit event - if (Keyboard::isKeyPressed(Keyboard::Escape)) { - _window.close(); - } - - // Player movements - if (Keyboard::isKeyPressed(Keyboard::Left) || Keyboard::isKeyPressed(Keyboard::Q)) { - _player.move(-1); - } - - if (Keyboard::isKeyPressed(Keyboard::Right) || Keyboard::isKeyPressed(Keyboard::D)) { - _player.move(1); - } - - if (Keyboard::isKeyPressed(Keyboard::Space)) { - if (_clock.getElapsedTime().asMilliseconds() > 500) { - const Vector2f playerPos = _player.getShape().getPosition(); - const float playerRadius = _player.getShape().getRadius(); - - Laser* laser = new Laser(_window); - laser->setInitialPosition(Vector2f(playerPos.x + playerRadius, playerPos.y - playerRadius)); - - _lasers.push_back(laser); - - _clock.restart(); - } - } -} - -void GameManager::handleLasersCollision() { - bool restartClock = false; - for (vector::iterator laserIt = _lasers.begin(); laserIt < _lasers.end();) { - bool deleteLaser = false; - for (vector::iterator shieldIt = _shields.begin(); shieldIt < _shields.end();) { - if ((*laserIt)->getShape().getGlobalBounds().intersects((*shieldIt)->getShape().getGlobalBounds())) { - if ((*shieldIt)->getLife() - 1 == 0) { - delete* shieldIt; - shieldIt = _shields.erase(shieldIt); - } - - (*shieldIt)->decreaseLife(); - deleteLaser = true; - break; - } - ++shieldIt; - } - - if (deleteLaser) { - delete* laserIt; - laserIt = _lasers.erase(laserIt); - - restartClock = true; - continue; - } - - ++laserIt; - } - - if (restartClock) { - _clock.restart(); - } +#include "GameManager.h" + +GameManager::GameManager() { + _window.setVerticalSyncEnabled(true); + + for (int i = 0; i < 4; i++) { + Shield* shield = new Shield(_window); + shield->setPosition(Vector2f(_window.getSize().x / 4.25f * i, _window.getSize().y * 0.9f)); + + _shields.push_back(shield); + } + + const int rows = 3; + const int cols = 6; + const int distX = 135; + const int distY = 85; + + for (int row = 0; row < rows; row++) { + for (int col = 0; col < cols; col++) { + Vector2f position(col * distX + alienScreenMargin + 1, row * distY + alienScreenMargin); + Alien* alien = new Alien(position); + + _aliens.push_back(alien); + } + } +} + +void GameManager::run() { + int alienDirection = 1; + const float alienXStep = 10; + const int moveDownStep = 5; + int moveDown = 0; + + Clock alienClock; + + while (_window.isOpen()) { + + Event event; + while (_window.pollEvent(event)) { + if (event.type == Event::Closed) { + _window.close(); + } + } + + // Handle user's inputs + handleInputs(); + + _window.clear(Color::Black); + + // Draw everything + _window.draw(_player.getShape()); + + for (auto shield : _shields) { + _window.draw(shield->getShape()); + } + + for (Alien* alien : _aliens) { + const FloatRect bounds = alien->getShape().getGlobalBounds(); + + if (bounds.left + bounds.width >= _window.getSize().x - alienScreenMargin || bounds.left <= alienScreenMargin) { + alienDirection *= -1; + moveDown = moveDownStep; + break; + } + } + + for (Alien* alien : _aliens) { + _window.draw(alien->getShape()); + } + + + if (alienClock.getElapsedTime().asMilliseconds() >= 500) { + for (Alien* alien : _aliens) { + alien->getShape().move(alienXStep * alienDirection, moveDown); + } + alienClock.restart(); + } + moveDown = 0; + + + for (vector::iterator it = _lasers.begin(); it < _lasers.end();) { + _window.draw((*it)->getShape()); + + (*it)->getShape().move(0, -5); + + if ((*it)->getShape().getPosition().y < -(*it)->getShape().getScale().y) { + delete* it; + it = _lasers.erase(it); + continue; + } + + ++it; + } + + handleLasersCollision(); + + _window.display(); + } +} + +void GameManager::handleInputs() { + // Exit event + if (Keyboard::isKeyPressed(Keyboard::Escape)) { + _window.close(); + } + + // Player movements + if (Keyboard::isKeyPressed(Keyboard::Left) || Keyboard::isKeyPressed(Keyboard::Q)) { + _player.move(-1); + } + + if (Keyboard::isKeyPressed(Keyboard::Right) || Keyboard::isKeyPressed(Keyboard::D)) { + _player.move(1); + } + + if (Keyboard::isKeyPressed(Keyboard::Space)) { + if (_clock.getElapsedTime().asMilliseconds() > 500) { + const Vector2f playerPos = _player.getShape().getPosition(); + const float playerRadius = _player.getShape().getRadius(); + + Laser* laser = new Laser(_window); + laser->setInitialPosition(Vector2f(playerPos.x + playerRadius, playerPos.y - playerRadius)); + + _lasers.push_back(laser); + + _clock.restart(); + } + } +} + +void GameManager::handleLasersCollision() { + bool restartClock = false; + for (vector::iterator laserIt = _lasers.begin(); laserIt < _lasers.end();) { + bool deleteLaser = false; + for (vector::iterator shieldIt = _shields.begin(); shieldIt < _shields.end();) { + if ((*laserIt)->getShape().getGlobalBounds().intersects((*shieldIt)->getShape().getGlobalBounds())) { + if ((*shieldIt)->getLife() - 1 == 0) { + delete* shieldIt; + shieldIt = _shields.erase(shieldIt); + } + + (*shieldIt)->decreaseLife(); + deleteLaser = true; + break; + } + ++shieldIt; + } + + for (vector::iterator alienIt = _aliens.begin(); alienIt < _aliens.end();) { + if ((*laserIt)->getShape().getGlobalBounds().intersects((*alienIt)->getShape().getGlobalBounds())) { + delete* alienIt; + alienIt = _aliens.erase(alienIt); + deleteLaser = true; + break; + } + ++alienIt; + } + + if (deleteLaser) { + delete* laserIt; + laserIt = _lasers.erase(laserIt); + + restartClock = true; + continue; + } + + ++laserIt; + } + + if (restartClock) { + _clock.restart(); + } } \ No newline at end of file diff --git a/lib/Laser.cpp b/lib/Laser.cpp index 62703f1..2650ef3 100644 --- a/lib/Laser.cpp +++ b/lib/Laser.cpp @@ -1,11 +1,11 @@ -#include "Laser.h" - -Laser::Laser(Window& _window) -{ - _laser = RectangleShape(Vector2f(_window.getSize().x / 600, _window.getSize().y / 30)); - _laser.setFillColor(Color::White); -} - -void Laser::setInitialPosition(Vector2f position) { - _laser.setPosition(Vector2f(position.x - (_laser.getScale().x / 2), position.y)); +#include "Laser.h" + +Laser::Laser(Window& _window) +{ + _laser = RectangleShape(Vector2f(_window.getSize().x / 600, _window.getSize().y / 30)); + _laser.setFillColor(Color::White); +} + +void Laser::setInitialPosition(Vector2f position) { + _laser.setPosition(Vector2f(position.x - (_laser.getScale().x / 2), position.y)); } \ No newline at end of file diff --git a/lib/Player.cpp b/lib/Player.cpp index 6fe7ac0..0904499 100644 --- a/lib/Player.cpp +++ b/lib/Player.cpp @@ -1,22 +1,22 @@ -#include "Player.h"; - -Player::Player(Vector2u screenSize): _screenWidth(screenSize.x) { - _shape = CircleShape(screenSize.x / 70, 3); - _shape.setFillColor(Color(52, 252, 5)); - - float shapeHeight = _shape.getRadius() * 1.5; - - _shape.setPosition(_screenWidth / 2.f, screenSize.y - shapeHeight); -} - -void Player::move(int direction) { - float max = _screenWidth - (_shape.getRadius() * 2); - float result = (_screenWidth / 724.0)*direction; - - // Prevent leaving the window - if (_shape.getPosition().x + result < 0 || _shape.getPosition().x + result > max) { - result = 0; - } - - _shape.move(result, 0); +#include "Player.h"; + +Player::Player(Vector2u screenSize): _screenWidth(screenSize.x) { + _shape = CircleShape(screenSize.x / 70, 3); + _shape.setFillColor(Color(52, 252, 5)); + + float shapeHeight = _shape.getRadius() * 1.5; + + _shape.setPosition(_screenWidth / 2.f, screenSize.y - shapeHeight); +} + +void Player::move(int direction) { + float max = _screenWidth - (_shape.getRadius() * 2); + float result = (_screenWidth / 724.0)*direction; + + // Prevent leaving the window + if (_shape.getPosition().x + result < 0 || _shape.getPosition().x + result > max) { + result = 0; + } + + _shape.move(result, 0); } \ No newline at end of file diff --git a/lib/Shield.cpp b/lib/Shield.cpp index a877c88..51bec26 100644 --- a/lib/Shield.cpp +++ b/lib/Shield.cpp @@ -1,13 +1,13 @@ -#include "Shield.h" - -Shield::Shield(Window& window) { - int shapeWidth = window.getSize().x / 10; - int shapeHeight = window.getSize().y / 35; - - _shape = RectangleShape(Vector2f(shapeWidth, shapeHeight)); - _shape.setFillColor(Color(Color(52, 252, 5))); -} - -void Shield::setPosition(Vector2f position) { - _shape.setPosition(position.x + _shape.getSize().x, position.y - _shape.getScale().y); +#include "Shield.h" + +Shield::Shield(Window& window) { + int shapeWidth = window.getSize().x / 10; + int shapeHeight = window.getSize().y / 35; + + _shape = RectangleShape(Vector2f(shapeWidth, shapeHeight)); + _shape.setFillColor(Color(Color(52, 252, 5))); +} + +void Shield::setPosition(Vector2f position) { + _shape.setPosition(position.x + _shape.getSize().x, position.y - _shape.getScale().y); } \ No newline at end of file diff --git a/main.cpp b/main.cpp index 9376abc..683b529 100644 --- a/main.cpp +++ b/main.cpp @@ -1,16 +1,16 @@ -using namespace std; - -#include - -#include "GameManager.h" - -int main() -{ - cout << "Starting game..." << endl; - GameManager gameManager; - - cout << "Game started." << endl; - gameManager.run(); - - return 0; +using namespace std; + +#include + +#include "GameManager.h" + +int main() +{ + cout << "Starting game..." << endl; + GameManager gameManager; + + cout << "Game started." << endl; + gameManager.run(); + + return 0; } \ No newline at end of file diff --git a/vcpkg.json b/vcpkg.json index 041cb26..1665743 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -1,10 +1,10 @@ -{ - "name": "space-invader", - "version": "0.0.0", - "description": "Space Invader Copy", - "dependencies": [ - { - "name": "sfml" - } - ] +{ + "name": "space-invader", + "version": "0.0.0", + "description": "Space Invader Copy", + "dependencies": [ + { + "name": "sfml" + } + ] } \ No newline at end of file From d6161f1f694daaa8070503fea75e9a540532aa2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Un=20=C3=89picier?= <62609210+UnEpicier@users.noreply.github.com> Date: Tue, 30 Jan 2024 16:21:56 +0100 Subject: [PATCH 2/2] Update README.md --- README.md | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 681b31f..e577a7b 100644 --- a/README.md +++ b/README.md @@ -1 +1,30 @@ -# Space-Invader \ No newline at end of file +# Space Invader + +## Concept et genre + +Space Invader est un jeu d'arcade de type shoot'em up. + +## Scénario de base + +Des aliens envahissent la Terre et le joueur doit les détruire avant qu'ils n'atteignent son vaisseau sur +Terre. + +## Mécaniques de base + +Le vaisseau peut se déplacer horizontalement et tirer des projectiles vers le haut. Les aliens +descendent progressivement vers le vaisseau et tirent des projectiles vers le bas. + +## Style visuel + +Le jeu est en 2D avec une vue de profil. Le vaisseau et les aliens sont représentés par des formes +simple (rectangulaire et triangluraire) et couleurs différentes pour les différencier. + +## Objectifs + +Le joueur doit détruire tous les aliens avant qu'ils n'atteignent le vaisseau. Tout en évitant les +projectiles aliens. + +## Contrôles + +Le joueur peut se déplacer horizontalement avec les touches Q (Gauche) et D (Droite). Il peut tirer +avec la barre d'espace.