NeHe Lesson 32 的小遊戲
http://nehe.gamedev.net/tutorial/picking_alpha_blending_alpha_testing_sorting/16005/
先出現畫面,並且按下滑鼠左鍵,就會出現槍聲(Shot.wav)
茶壺會從螢幕左邊跑到螢幕右邊,當超出螢幕時,會從最左邊再跑一次,不會停止
增加射中茶壺時有尖叫聲(ahh.wav),並顯示 "You got it! ",且會計算射中次數(Score)
程式如下:
#include <GL/glut.h>
#include <mmsystem.h>///PlaySound();
#include <stdio.h>
#include <math.h>///fabs();
float potX=-1, potY=0;///茶壺一開始的位置
int Score=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;///將滑鼠在螢幕的座標1280~1024換算成(-1)~1
if(fabs(mouseX-potX) < 0.1 && fabs(mouseY-potY) < 0.1)///滑鼠點中某一範圍(移動中的茶壺)
{
PlaySound("ahh.wav", NULL, SND_ASYNC);
printf("You got it!\n");
Score++;
}
}
}
void keyboard(unsigned char key, int x, int y)
{
printf("Your score:%d\n",Score);///顯示射中次數
exit(0);///按 Esc 可以離開螢幕
}
void display()
{
potX+=0.01;///茶壺每次往右移動0.01
if(potX>1.1)///當茶壺移動到螢幕最右邊,且已經超出螢幕時
potX=-1.1;///茶壺回到螢幕最左邊
glClearColor(0.2, 0.5, 1, 0);///螢幕顏色
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glTranslatef(potX, potY, 0);
glutSolidTeapot(0.1);
glPopMatrix();
glFlush();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutCreateWindow("Game");
glutFullScreen();///全螢幕
glutDisplayFunc(display);
glutIdleFunc(display);///閒置時就進入display
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMainLoop();
}
沒有留言:
張貼留言