子弹小游戏^^

1 篇文章 0 订阅
#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();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值