SFML c ++当包含文件在内的游戏中发生事件时,如何使用铃声功能为游戏创建声音类?

问题描述 投票:1回答:1

在我的示例中,我只能通过将main函数内部的所有内容设置为令人困惑的方法来使其工作。我希望知道是否可以创建一个新类,以便将一个新的游戏中的所有功能组织到一个include文件中。如果可能的话,帮助我们

正在处理cpp文件的主要功能,但我希望创建一个包含文件,我认为效果更好

      int main()
{
    //all inside main function not good confuse
    //sound play fire function
    sf::SoundBuffer fire;
    fire.loadFromFile("sounds/fire.wav");
    sf::Sound fired;
    fired.setBuffer(fire);

    // sound hit asteroid
    sf::SoundBuffer astHit;
    astHit.loadFromFile("sounds/explo_asteroid.wav");
    sf::Sound hited;
    hited.setBuffer(astHit);

    // sound Dead player
    sf::SoundBuffer pDead;
    pDead.loadFromFile("sounds/dead_player.wav");
    sf::Sound dead;
    dead.setBuffer(pDead);

    // sound Thrust Ship
    sf::SoundBuffer thrust;
    thrust.loadFromFile("sounds/thrust.wav");
    sf::Sound sthrust;
    sthrust.setBuffer(thrust);
    sthrust.setVolume(30);

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

            if (event.type == Event::KeyPressed)
             if (event.key.code == Keyboard::Space)
              {
                bullet *b = new bullet();
                b->settings(sBullet,p->x,p->y,p->angle,10);
                entities.push_back(b);
                fired.play();//fire play Sound
              }
        }

    if (Keyboard::isKeyPressed(Keyboard::Right)) p->angle+=3;
    if (Keyboard::isKeyPressed(Keyboard::Left))  p->angle-=3;
    if (Keyboard::isKeyPressed(Keyboard::Up)) p->thrust=true;
    else p->thrust=false;

    for(auto a:entities)
     for(auto b:entities)
     {
      if (a->name=="asteroid" && b->name=="bullet")
       if ( isCollide(a,b) )
           {
            hited.play();//Hited asteroid Sound
            a->life=false;
            b->life=false;

            Entity *e = new Entity();
            e->settings(sExplosion,a->x,a->y);
            e->name="explosion";
            entities.push_back(e);


            for(int i=0;i<2;i++)
            {
             if (a->R==15) continue;
             Entity *e = new asteroid();
             e->settings(sRock_small,a->x,a->y,rand()%360,15);
             entities.push_back(e);
            }

           }

      if (a->name=="player" && b->name=="asteroid")
       if ( isCollide(a,b) )
           {
            dead.play();//Dead Player Sound
            b->life=false;

            Entity *e = new Entity();
            e->settings(sExplosion_ship,a->x,a->y);
            e->name="explosion";
            entities.push_back(e);

            p->settings(sPlayer,W/2,H/2,0,20);
            p->dx=0; p->dy=0;
           }
     }


    if (p->thrust){   
        p->anim = sPlayer_go;
        sthrust.play();
    }   
    else   p->anim = sPlayer;
 }

}
c++ audio sfml
1个回答
0
投票
首先,我们将创建一个头文件:
© www.soinside.com 2019 - 2024. All rights reserved.