# Drawing multiple circles in a loop

Page 1 of 1

## 3 Replies - 4576 Views - Last Post: 13 December 2009 - 11:45 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=144332&amp;s=f5c5b2c06a7b7185ecddd75594b3a35b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Chidori-chan

Reputation: 0
• Posts: 8
• Joined: 06-December 09

# Drawing multiple circles in a loop

Posted 08 December 2009 - 05:38 PM

Hi, I'm making a simple 2D game where you use a cannon to shoot balls of the same color, I'd like to put my three rows of circles into three loops where 15 are drawn and translated over 28 units each time, but I've been running into some issues. Could I get ya'll to take a look at my code and explain what I'm doing wrong? Here's my code:

```int r;
int d = 1;
int aim = 0;
int randomValue;
int min;
int sec;
int Circle1 [20];
int Circle2 [20];
int Circle3 [20];
int d = 54;
void timer()
{
for (min = 5; min>0; min++)
{
for (sec=60; sec>0; sec++)
{

}
}
}

int random_generator(int min,int max)
{

randomValue = min + random()%(max - min);
return randomValue;

}

{

glBegin(GL_POLYGON);
for (r=0; r<segments; r++)
{
float x = radius * cos(r*2.0*M_PI/segments);
float y = radius * sin(r*2.0*M_PI/segments);
glVertex2f(x,y);
}
glEnd();
glFlush();
}

{

glBegin(GL_POLYGON);
for (r=0; r<segments; r++)
{
float x = radius * cos(r*2.0*M_PI/segments);
float y = radius * sin(r*2.0*M_PI/segments);
glVertex2f(x,y);
}
glEnd();
glFlush();
}

void drawOutside()
{
glPushMatrix();
glColor4f(.3, .3, .3, 1);
glLineWidth(1.0);
glBegin(GL_POLYGON);
glVertex2i(0,500);
glVertex2i(0,0);
glVertex2i(500, 0);
glVertex2i(500, 500);

glEnd();
glPopMatrix();
glFlush();
}

void drawInside()
{
glPushMatrix();
glColor4f(.5, .5, .5, 1);
glLineWidth(1.0);
glBegin(GL_POLYGON);
glVertex2i(30,470);
glVertex2i(30,30);
glVertex2i(470, 30);
glVertex2i(470, 470);

glEnd();
glPopMatrix();
glFlush();
}

void drawCannon()
{
glPushMatrix();
glTranslatef(aim,0,1);
glColor3f(0, 0, 0);
glLineWidth(1.0);
glBegin(GL_POLYGON);
glVertex2i(230,20);
glVertex2i(230,100);
glVertex2i(270, 100);
glVertex2i(270, 20);

glEnd();
glPopMatrix();
glFlush();
}

void drawText(char * string)
{
int len = strlen(string);
int i;

for (i=0; i < len; i++)
{
glutStrokeCharacter(GLUT_STROKE_ROMAN,string[i]);
}
}

void ballPhysics()
{
}

void displayFunction(void)
{
switch (d)
{

case 1 :
glClear(GL_COLOR_BUFFER_BIT);

drawOutside();

glPushMatrix();
glTranslatef(180,455,1);
glColor3f(1,1,1);
glScalef(.2,.2,2);
glLineWidth(2.0);
drawText("Bubble Buster");
glPopMatrix();

glPushMatrix();
glTranslatef(160,425,1);
glColor3f(1,1,1);
glScalef(0.1,0.1,1);
glLineWidth(1.0);
drawText("Press s to start and r to restart");
glPopMatrix();

glPushMatrix();
glTranslatef(30,400,1);
glColor3f(1,1,1);
glScalef(0.15,0.15,1);
glLineWidth(1.0);
drawText("Instructions:");
glPopMatrix();

glPushMatrix();
glTranslatef(30,375,1);
glColor3f(1,1,1);
glScalef(0.1,0.1,1);
glLineWidth(1.0);
drawText("Use the left and right arrow keys to aim the cannon.");
glPopMatrix();

glPushMatrix();
glTranslatef(30,350,1);
glColor3f(1,1,1);
glScalef(0.1,0.1,1);
glLineWidth(1.0);
drawText("Your goal is to hit balls of the same color. If you fail");
glPopMatrix();

glPushMatrix();
glTranslatef(30,325,1);
glColor3f(1,1,1);
glScalef(0.1,0.1,1);
glLineWidth(1.0);
drawText("to hit a ball of the same color, but another color, you ");
glPopMatrix();

glPushMatrix();
glTranslatef(30,300,1);
glColor3f(1,1,1);
glScalef(0.1,0.1,1);
glLineWidth(1.0);
drawText("will have an extra ball to get rid of, as it will stick. If");
glPopMatrix();

glPushMatrix();
glTranslatef(30,275,1);
glColor3f(1,1,1);
glScalef(0.1,0.1,1);
glLineWidth(1.0);
drawText("you do not destroy all the balls within the time limit, or");
glPopMatrix();

glPushMatrix();
glTranslatef(30,250,1);
glColor3f(1,1,1);
glScalef(0.1,0.1,1);
glLineWidth(1.0);
drawText("let your balls touch the bottom of the screen you will lose.");
glPopMatrix();

break;

case 2 :

glClear(GL_COLOR_BUFFER_BIT);

drawOutside();
drawInside();

glPushMatrix();
glTranslatef(250+aim,102,1);
random_generator(0,3);
if (randomValue == 0)
{
glColor3f(0,.5,0);

}
else if (randomValue == 1)
{
glColor3f(.5,0,0);

}
else
{
glColor3f(0,0,.5);

}

glLineWidth(1.0);
drawFire(14,200);
glPopMatrix();

for
(Circle1=0; Circle1<15; Circle1++)
{

glPushMatrix();
glTranslatef(d+28,455,1);
random_generator(0,3);
if (randomValue == 0)
{
glColor3f(0,.5,0);

}
else if (randomValue == 1)
{
glColor3f(.5,0,0);

}
else
{
glColor3f(0,0,.5);

}
glLineWidth(1.0);
drawCircle(14,200);
glPopMatrix();
}

drawCannon();

glPushMatrix();
glTranslatef(30,475,1);
glColor3f(1,1,1);
glScalef(0.15,0.15,1);
glLineWidth(1.0);
drawText("Score:");
glPopMatrix();

glPushMatrix();
glTranslatef(90,475,1);
glColor3f(1,1,1);
glScalef(0.15,0.15,1);
glLineWidth(1.0);
drawText("0");
glPopMatrix();

char Minutes [10];
sprintf(Minutes, "%d", min);
glPushMatrix();
glTranslatef(430,475,1);
glColor3f(1,1,1);
glScalef(0.15,0.15,1);
glLineWidth(1.0);
drawText(Minutes);
glPopMatrix();

char Seconds [300];
sprintf(Seconds, "%d", sec);
glPushMatrix();
glTranslatef(450,475,1);
glColor3f(1,1,1);
glScalef(0.15,0.15,1);
glLineWidth(1.0);
drawText(Seconds);
glPopMatrix();

break;

}

glFlush();
glutSwapBuffers();
}

void timerFunction(int value)
{

glutPostRedisplay();
glutTimerFunc(1,timerFunction,0);
}

void keyboardFunction(unsigned char key, int x, int y)
{
switch (key)
{
case 'r' : d=1; break;
case 's' : d=2; break;
case ' ' : break;

}
glutPostRedisplay();
}

void functionSpecial(int key, int x, int y)
{
switch (key)
{
case GLUT_KEY_LEFT :
if (aim >= -198)
{
aim -=5;			//Defines the use of the right and left arrow keys
}
break;

case GLUT_KEY_RIGHT :
if (aim <= 198)

aim +=5; break;
}
glutPostRedisplay();

}

int main(int argc, char* argv[])
{

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
glutInitWindowSize(WIDTH, HEIGHT);
glutCreateWindow("Bubble Buster");

glutDisplayFunc(displayFunction);
glutSpecialFunc(functionSpecial);
glutKeyboardFunc(keyboardFunction);
glutTimerFunc(1,timerFunction,0);

gluOrtho2D(0, WIDTH, 0, HEIGHT);

glClearColor(1, 1, 1, 0);

glutMainLoop();
return 0;

}

```

