第二堂
1. 全螢幕 + 滑鼠點擊發出槍聲
#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("Shot1.wav", NULL, SND_ASYNC);
puts("Shot!");
}
}
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("02160136");
glutFullScreen();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMainLoop();
}
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("Shot1.wav", NULL, SND_ASYNC);
puts("Shot!");
}
}
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.8, 0.8, 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("02160136");
glutFullScreen();
glutDisplayFunc(display);
glutIdleFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMainLoop();
}
第三堂
命中判定 + 上面的
#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("Shot1.wav", NULL, SND_ASYNC);
puts("Shot!");
float mouseX = 2*x/1280.0 -1, mouseY = 2*y/1024.0 -1;
if (fabs(mouseX - potX) < 0.1 && fabs(mouseY - potY) < 0.1)
{
PlaySound("ya.wav", NULL, SND_ASYNC);
puts("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.8, 0.8, 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("02160136");
glutFullScreen();
glutDisplayFunc(display);
glutIdleFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMainLoop();
}
沒有留言:
張貼留言