1 #include <GL/glut.h>
2 #include <ctime>
3
4 #define MULTIPLIER 0x015a4e35L
5 #define INCREMENT 1
6
7 static const unsigned char CD_UP = 24;
8 static const unsigned char CD_DOWN = 25;
9 static const unsigned char CD_RIGHT = 26;
10 static const unsigned char CD_LEFT = 27;
11
12 static const int WindowWidth = 640;
13 static const int WindowHeight = 480;
14
15 static Bottle* bottle = NULL;
16 static Capsule* capsule = NULL;
17
18 enum Connect
19 {
20 LEFT, RIGHT, UP, DOWN, NOT_CONNECTED
21 };
22
23 enum Color
24 {
25 YELLOW, BLUE, RED, BLACK
26 };
27
28 enum GridType
29 {
30 CAPSULE, WORM, NOTHING
31 };
32
33 class Bottle
34 {
35 private:
36 GridType b_grid_type[16][8];
37 Color b_color[16][8];
38 int b_x,b_y;
39 int b_seed;
40 public:
41 Bottle( int seed, int level, int xx, int yy )
42 {
43 b_seed = seed;
44 b_x = xx;
45 b_y = yy;
46 for ( int i = 0; i < 16; ++i )
47 {
48 for ( int j = 0; j < 8; ++j )
49 {
50 b_grid_type[i][j] = NOTHING;
51 b_color[16][8] = BLACK;
52 }
53 }
54 for ( int i = 0; i < level*4; ++i )
55 {
56 int x = myRand()%8;
57 int y = myRand()%16;
58 Color color = myRand()%3;
59 if ( ok( x, y, color, level ) )
60 {
61 b_grid_type[y][x] = WORM;
62 b_color[y][x] = color;
63 }
64 else
65 --i;
66 }
67 }
68 int myRand( void )
69 {
70 seed = MULTIPLIER*seed + INCREMENT;
71 return ((int)(seed>>16) & 0x7fff);
72 }
73 bool ok( int x, int y, Color color, int level )
74 {
75 if ( level < 15 && y <= 6 ) return false;
76 if ( ((level+1)>>1)+3+y < 16 ) return false;
77 if ( x-2 >= 0 && b_grid_type[x-1][y] == WORM && b_color[x-1][y] == color
78 && b_grid_type[x-2][y] == WORM && b_color[x-2][y] == color ) return false;
79 if ( x+2 < 8 && b_grid_type[x+1][y] == WORM && b_color[x+1][y] == color
80 && b_grid_type[x+2][y] == WORM && b_color[x+2][y] == color ) return false;
81 if ( y+2 < 16 && b_grid_type[x][y+1] == WORM && b_color[x][y+1] == color
82 && b_grid_type[x][y+2] == WORM && b_color[x][y+2] == color ) return false;
83 if ( b_grid_type[x][y-1] == WORM && b_color[x][y-1] == color
84 && b_grid_type[x][y-2] == WORM && b_color[x][y-2] == color ) return false;
85 return true;
86 }
87 };
88
89 class Capsule
90 {
91 private:
92 int c_x,c_y;
93 Connect c_connect;
94 Color C_color;
95 public:
96 Capsule( int xx, int yy, Connect con, int color)
97 {
98 c_x = xx; c_y = yy; c_connect = con; c_color = color;
99 }
100 };
101
102 void draw( void )
103 {
104 bottle->display();
105 }
106
107 void display( void )
108 {
109 glClear( GL_COLOR_BUFFER_BIT );
110 draw();
111 glutSwapBuffers();
112 }
113
114 void key( unsigned char key_char, int not_use, int not_use_1 )
115 {
116 capsule->setDirect( key_char );
117 }
118
119 int main( int argc, char * argv[] )
120 {
121 glutInit( &argc, argv );
122 glutInitDisplayMode( GLUT_RGBA|GLUT_DOUBLE );
123 glutInitWindowPosition( 50, 50 );
124 glutInitWindowSize( WindowWidth, WindowHeight );
125 glutCreateWindow( "Dr Mario By PureMilk" );
126 glutDisplayFunc( display );
127 glutKeyboardFunc( key );
128 glutTimerFunc( 250, timer, 0 );
129 gluOrtho2D( 0, WindowWidth, WindowHeight, 0 );
130 glutMainLoop();
131
132 return 0;
133 }
我是C++盲……