Example #1
0
int main(){
    srand(time(NULL));
    Background::load_background();
    Sound::load_sounds();
    Sprites::load_sprites();
    Font::load_font();
    window.setFramerateLimit(30);
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space) && shot_counter==0){
            break;
        }
        window.draw(Background::before_game_start_sprite);
        window.display();
    }
    Hero hero;
    Enemys enemys;
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        shot_counter++;
        shot_counter%=10;
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space) && shot_counter==0){
            hero.fire();
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
            hero.move_left();
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
            hero.move_right();
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){
            hero.move_up();
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){
            hero.move_down();
        }
        Sound::play_background_music();
        Background::show(window);
        hero.turn(window);
        enemys.turn(window);
        window.display();
        if (hero.get_health()<=0) break;
    }
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space) && shot_counter==0){
            break;
        }
        Sound::game_music.stop();
        window.draw(Background::gameover_sprite);
        window.display();
    }
    return 0;
}