2017年4月24日 星期一

week10 3D的上課筆記

一、玩遊戲(射擊)
1.點滑鼠有槍聲


首先要打
#include <mmsystem.h>
匯入mmsystem.h
之後在main函式加入,開啟滑鼠函式
int main(int argc, char *argv[])
{
    glutMouseFunc(mouse);
}
再新增mouse函式
void mouse(int button,int state,int x,int y)
{
    PlaySound("C:/Users/user/Desktop/freeglut/awp1",NULL,SND_ASYNC);
}
之後點滑鼠就會有槍聲

全部程式碼:
#include <mmsystem.h>

void mouse(int button,int state,int x,int y)
{
    PlaySound("C:/Users/user/Desktop/freeglut/awp1",NULL,SND_ASYNC);
}
int main(int argc, char *argv[])
{
    glutMouseFunc(mouse);
}

2.點鍵盤會有do re mi fa so

加入程式碼,開啟鍵盤函式
int main(int argc, char *argv[])
{
    glutKeyboardFunc(keyboard);
}
再新增keyboard函式
void keyboard(unsigned char key,int x,int y)
{
    if(key=='1') PlaySound("C:/Users/user/Desktop/freeglut/music/do.wav",NULL,SND_ASYNC);
    if(key=='2') PlaySound("C:/Users/user/Desktop/freeglut/music/re.wav",NULL,SND_ASYNC);
    if(key=='3') PlaySound("C:/Users/user/Desktop/freeglut/music/mi.wav",NULL,SND_ASYNC);
    if(key=='4') PlaySound("C:/Users/user/Desktop/freeglut/music/fa.wav",NULL,SND_ASYNC);
    if(key=='5') PlaySound("C:/Users/user/Desktop/freeglut/music/so.wav",NULL,SND_ASYNC);
}

全部程式碼:
void keyboard(unsigned char key,int x,int y)
{
    if(key=='1') PlaySound("C:/Users/user/Desktop/freeglut/music/do.wav",NULL,SND_ASYNC);
    if(key=='2') PlaySound("C:/Users/user/Desktop/freeglut/music/re.wav",NULL,SND_ASYNC);
    if(key=='3') PlaySound("C:/Users/user/Desktop/freeglut/music/mi.wav",NULL,SND_ASYNC);
    if(key=='4') PlaySound("C:/Users/user/Desktop/freeglut/music/fa.wav",NULL,SND_ASYNC);
    if(key=='5') PlaySound("C:/Users/user/Desktop/freeglut/music/so.wav",NULL,SND_ASYNC);
}
int main(int argc, char *argv[])
{
    glutKeyboardFunc(keyboard);
}
這樣點鍵盤12345就會有do re mi fa so

3.開啟式窗自動播放音樂

在資料夾裡面要有CMP3_MCI.h這個檔案
之後把他匯入到程式裡

並設一個變數叫做myMP3

然後加入播放MP3程式碼
int main(int argc, char *argv[])
{
    myMP3.Load("C:/Users/user/Desktop/freeglut/yukai.mp3");
    myMP3.Play();
}
之後執行就會有音樂

沒有留言:

張貼留言