Demo entry 2363352

sasdasd

   

Submitted by anonymous on Aug 14, 2015 at 20:24
Language: C++. Code size: 7.7 kB.

#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
#include <string>

#include "particles.h"
#include "player.h"
#include "bullet.h"
#include "EnemyManager.h"

#define WINDOW_SIZE_X 1024
#define WINDOW_SIZE_Y 768

using std::cout; using std::endl;

int main()
{
	float circlePosX = 0, circlePosY = 0;
	static float circleSpeed = 5;

	sf::ContextSettings settings;
	settings.antialiasingLevel = 8;

	sf::RenderWindow window(sf::VideoMode(WINDOW_SIZE_X, WINDOW_SIZE_Y), "Game", sf::Style::Default, settings);
	window.setMouseCursorVisible(true);
	window.setFramerateLimit(60);

	CirclePlayer player;

	// tracks frequency of bullets
	sf::Clock bulletClock;

	// clock for particle effect for main player
	sf::Clock playerParticleClock;
	ParticleSystem playerParticleEffect(1000, sf::Color::Blue);

	std::vector<CircleBullet*> bullets;
	std::vector<LineBullet*> lineBullets;

	// Load crosshair image and create sprite
    sf::Texture crosshairTexture;
    crosshairTexture.loadFromFile("crosshair.png");
    sf::Sprite crosshairSprite(crosshairTexture);

	sf::Texture buttonTex;
	buttonTex.loadFromFile("buttons/button.png");
	sf::Sprite button(buttonTex);
	button.setPosition(50, 50);

	sf::SoundBuffer buffer;
	buffer.loadFromFile("laser.wav");

	sf::Sound sound;
	sound.setBuffer(buffer);
	
	CirclePlayer* playerListener = &player;

	EnemyManager enemyManager;
	enemyManager.setMainPlayer(playerListener);

	std::vector<ParticleSystem> enemyParticleEffects;
	std::vector<sf::Clock> enemyParticleClock;

	sf::Font font;

	if(!font.loadFromFile("arial.ttf")){

		std::cout << "Could not find font" << std::endl;
	}

	sf::Text numberOfEnemiesText;
	numberOfEnemiesText.setFont(font);
	numberOfEnemiesText.setCharacterSize(15);
	numberOfEnemiesText.setStyle(sf::Text::Bold);
	numberOfEnemiesText.setColor(sf::Color::Red);
	numberOfEnemiesText.setPosition(20, 0);
	numberOfEnemiesText.setString("Enemies: ");

	bool MENU = true;
	bool GAME = false;
	bool PAUSE_MENU = false;


	sf::Clock playButtonClock;
	ParticleSystem playButtonParticle(1000, sf::Color::Red);
	playButtonParticle.setEmitter(sf::Vector2f(150, 60));

	/*
	*	Main Game Loop
	*/
    while (window.isOpen())
    {
		if(MENU)
		{
			sf::Event event;
			while (window.pollEvent(event))
			{
				if (event.type == sf::Event::Closed)
					window.close();
			}
			
			playButtonParticle.update(playButtonClock.restart());

			window.clear();
			window.draw(playButtonParticle);
			window.draw(button);
			sf::Vector2f mousePos = static_cast<sf::Vector2f>(sf::Mouse::getPosition(window));
			if(button.getGlobalBounds().contains(mousePos.x, mousePos.y) && sf::Mouse::isButtonPressed(sf::Mouse::Left))
			{
				MENU = false;
				GAME = true;
				window.setMouseCursorVisible(false);
			}
			

			window.display();
		}
		else if(PAUSE_MENU)
		{
			sf::Event event;
			while (window.pollEvent(event))
			{
				if (event.type == sf::Event::Closed)
					window.close();
				if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape){
					PAUSE_MENU = false;
					GAME = true;
				}
			}
			sf::RectangleShape rec(sf::Vector2f(WINDOW_SIZE_X, WINDOW_SIZE_Y));
			rec.setPosition(0,0);
			rec.setFillColor(sf::Color(0,0,0,40));
			window.draw(rec);

			window.display();
		}
        else if(GAME){
			sf::Event event;
			while (window.pollEvent(event))
			{
				if (event.type == sf::Event::Closed)
					window.close();
				if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape){
					PAUSE_MENU = true;
					GAME = false;
				}
			}

			if(sf::Keyboard::isKeyPressed(sf::Keyboard::W)){
				player.MoveForward();
			}
			if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)){
				player.MoveLeft();
			}
			if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)){
				player.MoveBackward();
			}
			if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)){
				player.MoveRight();
			}
			if(sf::Mouse::isButtonPressed(sf::Mouse::Left)){

				if(bulletClock.getElapsedTime().asMilliseconds() > 100){
					CircleBullet* bullet = new CircleBullet(player.getXPosition() + player.getRadius(), player.getYPosition(), static_cast<sf::Vector2f>(sf::Mouse::getPosition(window)));
					bullets.push_back(bullet);
				
					//LineBullet* lineBullet = new LineBullet(player.getXPosition() + player.getRadius(), player.getYPosition(), static_cast<sf::Vector2f>(sf::Mouse::getPosition(window)));
					//lineBullets.push_back(lineBullet);

					sound.play();
					bulletClock.restart();
				}
			}

			playerParticleEffect.setEmitter(window.mapPixelToCoords(player.getPosition()));

			// update the player particles
			playerParticleEffect.update(playerParticleClock.restart());

			// Set position        
			crosshairSprite.setPosition(static_cast<sf::Vector2f>(sf::Mouse::getPosition(window)));
        
			/*
			*	Draw all of the sprites
			*/
			window.clear();
		
			window.draw(playerParticleEffect);

			std::vector<Enemy*>* enemies = enemyManager.getEnemyVector();

			// loop through all of the bullets to check for collisions, upadate them
			// and draw them
			for(int i = 0; i < bullets.size(); i++){

				// draw the bullet
				window.draw(*bullets[i]);
				// update the position of the bullet
				bullets[i]->UpdatePosition();

				sf::FloatRect bulletBounds = bullets[i]->getGlobalBounds();
			
				// check for collision with enemy
				for(int j = 0; j < enemies->size(); j++){

					sf::FloatRect bounds = (*enemies)[j]->getGlobalBounds();

					if(bounds.intersects(bulletBounds)){

						// particle effect for destroyed block
						ParticleSystem destroyedBlockParticles (250, (*enemies)[j]->getColor());
						destroyedBlockParticles.setEmitter(window.mapPixelToCoords((*enemies)[j]->getPositionVector()));
					
						// create clock for tracking particle lifetime
						sf::Clock particleClock;

						// put particle effect and corresponding clock into vectors
						enemyParticleEffects.push_back(destroyedBlockParticles);
						enemyParticleClock.push_back(particleClock);

						// have the enemy manager class delete the enemy
						enemyManager.deleteEnemy(j);

						// delete the bullet that hit the block
						delete bullets[i];
						bullets.erase(bullets.begin() + i);
					}
				}
			}

			// check if bullets are off screen
			for(int i = 0; i < bullets.size(); i++){
				// check if out of bounds
				if(bullets[i]->getPosition().x > WINDOW_SIZE_X || bullets[i]->getPosition().x < 0 || 
					bullets[i]->getPosition().y > WINDOW_SIZE_Y || bullets[i]->getPosition().y < 0){
						delete bullets[i];
						bullets.erase(bullets.begin() + i);
				}
			}

			// draw all of the enemies
			for(int iter = 0; iter < enemies->size(); iter++){
				window.draw(*((*enemies)[iter]));
			}

			enemyManager.updateEnemies();

			for(int j = 0; j < enemyParticleEffects.size(); j++){
				window.draw(enemyParticleEffects[j]);
				enemyParticleEffects[j].update(enemyParticleClock[j].restart());

				if(enemyParticleEffects[j].getMasterTime().getElapsedTime().asSeconds() > 1){
					enemyParticleEffects.erase(enemyParticleEffects.begin() + j);
					enemyParticleClock.erase(enemyParticleClock.begin() + j);
				}
			}

			window.draw(crosshairSprite);
			window.draw(player);

			numberOfEnemiesText.setString("Enemies: " + std::to_string((long long)enemyManager.getNumberOfEnemies()));
			window.draw(numberOfEnemiesText);
			window.display();
		}
    }

    return 0;
}

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).