2015年6月25日 星期四

專題報告:Candy Crush
PPT
PDF
demo影片
程式碼:

  1. #include <GL/glut.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <time.h>
  5. #include <stdlib.h>
  6. #include <iostream>
  7. using namespace std;

  8. #define offset 50.0
  9. #define bias 200.0
  10. #define rad 50.0
  11. #define PI 3.14159265358979323846

  12. #define red 1
  13. #define yellow 2
  14. #define green 3
  15. #define blue 4
  16. #define purple 5
  17. #define black 10

  18. #define broad 16 //棋盤 
  19. #define back 15 //未翻開棋子 
  20. #define frame 17 //框框 

  21. #define zero 20 //0
  22. #define one 21 //1
  23. #define two 22 //2
  24. #define three 23 //3
  25. #define four 24//4
  26. #define five 25 //5
  27. #define six 26//6
  28. #define seven 27 //7
  29. #define eight 28 //8
  30. #define nine 29 //9

  31. void reset();//回歸初始 
  32. void check();
  33. void random();

  34. int state=0;//選第一個顏色 
  35. int temp;

  36. int AA[25]={0},BB[25]={0},CC[25]={0};//AA:rand初始用  BB:每個棋子的起始位置  CC:現在局面 
  37. int testcin0=0,testcin1=0;//0:來源棋子的位置  1:目標棋子的位置 
  38. int source=100;//來源棋子 
  39. int destination=100;//目標棋子 
  40. float frameX=0,frameY=400;//控制框框位置變數 
  41. float stayX=0,stayY=0;
  42. int score=0,scoretemp,scoretemptwo;
  43. void init (void)
  44. {
  45.     
  46.     glClearColor (0.9, 0.745, 0.788, 0.85);  // Set display-window color to white.
  47.     glMatrixMode (GL_PROJECTION);       // Set projection parameters.
  48.     gluOrtho2D (0.0, 1000.0, 0.0, 600.0);
  49.     reset();

  50. }

  51. static void display (void)
  52. {
  53.     glClear (GL_COLOR_BUFFER_BIT);  // Clear display window.
  54.     
  55.     int i, col, row;
  56.     GLfloat pi,temp_1;
  57.     GLfloat x=0,y=0;
  58.     int a,b,c,n=0; 
  59.     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}
  60.     ,{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}};
  61.     
  62.     
  63.     //未翻開棋子 
  64.     glNewList(back, GL_COMPILE);
  65.       //外圈 
  66.       glBegin(GL_POLYGON);
  67.         glColor3f(0.392, 0.651, 0.0);
  68.         for(i=0 ; i<360 ; i++)
  69.           glVertex2f(50.0+48.0*cos(i*PI/180), 150.0+48.0*sin(i*PI/180));
  70.       glEnd();    
  71.       //內圈 
  72.       glLineWidth(1);
  73.       glColor3f(0.0, 0.0, 0.0);
  74.       glBegin(GL_LINE_LOOP);
  75.         for(i=0 ; i<360 ; i++)
  76.           glVertex2f(50.0+48.0*cos(i*PI/180), 150.0+48.0*sin(i*PI/180));
  77.       glEnd();
  78.                 
  79.     glEndList();
  80.  
  81.     //red
  82.     glNewList(red, GL_COMPILE);

  83.     int rx=50,ry=150;   
  84.       glColor3f (1.0,0.3,0.0);
  85.      glBegin (GL_POLYGON);  //red  
  86.     for(x=0;x<6.28;x+=0.01)
  87.     {        
  88.     glVertex2i (rx+45*sin(x), ry+45*cos(x));       
  89.     }          
  90.      glEnd ( );
  91.      
  92.      glColor3f (1.0,0.0,0);
  93.      glBegin (GL_POLYGON);

  94.       for(x=4;x<5.43;x+=0.01)
  95.     {        
  96.     glVertex2i (rx+23+30*sin(x), ry+40*cos(x));       
  97.     }
  98.     glEnd ( );
  99.     glBegin (GL_POLYGON);
  100.     for(x=0.85;x<2.28;x+=0.01)
  101.     {        
  102.     glVertex2i (rx-23+30*sin(x), ry+40*cos(x));       
  103.     }
  104.     glEnd ( );
  105.     glBegin (GL_POLYGON);    
  106.     for(x=1.57;x<3.14;x+=0.01)
  107.     {        
  108.     glVertex2i (rx+2+30*sin(x), ry+30*cos(x));       
  109.     }          
  110.     glEnd ( );
  111.     glBegin (GL_POLYGON);
  112.     for(x=4.71;x<6.28;x+=0.01)
  113.     {        
  114.     glVertex2i (rx+30+2+30*sin(x), ry-30+30*cos(x));       
  115.     }          
  116.     glEnd ( );
  117.     glBegin (GL_POLYGON);    
  118.     for(x=3.14;x<4.71;x+=0.01)
  119.     {        
  120.     glVertex2i (rx-2+30*sin(x), ry+30*cos(x));       
  121.     }          
  122.     glEnd ( );
  123.     glBegin (GL_POLYGON);
  124.     for(x=0;x<1.57;x+=0.01)
  125.     {        
  126.     glVertex2i (rx-30-2+30*sin(x), ry-30+30*cos(x));       
  127.     }          
  128.     glEnd ( );
  129.     glBegin (GL_POLYGON);
  130.     for(x=4.5;x<6;x+=0.01)
  131.     {        
  132.     glVertex2i (rx+30+20*sin(x), ry+0+20*cos(x));       
  133.     }
  134.     glEnd ( );  
  135.     glBegin (GL_POLYGON);
  136.     for(x=1.36;x<2.86;x+=0.01)
  137.     {        
  138.     glVertex2i (rx+5+20*sin(x), ry+16+20*cos(x));       
  139.     }
  140.     glEnd ( );
  141.     glBegin (GL_POLYGON);
  142.     for(x=-4.5;x>-6;x-=0.01)
  143.     {        
  144.     glVertex2i (rx-30+20*sin(x), ry+20*cos(x));       
  145.     }
  146.     glEnd ( );  
  147.     glBegin (GL_POLYGON);
  148.     for(x=-1.36;x>-2.86;x-=0.01)
  149.     {        
  150.     glVertex2i (rx-5+20*sin(x), ry+16+20*cos(x));       
  151.     }
  152.     glEnd ( );
  153.     glEndList();
  154.     //yellow
  155.     glNewList(yellow, GL_COMPILE); 
  156.       glColor3f (1.0,1.0,0.0);//yellow
  157.       int yx=50,yy=150;
  158.      glBegin (GL_POLYGON);    
  159.     for(x=0;x<6.28;x+=0.01)
  160.     {        
  161.     glVertex2i (yx+45*sin(x), yy+45*cos(x));       
  162.     }          
  163.      glEnd ( );
  164.      glColor3f (1.0,0.8,0.0);  
  165.     for(int y=0;y<9;y+=1)
  166.     {
  167.     glBegin (GL_POLYGON);        
  168.     glVertex2i (yx+30*sin(x), yy+30*cos(x));
  169.     x+=6.28*3/8;
  170.     glVertex2i (yx+30*sin(x), yy+30*cos(x));
  171.     glVertex2i (yx, yy);
  172.     glEnd ( );       
  173.     }
  174.     glEndList();
  175.     //green
  176.     glNewList(green, GL_COMPILE); 
  177.     int gx=50,gy=150;
  178.       glColor3f (0.0,1.0,0.0);//green
  179.      glBegin (GL_POLYGON);    
  180.     for(x=0;x<6.28;x+=0.01)
  181.     {        
  182.     glVertex2i (gx+45*sin(x), gy+45*cos(x));       
  183.     }          
  184.      glEnd ( );
  185.      glColor3f (0.2,0.8,0.2);
  186.         
  187.      glBegin (GL_POLYGON);
  188.      for(x=1.57;x<3.14;x+=0.01)
  189.     {        
  190.     glVertex2i (gx+30*sin(x), gy+30+30*cos(x));       
  191.     }          
  192.      glEnd ( );
  193.      glBegin (GL_POLYGON);
  194.      for(x=4.71;x<6.28;x+=0.01)
  195.     {        
  196.     glVertex2i (gx+30+30*sin(x), gy+30*cos(x));       
  197.     }          
  198.      glEnd ( );
  199.         
  200.      glBegin (GL_POLYGON);    
  201.     for(x=0;x<1.57;x+=0.01)
  202.     {        
  203.     glVertex2i (gx+30*sin(x), gy-30+30*cos(x));       
  204.     }          
  205.      glEnd ( );
  206.      glBegin (GL_POLYGON);  
  207.      for(x=3.14;x<4.71;x+=0.01)
  208.     {        
  209.     glVertex2i (gx+30+30*sin(x), gy+30*cos(x));       
  210.     }          
  211.      glEnd ( );
  212.      
  213.      glBegin (GL_POLYGON);
  214.      for(x=1.57;x<3.14;x+=0.01)
  215.     {        
  216.     glVertex2i (gx-30+30*sin(x), gy+30*cos(x));       
  217.     }          
  218.      glEnd ( );
  219.      glBegin (GL_POLYGON);
  220.      for(x=4.71;x<6.28;x+=0.01)
  221.     {        
  222.     glVertex2i (gx+30*sin(x), gy-30+30*cos(x));       
  223.     }          
  224.      glEnd ( );
  225.        
  226.      glBegin (GL_POLYGON);
  227.      for(x=0;x<1.57;x+=0.01)
  228.     {        
  229.     glVertex2i (gx-30+30*sin(x), gy+30*cos(x));       
  230.     }          
  231.      glEnd ( );
  232.      glBegin (GL_POLYGON);  
  233.      for(x=3.14;x<4.71;x+=0.01)
  234.     {        
  235.     glVertex2i (gx+30*sin(x), gy+30+30*cos(x));       
  236.     }          
  237.      glEnd ( );
  238.     glEndList();
  239.     //blue
  240.     glNewList(blue, GL_COMPILE); 
  241.       glColor3f (0.0,1.0,1.0);//water
  242.       int wx=50,wy=150;
  243.      glBegin (GL_POLYGON);    
  244.     for(x=0;x<6.28;x+=0.01)
  245.     {        
  246.     glVertex2i (wx+45*sin(x), wy+45*cos(x));       
  247.     }          
  248.      glEnd ( );
  249.      glColor3f (0.0,0.6,0.8);
  250.      glBegin (GL_POLYGON);
  251.      for(x=-1.57;x<1.57;x+=0.01)
  252.     {        
  253.     glVertex2i (wx+30*sin(x), wy+7+30*cos(x));       
  254.     }          
  255.      glEnd ( );
  256.      
  257.      glColor3f (0.0,1.0,1.0);
  258.      glBegin (GL_POLYGON);
  259.      for(x=-1.57;x<1.57;x+=0.01)
  260.     {        
  261.     glVertex2i (wx-15+15*sin(x), wy+7+15*cos(x));       
  262.     }          
  263.      glEnd ( );
  264.      
  265.      glColor3f (0.0,0.6,0.8);
  266.      glBegin (GL_POLYGON);
  267.      for(x=1.57;x<=6;x+=0.01)
  268.     {        
  269.     glVertex2i (wx+15+15*sin(x), wy+7+15*cos(x));       
  270.     }          
  271.      glEnd ( );
  272.      
  273.      glColor3f (0.0,0.6,0.8);
  274.      
  275.      glBegin (GL_POLYGON);
  276.      for(x=1.57;x<4.71;x+=0.01)
  277.     {        
  278.     glVertex2i (wx+30*sin(x), wy-7+30*cos(x));       
  279.     }          
  280.      glEnd ( );
  281.      
  282.      glBegin (GL_POLYGON);
  283.      for(x=-2;x<2;x+=0.01)
  284.     {        
  285.     glVertex2i (wx-15+15*sin(x), wy-7+15*cos(x));       
  286.     }          
  287.      glEnd ( );
  288.      
  289.      glColor3f (0.0,1.0,1.0);
  290.      glBegin (GL_POLYGON);
  291.      for(x=1.57;x<4.73;x+=0.01)
  292.     {        
  293.     glVertex2i (wx+15+15*sin(x), wy-7+15*cos(x));       
  294.     }          
  295.      glEnd ( );
  296.     glEndList();
  297.     //purple
  298.     glNewList(purple, GL_COMPILE); 
  299.       glColor3f (0.0,0.0,1.0);//blue
  300.       int bx=50,by=150;
  301.      glBegin (GL_POLYGON);    
  302.     for(x=0;x<6.28;x+=0.01)
  303.     {        
  304.     glVertex2i (bx+45*sin(x), by+45*cos(x));       
  305.     }          
  306.      glEnd ( );
  307.      glBegin (GL_POLYGON);
  308.      glColor3f (0.5,0.1,0.9);
  309.      for(x=2.25;x<7.18;x+=0.01)
  310.     {        
  311.     glVertex2i (bx+30*sin(x), by+30*cos(x));       
  312.     }          
  313.      glEnd ( );
  314.      glBegin (GL_POLYGON);
  315.      glColor3f (0.0,0.0,1.0);
  316.      for(x=2.7;x<6.83;x+=0.01)
  317.     {        
  318.     glVertex2i (bx+15+20*sin(x), by+20*cos(x));       
  319.     }          
  320.      glEnd ( );
  321.     glEndList();
  322.     
  323.     glNewList(black, GL_COMPILE); 
  324.       glBegin(GL_POLYGON);
  325.         glColor3f(0.0, 0.0, 0.0);
  326.         for(i=0 ; i<360 ; i++)
  327.           glVertex2f(50.0+48.0*cos(i*PI/180), 150.0+48.0*sin(i*PI/180));
  328.       glEnd();
  329.     glEndList();
  330.     
  331.     int zerox=100,zeroy=50;
  332.     int onex=100,oney=50;
  333.     int twox=100,twoy=50;
  334.      int threex=100,threey=50;
  335.      int fourx=100,foury=50;
  336.      int fivex=100,fivey=50;
  337.      int sixx=100,sixy=50;
  338.      int sevenx=100,seveny=50;
  339.      int eightx=100,eighty=50;
  340.      int ninex=100,niney=50;
  341.     
  342.     glNewList(zero, GL_COMPILE);
  343.     
  344.     glColor3f (0.0,0.0,0.0);//zero
  345.      glBegin (GL_POLYGON);    
  346.     for(x=0;x<6.28;x+=0.01)
  347.     {        
  348.     glVertex2i (zerox+20*sin(x), zeroy+23*cos(x));       
  349.     }          
  350.      glEnd ( );
  351.      glColor3f (1.0,1.0,1.0);
  352.      glBegin (GL_POLYGON);    
  353.     for(x=0;x<6.28;x+=0.01)
  354.     {        
  355.     glVertex2i (zerox+9*sin(x), zeroy+12*cos(x));       
  356.     }          
  357.      glEnd ( );
  358.      glEndList();
  359.      
  360.      glNewList(one, GL_COMPILE);
  361.      
  362.      glColor3f (0.0,0.0,0.0);//one
  363.     glBegin (GL_POLYGON);
  364.     glVertex2i (onex-10, oney+5);             
  365.     glVertex2i (onex+5, oney+23);
  366.     glVertex2i (onex+5, oney+5);
  367.     glEnd ( );
  368.     glBegin (GL_POLYGON);
  369.     glVertex2i (onex+10, oney-15);
  370.     glVertex2i (onex+10, oney-23);
  371.     glVertex2i (onex-10, oney-23);
  372.     glVertex2i (onex-10, oney-15);
  373.     glEnd ( );
  374.     glBegin (GL_POLYGON);
  375.     glVertex2i (onex-5, oney-15);
  376.     glVertex2i (onex-5, oney+5);
  377.     glVertex2i (onex+5, oney+5);
  378.     glVertex2i (onex+5, oney-15);                
  379.     glEnd ( );
  380.     
  381.     glEndList();
  382.      
  383.      glNewList(two, GL_COMPILE);
  384.     
  385.     glColor3f (0.0,0.0,0.0);//two
  386.     glBegin (GL_POLYGON);
  387.     for(x=-1.57;x<1.57;x+=0.01)
  388.     {        
  389.     glVertex2i (twox+15*sin(x), twoy+23*cos(x));       
  390.     }
  391.     glEnd ( );
  392.     glColor3f (1.0,1.0,1.0);
  393.     glBegin (GL_POLYGON);
  394.     for(x=1.57;x>-1.57;x-=0.01)
  395.     {        
  396.     glVertex2i (twox+8*sin(x), twoy+11*cos(x));       
  397.     }
  398.     glEnd ( );
  399.     glColor3f (0.0,0.0,0.0);
  400.     glBegin (GL_POLYGON);
  401.     glVertex2i (twox+15*sin(1.57), twoy+23*cos(1.57));       
  402.     glVertex2i (twox-5, twoy-15);
  403.     glVertex2i (twox-15, twoy-15);
  404.     glVertex2i (twox+8*sin(1.2), twoy+11*cos(1.2));
  405.     glEnd ( );
  406.     glBegin (GL_POLYGON); 
  407.     glVertex2i (twox+15, twoy-15);
  408.     glVertex2i (twox+15, twoy-23);
  409.     glVertex2i (twox-15, twoy-23);
  410.     glVertex2i (twox-15, twoy-15);    
  411.     glEnd ( );
  412. glEndList();
  413.      
  414.      glNewList(three, GL_COMPILE);
  415.     
  416.     glColor3f (0.0,0.0,0.0);//three
  417.     glBegin (GL_POLYGON);
  418.     for(x=-1.57;x<1.57;x+=0.01)
  419.     {        
  420.     glVertex2i (threex+15*sin(x), threey+10+15*cos(x));       
  421.     }
  422.     glEnd ( );
  423.     glBegin (GL_POLYGON);
  424.     for(x=1.57;x<2.3;x+=0.01)
  425.     {        
  426.     glVertex2i (threex+15*sin(x), threey+10+15*cos(x));       
  427.     }
  428.     glVertex2i (threex, threey+10);
  429.     glEnd ( );
  430.     glBegin (GL_POLYGON);
  431.     
  432.     for(x=0.8;x<1.57;x+=0.01)
  433.     {        
  434.     glVertex2i (threex+15*sin(x), threey-10+15*cos(x));       
  435.     }
  436.     glVertex2i (threex, threey-10);
  437.     glEnd ( );
  438.     glBegin (GL_POLYGON);
  439.     
  440.     for(x=1.57;x<4.71;x+=0.01)
  441.     {        
  442.     glVertex2i (threex+15*sin(x), threey-10+15*cos(x));       
  443.     }
  444.     glEnd ( );
  445.     glBegin (GL_POLYGON);
  446.     glColor3f (1.0,1.0,1.0);
  447.     for(x=-1.57;x>-6.28;x-=0.01)
  448.     {        
  449.     glVertex2i (threex+5*sin(x), threey-10+5*cos(x));       
  450.     }
  451.     glEnd ( );
  452.     glColor3f (0.0,0.0,0.0);
  453.     glBegin (GL_POLYGON);
  454.     glVertex2i (threex-3, threey-3);
  455.     glVertex2i (threex-3, threey+3);
  456.     glVertex2i (threex+5*sin(2), threey+10+5*cos(2));
  457.     glVertex2i (threex+15*sin(2.3), threey+10+15*cos(2.3));
  458.     glEnd ( );
  459.     glBegin (GL_POLYGON);
  460.     glVertex2i (threex-3, threey-3);
  461.     glVertex2i (threex-3, threey+3);      
  462.     glVertex2i (threex+15*sin(2.3), threey+10+15*cos(2.3));
  463.     glVertex2i (threex+5*sin(1), threey-10+5*cos(1));  
  464.  
  465.     glEnd ( );
  466.     glColor3f (1.0,1.0,1.0);
  467.     glBegin (GL_POLYGON);   
  468.     for(x=3.14;x>-1.57;x-=0.01)
  469.     {        
  470.     glVertex2i (threex+5*sin(x), threey+10+5*cos(x));       
  471.     }
  472.     glEnd ( );
  473.     glEndList();
  474.      
  475.      glNewList(four, GL_COMPILE);   

  476.      glColor3f (0.0,0.0,0.0);//four
  477.      glBegin (GL_POLYGON);            
  478.     glVertex2i (fourx+5, foury+23);
  479.     glVertex2i (fourx+5, foury-23);
  480.     glVertex2i (fourx-3, foury-23);
  481.     glVertex2i (fourx-3, foury-15);
  482.     glVertex2i (fourx-20, foury-15);                
  483.     glEnd ( );
  484.     glBegin (GL_POLYGON);
  485.     glVertex2i (fourx+5, foury-8);
  486.     glVertex2i (fourx+13, foury-8);
  487.     glVertex2i (fourx+13, foury-15);
  488.     glVertex2i (fourx+5, foury-15);
  489.     glEnd ( );
  490.     glColor3f (1.0,1.0,1.0);
  491.     glBegin (GL_POLYGON);
  492.     glVertex2i (fourx-3, foury-8);
  493.     glVertex2i (fourx-3, foury+1);
  494.     glVertex2i (fourx-8, foury-8);
  495.     glEnd ( );
  496.     glEndList();
  497.      
  498.      glNewList(five, GL_COMPILE);
  499.     
  500.     glColor3f (0.0,0.0,0.0);//five
  501.      glBegin (GL_POLYGON);
  502.      
  503.      for(x=0;x<3.14;x+=0.01)
  504.     {        
  505.     glVertex2i (fivex+15*sin(x), fivey-10+15*cos(x));       
  506.     }
  507.     glEnd ( );  
  508.     glBegin (GL_POLYGON);
  509.     for(x=3.14;x<4.71;x+=0.01)
  510.     {        
  511.     glVertex2i (fivex+15*sin(x), fivey-10+15*cos(x));       
  512.     }
  513.     glVertex2i (fivex, fivey-10);
  514.     glEnd ( );
  515.     glColor3f (1.0,1.0,1.0);
  516.     glBegin (GL_POLYGON);
  517.      for(x=0;x>-6.28;x-=0.01)
  518.     {        
  519.     glVertex2i (fivex+5*sin(x), fivey-10+5*cos(x));       
  520.     }
  521.     glEnd ( );
  522.     glColor3f (0.0,0.0,0.0);  
  523.     glBegin (GL_POLYGON);
  524.     glVertex2i (fivex-15, fivey-5);
  525.     glVertex2i (fivex-15, fivey+20);
  526.     glVertex2i (fivex-5, fivey+20);
  527.     glVertex2i (fivex-5, fivey-5);  
  528.     glEnd ( );  
  529.     glBegin (GL_POLYGON);
  530.     glVertex2i (fivex+13, fivey+20);
  531.     glVertex2i (fivex+13, fivey+10);
  532.     glVertex2i (fivex-5, fivey+10);
  533.     glVertex2i (fivex-5, fivey+20);
  534.     glEnd ( );  
  535.     glBegin (GL_POLYGON);
  536.     glVertex2i (fivex-5, fivey-5);
  537.     glVertex2i (fivex-5, fivey+5);
  538.     glVertex2i (fivex, fivey+5);
  539.     glVertex2i (fivex, fivey-5);                             
  540.     glEnd ( );
  541.     glEndList();
  542.      
  543.      glNewList(six, GL_COMPILE);
  544.     
  545.     glBegin (GL_POLYGON);//six  
  546.     glColor3f (0.0,0.0,0.0); 
  547.     for(x=0.1;x<6.9;x+=0.01)
  548.     {        
  549.     glVertex2i (sixx+15*sin(x), sixy-10+15*cos(x));       
  550.     }  
  551.     glEnd ( );
  552.     glBegin (GL_POLYGON);
  553.     glVertex2i (sixx+10, sixy+20);
  554.     glVertex2i (sixx+15*sin(0.1), sixy-10+15*cos(0.1));
  555.     glVertex2i (sixx+15*sin(5.2), sixy-10+15*cos(5.2));
  556.     glVertex2i (sixx, sixy+20);  
  557.     glEnd ( );  
  558.     glColor3f (1.0,1.0,1.0);
  559.     glBegin (GL_POLYGON);
  560.      for(x=0;x>-6.28;x-=0.01)
  561.     {        
  562.     glVertex2i (sixx+5*sin(x), sixy-10+5*cos(x));       
  563.     }
  564.     glEnd ( );
  565.     glEndList();
  566.      
  567.      glNewList(seven, GL_COMPILE);
  568.     
  569.     glColor3f (0.0,0.0,0.0);
  570.     glBegin (GL_POLYGON);//seven   
  571.     glVertex2i (sevenx-10, seveny-23);
  572.     glVertex2i (sevenx, seveny-23);   
  573.     glVertex2i (sevenx+15, seveny+19);
  574.     glVertex2i (sevenx+3, seveny+10);
  575.     glEnd ( );
  576.     glBegin (GL_POLYGON);
  577.     glVertex2i (sevenx+15, seveny+19);
  578.     glVertex2i (sevenx-15, seveny+19);
  579.     glVertex2i (sevenx-15, seveny+10);
  580.     glVertex2i (sevenx+3, seveny+10);
  581.     glEnd ( );
  582.     
  583.     glEndList();
  584.      
  585.      glNewList(eight, GL_COMPILE);
  586.     
  587.     glColor3f (0.0,0.0,0.0);//eight
  588.     glBegin (GL_POLYGON);
  589.     for(x=-2.3;x<2.3;x+=0.01)
  590.     {        
  591.     glVertex2i (eightx+15*sin(x), eighty+10+15*cos(x));       
  592.     }
  593.     for(x=0.8;x<5.48;x+=0.01)
  594.     {        
  595.     glVertex2i (eightx+15*sin(x), eighty-10+15*cos(x));       
  596.     }
  597.     glEnd ( );
  598.     glBegin (GL_POLYGON);
  599.     glColor3f (1.0,1.0,1.0);
  600.     for(x=0;x>-6.28;x-=0.01)
  601.     {        
  602.     glVertex2i (eightx+5*sin(x), eighty-10+5*cos(x));       
  603.     }
  604.     glEnd ( );
  605.     glBegin (GL_POLYGON); 
  606.     for(x=4.71;x>-1.57;x-=0.01)
  607.     {        
  608.     glVertex2i (eightx+5*sin(x),eighty+10+5*cos(x));       
  609.     }
  610.     glEnd ( );
  611.     
  612.     glEndList();
  613.      
  614.      glNewList(nine, GL_COMPILE);
  615.     
  616.     glColor3f (0.0,0.0,0.0);//nine
  617.     glBegin (GL_POLYGON);  
  618.     for(x=3.24;x<8.6;x+=0.01)
  619.     {        
  620.     glVertex2i (ninex+15*sin(x), niney+10+15*cos(x));       
  621.     }  
  622.     glEnd ( );
  623.     glBegin (GL_POLYGON);
  624.     glVertex2i (ninex-10, niney-20);
  625.     glVertex2i (ninex+15*sin(3.24), niney+10+15*cos(3.24));
  626.     glVertex2i (ninex+15*sin(8.34), niney+10+15*cos(8.34));
  627.     glVertex2i (ninex, niney-20);  
  628.     glEnd ( );  
  629.     glColor3f (1.0,1.0,1.0);
  630.     glBegin (GL_POLYGON);
  631.      for(x=0;x>-6.28;x-=0.01)
  632.     {        
  633.     glVertex2i (ninex+5*sin(x), niney+10+5*cos(x));       
  634.     }
  635.     glEnd ( );
  636.     glEndList();
  637.      

  638.      
  639.       
  640.     
  641.     //棋盤 
  642.     glNewList(broad, GL_COMPILE);
  643.       
  644.        n=rand()%10;
  645.        for(int j=0;j<n;j++)
  646.        {             
  647.        i=rand()%10;                    
  648.        a=rand()%1000;
  649.        b=rand()%200;
  650.        c=rand()%600;
  651.        glColor3fv(color[i]);
  652.        
  653.                                                  
  654.        for(int y=0;y<9;y+=1)
  655.        {
  656.          glBegin (GL_POLYGON);        
  657.          glVertex2i (a+30*sin(x), c+30*cos(x));
  658.          x+=6.28*2/5;
  659.          glVertex2i (a+30*sin(x), c+30*cos(x));
  660.          glVertex2i (a, c);
  661.          glEnd ();       
  662.        }
  663. /*
  664.             for(temp_1=0 ; temp_1<6.28 ; temp_1+=0.01)
  665.             {
  666.               x=a+b*cos(temp_1);
  667.               y=c+b*sin(temp_1);
  668.               glVertex2f(x,y);
  669.             }
  670.             glEnd();
  671.             */                                   
  672.          
  673.        }
  674.        glBegin(GL_POLYGON);
  675.         glColor3f(0.0, 0.0, 0.0);
  676.         glVertex2i(0, 0);
  677.         glVertex2i(500, 0);
  678.         glVertex2i(500, 500);
  679.         glVertex2i(0, 500);
  680.       glEnd();
  681.       //背景白點
  682.       /*                 
  683.       glBegin(GL_POINTS);
  684.         glColor3f(1.0, 1.0, 1.0);
  685.         glPointSize(3);
  686.         for(i=0 ; i<1000 ; i++)
  687.           glVertex2i( rand()%500, rand()%500);
  688.       glEnd();
  689.       */
  690.       //row 
  691.       glColor3f(0.7, 0.7, 0.0);
  692.       glLineWidth(5);
  693.       for(row=0 ; row<=5 ; row++){
  694.         glBegin(GL_LINES);
  695.           glVertex2i(0, 0+100*row);
  696.           glVertex2i(500, 0+100*row);
  697.         glEnd();
  698.       }
  699.       //col 
  700.       for(col=0 ; col<=5 ; col++){
  701.         glBegin(GL_LINES);
  702.           glVertex2i(0+100*col, 0);
  703.           glVertex2i(0+100*col, 500);
  704.         glEnd();
  705.       }
  706.       
  707.     glEndList();
  708.     glPushMatrix();
  709.     glTranslatef(25.0, 25.0, 0.0);
  710.     glCallList(broad);
  711.     glPopMatrix();    
  712.     
  713.     //框框 
  714.     glNewList(frame,GL_COMPILE);
  715.     glColor3f(1.0,1.0,1.0);
  716.     glBegin(GL_LINES);
  717.     glVertex2f(0,0);
  718.     glVertex2f(0,30);
  719.     
  720.     glVertex2f(0,0);
  721.     glVertex2f(30,0);
  722.     
  723.     glVertex2f(100,100);
  724.     glVertex2f(100,70);
  725.     
  726.     glVertex2f(100,100);
  727.     glVertex2f(70,100);
  728.     
  729.     glVertex2f(0,100);
  730.     glVertex2f(0,70);
  731.     
  732.     glVertex2f(0,100);
  733.     glVertex2f(30,100);
  734.     
  735.     glVertex2f(100,0);
  736.     glVertex2f(70,0);
  737.     
  738.     glVertex2f(100,0);
  739.     glVertex2f(100,30);
  740.     
  741.     glEnd();
  742.     glEndList();
  743.         
  744.   //畫框框 
  745.     if(stayX >0 && stayY >0)//欲交換的位置 
  746.     {   
  747.         glPushMatrix();
  748.         glTranslatef(25, 25, 0.0);
  749.         glTranslatef(stayX, stayY, 0.0);
  750.         glCallList(frame);
  751.         glPopMatrix();
  752.              }
  753.              
  754.         glPushMatrix(); 
  755.         glTranslatef(25, 25, 0.0);
  756.         glTranslatef(frameX, frameY, 0.0);
  757.         glCallList(frame);
  758.         glPopMatrix();
  759.     
  760.         //畫出目前局勢 
  761.         glPushMatrix();
  762.         glTranslatef(25, 25, 0.0);
  763.         glTranslatef(0.0, 300.0, 0.0);
  764.         for(i=0 ; i<25 ; i++)
  765.         {
  766.             glCallList(CC[i]);
  767.             glTranslatef(100.0, 0.0, 0.0);
  768.             if(i%5==4)
  769.                 glTranslatef(-500.0, -100.0, 0.0);
  770.         }
  771.         
  772.             //分數 
  773.             glTranslated(800, 445, 0);
  774.             scoretemptwo=score;
  775.             for(int w=0;w<=6;w++)
  776.             {
  777.             scoretemp=score%10;
  778.             scoretemp=scoretemp+20;
  779.             glCallList(scoretemp);
  780.             score=score/10;
  781.             glTranslated(-50, 0, 0);
  782.             }
  783.             score=scoretemptwo;
  784.             
  785.              
  786.         
  787.        
  788.         glPopMatrix();
  789.         
  790.         
  791.         
  792.     glFlush ( );
  793. }
  794. void Keyboard(unsigned char key,int x,int y)
  795. {    
  796.      if(state==0){ 
  797.       switch(key)
  798.       {
  799.         case 'w':
  800.         case 'W':
  801.             if(frameY <=300){
  802.                 frameY=frameY+100;
  803.                 testcin0 -=5;
  804.             }
  805.             random();
  806.             glutPostRedisplay();
  807.             break;
  808.         case 's':
  809.         case 'S':
  810.             if(frameY >=100){
  811.                 frameY=frameY-100;
  812.                 testcin0 +=5;
  813.             }
  814.             random();
  815.             glutPostRedisplay();
  816.             break;
  817.         case 'a':
  818.         case 'A':
  819.             if(frameX >=100){
  820.                 frameX=frameX-100;
  821.                 testcin0--;
  822.             }
  823.             random();
  824.             glutPostRedisplay();
  825.             break;
  826.         case 'd':
  827.         case 'D':
  828.             if(frameX <=300){
  829.                 frameX=frameX+100;
  830.                 testcin0++;
  831.             }
  832.             random();
  833.             glutPostRedisplay();
  834.             break;
  835.         case 'r':
  836.         case 'R':
  837.             reset();
  838.             random();
  839.             glutPostRedisplay();
  840.             break;
  841.         case 13:
  842.             source=CC[testcin0];
  843.             stayX=frameX;
  844.             stayY=frameY;
  845.             
  846.             break;
  847.         default:
  848.             break;
  849.       }//SWITCH
  850.       
  851.       if(source>=1 && source<=5) {
  852.       
  853.                 temp=testcin0;
  854.                 state=1;
  855.                 
  856.                 } 
  857.    }//STATE0   
  858.     else if(state ==1){//state1
  859.     switch(key)
  860.       {
  861.         case 'w':
  862.         case 'W':
  863.             if(frameY <=300){
  864.                 frameY=frameY+100;
  865.                 testcin0 -=5;
  866.             }
  867.             glutPostRedisplay();
  868.             break;
  869.         case 's':
  870.         case 'S':
  871.             if(frameY >=100){
  872.                 frameY=frameY-100;
  873.                 testcin0 +=5;
  874.             }
  875.             glutPostRedisplay();
  876.             break;
  877.         case 'a':
  878.         case 'A':
  879.             if(frameX >=100){
  880.                 frameX=frameX-100;
  881.                 testcin0--;
  882.             }
  883.             glutPostRedisplay();
  884.             break;
  885.         case 'd':
  886.         case 'D':
  887.             if(frameX <=300){
  888.                 frameX=frameX+100;
  889.                 testcin0++;
  890.             }
  891.             glutPostRedisplay();
  892.             break;
  893.         case 'r':
  894.         case 'R':
  895.             reset();
  896.             glutPostRedisplay();
  897.             break;
  898.         case 13:
  899.             destination=CC[testcin0];
  900.            
  901.             break;
  902.         default:
  903.             break;
  904.       }//SWITCH
  905. /*      
  906.       if(destination == 0 ){//移動
  907.                 if(testcin1 == testcin0+1 || testcin1 == testcin0-1 || testcin1 == testcin0+5 || testcin1 == testcin0-5)
  908.                 {//位置合理(十字位置) 
  909.                     CC[testcin1] = source;
  910.                     CC[testcin0] = 0;
  911.                     state=1;
  912.                     testcin0=testcin1;
  913.                     destination=100;
  914.                     source =100;
  915.                     //player = 0;
  916.                     glutPostRedisplay();
  917.                 }else{//位置不合理 
  918.                     state =1;
  919.                     destination=100;
  920.                     source =100;
  921.                     testcin0 = testcin1;
  922.                 }
  923.                 
  924.                 }
  925.       else if (destination >5)
  926.       {//選擇錯誤
  927.                 state=1;
  928.                 destination=100;
  929.                 source =100;
  930.       }
  931. */                
  932.       if( destination>=1 && destination<=5) {
  933.                 CC[testcin0]=source;
  934.                 CC[temp]=destination;              
  935.                 state=0;
  936.                 source=0;
  937.                 destination=0;
  938.                 stayX=0;
  939.                 stayY=0;
  940.                 check();         
  941.                 glutPostRedisplay();
  942.                 } 
  943.       }//state1
  944. }
  945. int main(int argc, char *argv[])
  946. {
  947.     srand(time(0));
  948.     glutInit (&argc, argv);                         // Initialize GLUT.
  949.     glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);   // Set display mode.
  950.     glutInitWindowPosition (0, 0);   // Set top-left display-window position.
  951.     glutInitWindowSize (1000, 600);      // Set display-window width and height.
  952.     glutCreateWindow ("candy crush 方向=wsad & 確認=enter & 重玩按r");      // Create display window.

  953.     init ();                            // Execute initialization procedure.
  954.     glutDisplayFunc (display);       // Send graphics to display window.
  955.     glutKeyboardFunc(Keyboard);
  956.     glutMainLoop ( );
  957. }
  958. void reset()
  959. {
  960.     int n;
  961.     //變數初始值 
  962.    // player=-1;
  963.    // state=-1;
  964.     source=100;
  965.     destination=100;
  966.     testcin0=0;
  967.     testcin1=0;
  968.     //win=10;
  969.     frameX=0;
  970.     frameY=400;
  971.     //red=16;
  972.     //black=16;
  973.     for(int i=0; i<25; i++)
  974.       AA[i]=0;
  975.     
  976.     //shuffle
  977.     for (int i=0; i<25; i++) {
  978.         n=rand()%5+1;
  979.         CC[i]=n;
  980.         }//shuffle end 
  981.         
  982.      check();
  983.      random();
  984.     
  985. }

  986. void check()
  987. {
  988.      int i,map[25];
  989.      
  990.      for(i=0;i<25;i++)//初始化地圖值=0 
  991.      {
  992.          map[i]=0;
  993.      }
  994. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////        
  995.      for(i=1;i<=3;i++)//判斷顏色一樣的地圖值=1 
  996.      {
  997.          if(CC[i]==CC[i-1] && CC[i]==CC[i+1])
  998.          {
  999.              map[i-1]=1;
  1000.              map[i]=1;
  1001.              map[i+1]=1;
  1002.          }
  1003.      }
  1004.      for(i=6;i<=8;i++)
  1005.      {
  1006.          if(CC[i]==CC[i-1] && CC[i]==CC[i+1])
  1007.          {
  1008.              map[i-1]=1;
  1009.              map[i]=1;
  1010.              map[i+1]=1;
  1011.          }
  1012.      }
  1013.      for(i=11;i<=13;i++)
  1014.      {
  1015.          if(CC[i]==CC[i-1] && CC[i]==CC[i+1])
  1016.          {
  1017.              map[i-1]=1;
  1018.              map[i]=1;
  1019.              map[i+1]=1;
  1020.          }
  1021.      }
  1022.      for(i=16;i<=18;i++)
  1023.      {
  1024.          if(CC[i]==CC[i-1] && CC[i]==CC[i+1])
  1025.          {
  1026.              map[i-1]=1;
  1027.              map[i]=1;
  1028.              map[i+1]=1;
  1029.          }
  1030.      }
  1031.      for(i=21;i<=23;i++)
  1032.      {
  1033.          if(CC[i]==CC[i-1] && CC[i]==CC[i+1])
  1034.          {
  1035.              map[i-1]=1;
  1036.              map[i]=1;
  1037.              map[i+1]=1;
  1038.          }
  1039.      }     
  1040.      for(i=5;i<=9;i++)
  1041.      {
  1042.          if(CC[i]==CC[i-5] && CC[i]==CC[i+5])
  1043.          {
  1044.              map[i-5]=1;
  1045.              map[i]=1;
  1046.              map[i+5]=1;
  1047.          }
  1048.      }
  1049.      for(i=10;i<=14;i++)
  1050.      {
  1051.          if(CC[i]==CC[i-5] && CC[i]==CC[i+5])
  1052.          {
  1053.              map[i-5]=1;
  1054.              map[i]=1;
  1055.              map[i+5]=1;
  1056.          }
  1057.      }
  1058.      for(i=15;i<=19;i++)
  1059.      {
  1060.          if(CC[i]==CC[i-5] && CC[i]==CC[i+5])
  1061.          {
  1062.              map[i-5]=1;
  1063.              map[i]=1;
  1064.              map[i+5]=1;
  1065.          }
  1066.      }
  1067. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////     
  1068.      for(i=0;i<25;i++)//地圖值=1的變黑色 
  1069.      {
  1070.          if(map[i]==1)
  1071.          {
  1072.              CC[i]=0;
  1073.              score+=10;
  1074.              
  1075.          } 
  1076.      }
  1077.      for(i=0;i<25;i++)//重新初始化地圖值=0 
  1078.      {
  1079.          map[i]=0;
  1080.      }     
  1081. }

  1082. void random()
  1083. {
  1084.      
  1085.     int i,a=1;
  1086.    
  1087.     while(a>0)
  1088.     {
  1089.     
  1090.       for(i=0;i<25;i++)
  1091.       {
  1092.          if(CC[i]==0)
  1093.          {
  1094.              CC[i]=rand()%5+1;        
  1095.          }     
  1096.       }
  1097.       
  1098.       check();
  1099.       a=0;
  1100.       for(i=0;i<25;i++)
  1101.       {
  1102.         if(CC[i]==0)
  1103.         {
  1104.            a++;         
  1105.         }
  1106.       }
  1107.       
  1108.       
  1109.   }
  1110.     
  1111.     
  1112. }


