專題報告:Candy Crush
PPT
demo影片
程式碼:
- #include <GL/glut.h>
- #include <stdio.h>
- #include <math.h>
- #include <time.h>
- #include <stdlib.h>
- #include <iostream>
- using namespace std;
- #define offset 50.0
- #define bias 200.0
- #define rad 50.0
- #define PI 3.14159265358979323846
- #define red 1
- #define yellow 2
- #define green 3
- #define blue 4
- #define purple 5
- #define black 10
- #define broad 16 //棋盤
- #define back 15 //未翻開棋子
- #define frame 17 //框框
- #define zero 20 //0
- #define one 21 //1
- #define two 22 //2
- #define three 23 //3
- #define four 24//4
- #define five 25 //5
- #define six 26//6
- #define seven 27 //7
- #define eight 28 //8
- #define nine 29 //9
- void reset();//回歸初始
- void check();
- void random();
- int state=0;//選第一個顏色
- int temp;
- int AA[25]={0},BB[25]={0},CC[25]={0};//AA:rand初始用 BB:每個棋子的起始位置 CC:現在局面
- int testcin0=0,testcin1=0;//0:來源棋子的位置 1:目標棋子的位置
- int source=100;//來源棋子
- int destination=100;//目標棋子
- float frameX=0,frameY=400;//控制框框位置變數
- float stayX=0,stayY=0;
- int score=0,scoretemp,scoretemptwo;
- void init (void)
- {
- glClearColor (0.9, 0.745, 0.788, 0.85); // Set display-window color to white.
- glMatrixMode (GL_PROJECTION); // Set projection parameters.
- gluOrtho2D (0.0, 1000.0, 0.0, 600.0);
- reset();
- }
- static void display (void)
- {
- glClear (GL_COLOR_BUFFER_BIT); // Clear display window.
- int i, col, row;
- GLfloat pi,temp_1;
- GLfloat x=0,y=0;
- int a,b,c,n=0;
- GLfloat color[10][3]={{1.0,0.0,0.0},{0.0,1.0,0.0},{0.0,0.0,1.0},{1.0,1.0,0.0}
- ,{0.0,1.0,1.0},{0.5,1.0,1.0},{1.0,0.5,1.0},{1.0,1.0,0.5},{0.3,1.0,0.7},{0.7,0.3,1.0}};
- //未翻開棋子
- glNewList(back, GL_COMPILE);
- //外圈
- glBegin(GL_POLYGON);
- glColor3f(0.392, 0.651, 0.0);
- for(i=0 ; i<360 ; i++)
- glVertex2f(50.0+48.0*cos(i*PI/180), 150.0+48.0*sin(i*PI/180));
- glEnd();
- //內圈
- glLineWidth(1);
- glColor3f(0.0, 0.0, 0.0);
- glBegin(GL_LINE_LOOP);
- for(i=0 ; i<360 ; i++)
- glVertex2f(50.0+48.0*cos(i*PI/180), 150.0+48.0*sin(i*PI/180));
- glEnd();
- glEndList();
- //red
- glNewList(red, GL_COMPILE);
- int rx=50,ry=150;
- glColor3f (1.0,0.3,0.0);
- glBegin (GL_POLYGON); //red
- for(x=0;x<6.28;x+=0.01)
- {
- glVertex2i (rx+45*sin(x), ry+45*cos(x));
- }
- glEnd ( );
- glColor3f (1.0,0.0,0);
- glBegin (GL_POLYGON);
- for(x=4;x<5.43;x+=0.01)
- {
- glVertex2i (rx+23+30*sin(x), ry+40*cos(x));
- }
- glEnd ( );
- glBegin (GL_POLYGON);
- for(x=0.85;x<2.28;x+=0.01)
- {
- glVertex2i (rx-23+30*sin(x), ry+40*cos(x));
- }
- glEnd ( );
- glBegin (GL_POLYGON);
- for(x=1.57;x<3.14;x+=0.01)
- {
- glVertex2i (rx+2+30*sin(x), ry+30*cos(x));
- }
- glEnd ( );
- glBegin (GL_POLYGON);
- for(x=4.71;x<6.28;x+=0.01)
- {
- glVertex2i (rx+30+2+30*sin(x), ry-30+30*cos(x));
- }
- glEnd ( );
- glBegin (GL_POLYGON);
- for(x=3.14;x<4.71;x+=0.01)
- {
- glVertex2i (rx-2+30*sin(x), ry+30*cos(x));
- }
- glEnd ( );
- glBegin (GL_POLYGON);
- for(x=0;x<1.57;x+=0.01)
- {
- glVertex2i (rx-30-2+30*sin(x), ry-30+30*cos(x));
- }
- glEnd ( );
- glBegin (GL_POLYGON);
- for(x=4.5;x<6;x+=0.01)
- {
- glVertex2i (rx+30+20*sin(x), ry+0+20*cos(x));
- }
- glEnd ( );
- glBegin (GL_POLYGON);
- for(x=1.36;x<2.86;x+=0.01)
- {
- glVertex2i (rx+5+20*sin(x), ry+16+20*cos(x));
- }
- glEnd ( );
- glBegin (GL_POLYGON);
- for(x=-4.5;x>-6;x-=0.01)
- {
- glVertex2i (rx-30+20*sin(x), ry+20*cos(x));
- }
- glEnd ( );
- glBegin (GL_POLYGON);
- for(x=-1.36;x>-2.86;x-=0.01)
- {
- glVertex2i (rx-5+20*sin(x), ry+16+20*cos(x));
- }
- glEnd ( );
- glEndList();
- //yellow
- glNewList(yellow, GL_COMPILE);
- glColor3f (1.0,1.0,0.0);//yellow
- int yx=50,yy=150;
- glBegin (GL_POLYGON);
- for(x=0;x<6.28;x+=0.01)
- {
- glVertex2i (yx+45*sin(x), yy+45*cos(x));
- }
- glEnd ( );
- glColor3f (1.0,0.8,0.0);
- for(int y=0;y<9;y+=1)
- {
- glBegin (GL_POLYGON);
- glVertex2i (yx+30*sin(x), yy+30*cos(x));
- x+=6.28*3/8;
- glVertex2i (yx+30*sin(x), yy+30*cos(x));
- glVertex2i (yx, yy);
- glEnd ( );
- }
- glEndList();
- //green
- glNewList(green, GL_COMPILE);
- int gx=50,gy=150;
- glColor3f (0.0,1.0,0.0);//green
- glBegin (GL_POLYGON);
- for(x=0;x<6.28;x+=0.01)
- {
- glVertex2i (gx+45*sin(x), gy+45*cos(x));
- }
- glEnd ( );
- glColor3f (0.2,0.8,0.2);
- glBegin (GL_POLYGON);
- for(x=1.57;x<3.14;x+=0.01)
- {
- glVertex2i (gx+30*sin(x), gy+30+30*cos(x));
- }
- glEnd ( );
- glBegin (GL_POLYGON);
- for(x=4.71;x<6.28;x+=0.01)
- {
- glVertex2i (gx+30+30*sin(x), gy+30*cos(x));
- }
- glEnd ( );
- glBegin (GL_POLYGON);
- for(x=0;x<1.57;x+=0.01)
- {
- glVertex2i (gx+30*sin(x), gy-30+30*cos(x));
- }
- glEnd ( );
- glBegin (GL_POLYGON);
- for(x=3.14;x<4.71;x+=0.01)
- {
- glVertex2i (gx+30+30*sin(x), gy+30*cos(x));
- }
- glEnd ( );
- glBegin (GL_POLYGON);
- for(x=1.57;x<3.14;x+=0.01)
- {
- glVertex2i (gx-30+30*sin(x), gy+30*cos(x));
- }
- glEnd ( );
- glBegin (GL_POLYGON);
- for(x=4.71;x<6.28;x+=0.01)
- {
- glVertex2i (gx+30*sin(x), gy-30+30*cos(x));
- }
- glEnd ( );
- glBegin (GL_POLYGON);
- for(x=0;x<1.57;x+=0.01)
- {
- glVertex2i (gx-30+30*sin(x), gy+30*cos(x));
- }
- glEnd ( );
- glBegin (GL_POLYGON);
- for(x=3.14;x<4.71;x+=0.01)
- {
- glVertex2i (gx+30*sin(x), gy+30+30*cos(x));
- }
- glEnd ( );
- glEndList();
- //blue
- glNewList(blue, GL_COMPILE);
- glColor3f (0.0,1.0,1.0);//water
- int wx=50,wy=150;
- glBegin (GL_POLYGON);
- for(x=0;x<6.28;x+=0.01)
- {
- glVertex2i (wx+45*sin(x), wy+45*cos(x));
- }
- glEnd ( );
- glColor3f (0.0,0.6,0.8);
- glBegin (GL_POLYGON);
- for(x=-1.57;x<1.57;x+=0.01)
- {
- glVertex2i (wx+30*sin(x), wy+7+30*cos(x));
- }
- glEnd ( );
- glColor3f (0.0,1.0,1.0);
- glBegin (GL_POLYGON);
- for(x=-1.57;x<1.57;x+=0.01)
- {
- glVertex2i (wx-15+15*sin(x), wy+7+15*cos(x));
- }
- glEnd ( );
- glColor3f (0.0,0.6,0.8);
- glBegin (GL_POLYGON);
- for(x=1.57;x<=6;x+=0.01)
- {
- glVertex2i (wx+15+15*sin(x), wy+7+15*cos(x));
- }
- glEnd ( );
- glColor3f (0.0,0.6,0.8);
- glBegin (GL_POLYGON);
- for(x=1.57;x<4.71;x+=0.01)
- {
- glVertex2i (wx+30*sin(x), wy-7+30*cos(x));
- }
- glEnd ( );
- glBegin (GL_POLYGON);
- for(x=-2;x<2;x+=0.01)
- {
- glVertex2i (wx-15+15*sin(x), wy-7+15*cos(x));
- }
- glEnd ( );
- glColor3f (0.0,1.0,1.0);
- glBegin (GL_POLYGON);
- for(x=1.57;x<4.73;x+=0.01)
- {
- glVertex2i (wx+15+15*sin(x), wy-7+15*cos(x));
- }
- glEnd ( );
- glEndList();
- //purple
- glNewList(purple, GL_COMPILE);
- glColor3f (0.0,0.0,1.0);//blue
- int bx=50,by=150;
- glBegin (GL_POLYGON);
- for(x=0;x<6.28;x+=0.01)
- {
- glVertex2i (bx+45*sin(x), by+45*cos(x));
- }
- glEnd ( );
- glBegin (GL_POLYGON);
- glColor3f (0.5,0.1,0.9);
- for(x=2.25;x<7.18;x+=0.01)
- {
- glVertex2i (bx+30*sin(x), by+30*cos(x));
- }
- glEnd ( );
- glBegin (GL_POLYGON);
- glColor3f (0.0,0.0,1.0);
- for(x=2.7;x<6.83;x+=0.01)
- {
- glVertex2i (bx+15+20*sin(x), by+20*cos(x));
- }
- glEnd ( );
- glEndList();
- glNewList(black, GL_COMPILE);
- glBegin(GL_POLYGON);
- glColor3f(0.0, 0.0, 0.0);
- for(i=0 ; i<360 ; i++)
- glVertex2f(50.0+48.0*cos(i*PI/180), 150.0+48.0*sin(i*PI/180));
- glEnd();
- glEndList();
- int zerox=100,zeroy=50;
- int onex=100,oney=50;
- int twox=100,twoy=50;
- int threex=100,threey=50;
- int fourx=100,foury=50;
- int fivex=100,fivey=50;
- int sixx=100,sixy=50;
- int sevenx=100,seveny=50;
- int eightx=100,eighty=50;
- int ninex=100,niney=50;
- glNewList(zero, GL_COMPILE);
- glColor3f (0.0,0.0,0.0);//zero
- glBegin (GL_POLYGON);
- for(x=0;x<6.28;x+=0.01)
- {
- glVertex2i (zerox+20*sin(x), zeroy+23*cos(x));
- }
- glEnd ( );
- glColor3f (1.0,1.0,1.0);
- glBegin (GL_POLYGON);
- for(x=0;x<6.28;x+=0.01)
- {
- glVertex2i (zerox+9*sin(x), zeroy+12*cos(x));
- }
- glEnd ( );
- glEndList();
- glNewList(one, GL_COMPILE);
- glColor3f (0.0,0.0,0.0);//one
- glBegin (GL_POLYGON);
- glVertex2i (onex-10, oney+5);
- glVertex2i (onex+5, oney+23);
- glVertex2i (onex+5, oney+5);
- glEnd ( );
- glBegin (GL_POLYGON);
- glVertex2i (onex+10, oney-15);
- glVertex2i (onex+10, oney-23);
- glVertex2i (onex-10, oney-23);
- glVertex2i (onex-10, oney-15);
- glEnd ( );
- glBegin (GL_POLYGON);
- glVertex2i (onex-5, oney-15);
- glVertex2i (onex-5, oney+5);
- glVertex2i (onex+5, oney+5);
- glVertex2i (onex+5, oney-15);
- glEnd ( );
- glEndList();
- glNewList(two, GL_COMPILE);
- glColor3f (0.0,0.0,0.0);//two
- glBegin (GL_POLYGON);
- for(x=-1.57;x<1.57;x+=0.01)
- {
- glVertex2i (twox+15*sin(x), twoy+23*cos(x));
- }
- glEnd ( );
- glColor3f (1.0,1.0,1.0);
- glBegin (GL_POLYGON);
- for(x=1.57;x>-1.57;x-=0.01)
- {
- glVertex2i (twox+8*sin(x), twoy+11*cos(x));
- }
- glEnd ( );
- glColor3f (0.0,0.0,0.0);
- glBegin (GL_POLYGON);
- glVertex2i (twox+15*sin(1.57), twoy+23*cos(1.57));
- glVertex2i (twox-5, twoy-15);
- glVertex2i (twox-15, twoy-15);
- glVertex2i (twox+8*sin(1.2), twoy+11*cos(1.2));
- glEnd ( );
- glBegin (GL_POLYGON);
- glVertex2i (twox+15, twoy-15);
- glVertex2i (twox+15, twoy-23);
- glVertex2i (twox-15, twoy-23);
- glVertex2i (twox-15, twoy-15);
- glEnd ( );
- glEndList();
- glNewList(three, GL_COMPILE);
- glColor3f (0.0,0.0,0.0);//three
- glBegin (GL_POLYGON);
- for(x=-1.57;x<1.57;x+=0.01)
- {
- glVertex2i (threex+15*sin(x), threey+10+15*cos(x));
- }
- glEnd ( );
- glBegin (GL_POLYGON);
- for(x=1.57;x<2.3;x+=0.01)
- {
- glVertex2i (threex+15*sin(x), threey+10+15*cos(x));
- }
- glVertex2i (threex, threey+10);
- glEnd ( );
- glBegin (GL_POLYGON);
- for(x=0.8;x<1.57;x+=0.01)
- {
- glVertex2i (threex+15*sin(x), threey-10+15*cos(x));
- }
- glVertex2i (threex, threey-10);
- glEnd ( );
- glBegin (GL_POLYGON);
- for(x=1.57;x<4.71;x+=0.01)
- {
- glVertex2i (threex+15*sin(x), threey-10+15*cos(x));
- }
- glEnd ( );
- glBegin (GL_POLYGON);
- glColor3f (1.0,1.0,1.0);
- for(x=-1.57;x>-6.28;x-=0.01)
- {
- glVertex2i (threex+5*sin(x), threey-10+5*cos(x));
- }
- glEnd ( );
- glColor3f (0.0,0.0,0.0);
- glBegin (GL_POLYGON);
- glVertex2i (threex-3, threey-3);
- glVertex2i (threex-3, threey+3);
- glVertex2i (threex+5*sin(2), threey+10+5*cos(2));
- glVertex2i (threex+15*sin(2.3), threey+10+15*cos(2.3));
- glEnd ( );
- glBegin (GL_POLYGON);
- glVertex2i (threex-3, threey-3);
- glVertex2i (threex-3, threey+3);
- glVertex2i (threex+15*sin(2.3), threey+10+15*cos(2.3));
- glVertex2i (threex+5*sin(1), threey-10+5*cos(1));
- glEnd ( );
- glColor3f (1.0,1.0,1.0);
- glBegin (GL_POLYGON);
- for(x=3.14;x>-1.57;x-=0.01)
- {
- glVertex2i (threex+5*sin(x), threey+10+5*cos(x));
- }
- glEnd ( );
- glEndList();
- glNewList(four, GL_COMPILE);
- glColor3f (0.0,0.0,0.0);//four
- glBegin (GL_POLYGON);
- glVertex2i (fourx+5, foury+23);
- glVertex2i (fourx+5, foury-23);
- glVertex2i (fourx-3, foury-23);
- glVertex2i (fourx-3, foury-15);
- glVertex2i (fourx-20, foury-15);
- glEnd ( );
- glBegin (GL_POLYGON);
- glVertex2i (fourx+5, foury-8);
- glVertex2i (fourx+13, foury-8);
- glVertex2i (fourx+13, foury-15);
- glVertex2i (fourx+5, foury-15);
- glEnd ( );
- glColor3f (1.0,1.0,1.0);
- glBegin (GL_POLYGON);
- glVertex2i (fourx-3, foury-8);
- glVertex2i (fourx-3, foury+1);
- glVertex2i (fourx-8, foury-8);
- glEnd ( );
- glEndList();
- glNewList(five, GL_COMPILE);
- glColor3f (0.0,0.0,0.0);//five
- glBegin (GL_POLYGON);
- for(x=0;x<3.14;x+=0.01)
- {
- glVertex2i (fivex+15*sin(x), fivey-10+15*cos(x));
- }
- glEnd ( );
- glBegin (GL_POLYGON);
- for(x=3.14;x<4.71;x+=0.01)
- {
- glVertex2i (fivex+15*sin(x), fivey-10+15*cos(x));
- }
- glVertex2i (fivex, fivey-10);
- glEnd ( );
- glColor3f (1.0,1.0,1.0);
- glBegin (GL_POLYGON);
- for(x=0;x>-6.28;x-=0.01)
- {
- glVertex2i (fivex+5*sin(x), fivey-10+5*cos(x));
- }
- glEnd ( );
- glColor3f (0.0,0.0,0.0);
- glBegin (GL_POLYGON);
- glVertex2i (fivex-15, fivey-5);
- glVertex2i (fivex-15, fivey+20);
- glVertex2i (fivex-5, fivey+20);
- glVertex2i (fivex-5, fivey-5);
- glEnd ( );
- glBegin (GL_POLYGON);
- glVertex2i (fivex+13, fivey+20);
- glVertex2i (fivex+13, fivey+10);
- glVertex2i (fivex-5, fivey+10);
- glVertex2i (fivex-5, fivey+20);
- glEnd ( );
- glBegin (GL_POLYGON);
- glVertex2i (fivex-5, fivey-5);
- glVertex2i (fivex-5, fivey+5);
- glVertex2i (fivex, fivey+5);
- glVertex2i (fivex, fivey-5);
- glEnd ( );
- glEndList();
- glNewList(six, GL_COMPILE);
- glBegin (GL_POLYGON);//six
- glColor3f (0.0,0.0,0.0);
- for(x=0.1;x<6.9;x+=0.01)
- {
- glVertex2i (sixx+15*sin(x), sixy-10+15*cos(x));
- }
- glEnd ( );
- glBegin (GL_POLYGON);
- glVertex2i (sixx+10, sixy+20);
- glVertex2i (sixx+15*sin(0.1), sixy-10+15*cos(0.1));
- glVertex2i (sixx+15*sin(5.2), sixy-10+15*cos(5.2));
- glVertex2i (sixx, sixy+20);
- glEnd ( );
- glColor3f (1.0,1.0,1.0);
- glBegin (GL_POLYGON);
- for(x=0;x>-6.28;x-=0.01)
- {
- glVertex2i (sixx+5*sin(x), sixy-10+5*cos(x));
- }
- glEnd ( );
- glEndList();
- glNewList(seven, GL_COMPILE);
- glColor3f (0.0,0.0,0.0);
- glBegin (GL_POLYGON);//seven
- glVertex2i (sevenx-10, seveny-23);
- glVertex2i (sevenx, seveny-23);
- glVertex2i (sevenx+15, seveny+19);
- glVertex2i (sevenx+3, seveny+10);
- glEnd ( );
- glBegin (GL_POLYGON);
- glVertex2i (sevenx+15, seveny+19);
- glVertex2i (sevenx-15, seveny+19);
- glVertex2i (sevenx-15, seveny+10);
- glVertex2i (sevenx+3, seveny+10);
- glEnd ( );
- glEndList();
- glNewList(eight, GL_COMPILE);
- glColor3f (0.0,0.0,0.0);//eight
- glBegin (GL_POLYGON);
- for(x=-2.3;x<2.3;x+=0.01)
- {
- glVertex2i (eightx+15*sin(x), eighty+10+15*cos(x));
- }
- for(x=0.8;x<5.48;x+=0.01)
- {
- glVertex2i (eightx+15*sin(x), eighty-10+15*cos(x));
- }
- glEnd ( );
- glBegin (GL_POLYGON);
- glColor3f (1.0,1.0,1.0);
- for(x=0;x>-6.28;x-=0.01)
- {
- glVertex2i (eightx+5*sin(x), eighty-10+5*cos(x));
- }
- glEnd ( );
- glBegin (GL_POLYGON);
- for(x=4.71;x>-1.57;x-=0.01)
- {
- glVertex2i (eightx+5*sin(x),eighty+10+5*cos(x));
- }
- glEnd ( );
- glEndList();
- glNewList(nine, GL_COMPILE);
- glColor3f (0.0,0.0,0.0);//nine
- glBegin (GL_POLYGON);
- for(x=3.24;x<8.6;x+=0.01)
- {
- glVertex2i (ninex+15*sin(x), niney+10+15*cos(x));
- }
- glEnd ( );
- glBegin (GL_POLYGON);
- glVertex2i (ninex-10, niney-20);
- glVertex2i (ninex+15*sin(3.24), niney+10+15*cos(3.24));
- glVertex2i (ninex+15*sin(8.34), niney+10+15*cos(8.34));
- glVertex2i (ninex, niney-20);
- glEnd ( );
- glColor3f (1.0,1.0,1.0);
- glBegin (GL_POLYGON);
- for(x=0;x>-6.28;x-=0.01)
- {
- glVertex2i (ninex+5*sin(x), niney+10+5*cos(x));
- }
- glEnd ( );
- glEndList();
- //棋盤
- glNewList(broad, GL_COMPILE);
- n=rand()%10;
- for(int j=0;j<n;j++)
- {
- i=rand()%10;
- a=rand()%1000;
- b=rand()%200;
- c=rand()%600;
- glColor3fv(color[i]);
- for(int y=0;y<9;y+=1)
- {
- glBegin (GL_POLYGON);
- glVertex2i (a+30*sin(x), c+30*cos(x));
- x+=6.28*2/5;
- glVertex2i (a+30*sin(x), c+30*cos(x));
- glVertex2i (a, c);
- glEnd ();
- }
- /*
- for(temp_1=0 ; temp_1<6.28 ; temp_1+=0.01)
- {
- x=a+b*cos(temp_1);
- y=c+b*sin(temp_1);
- glVertex2f(x,y);
- }
- glEnd();
- */
- }
- glBegin(GL_POLYGON);
- glColor3f(0.0, 0.0, 0.0);
- glVertex2i(0, 0);
- glVertex2i(500, 0);
- glVertex2i(500, 500);
- glVertex2i(0, 500);
- glEnd();
- //背景白點
- /*
- glBegin(GL_POINTS);
- glColor3f(1.0, 1.0, 1.0);
- glPointSize(3);
- for(i=0 ; i<1000 ; i++)
- glVertex2i( rand()%500, rand()%500);
- glEnd();
- */
- //row
- glColor3f(0.7, 0.7, 0.0);
- glLineWidth(5);
- for(row=0 ; row<=5 ; row++){
- glBegin(GL_LINES);
- glVertex2i(0, 0+100*row);
- glVertex2i(500, 0+100*row);
- glEnd();
- }
- //col
- for(col=0 ; col<=5 ; col++){
- glBegin(GL_LINES);
- glVertex2i(0+100*col, 0);
- glVertex2i(0+100*col, 500);
- glEnd();
- }
- glEndList();
- glPushMatrix();
- glTranslatef(25.0, 25.0, 0.0);
- glCallList(broad);
- glPopMatrix();
- //框框
- glNewList(frame,GL_COMPILE);
- glColor3f(1.0,1.0,1.0);
- glBegin(GL_LINES);
- glVertex2f(0,0);
- glVertex2f(0,30);
- glVertex2f(0,0);
- glVertex2f(30,0);
- glVertex2f(100,100);
- glVertex2f(100,70);
- glVertex2f(100,100);
- glVertex2f(70,100);
- glVertex2f(0,100);
- glVertex2f(0,70);
- glVertex2f(0,100);
- glVertex2f(30,100);
- glVertex2f(100,0);
- glVertex2f(70,0);
- glVertex2f(100,0);
- glVertex2f(100,30);
- glEnd();
- glEndList();
- //畫框框
- if(stayX >0 && stayY >0)//欲交換的位置
- {
- glPushMatrix();
- glTranslatef(25, 25, 0.0);
- glTranslatef(stayX, stayY, 0.0);
- glCallList(frame);
- glPopMatrix();
- }
- glPushMatrix();
- glTranslatef(25, 25, 0.0);
- glTranslatef(frameX, frameY, 0.0);
- glCallList(frame);
- glPopMatrix();
- //畫出目前局勢
- glPushMatrix();
- glTranslatef(25, 25, 0.0);
- glTranslatef(0.0, 300.0, 0.0);
- for(i=0 ; i<25 ; i++)
- {
- glCallList(CC[i]);
- glTranslatef(100.0, 0.0, 0.0);
- if(i%5==4)
- glTranslatef(-500.0, -100.0, 0.0);
- }
- //分數
- glTranslated(800, 445, 0);
- scoretemptwo=score;
- for(int w=0;w<=6;w++)
- {
- scoretemp=score%10;
- scoretemp=scoretemp+20;
- glCallList(scoretemp);
- score=score/10;
- glTranslated(-50, 0, 0);
- }
- score=scoretemptwo;
- glPopMatrix();
- glFlush ( );
- }
- void Keyboard(unsigned char key,int x,int y)
- {
- if(state==0){
- switch(key)
- {
- case 'w':
- case 'W':
- if(frameY <=300){
- frameY=frameY+100;
- testcin0 -=5;
- }
- random();
- glutPostRedisplay();
- break;
- case 's':
- case 'S':
- if(frameY >=100){
- frameY=frameY-100;
- testcin0 +=5;
- }
- random();
- glutPostRedisplay();
- break;
- case 'a':
- case 'A':
- if(frameX >=100){
- frameX=frameX-100;
- testcin0--;
- }
- random();
- glutPostRedisplay();
- break;
- case 'd':
- case 'D':
- if(frameX <=300){
- frameX=frameX+100;
- testcin0++;
- }
- random();
- glutPostRedisplay();
- break;
- case 'r':
- case 'R':
- reset();
- random();
- glutPostRedisplay();
- break;
- case 13:
- source=CC[testcin0];
- stayX=frameX;
- stayY=frameY;
- break;
- default:
- break;
- }//SWITCH
- if(source>=1 && source<=5) {
- temp=testcin0;
- state=1;
- }
- }//STATE0
- else if(state ==1){//state1
- switch(key)
- {
- case 'w':
- case 'W':
- if(frameY <=300){
- frameY=frameY+100;
- testcin0 -=5;
- }
- glutPostRedisplay();
- break;
- case 's':
- case 'S':
- if(frameY >=100){
- frameY=frameY-100;
- testcin0 +=5;
- }
- glutPostRedisplay();
- break;
- case 'a':
- case 'A':
- if(frameX >=100){
- frameX=frameX-100;
- testcin0--;
- }
- glutPostRedisplay();
- break;
- case 'd':
- case 'D':
- if(frameX <=300){
- frameX=frameX+100;
- testcin0++;
- }
- glutPostRedisplay();
- break;
- case 'r':
- case 'R':
- reset();
- glutPostRedisplay();
- break;
- case 13:
- destination=CC[testcin0];
- break;
- default:
- break;
- }//SWITCH
- /*
- if(destination == 0 ){//移動
- if(testcin1 == testcin0+1 || testcin1 == testcin0-1 || testcin1 == testcin0+5 || testcin1 == testcin0-5)
- {//位置合理(十字位置)
- CC[testcin1] = source;
- CC[testcin0] = 0;
- state=1;
- testcin0=testcin1;
- destination=100;
- source =100;
- //player = 0;
- glutPostRedisplay();
- }else{//位置不合理
- state =1;
- destination=100;
- source =100;
- testcin0 = testcin1;
- }
- }
- else if (destination >5)
- {//選擇錯誤
- state=1;
- destination=100;
- source =100;
- }
- */
- if( destination>=1 && destination<=5) {
- CC[testcin0]=source;
- CC[temp]=destination;
- state=0;
- source=0;
- destination=0;
- stayX=0;
- stayY=0;
- check();
- glutPostRedisplay();
- }
- }//state1
- }
- int main(int argc, char *argv[])
- {
- srand(time(0));
- glutInit (&argc, argv); // Initialize GLUT.
- glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); // Set display mode.
- glutInitWindowPosition (0, 0); // Set top-left display-window position.
- glutInitWindowSize (1000, 600); // Set display-window width and height.
- glutCreateWindow ("candy crush 方向=wsad & 確認=enter & 重玩按r"); // Create display window.
- init (); // Execute initialization procedure.
- glutDisplayFunc (display); // Send graphics to display window.
- glutKeyboardFunc(Keyboard);
- glutMainLoop ( );
- }
- void reset()
- {
- int n;
- //變數初始值
- // player=-1;
- // state=-1;
- source=100;
- destination=100;
- testcin0=0;
- testcin1=0;
- //win=10;
- frameX=0;
- frameY=400;
- //red=16;
- //black=16;
- for(int i=0; i<25; i++)
- AA[i]=0;
- //shuffle
- for (int i=0; i<25; i++) {
- n=rand()%5+1;
- CC[i]=n;
- }//shuffle end
- check();
- random();
- }
- void check()
- {
- int i,map[25];
- for(i=0;i<25;i++)//初始化地圖值=0
- {
- map[i]=0;
- }
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- for(i=1;i<=3;i++)//判斷顏色一樣的地圖值=1
- {
- if(CC[i]==CC[i-1] && CC[i]==CC[i+1])
- {
- map[i-1]=1;
- map[i]=1;
- map[i+1]=1;
- }
- }
- for(i=6;i<=8;i++)
- {
- if(CC[i]==CC[i-1] && CC[i]==CC[i+1])
- {
- map[i-1]=1;
- map[i]=1;
- map[i+1]=1;
- }
- }
- for(i=11;i<=13;i++)
- {
- if(CC[i]==CC[i-1] && CC[i]==CC[i+1])
- {
- map[i-1]=1;
- map[i]=1;
- map[i+1]=1;
- }
- }
- for(i=16;i<=18;i++)
- {
- if(CC[i]==CC[i-1] && CC[i]==CC[i+1])
- {
- map[i-1]=1;
- map[i]=1;
- map[i+1]=1;
- }
- }
- for(i=21;i<=23;i++)
- {
- if(CC[i]==CC[i-1] && CC[i]==CC[i+1])
- {
- map[i-1]=1;
- map[i]=1;
- map[i+1]=1;
- }
- }
- for(i=5;i<=9;i++)
- {
- if(CC[i]==CC[i-5] && CC[i]==CC[i+5])
- {
- map[i-5]=1;
- map[i]=1;
- map[i+5]=1;
- }
- }
- for(i=10;i<=14;i++)
- {
- if(CC[i]==CC[i-5] && CC[i]==CC[i+5])
- {
- map[i-5]=1;
- map[i]=1;
- map[i+5]=1;
- }
- }
- for(i=15;i<=19;i++)
- {
- if(CC[i]==CC[i-5] && CC[i]==CC[i+5])
- {
- map[i-5]=1;
- map[i]=1;
- map[i+5]=1;
- }
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
- for(i=0;i<25;i++)//地圖值=1的變黑色
- {
- if(map[i]==1)
- {
- CC[i]=0;
- score+=10;
- }
- }
- for(i=0;i<25;i++)//重新初始化地圖值=0
- {
- map[i]=0;
- }
- }
- void random()
- {
- int i,a=1;
- while(a>0)
- {
- for(i=0;i<25;i++)
- {
- if(CC[i]==0)
- {
- CC[i]=rand()%5+1;
- }
- }
- check();
- a=0;
- for(i=0;i<25;i++)
- {
- if(CC[i]==0)
- {
- a++;
- }
- }
- }
- }
