#include "stdafx.h"
#include "windows.h"
#include "GL\glut.h"
#include <iostream>
#include <cstdlib>
using namespace std;
//int _tmain(int argc, _TCHAR* argv[])
//{
// return 0;
//}
//定义各种变量
GLfloat xb=0,yb=-230;//the position of bullet
GLfloat xq=0,yq=5;//the position of the square
GLfloat xm=0,ym=-180;//the position of the moving bullet
GLfloat xd=0.5,yd=0.5;//the distance of the moving square
int c =1;//指定颜色
GLfloat colors[2][3]={{1,0,0},{0,0,1}};
int i=0;
bool bulletmove=false, smove= true;
void myinit(){
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0,0,0,1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-250,250,-250,250);
//glMatrixMode(GL_MODELVIEW)
}
void bullet1()
{
glColor3f(0.5,0.5,1);
glPointSize( 10);
glBegin( GL_POINTS);
glVertex2f( xb, yb);
glEnd();
}
void bullet()
{
if(bulletmove==true){
glColor3f(0.5,0.5,0.5);
glPointSize( 10);
glBegin( GL_POINTS);
glVertex2f( xm, ym);
glEnd();}
}
void square(){
glColor3fv(colors[c]);
glBegin( GL_POLYGON);
glVertex2f( xq+30, yq+30);
glVertex2f( xq+30, yq-30);
glVertex2f( xq-30, yq-30);
glVertex2f( xq-30, yq+30);
glEnd();
}
void move(){
if(smove){
if(((xq+30)==250)||((xq-30)==-250))
{xd=-xd;}
if(((yq+30)==250)||((yq-30)==-150))
{yd=-yd;}
/*xd=xd+ 0.005*(xd/xd);
yd=yd+ 0.005*(yd/yd);*/
xq=xq+xd;
yq=yq+yd;
cout<<"xq="<<xq<<endl;
cout<<"yq="<<yq<<endl;
cout<<"xd="<<xd<<endl;
cout<<"yd="<<yd<<endl;
glutPostRedisplay();
}}
void myspecial(int key,int x,int y){
switch(key){
case GLUT_KEY_LEFT:{
if(xb>-250)
{xb=xb-10;
glutPostRedisplay();
break;}
}
case GLUT_KEY_RIGHT:{
//glMatrixMode(GL_MODELVIEW);
if(xb<250)
{ xb=xb+10;
glutPostRedisplay();
break;
} }
return;
}}
void keyboard(unsigned char key,int x,int y){
switch(key){
case ' ':{
i++;//the amount of bullet
xm=xb;
ym=yb; //将位置付给运动的子弹
bulletmove=true;
glutPostRedisplay();
}
/*case ' ':{
if(can_move==false){
can_move=true;
c=0;
}
else{
shoot=true;
glClear(GL_COLOR_BUFFER_BIT);
}*/
}
}
void shoot(){
if((xm<(xq+30))&&(xm>(xq-30))&&(ym<(yq+30))&&(ym>(yq-30)))
{
smove = false;
ym=ym-3;
c=0;
}
}
void myTimer(int v){
ym=ym+3;
glutPostRedisplay();
//moving();
glutTimerFunc(30,myTimer,1);
}
void display(){
glClear(GL_COLOR_BUFFER_BIT);
square();
bullet1();
bullet();
move();
shoot();
glFlush();
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE |GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("bullet");
glutDisplayFunc(display);
glutSpecialFunc(myspecial);
glutKeyboardFunc(keyboard);
glutTimerFunc(30,myTimer,1);
myinit();
glutMainLoop();
}
子弹小游戏^^
最新推荐文章于 2024-10-13 18:53:05 发布