2.6Music
Generally, in order to solve the boring problem, games usually need to add music. Well, we also need to add music. In fact, it is not that difficult. Like all other programming, you can simply call Microsoft's API functions.
We can use MCI to easily play MIDI, WAV, and other sounds in the program. To use it, we need to declare it in advance. In the file header # include <mmsystem. h>, and add "winmm. lib" to the project"
Let's take a look at the process of playing Midi. First, open the device:
Mci_open_parms openparms;
Openparms. lpstrdevicetype =
(Lpcstr) mci_devtype_sequencer;//Is a MIDI file
Openparms. lpstrelementname = (lpcstr) filename;//File Name
Openparms. wdeviceid = 0;//The identifier of the device to be opened.
Mcisendcommand (null, mci_open,
Mci_wait | mci_open_type |
Mci_open_type_id | mci_open_element,
(DWORD) (lpvoid) & openparms );//Enable the device
Then you can play the Midi:
Mci_play_parms playparms;
Playparms. dwfrom = 0;//Time from which the video is played, in milliseconds
Mcisendcommand (DeviceID, mci_play,// DeviceIDMust be equal to the above device ID
Mci_from, (DWORD) (lpvoid) & playparms );//Play MIDI
Stop playing:
Mcisendcommand (DeviceID, mci_stop, null, null );
Close the device:
Mcisendcommand (DeviceID, mci_close, null, null );
Opening a WAV file is almost the same as opening a MIDI file. You only need to change mci_devtype_sequencer to mci_devtype_waveform_audio.
We can also use our demo to make it clearer. In our demo, I used a classic music in the legend of xianjian and Qixia, you will surely know which one it is (Hope Daewoo won't sue me for infringement, just borrow it for use ). I loaded this piece of music during map initialization, so I wrote a void map: Sound () function.
Void map: Sound ()
{
Mci_open_parms openparms;
Openparms. lpstrdevicetype = (lpcstr) mci_devtype_sequencer;
Openparms. lpstrelementname = (lpcstr) "0311.mid"; // name of the music file to be played
Openparms. wdeviceid = 0;
Mcisendcommand (null, mci_open, mci_wait | mci_open_type | mci_open_type_id | mci_open_element, (DWORD) (lpvoid) & openparms );
Mci_play_parms playparms;
Playparms. dwfrom = 0;
Mcisendcommand (openparms. wdeviceid, mci_play, mci_from, (DWORD) (lpvoid) & playparms );
}
Other functions and statements have been introduced before and are no longer cumbersome. After completing these functions, you will be able to hear the background music in my demo.
2.7Go for adventure
In combination with the method described above, you can take risks by writing a better script, so I have the science and technology adventure, although there are still many problems. however, it is barely a single-line RPG. As for the individual taste, it is important for all players to experience it themselves. this section is purely a game promotion with no technical content. I hope you will not hit me with bricks.
2.8Edited remarks
At this point, the game programming stuff I want to talk about is basically over, but it must be reminded that this is just a small drop, and it is a profound and profound thing, it contains the wisdom of countless predecessors. you can find a book on game programming. The book also mentions: artificial intelligence, physical action design, animation production, directsound, directinput, directplay ......, In the graphic display area, there are also particle applications, collision detection, and map scroll technologies ......, There is nothing to mention here, especially about artificial intelligence: I remember an old game programmer told me a word, and now I will tell you: "an AI represents the level of a programmer, the more intelligent the programmer is, the more intelligent the computer AI can be written. ", there is no other meaning. I like this sentence, so I will give it to you. after talking about such nonsense for a long time, I still want to talk about it. game programming is a profound field. If the readers of this article are interested in developing in this field, it is strongly recommended that you buy a good book (why should I buy a book again without saying a few words ?), Take a good look and write code. In this case, both your programming level and your game level will be improved.