我试图让球落在随机位置并循环但我的代码中出错了。出现此错误:' - =' :左操作数必须是l值。我可以知道我的代码有什么问题吗?
这是我的代码:
const int maxBall = 500;
int ball [maxBall][3];
float ballRadius = 1.0f;
float speed=0.010f;
void initBall()
{
for (int i=0; i<=maxBall -1;i++)
{
ball[i][0] = rand() % maxBall-maxBall / 2;
ball[i][1] = rand() % maxBall-maxBall / 2;
}
}
void updateBallPos(int value)
{
glutTimerFunc(10, updateBallPos, 0);
for (int i=0; i<=maxBall - 1; i++)
{
ball[i][1]=ball[i][1] + 2;
if(ball[i][1] > 0 )
ball[i][1] = -maxBall;
}
}
void display ()
{
glClear(GL_COLOR_BUFFER_BIT);
for(int i=0; i<=maxBall - 1; i++)
{
glLoadIdentity();
glTranslatef(0.0f, ball[i][1], 0.0f);
glPushMatrix();
glBegin(GL_TRIANGLE_FAN);
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex2f(0.0f, 0.0f); // Center of circle
int numSegments = 100;
float angle;
for (int i = 0; i <= numSegments; i++) { // Last vertex same as first vertex
angle = i * 2.0f * PI / numSegments; // 360 deg for all segments
glVertex2f(cos(angle) * ballRadius, sin(angle) * ballRadius);
}
glEnd();
glPopMatrix();
}
glutSwapBuffers();
ball -= speed;
glFlush();
}
void reshape(int x, int y)
{
glViewport(0, 0, x, y);
//Create projection matrix, 60 degree field of view, near clip plane at 1, far clip plane at 1000
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (60, (1.0*x) / y, 1, 1000);
glMatrixMode(GL_MODELVIEW);
}
答案 0 :(得分:0)
C和C ++是极简主义语言,它们的运算符非常低级(您可以在C ++中重载运算符)。 ball
是一个数组,-
运算符不对数组进行操作,就像它们是向量一样(元素减法)。所以ball -= speed
事情并不合适。
你必须迭代数组的元素并按元素执行减法元素。