2015年1月21日 星期三

期末專題報告

使用python實作如何計算圖像的相似度。
程式碼:
閱讀更多 »

2014年12月4日 星期四

HW1_3_對原有圖形進行座標方向的修改

from turtle import *

def main():

    reset()
    Screen()
    up()
    width(25)
    color("red3")
    goto(0,-170)
    down()
    circle(170)


    up()
    width(25)
    color("orange")
    goto(0,-170)
    down()
    circle(140)

    up()
    width(25)
    color("yellow")
    goto(0,-170)
    down()
    circle(110)

    up()
    width(25)
    color("seagreen4")
    goto(0,-170)
    down()
    circle(80)
   
    up()
    width(25)
    color("orchid4")
    goto(0,-170)
    down()
    circle(50)
   
    up()
    width(25)
    color("royalblue1")
    goto(0,-170)
    down()
    circle(20)

    color("dodgerblue4")
    goto(0,-300) # vanish if hideturtle() is not available ;-)

    up()
    goto(-50,250);
    write("彩虹扇子完成~", font= 50)

   
    return "Done!"

if __name__ == "__main__":
    main()
    mainloop()

HW01_2_更改圖形變化方向(發散)及色彩

from turtle import Screen, Turtle, mainloop
from time import clock, sleep

def mn_eck(p, ne,sz):
    turtlelist = [p]
    #create ne-1 additional turtles
    for i in range(1,ne):
        q = p.clone()
        q.rt(180.0/ne)
        turtlelist.append(q)
        p = q
    for i in range(ne):
        c = abs(ne/2.0-i)/(ne*.7)
        # let those ne turtles make a step
        # in parallel:
        for t in turtlelist:
            t.rt(360./ne)
            t.pencolor(1-c,0,c)
            t.fd(sz)
    for i in range(1,ne):
        q = p.clone()
        q.rt(90.0/ne)
        turtlelist.append(q)
        p = q
    for i in range(ne):
        c = abs(ne/2.0-i)/(ne*.7)
        # let those ne turtles make a step
        # in parallel:
        for t in turtlelist:
            t.rt(180./ne)
            t.pencolor(1-c,0,c)
            t.fd(sz)

