在每次移动期间在 Opengl 中玩游戏时会形成额外的对象(块,而不是 OOP)。我尝试制作了一个 Bloxerz 游戏,其中用板上的键移动块,问题是每次移动块时,板下都会形成一个新的块图像。我找不到这件事的踪迹,我认为,代码和逻辑没有任何问题,只是它是像 OpenGl 程序员那样非专业编写的。必须有一些东西丢失,一些方法来阻止它。为了让每件事都可见,我分享了我所有的代码。以下是我的代码:
int s=-1;
int stage=0;
int arr[]={5,5,5,4,4,4,4,5,9,9,10,9,10,8,9,8,10,7,11,7,11,6,10,6};
int fallcount=0;
int fallset=0;
int rot[]={0,0,0};
int axis=0;
int a=0;
double x=0.8,y=1.3,z=1;
double move1=0;
int moved=0;
int pos=1;
int position;
void Init()
{
x=0.8;
y=1.3;
z=1;
move1=0;
moved=0;
pos=1;
position=1;
fallcount=0;
}
void wait ( int seconds )
{
clock_t endwait;
endwait = clock () + seconds * CLOCKS_PER_SEC ;
while (clock() < endwait) {}
}
void FallOn()
{
if(position==1||position==3)
{
if(x<=-0.7||y<=-0.7||x>=11.3||y>=11.3)
{
Init();
}
}
if(stage==1)
{
if(x>=2.8&&y<=5.3&&y>=3.8)
{
Init();
}
if((x>=1.8)&&(x<=5.8)&&(y>=8.8))
{
Init();
}
}
}
void checkpos(int arg)
{
if((position==1)&&(arg==1))
{
position=1;
x=x+1;
return;
}
else if((position==1)&&(arg==2))
{
position=2;
rot[1]=-1;
rot[2]=0;
rot[3]=0;
y=y+2;
x=x+0.5;
return;
}
else if((position==1)&&(arg==3))
{
position=1;
rot[1]=0;
rot[2]=-1;
rot[3]=0;
x=x-1;
return;
}
else if((position==1)&&(arg==4))
{
position=2;
rot[1]=0;
rot[2]=0;
rot[3]=1;
y=y-1;
x=x+0.5;
return;
}
if((position==2)&&(arg==1))
{
position=3;
rot[1]=0;
rot[2]=1;
rot[3]=0;
x=x+1;
y=y-0.5;
return;
}
else if((position==2)&&(arg==2))
{
position=1;
rot[1]=0;
rot[2]=-1;
rot[3]=0;
y=y+1;
x=x-0.5;
return;
}
else if((position==2)&&(arg==3))
{
position=3;
rot[1]=0;
rot[2]=-1;
rot[3]=0;
x=x-2;
y=y-0.5;
return;
}
else if((position==2)&&(arg==4))
{
position=1;
rot[1]=0;
rot[2]=0;
rot[3]=1;
y=y-2;
x=x-0.5;
return;
}
if((position==3)&&(arg==1))
{
position=2;
rot[1]=0;
rot[2]=1;
rot[3]=0;
x=x+2;
y=y+0.5;
return;
}
else if((position==3)&&(arg==2))
{
position=3;
rot[1]=0;
rot[2]=0;
rot[3]=-1;
y=y+1;
return;
}
else if((position==3)&&(arg==3))
{
position=2;
rot[1]=0;
rot[2]=-1;
rot[3]=0;
x=x-1;
y=y+0.5;
return;
}
else if((position==3)&&(arg==4))
{
position=3;
rot[1]=1;
rot[2]=0;
rot[3]=0;
y=y-1;
return;
}
}
void draw_string( int x, int y,int z ,const char * buffer ) {
// function to draw a string in glut
glRasterPos3i( x, y,z );
// i think its safe to assume a null terminator appears somewhere
while( *buffer )
glutBitmapCharacter( (void *)GLUT_BITMAP_TIMES_ROMAN_24, *(buffer++) );
}
void Display()
{
FallOn();
glLoadIdentity();
//glu look at
gluLookAt(-30,-30,30,2.5,4,0,0,0,1);
//***********************************************Board************************************************************************
if(stage==0)
{
glBegin(GL_POLYGON);
for(int i=0;i<=12;i++)
{
for(int j=0;j<=12;j++)
{
glColor3d(1,0,1);
glVertex3d(i,i,0);
glVertex3d(j,i,0);
glVertex3d(j,j,0);
glVertex3d(i,j,0);
}
}
glEnd();
glBegin(GL_POLYGON);
glColor3d(0,0,0);
glVertex3d(arr[s+1],arr[s+2],0);
glVertex3d(arr[s+3],arr[s+4],0);
glVertex3d(arr[s+5],arr[s+6],0);
glVertex3d(arr[s+7],arr[s+8],0);
glEnd();
glBegin(GL_LINES);
glColor3d(0,0,0);
for(int i=0;i<=12;i++)
{
glVertex3d(0,i,0);
glVertex3d(12,i,0);
glVertex3d(i,0,0);
glVertex3d(i,12,0);
}
glEnd();
}
else if(stage==1)
{
glFlush();
glClear(GL_CLIP_PLANE0);
glClear(GL_ACCUM_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT);
glBegin(GL_POLYGON);
for(int i=0;i<=12;i++)
{
for(int j=0;j<=12;j++)
{
glColor3d(1,0,1);
glVertex3d(i,i,0);
glVertex3d(j,i,0);
glVertex3d(j,j,0);
glVertex3d(i,j,0);
}
}
glEnd();
glBegin(GL_POLYGON);
glColor3d(0,0,0);
glVertex3d(3,4,0);
glVertex3d(3,6,0);
glVertex3d(12,6,0);
glVertex3d(12,4,0);
glEnd();
glFlush();
glBegin(GL_POLYGON);
glColor3d(0,0,0);
glVertex3d(2,12,0);
glVertex3d(7,12,0);
glVertex3d(7,9,0);
glVertex3d(2,9,0);
glEnd();
glFlush();
s=7;
glBegin(GL_POLYGON);
glColor3d(0,0,0);
glVertex3d(arr[s+1],arr[s+2],0);
glVertex3d(arr[s+3],arr[s+4],0);
glVertex3d(arr[s+5],arr[s+6],0);
glVertex3d(arr[s+7],arr[s+8],0);
glEnd();
glBegin(GL_LINES);
glColor3d(0,0,0);
for(int i=0;i<=12;i++)
{
glVertex3d(0,i,0);
glVertex3d(12,i,0);
glVertex3d(i,0,0);
glVertex3d(i,12,0);
}
glEnd();
}
else if(stage==2)
{
glFlush();
glClear(GL_CLIP_PLANE0);
glClear(GL_ACCUM_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT);
glBegin(GL_POLYGON);
for(int i=0;i<=12;i++)
{
for(int j=0;j<=12;j++)
{
glColor3d(1,0,1);
glVertex3d(i,i,0);
glVertex3d(j,i,0);
glVertex3d(j,j,0);
glVertex3d(i,j,0);
}
}
glEnd();
glBegin(GL_POLYGON);
glColor3d(0,0,0);
glVertex3d(3,11,0);
glVertex3d(9,11,0);
glVertex3d(9,0,0);
glVertex3d(3,0,0);
glEnd();
glFlush();
s=15;
glBegin(GL_POLYGON);
glColor3d(0,0,0);
glVertex3d(arr[s+1],arr[s+2],0);
glVertex3d(arr[s+3],arr[s+4],0);
glVertex3d(arr[s+5],arr[s+6],0);
glVertex3d(arr[s+7],arr[s+8],0);
glEnd();
glBegin(GL_LINES);
glColor3d(0,0,0);
for(int i=0;i<=12;i++)
{
glVertex3d(0,i,0);
glVertex3d(12,i,0);
glVertex3d(i,0,0);
glVertex3d(i,12,0);
}
glEnd();
}
if(moved==0)
{
glPushMatrix();
glColor3d(1,0,0);
glTranslated(x,y,z);
glScalef(1,2.1,1);
glutSolidCube(.9);
glPopMatrix();
}
if(moved==1)
{
if(position==1)
{
glPushMatrix();
glColor3d(1,0,0);
glTranslated(x,y,z);
glScalef(1,2.1,1);
glutSolidCube(.9);
glPopMatrix();
moved=0;
}
else if(position==2)
{
cout<<"2:- "<<"x="<<x<<"y="<<y<<"z="<<z<<"position="<<position<<endl;
if(((x==4.3&&y==4.3)&&stage==0)||((x==9.3&&y==8.3)&&stage==1))
{
cout<<"Event Happened"<<endl;
z=z-1;
cout<<"Stage Cleared"<<endl;
glPushMatrix();
glColor3d(1,0,0);
glTranslated(x,y,z);
glScalef(1,1,2.1);
glutSolidCube(.9);
glPopMatrix();
moved=0;
wait(1);
Init();
stage+=1;
glutPostRedisplay();
glFlush();
}
else if((x==10.3&&y==6.3)&&(stage==2))
{
cout<<"Event Happened"<<endl;
z=z-1;
cout<<"Stage Cleared"<<endl;
glPushMatrix();
glColor3d(1,0,0);
glTranslated(x,y,z);
glScalef(1,1,2.1);
glutSolidCube(.9);
glPopMatrix();
moved=0;
wait(1);
Init();
cout<<"Congratulations!!!!! You Won"<<endl;
stage=0;
glFlush();
}
else
{
glPushMatrix();
glColor3d(1,0,0);
glTranslated(x,y,z);
glScalef(1,1,2.1);
glutSolidCube(.9);
glPopMatrix();
moved=0;
}
}
else if(position==3)
{
glPushMatrix();
glColor3d(1,0,0);
glTranslated(x,y,z);
glScalef(2.1,1,1);
glutSolidCube(.9);
glPopMatrix();
moved=0;
}
moved=3;
}
glFlush();
glutSwapBuffers();
}
void Resize(int h,int w)
{
if(h==0)
{
h=1;
}
float ratio=w*1/h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0,0,w,h);
gluPerspective(14.0f ,ratio,0.1f,100.f);
glMatrixMode(GL_MODELVIEW);
}
void Board(unsigned char key,int x,int y)
{
switch (key)
{
case 'w':
case 'W':
move1=1;
moved=1;
axis=1;
checkpos(1);
glFlush();
glutSwapBuffers();
glutPostRedisplay();
break;
case 'a':
case 'A':
move1=2;
moved=1;
axis=2;
checkpos(2);
glFlush();
glutSwapBuffers();
glutPostRedisplay();
break;
case 'S':
case 's':
move1=3;
moved=1;
axis=1;
checkpos(3);
glFlush();
glutSwapBuffers();
glutPostRedisplay();
break;
case 'd':
case 'D':
move1=4;
moved=1;
axis=2;
checkpos(4);
glFlush();
glutSwapBuffers();
glutPostRedisplay();
break;
default:
break;
}
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(400, 400);
glutInitWindowPosition(200, 200);
glutCreateWindow("3D Object in OpenGL");
Init();
glutDisplayFunc(Display);
glutReshapeFunc(Resize);
glutKeyboardFunc(Board);
glutMainLoop();
return 0;
}
openGl 是否显示异常?