More stuff.

I want to program a game but to have some other stuff added to it.
How do you make a beep or put music into a program.
How do you make words delay before showing up (for example when you press enter, the screen will show "Hello" and then 2 seconds later it'll show "World" under it.)
How do you make a menu with using arrows keys instead of number pressing.
And last, how do you change the colour of some words.

Thank you if you could help with ANY of these questions.

-legit
i know one way you can make a beep is using numbers with char. like char beep = then a number i forgot which one run this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <cstdlib>
#include <iostream>
#include <windows.h>
using namespace std;

int main(int argc, char *argv[])
{
    char beep = 1;
    int x = 1;
    bool done = false;
    while(!done){
    cout << beep << " = " << x << endl;
    beep++;
    x++;
    system("PAUSE>NULL");
   
}
 
    return EXIT_SUCCESS;
}




i think in one of the first 10 theres a beep.

to change the system color use ;
1
2
#include <windows.h>system("color p");//look  at the chart and replace the
// p with what you need,do 2 things off that chart to do the text then the background 

for the hello then world do
1
2
3
4
5
#include <iostream.h>
#include <windows.h>
cout << "hello ";
Sleep(2000);
cout << "world;  

the arrow keys is a good bit to learn i could teach you or u could learn your own if u want me to teach you tell me and if you want to learn on your own learn how to use getch() and if u dont know already switch statements
Last edited on
The solution for you is windows.h library:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <windows.h> //Here
using namespace std;

void SetColor(int color){ //A simple function, don't ask about this.
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),  color);
}

int main{
    Beep(200,50); //A beep, first number is the frequence, second number is the duration.
    SetColor(12); //Changing the color to bright red
    cout << "\n Hello";
    Sleep(2000); //Delay, the number is the duration
    SetColor(15); //Changing the color to bright white
    cout << " World!";
    cin.sync(); cin.ignore(); //A way of pausing
}


Experiment with SetConsoleTextAttribute, there are 15 text colors, and numbers after 15 are background colors.

Working with the arrow keys, well, it's very tricky, you have to instantly scan key pressing, then knowing what you pressed, to change the position of a pointer and refreshing the screen every single time. I can teach you, you can see this in my console game Skeptor: http://www.cplusplus.com/forum/beginner/77350/
Tell me if you really want to, but it's a little bit advanced... Trust me!


And avoid using system()!!!
Last edited on
yeah i guess i should not have put system there instead use
1
2
3
4
HANDLE color = GetStdHandle(STD_OUTPUT_HANDLE);

SetConsoleTextAttribute(color,11);

i think thats right just change the 11 to whatever theres alot
Just added a color changing example. Please read my comment a few times to understand what the things do.
Wait, why shouldn't i use
system()
?

(Oh and samrux, I love your game! :D That's why I wanted to learn how to use it. Plus I want to put music into my games using beeps. But I think that may take a while to learn...)
Last edited on
Oh. .-.
Hey legitiment, I already showed beeps to you, now, experiment with them! :3
Adding real music files is easy, there are several ways, but I'll wait for someone to explain you better, as I don't remember the "easy" way.

I'll write a comment/private message with step-by-step guide to option selection with arrow keys.

It's really important to know exactly the keys you'll use. These are the IDs of the most used keys by me and other people:

Enter: 13
Backspace: 8
Up arrow: 72
Down arrow: 80
Right arrow: 77
Left arrow: 75


For more comfortable using of those key numbers, you can add something like this at the start of your program, and use the names to refer to the numbers:

1
2
3
4
5
6
#define Key_Enter 13
#define Key_Backspace 8
#define Key_Up 72
#define Key_Down 80
#define Key_Right 77
#define Key_Left 75 



To recognize those keys, you need an instant pausing/key scanning. You can use conio.h library and getch, like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <conio.h>

int main(){
    cout << "\n Press an arrow key:\n";
    int choice;
    choice = getch();
    switch(choice){
        case Key_Up:
            cout << "\n You pressed up.\n";
            break;
        case Key_Down:
            //Etc.
    }

    cout << "\n Press any key to continue.";
    getch();
    return 0;
}



Maybe I'll continue tomorrow, but after having this clearly, you need to start working on the main function, with parameters to know what to show before the options and what are the options themselves.
@samrux, I found a youtube video saying about the frequensie and lenth required to create simple music! I'll send a PM when I'm done. Done with my song! :D
samrux i had a feeling you were going to post my idea here :D
@samrux
That code won't work for the arrow keys, those are not the ascii codes for those keys. On my system using your code all arrow keys result in 224. Those may be the key codes, but key codes and ascii (character) codes are not necessarily equivalent.
Last edited on
I didn't say that ASCII codes and key codes are the same, and I'm almost sure those numbers work. Try downloading my console game; Skeptor, and see if the arrow keys work. If they don't... I'll see... D:
Last edited on
@samrux
The example you posted in this thread doesn't work, at least not for me. I did some research and it appears the control code for special keys are in the second byte of the extended 2 byte character like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main(){
	
    cout << "\n Press an arrow key:\n";
    unsigned char choice;
    choice = _getch();
	if(choice == 224)
		choice = _getch();

    switch(choice){
        case Key_Up:
            cout << "\n You pressed up.\n";
            break; 
		case Key_Down:
			cout << "\n You pressed down.\n";
        break; 
    }

    return 0;
}

I have seen yours and oppositescopez code and see it works without the second getch call. If you put a break point on the switch line you will see the value change from 224 for int or unsigned char (or -32 for char I believe) to the correct control code.
Hopefully someone can shed some light as to why the single call to getch works sometimes as is the case for your game.
At any rate, I apologize for posting before full researching and testing.
Well, I don't understand. Does the option selection with arrow keys work in my game for you? Do the instant pausing work in my game for you?

What is 224 for you, and why to have 2 choice = getchs?

And Legitiment, I sent you the basic code with commentary to you already. Check it out! :D



Edit: WTF, that code i posted DOESN'T WORKS AT ALL. But both the code in my game and the code I sent to Leg work. WHAT IS GOING OOOOON
Last edited on
http://support.microsoft.com/kb/57888 should explain the two calls to _getch(). As to why your program doesn't need them, I don't know.
Because that's C, not C++, I think
Last edited on
Topic archived. No new replies allowed.