I am making a game “Frozen Bubbles”, but when I maximize the window, the initial colors of the spheres change. What can be the problem?
This is my source code:
#include “stdafx.h”
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <gl/glut.h>
using namespace std;
GLfloat Array[8][16][3];
GLfloat r[3];
int button, state, x, y;
const float w=304.0, h=272.0;
void init()
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluOrtho2D(0.0f, w, 0.0f, h);
srand((unsigned) time(NULL));
}
// end init()
static void renderSphere(GLfloat p, GLfloat q, GLfloat r)
{
glPushMatrix();
glTranslatef(p, q, r);
glutWireSphere(8, 50, 50);
glPopMatrix();
}
void NextSphere()
{
int q;
glPushMatrix();
GLfloat Color[6][3]={{1, 0.5, 0}, {1, 0, 0}, {1, 0, 1}, {0, 1, 0}, {0, 0, 1}, {1, 1, 0}};
q=GLint(rand())%6;
glColor3f(Color[q][0], Color[q][1], Color[q][2]);
renderSphere(24, 8, 0);
glPopMatrix();
}
void arrow()
{
if ((((x>=0) && (x<=124)) || ((x>=140) && (x<=264))) && ((y>=16) && (y<=272))) {
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_LINES);
glVertex2f(132,16);
glVertex2f(132+6*(x-132)/sqrt((x-132.0)(x-132.0)+(y-16.0)(y-16.0)), 16+6*(y-16)/sqrt((x-132.0)(x-132.0)+(y-16.0)(y-16.0)));
glEnd();
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_TRIANGLES);
glVertex2f(132+6*(x-132)/sqrt((x-132.0)(x-132.0)+(y-16.0)(y-16.0)), 16+6*(y-16)/sqrt((x-132.0)(x-132.0)+(y-16.0)(y-16.0)));
glVertex2f((4*(x-132)-2sqrt(3.0)/3(y-16))/sqrt((x-132.0)(x-132.0)+(y-16.0)(y-16.0))+132, (4*(y-16)+2sqrt(3.0)/3(x-132))/sqrt((x-132.0)(x-132.0)+(y-16.0)(y-16.0))+16);
glVertex2f((4*(x-132)+2sqrt(3.0)/3(y-16))/sqrt((x-132.0)(x-132.0)+(y-16.0)(y-16.0))+132, (4*(y-16)-2sqrt(3.0)/3(x-132))/sqrt((x-132.0)(x-132.0)+(y-16.0)(y-16.0))+16);
glEnd();
}
glFlush();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glShadeModel(GL_FLAT);
int i, j, p, q;
GLfloat color_array[6][3]={{1, 0.5, 0}, {1, 0, 0}, {1, 0, 1}, {0, 1, 0}, {0, 0, 1}, {1, 1, 0}};
for (i=0; i<8; i++)
{
for (j=0; j<16; j++)
{
p=GLint(rand())%6;
glColor3f(color_array[p][0], color_array[p][1], color_array[p][2]);
Array[i][j][0]=color_array[p][0];
Array[i][j][1]=color_array[p][1];
Array[i][j][2]=color_array[p][2];
if (i%2==0)
renderSphere(8+16*j, 264-16*i, 0);
else
renderSphere(16+16*j, 264-16*i, 0);
}
}
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_LINES);
glVertex2f(0, 48);
glVertex2f(264, 48);
glVertex2f(264, 0);
glVertex2f(264, 272);
glEnd();
GLfloat Color[6][3]={{1, 0.5, 0}, {1, 0, 0}, {1, 0, 1}, {0, 1, 0}, {0, 0, 1}, {1, 1, 0}};
q=GLint(rand())%6;
r[0]=Color[q][0];
r[1]=Color[q][1];
r[2]=Color[q][2];
glColor3f(Color[q][0], Color[q][1], Color[q][2]);
renderSphere(132, 8, 0);
NextSphere();
arrow();
glutSwapBuffers();
}
void mouse(int button, int state, int x, int y)
{
int l=7;
int m=2;
int p, q, v, s, n;
int points=0;
if (button==GLUT_LEFT_BUTTON) {
if (state==GLUT_DOWN) {
if (((136x+132y-20064)/(y-16)>0) && ((136x+132y-20064)/(y-16)<264)) {
glColor3f(r[0], r[1], r[2]);
glTranslatef((136*x-17592)/(y-16), 128, 0);
renderSphere(132, 8, 0);
for (int j=0; j<16; j++) {
if (l%2) {
if ((136*x+132*y-20064)/(y-16)>8+j*16 && (136*x+132*y-20064)/(y-16)<24+j*16){
if ((r[0]==Array[j][l][0]) && (r[1]==Array[j][l][1]) && (r[2]==Array[j][l][2]))
{
do
{
v=l--;
if ((Array[j][l][0]==Array[j][v][0]) && (Array[j][l][1]==Array[j][v][1]) && (Array[j][l][2]==Array[j][v][2]))
{
m++;
glColor3f(1.0f, 1.0f, 1.0f);
renderSphere((136*x+132*y-19008)/(y-16), 136, 0);
renderSphere(j*16+16-(136*x+132*y-19008)/(y-16), 16, 0);
renderSphere(0, 128-16*(v+1), 0);
do
{
q=j++;
if ((Array[j][v][0]==Array[q][v][0]) && (Array[j][v][1]==Array[q][v][1]) && (Array[j][v][2]==Array[q][v][2]))
{
m++;
if (v%2)
{
glColor3f(1.0f, 1.0f, 1.0f);
renderSphere(16*(q-j), 0, 0);
}
else
{
glColor3f(1.0f, 1.0f, 1.0f);
renderSphere(16*(q-j)-8, 0, 0);
}
}
}
while ((q<15) || ((Array[j][v][0]!=Array[q][v][0]) || (Array[j][v][1]!=Array[q][v][1]) || (Array[j][v][2]!=Array[q][v][2])));
do
{
p=j--;
if ((Array[j][v][0]==Array[p][v][0]) && (Array[j][v][1]==Array[p][v][1]) && (Array[j][v][2]==Array[p][v][2]))
{
m++;
if (v%2) {
glColor3f(1.0f, 1.0f, 1.0f);
renderSphere(16*(p-j), 0, 0);
}
else
{
glColor3f(1.0f, 1.0f, 1.0f);
renderSphere((p-j)*16-8, 0, 0);
}
}
}
while ((p>0) || ((Array[j][v][0]!=Array[p][v][0]) || (Array[j][v][1]!=Array[p][v][1]) || (Array[j][v][2]!=Array[p][v][2])));
}
}
while ((v>0) || ((Array[j][l][0]!=Array[j][v][0]) || (Array[j][l][1]!=Array[j][v][1]) || (Array[j][l][2]!=Array[j][v][2])));
}
}
}
else
{
if ((136*x+132*y-20064)/(y-16)>j*16 && (136*x+132*y-20064)/(y-16)<16+j*16)
{
if ((r[0]==Array[j][l][0]) && (r[1]==Array[j][l][1]) && (r[2]==Array[j][l][2]))
{
do
{
v=l--;
if ((Array[j][l][0]==Array[j][v][0]) && (Array[j][l][1]==Array[j][v][1]) && (Array[j][l][2]==Array[j][v][2]))
{
m++;
glColor3f(1.0, 1.0, 1.0);
renderSphere((136*x+132*y-19008)/(y-16), 136, 0);
renderSphere(8+j*16-(136*x+132*y-19008)/(y-16), 16, 0);
renderSphere(0, 128-16*(v+1), 0);
do
{
q=j++;
if ((Array[j][v][0]==Array[q][v][0]) && (Array[j][v][1]==Array[q][v][1]) && (Array[j][v][2]==Array[q][v][2]))
{
m++;
if (v%2)
{
glColor3f(1.0f, 1.0f, 1.0f);
renderSphere(16*(q-j), 0, 0);
}
else
{
glColor3f(1.0f, 1.0f, 1.0f);
renderSphere(16*(q-j)-8, 0, 0);
}
}
}
while ((q<15) || ((Array[j][v][0]!=Array[q][v][0]) || (Array[j][v][1]!=Array[q][v][1]) || (Array[j][v][2]!=Array[q][v][2])));
do
{
p=j--;
if ((Array[j][v][0]==Array[p][v][0]) && (Array[j][v][1]==Array[p][v][1]) && (Array[j][v][2]==Array[p][v][2]))
{
m++;
if (v%2)
{
glColor3f(1.0f, 1.0f, 1.0f);
renderSphere(16*(p-j), 0, 0);
}
else
{
glColor3f(1.0f, 1.0f, 1.0f);
renderSphere(16*(p-j)-8, 0, 0);
}
}
}
while ((p>0) || ((Array[j][v][0]!=Array[p][v][0]) || (Array[j][v][1]!=Array[p][v][1]) || (Array[j][v][2]!=Array[p][v][2])));
}
}
while ((v>0) || ((Array[j][l][0]!=Array[j][v][0]) || (Array[j][l][1]!=Array[j][v][1]) || (Array[j][l][2]!=Array[j][v][2])));
}
}
}
do
{
s=j++;
if ((Array[j][l][0]==Array[s][l][0]) && (Array[j][l][1]==Array[s][l][1]) && (Array[j][l][2]==Array[s][l][2]))
{
m++;
glColor3f(1.0f, 1.0f, 1.0f);
renderSphere(s*16+8, 152, 0);
do
{
v=l--;
if ((Array[j][l][0]==Array[j][v][0]) && (Array[j][l][1]==Array[j][v][1]) && (Array[j][l][2]==Array[j][v][2]))
{
m++;
glColor3f(1.0f, 1.0f, 1.0f);
renderSphere(8, 16, 0);
}
}
while ((v>0) || ((Array[j][l][0]!=Array[j][v][0]) || (Array[j][l][1]!=Array[j][v][1]) || (Array[j][l][2]!=Array[j][v][2])));
}
}
while ((s<15) || ((Array[j][l][0]!=Array[s][l][0]) || (Array[j][l][1]!=Array[s][l][1]) || (Array[j][l][2]!=Array[s][l][2])));
do
{
n=j--;
if ((Array[j][l][0]==Array[n][l][0]) && (Array[j][l][1]==Array[n][l][1]) && (Array[j][l][2]==Array[n][l][2]))
{
m++;
glColor3f(1.0f, 1.0f, 1.0f);
renderSphere(n*16+8, 152, 0);
do
{
int v=l--;
if ((Array[j][l][0]==Array[j][v][0]) && (Array[j][l][1]==Array[j][v][1]) && (Array[j][l][2]==Array[j][v][2]))
{
m++;
glColor3f(1.0f, 1.0f, 1.0f);
renderSphere(8, 16, 0);
}
}
while ((v>0) || ((Array[j][l][0]!=Array[j][v][0]) || (Array[j][l][1]!=Array[j][v][1]) || (Array[j][l][2]!=Array[j][v][2])));
}
}
while ((n>0) || ((Array[j][l][0]!=Array[n][l][0]) || (Array[j][l][1]!=Array[n][l][1]) || (Array[j][l][2]!=Array[j][v][2])));
}
}
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(w, h);
glutCreateWindow(“Frozen Bubbles”);
init();
glutReshapeFunc(NULL);
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}