考期中考
第2堂課:
我們要將所有之前所教過的東西整合,做出一個射擊遊戲。
首先我們要會寫全銀幕的程式。
#include <GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
}
void keyboard(unsigned char key, int x, int y)
{
exit(0);
}
int main(int argc, char**argv)
{
glutInit(&argc, argv );
glutCreateWindow("BOOM");
glutFullScreen(); //遍全屏
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
再來我們要利用上禮拜學過的滑鼠點擊程式
#include <stdio.h>
#include <GL/glut.h>
#include <mmsystem.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
void keyboard(unsigned char key, int x, int y)
{
exit(0);
}
void mouse(int button,int state, int x,int y)
{
if(state==GLUT_DOWN){
PlaySound("Data/Shot.wav",NULL, SND_ASYNC);
printf("PAPAPA\n");
}
}
int main(int argc, char**argv)
{
glutInit(&argc, argv );
glutCreateWindow("BOOM");
glutFullScreen();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMainLoop();
}
記得要把DATA裡的槍聲放進BIN裡才會有槍聲
第3堂:
加上黃色部分的程式碼,茶壺會隨著自己按滑鼠而移動.
#include <stdio.h>
#include <GL/glut.h>
#include <mmsystem.h>
float potX=-1, potY=0;
void display()
{
glClearColor(0.5,0.5,1,0);
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glTranslatef(potX,potY,0);
glutSolidTeapot(0.05);
glPopMatrix();
glFlush();
potX+=0.1;
if(potX>1.1) potX=-1.1;
}
這裡的程式碼是用來讓射擊遊戲變得更生動碰觸到物件可以讓它發出不同的聲音!!!!
void mouse(int button,int state, int x,int y)
{
if(state==GLUT_DOWN){
PlaySound("Data/Shot.wav",NULL, SND_ASYNC);
printf("PAPAPA\n");
float mouseX=2*x/1280.0 -1, mouseY = -(2*y/1024.0 -1);
if(abs(mouseX=potX)<0.01 &&abs(mouseY-potY)<0.01){
printf("oh my god,you so great!!!\n");
PlaySound("bscream.wav",NULL, SND_ASYNC);
}
}
}
第3堂:
加上黃色部分的程式碼,茶壺會隨著自己按滑鼠而移動.
#include <stdio.h>
#include <GL/glut.h>
#include <mmsystem.h>
float potX=-1, potY=0;
void display()
{
glClearColor(0.5,0.5,1,0);
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glTranslatef(potX,potY,0);
glutSolidTeapot(0.05);
glPopMatrix();
glFlush();
potX+=0.1;
if(potX>1.1) potX=-1.1;
}
void mouse(int button,int state, int x,int y)
{
if(state==GLUT_DOWN){
PlaySound("Data/Shot.wav",NULL, SND_ASYNC);
printf("PAPAPA\n");
float mouseX=2*x/1280.0 -1, mouseY = -(2*y/1024.0 -1);
if(abs(mouseX=potX)<0.01 &&abs(mouseY-potY)<0.01){
printf("oh my god,you so great!!!\n");
PlaySound("bscream.wav",NULL, SND_ASYNC);
}
}
}
網誌管理員已經移除這則留言。
回覆刪除網誌管理員已經移除這則留言。
刪除作者已經移除這則留言。
回覆刪除