標籤:log 返回 window return div 貪吃蛇 main while nap
一、封面
1、封面樣式(例)
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、添加背景音樂
(1)用到函數:BOOL WINAPI PlaySound( LPCSTR pszSound, HMODULE hmod, DWORD fdwSound );//詳查MSDN等
(2)PlaySound函數用到標頭檔:
#include <Windows.h>#include <mmsystem.h>#pragma comment(lib,"winmm.lib")
(3)函數傳回值類型為布爾型。
3、主函數中切換到遊戲頁的按鍵檢測
char chinput;while(1) { chinput = _getch(); if(‘ ‘ == chinput) { break; } }
全部代碼如下:
#include <stdio.h>#include <stdlib.h>#include <conio.h>//三件套:添加音樂的函數#include <Windows.h>#include <mmsystem.h>#pragma comment(lib,"winmm.lib")//首頁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");}//播放音樂void BGM(){ PlaySound("C:\\Windows\\Media\\Alarm03.wav" , NULL, SND_FILENAME | SND_ASYNC); //以C盤系統檔案中.wav檔案為例}int main(){ char chinput; //播放音樂 BGM(); //顯示首頁 Cover(); //檢測按鍵 while(1) { chinput = _getch(); if(‘ ‘ == chinput) { break; } } //停止播放 PlaySound(NULL, 0, 0); //清屏 system("cls"); printf("\n\n\n\t\t開始遊戲\n"); system("pause"); return 0;}
C語言控制台貪吃蛇1