2015年4月16日 星期四

02160234_洪振瑋 WEEK08

今日目標
(1) 模擬考 期中考試
(2) 用範例學習
(3) 打光->法向量
(4) 貼圖->貼圖座標
(5) NeHe Lesson 32 小遊戲 



自己嘗試做出一個射擊遊戲!
先做出全螢幕視窗
glutFullScreen();

glutKeyboardFunc(keyboard);  < <  鍵盤函式

glutMouseFunc(mouse);           < <  滑鼠函式

再來寫滑鼠射擊函式
void mouse (int button, int state,int x,int y)
{
    if(state == GLUT_DOWN)
    {
        PlaySound("Shot.wav", NULL,SND_ASYNC);
        /////////C:\Users\USER\Desktop\freeglut\bin
        /////////聲音檔路徑
        printf("Shot!!!!\n");
    }

}

鍵盤離開全螢幕
void keyboard (unsigned char key,int x, int y)
{
    exit (0);

}



接下來新增一個茶壺 讓他自己移動
float potX=-1, potY=0;
void display()
{
    potX += 0.01;   ///讓他自己移動
    if(potX>1.1) potX=-1.1;
    glClearColor(0.2,0.5,0.8,0);
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();     ////茶壺
        glTranslatef(potX,potY,0);
        glutSolidTeapot(0.05);
    glPopMatrix();
    glFlush();
}

再加入擊中的音效
void mouse (int button, int state,int x,int y)
{
    if(state == GLUT_DOWN)
    {
        PlaySound("shot.wav", NULL,SND_ASYNC);
        printf("Shot!!!!\n");

        float mouseX = 2*x/1280.0 -1,mouseY = 2*y/1024-1;
        
        //// 1280.1024為螢幕解析度
        ///   原本算法是0>1   乘以2之後 再減1 可以變回-1>1
       
         if(fabs(mouseX - potX)<0.1 && fabs(mouseY - potY)< 0.1)
         ////fabs:絕對值
        {
            PlaySound("glass.wav", NULL, SND_ASYNC);
            printf("You shot it!!!!\n");
        }
    }
}





沒有留言:

張貼留言