NeHe Lesson32.zip 解壓縮,玩射擊遊戲。
Light Material(打光)、Texture(貼圖)的範例程式
接下來就是要試著做上面那個射擊遊戲囉~~~~
當滑鼠按下時 發出槍聲 並印出Shot!
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;
修改display 畫出茶壺 並且讓他依照座標跑
void display()
{
potX+=0.01;
if(potX>1.1) potX=-1.1;
glClearColor(0.5,0.5,0.5,0);
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glTranslated(potX,potY,0);
glutSolidTeapot(0.05);
glPopMatrix();
glFlush();
}
在main() 裡面增加
glutIdleFunc(display);
在系統閒暇時 就執行display的函數
如果點擊位置接近 則有音效跑出
float mouseX=2*x/1280.0 -1, mouseY=2*y/1024 -1;
if( fabs(mouseX - potX)< 0.1 && fabs(mouseY - potY)<0.1){
PlaySound("glass.wav",NULL, SND_ASYNC);
printf("You got it!");
}
#include <GL/glut.h>
#include <mmsystem.h>
#include <stdio.h>
#include <math.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 -1;
if( fabs(mouseX - potX)< 0.1 && fabs(mouseY - potY)<0.1){
PlaySound("glass.wav",NULL, SND_ASYNC);
printf("You got it!");
}
}
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,0.5,0);
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glTranslated(potX,potY,0);
glutSolidTeapot(0.05);
glPopMatrix();
glFlush();
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutCreateWindow("01160536");
glutFullScreen();
glutDisplayFunc(display);
glutIdleFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMainLoop();
}
沒有留言:
張貼留言