When I compile it I get errors for the

```(Circle1=0; Circle1<15; Circle1++)

```

line of code saying:

In function 'displayFunction':
error: incompatible types when assigning to type 'int[20]' from type 'int'
warning: comparison between point and integer
error: lvalue required as increment operand

This post has been edited by Chidori-chan: 09 December 2009 - 12:37 AM

Is This A Good Question/Topic? 0

## Replies To: Drawing multiple circles in a loop

### #2 bsaunders

Reputation: 44
• Posts: 571
• Joined: 18-January 09

## Re: Drawing multiple circles in a loop

Posted 09 December 2009 - 10:09 PM

Can you show us a model of the layout of the circles?

### #3 Lucas Kell

Reputation: 1
• Posts: 21
• Joined: 24-November 09

## Re: Drawing multiple circles in a loop

Posted 09 December 2009 - 11:24 PM

Well the error is because you have defined your circle arrays, then you are trying to compare the whole arrays with integers, rather than specific values within those arrays.

### #4 Chidori-chan

Reputation: 0
• Posts: 8
• Joined: 06-December 09

## Re: Drawing multiple circles in a loop

Posted 13 December 2009 - 11:45 AM

I managed to get my circles to draw in a loop as they were supposed to look:

However, I wanted to put them in an array since later I will be using flags to turn them on and off, and I need to initialize it in my main function so the random generator won't run every time the screen refreshes. This is what I have so far in putting my circles in an array:

I get an error on this first line here saying : c:26: error: expected identifier or ‘(’ before ‘[’ token
I've no idea why, it looks like it's set up right to me

```struct Row1[15] = {drawCircleRow1()};
```

```int drawCircleRow1()
{
for
(o=0; o<15; o++)
{
random_generator(0,3);
glPushMatrix();
glTranslatef((o+1.9)*28,455,1);

if (randomValue == 0)
{
glColor3f(0,.5,0);

}
else if (randomValue == 1)
{
glColor3f(.5,0,0);

}
else
{
glColor3f(0,0,.5);

}
glLineWidth(1.0);
drawCircle(14,200);
glPopMatrix();

}

}
```

```int main(int argc, char* argv[])
{

glutInit(&argc, argv);

ballPhysics();
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
glutInitWindowSize(WIDTH, HEIGHT);
glutCreateWindow("Bubble Buster");

glutDisplayFunc(displayFunction);
glutSpecialFunc(functionSpecial);
glutKeyboardFunc(keyboardFunction);
glutTimerFunc(1,timerFunction,0);

gluOrtho2D(0, WIDTH, 0, HEIGHT);

glClearColor(1, 1, 1, 0);

glutMainLoop();
return 0;

}
```

This post has been edited by Chidori-chan: 13 December 2009 - 12:27 PM