def main():
    s = Screen()
    s.bgcolor("purple")
    p=Turtle()
    p.speed(0)
    p.hideturtle()
    p.pencolor("black")
    p.pensize(3)

    s.tracer(36,0)

    at = clock()
    mn_eck(p, 36, 19)
    et = clock()
    z1 = et-at

    sleep(1)

    at = clock()
    while any([t.undobufferentries() for t in s.turtles()]):
        for t in s.turtles():
            t.undo()
    et = clock()
    return "runtime: %.3f sec" % (z1+et-at)


if __name__ == '__main__':
    msg = main()
    print(msg)
    mainloop()

HW01_1_改變圖形及其個數與色彩大小等

from turtle import *

def stop_running():
    global running
 
    running= False

def create_compound_shape(basic_shape= 'square'):
    '''
    由基本形狀,製造出複合形狀。
    '''
    shape(basic_shape)  

    f=   .7 # fraction
    a=   10 # angle
    s=    6 # size
    c=    1 # color
     
    sh= Shape("compound")
 
    #
    # 用 8個 基本形狀 結合成 1個 複合形狀
    #
    for i in range(8):
     
        shapesize(s)
        tilt(a)
     
        p=  get_shapepoly()

        c1= (  c, 0.2, 1-c)
        c2= (1-c, 0.8,   c)
     
        sh.addcomponent(p, c1, c2)
     
        s *= f
        c *= f

    return sh

