Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Sfml 2.1 Pong Game: Play a random (1 of 3) sound when two rectangles collide  (Read 3488 times)

0 Members and 1 Guest are viewing this topic.

exitcode

  • Guest
I want to play one of three sounds whenever a controllable square shape touches a rectangle.

I did something similar at the start of the program in which it will randomly pick 1 of 6 titles and display it.


 string randomTitle1 = "Apples are under-rated!";
    string randomTitle2 = "#datjaffa";
    string randomTitle3 = "Cheese is epic!";
    string randomTitle4 = "DjangoFTW!";
    string randomTitle5 = "Bad games are under-rated!";
    string randomTitle6 = "Pong by Finn Fallowfield - Copyright Digital Native Labs ltd.";
    string chosenTitle = randomTitle6;

   
    // EXAMPLE RANDOMIZER
   
    srand(time(0));
    int x = 1+(rand()%6);
    cout << endl << endl << endl << endl;
    cout << x << endl;
   
    if(x == 1){
        chosenTitle = randomTitle1;
    }
    else if(x == 2){
        chosenTitle = randomTitle2;
    }
    else if(x == 3){
        chosenTitle = randomTitle3;
    }
    else if(x == 4){
        chosenTitle = randomTitle4;
    }
    else if(x == 5){
        chosenTitle = randomTitle5;
    }
    else if(x == 6){
        chosenTitle = randomTitle6;
    }
    else {
        cout << "A fatal error occured!" << endl;
        return 9;
    }
   
   // Creating the window
   sf::RenderWindow window(sf::VideoMode(1200, 800), chosenTitle);
 
[/spoiler]

I would like to do the same for playing a sound, but it would have to randomly generate numbers all the time as I want the sound to continue to change randomly. When I tried, it would just take ages for it to play and then it would play the same sound everytime anyway. I tried putting a randomiser in a while loop but the program didn't played the music and no window appeared. Here is the code of the whole program so far:

(click to show/hide)

Any help would be much appreciated. I have just started learning so try to make your answer as simple and understandable as possible and try to include some code I could use. Thank you in advance!

Im using sfml 2.1 in Xcode on a mac running mavericks. FYI.
« Last Edit: December 16, 2013, 06:27:53 pm by exitcode »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10821
    • View Profile
    • development blog
    • Email
The problem with your post is, that you didn't read/follow this thread, especially the following part:

Identify the problem (a.k.a "complete and minimal code")

At this point you're about to write your post. Many people will read it and try to help, but none of them have hours to spend on your problem. So it is a very bad idea to copy-paste pieces of your original code, or even worse, the whole thing.
Why is it a bad idea? Because:
  • your code contains 95% of code which is irrelevant to the problem
  • people just give up reading when they see too much code
  • people can't test it themselves
  • everybody loses precious time because the problem is lost in tens or hundreds of lines of code
So, let's say you have a problem when displaying a sprite. Nobody cares about how you check collisions, how you play sounds, how you export your game files, etc... It would just be a waste of time to read this code. So what you must do is to extract the relevant lines of code from your application and write a new piece of code that contains only them. We don't care if the new code doesn't do exactly what your original application does, all it has to do is to reproduce the problem.

It is important that:
  • the code is complete, so people can test it
  • the code is as simple as possible, the best is one main() with all the code inside -- people don't want to setup a complex project just to test your code
  • the code reproduces the initial problem
  • the application doesn't depend on external resource files (unless it's related to the problem)
While reducing the code, chances are that you solve the problem yourself: things are clearer and more obvious with less code. And if you can't find anything, we will most likely give you an answer very quickly because the code to examine is very small.

Reduce the code and we'll take a look at it. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

exitcode

  • Guest
The problem with your post is, that you didn't read/follow this thread, especially the following part:

Identify the problem (a.k.a "complete and minimal code")

At this point you're about to write your post. Many people will read it and try to help, but none of them have hours to spend on your problem. So it is a very bad idea to copy-paste pieces of your original code, or even worse, the whole thing.
Why is it a bad idea? Because:
  • your code contains 95% of code which is irrelevant to the problem
  • people just give up reading when they see too much code
  • people can't test it themselves
  • everybody loses precious time because the problem is lost in tens or hundreds of lines of code
So, let's say you have a problem when displaying a sprite. Nobody cares about how you check collisions, how you play sounds, how you export your game files, etc... It would just be a waste of time to read this code. So what you must do is to extract the relevant lines of code from your application and write a new piece of code that contains only them. We don't care if the new code doesn't do exactly what your original application does, all it has to do is to reproduce the problem.

