🚀
This commit is contained in:
@@ -141,6 +141,7 @@
|
|||||||
<None Include="vcpkg.json" />
|
<None Include="vcpkg.json" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClCompile Include="lib\Alien.cpp" />
|
||||||
<ClCompile Include="lib\GameManager.cpp" />
|
<ClCompile Include="lib\GameManager.cpp" />
|
||||||
<ClCompile Include="lib\Laser.cpp" />
|
<ClCompile Include="lib\Laser.cpp" />
|
||||||
<ClCompile Include="lib\Shield.cpp" />
|
<ClCompile Include="lib\Shield.cpp" />
|
||||||
@@ -148,6 +149,7 @@
|
|||||||
<ClCompile Include="lib\Player.cpp" />
|
<ClCompile Include="lib\Player.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClInclude Include="include\Alien.h" />
|
||||||
<ClInclude Include="include\GameManager.h" />
|
<ClInclude Include="include\GameManager.h" />
|
||||||
<ClInclude Include="include\Laser.h" />
|
<ClInclude Include="include\Laser.h" />
|
||||||
<ClInclude Include="include\Player.h" />
|
<ClInclude Include="include\Player.h" />
|
||||||
|
|||||||
@@ -33,6 +33,9 @@
|
|||||||
<ClCompile Include="lib\Shield.cpp">
|
<ClCompile Include="lib\Shield.cpp">
|
||||||
<Filter>Fichiers sources</Filter>
|
<Filter>Fichiers sources</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="lib\Alien.cpp">
|
||||||
|
<Filter>Fichiers sources</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="include\Player.h">
|
<ClInclude Include="include\Player.h">
|
||||||
@@ -47,5 +50,8 @@
|
|||||||
<ClInclude Include="include\Shield.h">
|
<ClInclude Include="include\Shield.h">
|
||||||
<Filter>Fichiers d%27en-tête</Filter>
|
<Filter>Fichiers d%27en-tête</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="include\Alien.h">
|
||||||
|
<Filter>Fichiers d%27en-tête</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
@@ -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;
|
||||||
|
};
|
||||||
@@ -5,6 +5,7 @@
|
|||||||
#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 std;
|
||||||
using namespace sf;
|
using namespace sf;
|
||||||
@@ -21,6 +22,8 @@ public:
|
|||||||
private:
|
private:
|
||||||
Clock _clock;
|
Clock _clock;
|
||||||
|
|
||||||
|
const int alienScreenMargin = 50;
|
||||||
|
|
||||||
//RenderWindow _window{ VideoMode::getFullscreenModes()[0], "Space Invader", Style::Fullscreen };
|
//RenderWindow _window{ VideoMode::getFullscreenModes()[0], "Space Invader", Style::Fullscreen };
|
||||||
RenderWindow _window{ VideoMode(1024, 800), "Space Invader", Style::Close | Style::Titlebar};
|
RenderWindow _window{ VideoMode(1024, 800), "Space Invader", Style::Close | Style::Titlebar};
|
||||||
|
|
||||||
@@ -28,5 +31,6 @@ private:
|
|||||||
|
|
||||||
vector<Laser*> _lasers;
|
vector<Laser*> _lasers;
|
||||||
vector<Shield*> _shields;
|
vector<Shield*> _shields;
|
||||||
|
vector<Alien*> _aliens;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ using namespace sf;
|
|||||||
class Laser
|
class Laser
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Laser() {};
|
|
||||||
Laser(Window& window);
|
Laser(Window& window);
|
||||||
|
|
||||||
RectangleShape& getShape() { return _laser; }
|
RectangleShape& getShape() { return _laser; }
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ using namespace std;
|
|||||||
class Shield
|
class Shield
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Shield() {};
|
|
||||||
Shield(Window& window);
|
Shield(Window& window);
|
||||||
|
|
||||||
void setPosition(Vector2f position);
|
void setPosition(Vector2f position);
|
||||||
|
|||||||
@@ -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));
|
||||||
|
}
|
||||||
+57
-2
@@ -1,17 +1,38 @@
|
|||||||
#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;
|
||||||
|
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() {
|
void GameManager::run() {
|
||||||
|
int alienDirection = 1;
|
||||||
|
const float alienXStep = 10;
|
||||||
|
const int moveDownStep = 5;
|
||||||
|
int moveDown = 0;
|
||||||
|
|
||||||
|
Clock alienClock;
|
||||||
|
|
||||||
while (_window.isOpen()) {
|
while (_window.isOpen()) {
|
||||||
|
|
||||||
Event event;
|
Event event;
|
||||||
@@ -33,6 +54,30 @@ void GameManager::run() {
|
|||||||
_window.draw(shield->getShape());
|
_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<Laser *>::iterator it = _lasers.begin(); it < _lasers.end();) {
|
for (vector<Laser *>::iterator it = _lasers.begin(); it < _lasers.end();) {
|
||||||
_window.draw((*it)->getShape());
|
_window.draw((*it)->getShape());
|
||||||
|
|
||||||
@@ -101,6 +146,16 @@ void GameManager::handleLasersCollision() {
|
|||||||
++shieldIt;
|
++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) {
|
if (deleteLaser) {
|
||||||
delete* laserIt;
|
delete* laserIt;
|
||||||
laserIt = _lasers.erase(laserIt);
|
laserIt = _lasers.erase(laserIt);
|
||||||
|
|||||||
Reference in New Issue
Block a user