def create_dancers():

    global dancers

    clearscreen()
    bgcolor("blue") # 背景色。
    tracer(False)
 
    sh= create_compound_shape()
    register_shape('dancer', sh)
    shape('dancer')
 
    pu(); goto(0, -200)
 
    dancers= []
    for i in range(180):
        fd(8); lt(2); tilt(4)

        if i % 10 == 0:
            d=     clone()
            dancers += [d]

    home()

    update()


def dancers_running():
 
    global running
    global dancers
 
    running= True
 
    t= 0
    while running:

     
        for d in dancers:
            d.fd(8); d.lt(2); d.tilt(4)

   
        rt(4)    

   
        t %= 100
        s= t if t<50 else (100-t)
        if s==0: s += .1

        shapesize(s)

        t += .1

        update()
     
def write_mesage():
    '''
    按鍵說明。
    '''
    pencolor('white')
 
    goto(-300,300);
    write("Key-x:     start, 開始", font= 30)
 
    goto(-300,280);
    write("Key-x: stop, 停", font= 30)

    pencolor('black')

    goto(-70,0);
    write("轉吧轉吧八彩正方形", font= 30)
 
    home()

def main():
    global running, dancers

    create_dancers()

    write_mesage()
 
    onkey(dancers_running,'z')     # 用 'z' 鍵 來開始
    onkey(stop_running,   'xx') # 用 'x' 來停止

    listen()
 
    return "DONE!"


if __name__=='__main__':

    msg= main()
    print(msg)
    mainloop()