예제 #1
0
파일: main.cpp 프로젝트: bobrusha/my_game
void eventsAfterWin(sf::Event &event, sf::RenderWindow &window, bool &in_menu, int x1, int y1, int x2, int y2)
{
	switch(event.type)
	{
	case sf::Event::MouseButtonPressed:
		{
			if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
			{
				sf::Vector2i mPos = sf::Mouse::getPosition(window);
				if ( mPos.x >= x1 && mPos.x <= 500 && mPos.y >= y1 && mPos.y <= y1 + 100 )
				{
					scrn.setRun(true);
					in_menu = true;
				}

				if ( mPos.x >= x2 && mPos.x <= 500 && mPos.y >= y2 && mPos.y <= y2 + 100)
				{
					window.close();
				}
			}
		}
	}
}
예제 #2
0
파일: main.cpp 프로젝트: bobrusha/my_game
void eventsAfterLosing(sf::Event &event, sf::RenderWindow &window, bool &in_menu)
{
	switch(event.type)
	{
	case sf::Event::MouseButtonPressed:
		{
			if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
			{
				sf::Vector2i mPos = sf::Mouse::getPosition(window);
				if ( mPos.x >= 100 && mPos.x <= 200 && mPos.y >= 250 && mPos.y <= 350 )
				{
					scrn.setRun(true);
					in_menu = true;
				}

				if ( mPos.x >= 250 && mPos.x <= 350 && mPos.y >= 250 && mPos.y <= 300 )
				{
					window.close();
				}
			}
		}
	}
}
예제 #3
0
파일: main.cpp 프로젝트: bobrusha/my_game
int main ( int argc, char** argv)
{
	sf::RenderWindow window(sf::VideoMode(wW, wH), "MyDynablaster", sf::Style::Titlebar);

	sf::Clock clock;

	//==================================================================================================== Textures for objects
	
	sf::Texture texMainHero;
	texMainHero.loadFromFile("main_hero.png");
	sf::Sprite sprMainHero;
	sprMainHero.setTexture(texMainHero);

	sf::Texture texBrick;
	texBrick.loadFromFile("brick.png");
	sf::Sprite sprBrick;
	sprBrick.setTexture(texBrick);

	sf::Texture texEnemy;
	texEnemy.loadFromFile("enemy.png");
	sf::Sprite sprEnemy;
	sprEnemy.setTexture(texEnemy);

	sf::Texture texBomb;
	texBomb.loadFromFile("bomb.png");
	sf::Sprite sprBomb;
	sprBomb.setTexture(texBomb);

	sf::Texture texPortal;
	texPortal.loadFromFile("portal.png");
	sf::Sprite sprPortal;
	sprPortal.setTexture(texPortal);

	sf::Texture texFire;
	texFire.loadFromFile("fire.png");
	sf::Sprite sprFire;
	sprFire.setTexture(texFire);

	sf::Font font;
	if (!font.loadFromFile("font.ttf"))
	{
		return 3;
   	}

	
	//-----------------------------------------------------------------------------------------------Boolean variables	
	bool lvl_is_completed = false;
	bool in_menu = true;
	bool gamer_lose = false;
	// -----------------------------------------------------------------------------------------------Game
	while (window.isOpen())
    {
		sf::Event event;

		while ( in_menu)
		{
			window.clear(sf::Color::White);

			sf::Text txt1, txt2;
			const int txt1_x = 100, txt1_y=100;

			txt1.setFont(font);
			txt1.setString("Start game");
			txt1.setColor(sf::Color::Black);
			txt1.setPosition(txt1_x, txt1_y);
			txt1.setCharacterSize(72);
			window.draw(txt1);

			const int txt2_x = 100, txt2_y=180;
			txt2.setFont(font);
			txt2.setString("Exit");
			txt2.setColor(sf::Color::Black);
			txt2.setPosition(txt2_x,txt2_y);
			txt2.setCharacterSize(72);
			window.draw(txt2);

			window.display();
			while ( window.pollEvent(event))
			{
				eventsInMenu( event, window, in_menu, txt1_x, txt1_y, txt2_x, txt2_y);
			}
			
		}

		while ( window.pollEvent(event) )
		{
			if(scrn.getRun())
			{
				eventsInGame(event, window);
			}
			else
			{
				if(gamer_lose)
				{
					eventsAfterLosing (event, window, in_menu);
				}
				else
				{
					eventsAfterWin (event, window, in_menu, 100, 150, 100, 250);
				}
			}
		}

		//--------------------------------------------------------------------------------------------------------------------Draw
		window.clear(sf::Color::White);

		if (scrn.getRun() == true)
		{
			sprPortal.setPosition(sf::Vector2f(lvlup.l, lvlup.b));
			window.draw(sprPortal);

				if (! bricks.empty())
				{
					for (list<brick>::iterator i = bricks.begin(); i != bricks.end(); ++i)
					{
						sprBrick.setPosition(sf::Vector2f(i->l, i->b));
						window.draw(sprBrick);
					}
				}
				if (! enemies.empty())
				{

					sf::Time elapsed1 = clock.getElapsedTime();

					if (elapsed1.asSeconds() >= 0.5f)
					{
						for (list<enemy>::iterator i = enemies.begin(); i != enemies.end(); ++i)
						{

							i->motion(scrn, Main);
						}
						sf::Time elapsed1 = clock.restart();
					}
					for (list<enemy>::iterator i = enemies.begin(); i != enemies.end(); ++i)
					{	
						sprEnemy.setPosition(sf::Vector2f(i->l, i->b));
						window.draw(sprEnemy);
					}
				}
				if ( !bombs.empty())
				{
					for (list<bomb>::iterator i = bombs.begin(); i != bombs.end(); ++i)
					{
						sprBomb.setPosition(sf::Vector2f(i->l, i->b));

						if ( (i->clock).getElapsedTime().asSeconds() >= 3.0)
						{
							i->damage(window, sprFire);
							if ( (i->clock).getElapsedTime().asSeconds() > 5.0)
							{
								lst_iter.push_back(i);
							}

						}
						window.draw(sprBomb);
					}
				}
				sprMainHero.setPosition (sf::Vector2f (Main.l, Main.b));
				window.draw (sprMainHero);

				if ( enemies.empty() && Main.b == lvlup.b && Main.r ==lvlup.r)
				{
					std::cout<<NUM_LVL<<std::endl;
					if ( NUM_LVL < max_num_lvls -1)
					{
						levelUp();
					}
					else
					{
						std::cout<<"I'm here"<< std::endl;
						scrn.setRun(false); 
					}
				}
			}
			else
			{
				bricks.clear();
				bombs.clear();
				enemies.clear();
				scrn.clearScreen();
				scrn.setRun(false);

				sf::Text txt1, txt2, txt3, txt4;

				if ( NUM_LVL >= max_num_lvls - 1)
				{
					getText(txt1, font, "You Win!!", 100, 50, 72);
					window.draw(txt1);

					getText(txt2, font, "Back in menu", 100, 150, 72);
					window.draw(txt2);

					getText(txt3, font, "Exit", 100, 250, 72);
					window.draw(txt3);
				}
				else
				{
					gamer_lose = true;

					getText(txt1, font, "You lose", 100, 50, 72);
					window.draw(txt1);

					getText(txt2, font, "Try again?", 100, 150, 72);
					window.draw(txt2);

					getText(txt3, font, "Yes", 100, 250, 72);
					window.draw(txt3);

					getText(txt4, font, "No", 250, 250, 72);
					window.draw(txt4);
				}
			}

			window.display();
			
			if ( bombs.size() != 0)
			{ 
				for (list<list<bomb>::iterator>::iterator i = lst_iter.begin(); i != lst_iter.end(); ++i)
				{
					bombs.remove( *(*i));
				}
				lst_iter.clear();
			}
	} //while (window.isOpen())

    return 0;
}