It is important that:
  • the code is complete, so people can test it
  • the code is as simple as possible, the best is one main() with all the code inside -- people don't want to setup a complex project just to test your code
  • the code reproduces the initial problem
  • the application doesn't depend on external resource files (unless it's related to the problem)
While reducing the code, chances are that you solve the problem yourself: things are clearer and more obvious with less code. And if you can't find anything, we will most likely give you an answer very quickly because the code to examine is very small.

Reduce the code and we'll take a look at it. :)

Sorry, eek, I'm new. I compacted code. I though all of it may have been useful but now I have just included the area where I wrote the code for the sounds and the bit where I would like to play them.

Anyway, its a lot clearer.

exitcode

  • Guest
Any idea anyone?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10821
    • View Profile
    • development blog
    • Email
Unfortunately you didn't read the full quote, because you missed the following two very important points:
Quote
  • the code is complete, so people can test it
  • the code is as simple as possible, the best is one main() with all the code inside -- people don't want to setup a complex project just to test your code

The idea essentially is to get a code base that only loads and plays the sound, without the whole stuff for your entities and collision parts. If that doesn't yield a problem, then you can add some very simplified entity logic. Doing so, you might find a cause of the issue yourself.
The other way around where you have the full source and slowly remove pieces works as well, but can be a bit harder.

I currently can't see where the problem is in the code. If you have preloaded the sound and then call play(), it should instantly play the sound.

Btw what's the actual issue here? Is it the window that doesn't show? Or is it the sound that doesn't play? Or is it the delay you have till a sound plays? Or is it the collision that you have problem with?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

exitcode

  • Guest
Unfortunately you didn't read the full quote, because you missed the following two very important points:
Quote
  • the code is complete, so people can test it
  • the code is as simple as possible, the best is one main() with all the code inside -- people don't want to setup a complex project just to test your code

The idea essentially is to get a code base that only loads and plays the sound, without the whole stuff for your entities and collision parts. If that doesn't yield a problem, then you can add some very simplified entity logic. Doing so, you might find a cause of the issue yourself.
The other way around where you have the full source and slowly remove pieces works as well, but can be a bit harder.

I currently can't see where the problem is in the code. If you have preloaded the sound and then call play(), it should instantly play the sound.

Btw what's the actual issue here? Is it the window that doesn't show? Or is it the sound that doesn't play? Or is it the delay you have till a sound plays? Or is it the collision that you have problem with?

Maybe this is the wrong subforum. Its not really a problem which is why I didn't include runable code. I was just wandering how I play a random sound. Whatever I try it seems to just play the same sound and not a truly random one. I was more asking for code rather than for mine to be fixed. Sorry if this is the wrong place to post.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Your initial code is horrible, it contains masses of code duplication. If you have to enumerate variables such as string1, string2 etc., you're doing something wrong. Have a look at STL containers and loops, they would greatly simplify everything. And if (condition == true) is not necessary, since condition is already a bool. if (condition) works just fine.

By the way, instead of std::rand(), you could also use C++11 random number generation library to produce random numbers. If you use Thor, you can also directly call its random functions.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

exitcode

  • Guest
I was using the code from this tutorial: https://www.udemy.com/learn-c-game-development/

Im a bit of a newbie btw. Whats wrong with enumeration? I know I could have just put the raw string in the if statements but I declared strings to make it clearer to read. And where is the duplication?

C++ is so unforgiving :(

wintertime

  • Sr. Member
  • ****
  • Posts: 255
    • View Profile
The duplication is in many places.
- You use variable1, variable2, ... when you should at least use an array.
- You have many repeated ifs when a single array indexing would suffice.
- You do the same load operation many times when a loop would suffice.
- You have duplicated check and set for the movements when you can easily use multiplication and addition.
- You handle x and y separately when they should be combined in a vector.
- You do the same intersection test twice.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Whats wrong with enumeration?
Wrong is that you don't use the language and library features (arrays, containers) for collections of similar objects. It makes code longer as well as more difficult to read, to maintain, to extend and to debug. Code duplication is simply bad practice that can be easily avoided.

You have to ask yourself: How many times did you use copy&paste when you wrote that code? Every time you used it is a potential place for code duplication. Before copying code, reflect if there is no way to avoid it (mostly there is, see wintertime's post for concrete tips). But of course you have to learn the programming language in order to know about these techniques. Do you have a good C++ book?

C++ is so unforgiving :(
True, but that's not a C++ problem. Code duplication is bad in any language.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

exitcode

  • Guest
I understand... Ordered a c++ book to read up on arrays :P
You have been very helpful although I still don't really understand why random sounds don't really work.

Thanks anyway.