Added laser collision on aliens

This commit is contained in:
2024-01-30 16:22:43 +01:00
parent d67394aaec
commit 0bc7a96c32
9 changed files with 499 additions and 409 deletions
+160 -158
View File
@@ -1,159 +1,161 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations"> <ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32"> <ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration> <Configuration>Debug</Configuration>
<Platform>Win32</Platform> <Platform>Win32</Platform>
</ProjectConfiguration> </ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32"> <ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration> <Configuration>Release</Configuration>
<Platform>Win32</Platform> <Platform>Win32</Platform>
</ProjectConfiguration> </ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64"> <ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration> <Configuration>Debug</Configuration>
<Platform>x64</Platform> <Platform>x64</Platform>
</ProjectConfiguration> </ProjectConfiguration>
<ProjectConfiguration Include="Release|x64"> <ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration> <Configuration>Release</Configuration>
<Platform>x64</Platform> <Platform>x64</Platform>
</ProjectConfiguration> </ProjectConfiguration>
</ItemGroup> </ItemGroup>
<PropertyGroup Label="Globals"> <PropertyGroup Label="Globals">
<VCProjectVersion>17.0</VCProjectVersion> <VCProjectVersion>17.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword> <Keyword>Win32Proj</Keyword>
<ProjectGuid>{77e81a7a-9e66-4c14-8c8c-2d046feb7de8}</ProjectGuid> <ProjectGuid>{77e81a7a-9e66-4c14-8c8c-2d046feb7de8}</ProjectGuid>
<RootNamespace>SpaceInvader</RootNamespace> <RootNamespace>SpaceInvader</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion> <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup> </PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries> <UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset> <PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet> <CharacterSet>Unicode</CharacterSet>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries> <UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset> <PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization> <WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet> <CharacterSet>Unicode</CharacterSet>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries> <UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset> <PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet> <CharacterSet>Unicode</CharacterSet>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries> <UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset> <PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization> <WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet> <CharacterSet>Unicode</CharacterSet>
</PropertyGroup> </PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings"> <ImportGroup Label="ExtensionSettings">
</ImportGroup> </ImportGroup>
<ImportGroup Label="Shared"> <ImportGroup Label="Shared">
</ImportGroup> </ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup> </ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup> </ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup> </ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup> </ImportGroup>
<PropertyGroup Label="UserMacros" /> <PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<CopyLocalDeploymentContent>true</CopyLocalDeploymentContent> <CopyLocalDeploymentContent>true</CopyLocalDeploymentContent>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<CopyLocalDeploymentContent>true</CopyLocalDeploymentContent> <CopyLocalDeploymentContent>true</CopyLocalDeploymentContent>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Label="Vcpkg"> <PropertyGroup Label="Vcpkg">
<VcpkgEnableManifest>true</VcpkgEnableManifest> <VcpkgEnableManifest>true</VcpkgEnableManifest>
</PropertyGroup> </PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile> <ClCompile>
<WarningLevel>Level3</WarningLevel> <WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck> <SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode> <ConformanceMode>true</ConformanceMode>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Console</SubSystem> <SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile> <ClCompile>
<WarningLevel>Level3</WarningLevel> <WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking> <FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions> <IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck> <SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode> <ConformanceMode>true</ConformanceMode>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Console</SubSystem> <SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding> <EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences> <OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile> <ClCompile>
<WarningLevel>Level3</WarningLevel> <WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck> <SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode> <ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Console</SubSystem> <SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile> <ClCompile>
<WarningLevel>Level3</WarningLevel> <WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking> <FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions> <IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck> <SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode> <ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Console</SubSystem> <SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding> <EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences> <OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<None Include="vcpkg.json" /> <None Include="vcpkg.json" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="lib\GameManager.cpp" /> <ClCompile Include="lib\Alien.cpp" />
<ClCompile Include="lib\Laser.cpp" /> <ClCompile Include="lib\GameManager.cpp" />
<ClCompile Include="lib\Shield.cpp" /> <ClCompile Include="lib\Laser.cpp" />
<ClCompile Include="main.cpp" /> <ClCompile Include="lib\Shield.cpp" />
<ClCompile Include="lib\Player.cpp" /> <ClCompile Include="main.cpp" />
</ItemGroup> <ClCompile Include="lib\Player.cpp" />
<ItemGroup> </ItemGroup>
<ClInclude Include="include\GameManager.h" /> <ItemGroup>
<ClInclude Include="include\Laser.h" /> <ClInclude Include="include\Alien.h" />
<ClInclude Include="include\Player.h" /> <ClInclude Include="include\GameManager.h" />
<ClInclude Include="include\Shield.h" /> <ClInclude Include="include\Laser.h" />
</ItemGroup> <ClInclude Include="include\Player.h" />
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <ClInclude Include="include\Shield.h" />
<ImportGroup Label="ExtensionTargets"> </ItemGroup>
</ImportGroup> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project> </Project>
+56 -50
View File
@@ -1,51 +1,57 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup> <ItemGroup>
<Filter Include="Fichiers sources"> <Filter Include="Fichiers sources">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier> <UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions> <Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter> </Filter>
<Filter Include="Fichiers d%27en-tête"> <Filter Include="Fichiers d%27en-tête">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier> <UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions> <Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter> </Filter>
<Filter Include="Fichiers de ressources"> <Filter Include="Fichiers de ressources">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier> <UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions> <Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter> </Filter>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="vcpkg.json" /> <None Include="vcpkg.json" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="main.cpp"> <ClCompile Include="main.cpp">
<Filter>Fichiers sources</Filter> <Filter>Fichiers sources</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="lib\Player.cpp"> <ClCompile Include="lib\Player.cpp">
<Filter>Fichiers sources</Filter> <Filter>Fichiers sources</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="lib\Laser.cpp"> <ClCompile Include="lib\Laser.cpp">
<Filter>Fichiers sources</Filter> <Filter>Fichiers sources</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="lib\GameManager.cpp"> <ClCompile Include="lib\GameManager.cpp">
<Filter>Fichiers sources</Filter> <Filter>Fichiers sources</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="lib\Shield.cpp"> <ClCompile Include="lib\Shield.cpp">
<Filter>Fichiers sources</Filter> <Filter>Fichiers sources</Filter>
</ClCompile> </ClCompile>
</ItemGroup> <ClCompile Include="lib\Alien.cpp">
<ItemGroup> <Filter>Fichiers sources</Filter>
<ClInclude Include="include\Player.h"> </ClCompile>
<Filter>Fichiers d%27en-tête</Filter> </ItemGroup>
</ClInclude> <ItemGroup>
<ClInclude Include="include\Laser.h"> <ClInclude Include="include\Player.h">
<Filter>Fichiers d%27en-tête</Filter> <Filter>Fichiers d%27en-tête</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="include\GameManager.h"> <ClInclude Include="include\Laser.h">
<Filter>Fichiers d%27en-tête</Filter> <Filter>Fichiers d%27en-tête</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="include\Shield.h"> <ClInclude Include="include\GameManager.h">
<Filter>Fichiers d%27en-tête</Filter> <Filter>Fichiers d%27en-tête</Filter>
</ClInclude> </ClInclude>
</ItemGroup> <ClInclude Include="include\Shield.h">
<Filter>Fichiers d%27en-tête</Filter>
</ClInclude>
<ClInclude Include="include\Alien.h">
<Filter>Fichiers d%27en-tête</Filter>
</ClInclude>
</ItemGroup>
</Project> </Project>
+5 -5
View File
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup> <PropertyGroup>
<ShowAllFiles>true</ShowAllFiles> <ShowAllFiles>true</ShowAllFiles>
</PropertyGroup> </PropertyGroup>
</Project> </Project>
+18
View File
@@ -0,0 +1,18 @@
#pragma once
#include <iostream>
#include <SFML/Graphics.hpp>
using namespace std;
using namespace sf;
class Alien
{
public:
Alien(Vector2f& position);
RectangleShape& getShape() { return _shape; };
private:
RectangleShape _shape;
};
+36 -32
View File
@@ -1,32 +1,36 @@
#pragma once #pragma once
#include <cstdlib> #include <cstdlib>
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include "Player.h" #include "Player.h"
#include "Laser.h" #include "Laser.h"
#include "Shield.h" #include "Shield.h"
#include "Alien.h"
using namespace std;
using namespace sf; using namespace std;
using namespace sf;
class GameManager
{ class GameManager
public: {
GameManager(); public:
GameManager();
void run();
void handleInputs(); void run();
void handleLasersCollision(); void handleInputs();
void handleLasersCollision();
private:
Clock _clock; private:
Clock _clock;
//RenderWindow _window{ VideoMode::getFullscreenModes()[0], "Space Invader", Style::Fullscreen };
RenderWindow _window{ VideoMode(1024, 800), "Space Invader", Style::Close | Style::Titlebar}; const int alienScreenMargin = 50;
Player _player = { _window.getSize() }; //RenderWindow _window{ VideoMode::getFullscreenModes()[0], "Space Invader", Style::Fullscreen };
RenderWindow _window{ VideoMode(1024, 800), "Space Invader", Style::Close | Style::Titlebar};
vector<Laser*> _lasers;
vector<Shield*> _shields; Player _player = { _window.getSize() };
};
vector<Laser*> _lasers;
vector<Shield*> _shields;
vector<Alien*> _aliens;
};
+21 -22
View File
@@ -1,22 +1,21 @@
#pragma once #pragma once
#include <iostream> #include <iostream>
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
using namespace std; using namespace std;
using namespace sf; using namespace sf;
class Laser class Laser
{ {
public: public:
Laser() {}; Laser(Window& window);
Laser(Window& window);
RectangleShape& getShape() { return _laser; }
RectangleShape& getShape() { return _laser; }
void setInitialPosition(Vector2f position);
void setInitialPosition(Vector2f position);
private:
private: RectangleShape _laser;
RectangleShape _laser; };
};
+24 -25
View File
@@ -1,26 +1,25 @@
#pragma once #pragma once
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
using namespace sf; using namespace sf;
using namespace std; using namespace std;
class Shield class Shield
{ {
public: public:
Shield() {}; Shield(Window& window);
Shield(Window& window);
void setPosition(Vector2f position);
void setPosition(Vector2f position);
RectangleShape& getShape() { return _shape; };
RectangleShape& getShape() { return _shape; };
int getLife() const { return _life; };
int getLife() const { return _life; }; void decreaseLife() { _life -= 1; };
void decreaseLife() { _life -= 1; };
private:
private: int _life = 10;
int _life = 10; RectangleShape _shape;
RectangleShape _shape;
}; };
+7
View File
@@ -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));
}
+172 -117
View File
@@ -1,118 +1,173 @@
#include "GameManager.h"; #include "GameManager.h"
GameManager::GameManager() { GameManager::GameManager() {
_window.setVerticalSyncEnabled(true); _window.setVerticalSyncEnabled(true);
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
Shield* shield = new Shield(_window); Shield* shield = new Shield(_window);
shield->setPosition(Vector2f(_window.getSize().x / 4.25 * i, _window.getSize().y * 0.9)); shield->setPosition(Vector2f(_window.getSize().x / 4.25f * i, _window.getSize().y * 0.9f));
_shields.push_back(shield); _shields.push_back(shield);
} }
}
const int rows = 3;
void GameManager::run() { const int cols = 6;
while (_window.isOpen()) { const int distX = 135;
const int distY = 85;
Event event;
while (_window.pollEvent(event)) { for (int row = 0; row < rows; row++) {
if (event.type == Event::Closed) { for (int col = 0; col < cols; col++) {
_window.close(); Vector2f position(col * distX + alienScreenMargin + 1, row * distY + alienScreenMargin);
} Alien* alien = new Alien(position);
}
_aliens.push_back(alien);
// Handle user's inputs }
handleInputs(); }
}
_window.clear(Color::Black);
void GameManager::run() {
// Draw everything int alienDirection = 1;
_window.draw(_player.getShape()); const float alienXStep = 10;
const int moveDownStep = 5;
for (auto shield : _shields) { int moveDown = 0;
_window.draw(shield->getShape());
} Clock alienClock;
for (vector<Laser *>::iterator it = _lasers.begin(); it < _lasers.end();) { while (_window.isOpen()) {
_window.draw((*it)->getShape());
Event event;
(*it)->getShape().move(0, -5); while (_window.pollEvent(event)) {
if (event.type == Event::Closed) {
if ((*it)->getShape().getPosition().y < -(*it)->getShape().getScale().y) { _window.close();
delete* it; }
it = _lasers.erase(it); }
continue;
} // Handle user's inputs
handleInputs();
++it;
} _window.clear(Color::Black);
handleLasersCollision(); // Draw everything
_window.draw(_player.getShape());
_window.display();
} for (auto shield : _shields) {
} _window.draw(shield->getShape());
}
void GameManager::handleInputs() {
// Exit event for (Alien* alien : _aliens) {
if (Keyboard::isKeyPressed(Keyboard::Escape)) { const FloatRect bounds = alien->getShape().getGlobalBounds();
_window.close();
} if (bounds.left + bounds.width >= _window.getSize().x - alienScreenMargin || bounds.left <= alienScreenMargin) {
alienDirection *= -1;
// Player movements moveDown = moveDownStep;
if (Keyboard::isKeyPressed(Keyboard::Left) || Keyboard::isKeyPressed(Keyboard::Q)) { break;
_player.move(-1); }
} }
if (Keyboard::isKeyPressed(Keyboard::Right) || Keyboard::isKeyPressed(Keyboard::D)) { for (Alien* alien : _aliens) {
_player.move(1); _window.draw(alien->getShape());
} }
if (Keyboard::isKeyPressed(Keyboard::Space)) {
if (_clock.getElapsedTime().asMilliseconds() > 500) { if (alienClock.getElapsedTime().asMilliseconds() >= 500) {
const Vector2f playerPos = _player.getShape().getPosition(); for (Alien* alien : _aliens) {
const float playerRadius = _player.getShape().getRadius(); alien->getShape().move(alienXStep * alienDirection, moveDown);
}
Laser* laser = new Laser(_window); alienClock.restart();
laser->setInitialPosition(Vector2f(playerPos.x + playerRadius, playerPos.y - playerRadius)); }
moveDown = 0;
_lasers.push_back(laser);
_clock.restart(); for (vector<Laser *>::iterator it = _lasers.begin(); it < _lasers.end();) {
} _window.draw((*it)->getShape());
}
} (*it)->getShape().move(0, -5);
void GameManager::handleLasersCollision() { if ((*it)->getShape().getPosition().y < -(*it)->getShape().getScale().y) {
bool restartClock = false; delete* it;
for (vector<Laser*>::iterator laserIt = _lasers.begin(); laserIt < _lasers.end();) { it = _lasers.erase(it);
bool deleteLaser = false; continue;
for (vector<Shield*>::iterator shieldIt = _shields.begin(); shieldIt < _shields.end();) { }
if ((*laserIt)->getShape().getGlobalBounds().intersects((*shieldIt)->getShape().getGlobalBounds())) {
if ((*shieldIt)->getLife() - 1 == 0) { ++it;
delete* shieldIt; }
shieldIt = _shields.erase(shieldIt);
} handleLasersCollision();
(*shieldIt)->decreaseLife(); _window.display();
deleteLaser = true; }
break; }
}
++shieldIt; void GameManager::handleInputs() {
} // Exit event
if (Keyboard::isKeyPressed(Keyboard::Escape)) {
if (deleteLaser) { _window.close();
delete* laserIt; }
laserIt = _lasers.erase(laserIt);
// Player movements
restartClock = true; if (Keyboard::isKeyPressed(Keyboard::Left) || Keyboard::isKeyPressed(Keyboard::Q)) {
continue; _player.move(-1);
} }
++laserIt; if (Keyboard::isKeyPressed(Keyboard::Right) || Keyboard::isKeyPressed(Keyboard::D)) {
} _player.move(1);
}
if (restartClock) {
_clock.restart(); 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<Laser*>::iterator laserIt = _lasers.begin(); laserIt < _lasers.end();) {
bool deleteLaser = false;
for (vector<Shield*>::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<Alien*>::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();
}
} }