First, the cover
1. Cover Style (example)
void Cover () { printf ("\n\n\n\t\t\t <<snake>>\n\n"); printf ("\n\n\n\t\t\t<<w,s,a,d control move>>\n\n"); printf ("\n\n\n\t\t\t <<space begin>>\n\n\n\n");}
2. Add background music
(1) Use the function : BOOL WINAPI PlaySound(LPCSTR pszsound, hmodule hmod, DWORD fdwsound);//check MSDN, etc.
(2) The PlaySound function is used to the header file :
#include <Windows.h> #include <mmsystem.h> #pragma comment (lib, "Winmm.lib")
(3) The function return value type is boolean .
3. Key detection of Switch to game page in main function
Char Chinput; while (1) { = _getch (); if (' = = chinput ') { break; } }
The full code is as follows:
#include <stdio.h>#include<stdlib.h>#include<conio.h>//three-piece set: function to add music#include <Windows.h>#include<mmsystem.h>#pragmaComment (lib, "Winmm.lib")//Home PagevoidCover () {printf ("\n\n\n\t\t\t <<snake>>\n\n"); printf ("\n\n\n\t\t\t<<w,s,a,d Control move>>\n\n"); printf ("\n\n\n\t\t\t <<space begin>>\n\n\n\n");}//Play MusicvoidBGM () {PlaySound ("C:\\windows\\media\\alarm03.wav", NULL, Snd_filename |Snd_async); //example of a. wav file in a C-drive system file}intMain () {CharChinput; //Play MusicBGM (); //Show HomeCover (); //Detection Keys while(1) {Chinput=_getch (); if(' '==chinput) { Break; } } //Stop playingPlaySound (NULL,0,0); //Clear ScreenSystem"CLS"); printf ("\n\n\n\t\t start the game \ n"); System ("Pause"); return 0;}
C Language Console Snake 1