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.
#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
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
#include <iostream>
#include <windows.h> //Here
usingnamespace 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!
(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...)
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:
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
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.
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:
@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";
unsignedchar 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.