Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}\\**",
"C:\\Users\\mruna\\Downloads\\SFML_libraries\\SFML-2.5.1\\include"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"cppStandard": "gnu++14"
}
],
"version": 4
}
19 changes: 19 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"version": "0.2.0",
"configurations": [

{
"name": "Debug SFML",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}\\bin\\space_invaders.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/bin",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe"
}
]
}
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"C_Cpp.errorSquiggles": "disabled"
}
32 changes: 32 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "Build SFML Project",
"type": "shell",
"command": "g++",
"args": [
"main.cpp",
"Menu.cpp",
"Enemy.cpp",
"-o", "${workspaceFolder}\\bin\\space_invaders.exe",
"-I", "C:\\Users\\mruna\\Downloads\\SFML_libraries\\SFML-2.5.1\\include",
"-L", "C:\\Users\\mruna\\Downloads\\SFML_libraries\\SFML-2.5.1\\lib",
"-lsfml-graphics",
"-lsfml-window",
"-lsfml-system",
"-lsfml-audio",
"-mwindows",
"-static",
"-static-libgcc",
"-static-libstdc++"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": ["$gcc"],
"detail": "Compile SFML project with g++"
}
]
}
Binary file added Code/bin/assets/Alien.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Code/bin/assets/Missile.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Code/bin/assets/Shield1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Code/bin/assets/Shield2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Code/bin/assets/Shield3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Code/bin/assets/Shield4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Code/bin/assets/Ship.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Code/bin/assets/enemyMissile.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Code/bin/assets/explosion.wav
Binary file not shown.
Binary file added Code/bin/assets/font.ttf
Binary file not shown.
Binary file added Code/bin/assets/invaderkilled.wav
Binary file not shown.
Binary file added Code/bin/assets/music.ogg
Binary file not shown.
Binary file added Code/bin/assets/shoot.wav
Binary file not shown.
Binary file added Code/bin/openal32.dll
Binary file not shown.
Binary file added Code/bin/sfml-audio-2.dll
Binary file not shown.
Binary file added Code/bin/sfml-audio-d-2.dll
Binary file not shown.
Binary file added Code/bin/sfml-graphics-2.dll
Binary file not shown.
Binary file added Code/bin/sfml-graphics-d-2.dll
Binary file not shown.
Binary file added Code/bin/sfml-network-2.dll
Binary file not shown.
Binary file added Code/bin/sfml-network-d-2.dll
Binary file not shown.
Binary file added Code/bin/sfml-system-2.dll
Binary file not shown.
Binary file added Code/bin/sfml-system-d-2.dll
Binary file not shown.
Binary file added Code/bin/sfml-window-2.dll
Binary file not shown.
Binary file added Code/bin/sfml-window-d-2.dll
Binary file not shown.
Binary file added Code/bin/space_invaders.exe
Binary file not shown.
4 changes: 2 additions & 2 deletions Code/header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
#include <vector>
#include <time.h>

#include <SFML/Graphics.hpp>
#include "SFML/Graphics.hpp"
#include <SFML/Audio.hpp>

using namespace sf;

using std::cout;
using std::endl;
using std::to_string;
using std::vector;
using std::vector;
91 changes: 56 additions & 35 deletions Code/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "Missile.hpp"
#include "Menu.hpp"

