然後老師傳了windows壓縮檔,
1.讓我們試一試法座標和貼圖座標的執行結果。
2.再來老師也傳了一張圖給大家,加深我們的印象,利於我們考期中考。
◎今日目標:做出小遊戲!
#include <GL/glut.h>
#include <mmsystem.h>
#include <stdio.h>
void mouse(int button,int state, int x,int y)
{
if(state==GLUT_DOWN)
{
PlaySound("Shot.wav",NULL,SND_ASYNC);
printf("Shot!\n");
}
}
void keyboard(unsigned char key, int x, int y)
{
exit(0);
}
void display()
{
glClearColor(0.5,0.5,1,0);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutCreateWindow("hello");
glutFullScreen();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMainLoop();
}
→成功發出shotshotshot的聲音囉!! 哈哈
◎再來新增程式碼,讓移動的茶壺跑出來,增加畫面豐富度還有做實體的應用!
#include <GL/glut.h>
#include <mmsystem.h>
#include <stdio.h>
void mouse(int button,int state, int x,int y)
{
if(state==GLUT_DOWN)
{
PlaySound("Shot.wav",NULL,SND_ASYNC);
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.5,0.5,1,0);
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glTranslatef(potX,potY,0);
glutSolidTeapot(0.05);
glPopMatrix();
glFlush();
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutCreateWindow("hello");
glutFullScreen();
glutDisplayFunc(display);
glutIdleFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMainLoop();
}
◎最後我們到網路上,下載wav的聲音檔,我選擇大象的叫聲,
在我射擊中茶壺時就會發出大象的叫聲,
並在小黑視窗顯示" You got it"這句話~~~
來,我先秀出程式碼,再來貼上我的成果圖(黃色部分為新增)
#include <GL/glut.h>
#include <mmsystem.h>
#include <stdio.h>
float potX=-1,potY=0;
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.0-1;
if( abs(mouseX-potX)<0.1&& abs(mouseY-potY)<0.1){
PlaySound("a.wav",NULL, SND_ASYNC);
printf("You got it!\n");
}
}
}
void keyboard(unsigned char key, int x, int y)
{
exit(0);
}
void display()
{
potX+=0.01;
if(potX>1.1)potX=-1.1;
glClearColor(0.5,0.5,1,0);
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glTranslatef(potX,potY,0);
glutSolidTeapot(0.05);
glPopMatrix();
glFlush();
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutCreateWindow("hello");
glutFullScreen();
glutDisplayFunc(display);
glutIdleFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMainLoop();
}
沒有留言:
張貼留言