bool checkCollision(const sf::Sprite& s1, const sf::Sprite& s2);
void delay(int x);
void ship_movement(Character& ship, RenderWindow& window);
void missile_movement(vector<Missile*>& missiles, vector<Missile*>& enemyM);
Expand All @@ -27,14 +28,21 @@ int main() {
int level = 1, lives = 5, score = 0, tChange1 = 1, tChange2 = 1, tChange3 = 1;

Clock c, c2, c3;
sf::ContextSettings settings;
settings.antialiasingLevel = 0; //Disables anti-aliasing (might fix GPU issues)
settings.attributeFlags = sf::ContextSettings::Core;
sf::RenderWindow window(sf::VideoMode(950, 950), "Space Invaders", sf::Style::Default, settings); //Size of window

RenderWindow window(VideoMode(950, 950), "Space Invaders"); //Size of window
window.setFramerateLimit(60);

Texture t1, t2, t3, t4, t5, t6, t7, t8; //Allows different textures for items
sf::Texture texture, t1, t2, t3, t4, t5, t6, t7, t8; //Allows different textures for items
if (!texture.loadFromFile("assets/Ship.png")) {
std::cerr << "Failed to load ship.png!" << std::endl;
}

//In-game music
Music music;
music.openFromFile("music.ogg");
music.openFromFile("assets/music.ogg");
music.setLoop(true);
music.play();
music.setVolume(10.f);
Expand All @@ -43,9 +51,9 @@ int main() {
SoundBuffer buffer;
SoundBuffer buffer2;
SoundBuffer buffer3;
buffer.loadFromFile("shoot.wav");
buffer2.loadFromFile("invaderkilled.wav");
buffer3.loadFromFile("explosion.wav");
buffer.loadFromFile("assets/shoot.wav");
buffer2.loadFromFile("assets/invaderkilled.wav");
buffer3.loadFromFile("assets/explosion.wav");

Sound missile_sound;
Sound invader_killed;
Expand All @@ -61,8 +69,11 @@ int main() {

//Sets up different text in the game
Text scoreT, livesT, levelT, scoreEnd, levelEnd, endMessage, testCase;
Font font;
font.loadFromFile("font.ttf");
sf::Font font;
font.loadFromFile("assets/font.ttf");
if (!font.loadFromFile("assets/fonts/arial.ttf")) {
std::cerr << "Failed to load font!" << std::endl;
}

scoreT.setFont(font);
livesT.setFont(font);
Expand Down Expand Up @@ -98,20 +109,20 @@ int main() {

endMessage.setString("Press enter to continue...");

t1.loadFromFile("Ship.jpg"); //Loads image
t1.loadFromFile("assets/Ship.jpg"); //Loads image
Character* ship = new User(Vector2f(425.f, 825.f), t1, Vector2f(0.1f, 0.1f)); //Polymorphism because uer is a character

Character* enemies[10][5] = { nullptr }; //Allows for matrix of enemies
t2.loadFromFile("Alien.jpg");
t2.loadFromFile("assets/Alien.jpg");

for (int i = 0; i < 10; i++)
for (int j = 0; j < 5; j++)
enemies[i][j] = new Enemy(Vector2f(175.f + 60 * i, 100.f + 50 * j), t2, Vector2f(0.75f, 0.75f));

t3.loadFromFile("Missile.jpg");
t3.loadFromFile("assets/Missile.jpg");
vector<Missile*> missiles; //Keeps track of user missiles on the screen

t4.loadFromFile("enemyMissile.jpg");
t4.loadFromFile("assets/enemyMissile.jpg");
vector<Missile*> enemyM; //Keeps track of enemy missiles on the screen

//Helper variables for insertion into the vectors
Expand All @@ -123,10 +134,10 @@ int main() {
vector<Texture> shieldT2;
vector<Texture> shieldT3;

t5.loadFromFile("Shield1.jpg");
t6.loadFromFile("Shield2.jpg");
t7.loadFromFile("Shield3.jpg");
t8.loadFromFile("Shield4.jpg");
t5.loadFromFile("assets/Shield1.jpg");
t6.loadFromFile("assets/Shield2.jpg");
t7.loadFromFile("assets/Shield3.jpg");
t8.loadFromFile("assets/Shield4.jpg");

//Inserts the cycle of textures
shieldT1.push_back(t5);
Expand Down Expand Up @@ -156,9 +167,9 @@ int main() {
shieldS3.setScale(Vector2f(0.5f, 0.5f));

while (window.isOpen()) {
Event event;
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == Event::Closed)
if (event.type == sf::Event::Closed)
window.close();
}

Expand Down Expand Up @@ -256,6 +267,11 @@ int main() {

window.clear();

sf::CircleShape testShape(50.f); //Check the texture issue
testShape.setFillColor(sf::Color::Green);
testShape.setPosition(100,100);
window.draw(testShape);

//Draw statistics
window.draw(scoreT);
window.draw(livesT);
Expand All @@ -280,8 +296,7 @@ int main() {
for (int j = 0; j < 5; j++) {
for (int k = 0; k < missiles.size(); k++) {
//If user missile hits a live enemy with lots of offset and hitboxing
if (!missiles.empty() && (dynamic_cast <Enemy*> (enemies[i][j]))->isAlive() && missiles.at(k)->getPosition().y == enemies[i][j]->getPosition().y && missiles.at(k)->getPosition().x >
enemies[i][j]->getPosition().x - 20 && missiles.at(k)->getPosition().x < enemies[i][j]->getPosition().x + 20) {
if (!missiles.empty() && (dynamic_cast <Enemy*> (enemies[i][j]))->isAlive() && checkCollision(*missiles.at(k), *enemies[i][j])) {
missiles.erase(missiles.begin() + k); //Deletes that particular missile
(dynamic_cast <Enemy*> (enemies[i][j]))->kill();
invader_killed.play();
Expand All @@ -301,28 +316,25 @@ int main() {
for (int i = 0; i < enemyM.size(); i++) {

//If enemy missile hits ship
if (!enemyM.empty() && enemyM.at(i)->getPosition().y == ship->getPosition().y && enemyM.at(i)->getPosition().x > ship->getPosition().x - 20 && enemyM.at(i)->getPosition().x < ship->getPosition().x + 120) {
if (!enemyM.empty() && checkCollision(*enemyM.at(i), *ship)) {
enemyM.erase(enemyM.begin() + i);
lives--;
ship_hit.play();
}

//If enemy missile hits one of the shields

if (!enemyM.empty() && enemyM.at(i)->getPosition().y > shieldS1.getPosition().y && enemyM.at(i)->getPosition().x > shieldS1.getPosition().x - 20 && enemyM.at(i)->getPosition().x < shieldS1.getPosition().x + 120 &&
tChange1 <= 3) {
if (!enemyM.empty() && checkCollision(*enemyM.at(i), shieldS1) && tChange1 <= 3) {
enemyM.erase(enemyM.begin() + i);
shieldS1.setTexture(shieldT1.at(tChange1));
tChange1++;
}
if (!enemyM.empty() && enemyM.at(i)->getPosition().y > shieldS2.getPosition().y && enemyM.at(i)->getPosition().x > shieldS2.getPosition().x - 20 && enemyM.at(i)->getPosition().x < shieldS2.getPosition().x + 120 &&
tChange2 <= 3) {
if (!enemyM.empty() && checkCollision(*enemyM.at(i), shieldS2) && tChange2 <= 3) {
enemyM.erase(enemyM.begin() + i);
shieldS2.setTexture(shieldT2.at(tChange2));
tChange2++;
}
if (!enemyM.empty() && enemyM.at(i)->getPosition().y > shieldS3.getPosition().y && enemyM.at(i)->getPosition().x > shieldS3.getPosition().x - 20 && enemyM.at(i)->getPosition().x < shieldS3.getPosition().x + 120 &&
tChange3 <= 3) {
if (!enemyM.empty() && checkCollision(*enemyM.at(i), shieldS3) && tChange3 <= 3) {
enemyM.erase(enemyM.begin() + i);
shieldS3.setTexture(shieldT3.at(tChange3));
tChange3++;
Expand All @@ -341,25 +353,22 @@ int main() {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 5; j++) {
//If enemies get to y = 900 pixels, the user automatically loses
if ((dynamic_cast <Enemy*> (enemies[i][j]))->isAlive() && (dynamic_cast <Enemy*> (enemies[i][j]))->getPosition().y + 50 == 900)
if ((dynamic_cast <Enemy*> (enemies[i][j]))->isAlive() && enemies[i][j]->getPosition().y + enemies[i][j]->getGlobalBounds().height >= 900)
lives = 0;

//If enemy hits a shield, they die and the shield takes some damage

if ((dynamic_cast <Enemy*> (enemies[i][j]))->isAlive() && (dynamic_cast <Enemy*> (enemies[i][j]))->getPosition().y == shieldS1.getPosition().y && (dynamic_cast <Enemy*> (enemies[i][j]))->getPosition().x >
shieldS1.getPosition().x - 20 && (dynamic_cast <Enemy*> (enemies[i][j]))->getPosition().x < shieldS1.getPosition().x + 120 && tChange1 <= 3) {
if ((dynamic_cast <Enemy*> (enemies[i][j]))->isAlive() && checkCollision(*enemies[i][j], shieldS1) && tChange1 <= 3) {
(dynamic_cast <Enemy*> (enemies[i][j]))->kill();
shieldS1.setTexture(shieldT1.at(tChange1));
tChange1++;
}
if ((dynamic_cast <Enemy*> (enemies[i][j]))->isAlive() && (dynamic_cast <Enemy*> (enemies[i][j]))->getPosition().y == shieldS2.getPosition().y && (dynamic_cast <Enemy*> (enemies[i][j]))->getPosition().x >
shieldS2.getPosition().x - 20 && (dynamic_cast <Enemy*> (enemies[i][j]))->getPosition().x < shieldS2.getPosition().x + 120 && tChange2 <= 3) {
if ((dynamic_cast <Enemy*> (enemies[i][j]))->isAlive() && checkCollision(*enemies[i][j], shieldS2) && tChange2 <= 3) {
(dynamic_cast <Enemy*> (enemies[i][j]))->kill();
shieldS2.setTexture(shieldT2.at(tChange2));
tChange2++;
}
if ((dynamic_cast <Enemy*> (enemies[i][j]))->isAlive() && (dynamic_cast <Enemy*> (enemies[i][j]))->getPosition().y == shieldS3.getPosition().y && (dynamic_cast <Enemy*> (enemies[i][j]))->getPosition().x >
shieldS3.getPosition().x - 20 && (dynamic_cast <Enemy*> (enemies[i][j]))->getPosition().x < shieldS3.getPosition().x + 120 && tChange3 <= 3) {
if ((dynamic_cast <Enemy*> (enemies[i][j]))->isAlive() && checkCollision(*enemies[i][j], shieldS3) && tChange3 <= 3) {
(dynamic_cast <Enemy*> (enemies[i][j]))->kill();
shieldS3.setTexture(shieldT3.at(tChange3));
tChange3++;
Expand Down Expand Up @@ -729,6 +738,18 @@ void ship_movement(Character& ship, RenderWindow& window) {
ship.move(-0.5, 0);
}

/****************************************************************
* Function: checkCollision() *
* Description: Checks if two sprites intersect *
* Input parameters: const Sprite&, const Sprite& *
* Returns: bool *
* Preconditions:Two valid sprites *
* Postconditions: Returns true if sprites intersect *
*****************************************************************/
bool checkCollision(const Sprite& s1, const Sprite& s2){
return s1.getGlobalBounds().intersects(s2.getGlobalBounds());
}

/****************************************************************
* Function: missile_movement() *
* Date Created: 4/17/2021 *
Expand Down Expand Up @@ -773,4 +794,4 @@ void draw_missiles(vector<Missile*>& missiles, vector<Missile*>& enemyM, RenderW
for (int i = 0; i < enemyM.size(); i++)
window.draw(*enemyM.at(i));
}
}
}
Loading