1 #include <GL/glut.h>
2 #include <ctime>
3 #include <cstdlib>
4
5 static const int WindowWidth = 640;
6 static const int WindowHeight = 480;
7
8 int count = 0;
9 int speed = 4;
10
11 enum GridType
12 {
13 NOTHING, CAPSULE, WORM
14 };
15
16 enum Connect
17 {
18 NOT_CONNECTED, LEFT, RIGHT, UP, DOWN
19 };
20
21 enum Color
22 {
23 BLACK, YELLOW, BLUE, RED
24 };
25
26 enum DoctorDirect
27 {
28 DD_NODIRECT, DD_UP, DD_DOWN, DD_LEFT, DD_RIGHT
29 };
30
31
32 class Bottle
33 {
34 private:
35 int b_x,b_y; //bottle的位置
36 GridType b_grid_type[16][8];
37 Color b_color[16][8];
38 Connect b_connect[16][8];
39 public:
40 void drawBottle( void )
41 {
42 //画瓶子的外壳
43 glColor3f( 0.0f, 1.0f, 1.0f );
44 glLineWidth(1);
45 lineUpDown( 220, 160, 26*16 );
46 //画瓶子的内部,根据b_x,b_y和瓶子的内容物
47 }
48
49 void lineUpDown( int x, int y0, int yEnd )
50 {
51 glBegin( GL_LINES );
52 glVertex2i( x, y0 );
53 glVertex2i( x, yEnd );
54 glEnd();
55 }
56
57 Bottle( int level, int xx, int yy )
58 {
59 int i; //vc6这点真是恨死人
60 b_x = xx;
61 b_y = yy;
62 for ( i = 0; i < 16; ++i )
63 {
64 for ( int j = 0; j < 8; ++j )
65 {
66 b_grid_type[i][j] = NOTHING;
67 b_color[i][j] = BLACK;
68 b_connect[i][j] = NOT_CONNECTED;
69 }
70 }
71 for ( i = 0; i < level*4; ++i )
72 {
73 int x = rand()%16;
74 int y = rand()%8;
75 Color color = (Color)(rand()%3);
76 if ( ok( x, y, color, level ) )
77 {
78 b_grid_type[x][y] = WORM;
79 b_color[x][y] = color;
80 }
81 else
82 --i;
83 }
84 }
85 bool ok( int x, int y, Color color, int level )
86 {
87 if ( level < 15 && x <= 6 ) return false;
88 if ( ((level+1)>>1)+3+x < 16 ) return false;
89 if ( x-2 >= 0 && b_grid_type[x-1][y] == WORM && b_color[x-1][y] == color
90 && b_grid_type[x-2][y] == WORM && b_color[x-2][y] == color ) return false;
91 if ( x+2 < 8 && b_grid_type[x+1][y] == WORM && b_color[x+1][y] == color
92 && b_grid_type[x+2][y] == WORM && b_color[x+2][y] == color ) return false;
93 if ( y+2 < 16 && b_grid_type[x][y+1] == WORM && b_color[x][y+1] == color
94 && b_grid_type[x][y+2] == WORM && b_color[x][y+2] == color ) return false;
95 if ( b_grid_type[x][y-1] == WORM && b_color[x][y-1] == color
96 && b_grid_type[x][y-2] == WORM && b_color[x][y-2] == color ) return false;
97 return true;
98 }
99 };
100
101 class Doctor
102 {
103 private:
104 int d_x,d_y; //活动药丸(左下角)相对于瓶子的位置
105 bool d_state; //横的0,竖的1
106 Color d_color[2]; //俩药丸的颜色
107 DoctorDirect d_direct;
108 public:
109 void drawDoctor( void )
110 {
111 //画医生的形象
112 //画活动药丸
113 }
114 Doctor( void )
115 {
116 //确定初始位置
117 //随机生成两药丸颜色
118 }
119 void move( void )
120 {
121 //伪码就先不写了,我纸上写了一堆,思路很清晰
122 }
123 void fall( void )
124 {
125 d_direct = DD_DOWN;
126 move();
127 }
128 void setDirect( DoctorDirect direct )
129 {
130 d_direct = direct;
131 }
132 };
133
134 static Bottle* bottle = NULL;
135 static Doctor* doctor = NULL;
136
137 void init( void )
138 {
139 bottle = new Bottle( 3, 110, 40 );
140 doctor = new Doctor();
141 }
142
143 void key( unsigned char key_char, int not_use, int not_use_1 )
144 {
145 switch ( key_char )
146 {
147 case 'w':
148 case 'W':
149 doctor->setDirect( DD_UP );
150 break;
151 case 's':
152 case 'S':
153 doctor->setDirect( DD_DOWN );
154 break;
155 case 'a':
156 case 'A':
157 doctor->setDirect( DD_LEFT );
158 break;
159 case 'd':
160 case 'D':
161 doctor->setDirect( DD_RIGHT );
162 break;
163 default:
164 doctor->setDirect( DD_NODIRECT );
165 }
166 }
167
168 bool dead( void )
169 {
170 return false;
171 }
172
173 void step( void )
174 {
175 if ( dead() ) return ;
176 //还没写完
177 ++count; if ( count == speed ) { count = 0; doctor->fall(); }
178 }
179
180 void timer( int not_use )
181 {
182 step();
183 glutPostRedisplay();
184 if ( !dead() )
185 {
186 glutTimerFunc( 1000/speed, timer, 0 );
187 }
188 }
189
190 void drawBG( void )
191 {
192 int i,j;
193 glColor3f( 0.0f, 0.0f, 0.7f );
194 for ( i = 0; i < 32; ++i )
195 {
196 for ( j = 0; j < 30; ++j )
197 {
198 if ( ((i+j)&1) == 0 )
199 //画方块
200 }
201 }
202 }
203
204 void draw( void )
205 {
206 drawBG();
207 bottle->drawBottle();
208 doctor->drawDoctor();
209 }
210
211 void display( void )
212 {
213 glClear( GL_COLOR_BUFFER_BIT );
214 draw();
215 glutSwapBuffers();
216 }
217
218 int main( int argc, char * argv[] )
219 {
220 srand( (unsigned int)time(NULL) );
221 glutInit( &argc, argv );
222 glutInitDisplayMode( GLUT_RGBA|GLUT_DOUBLE );
223 glutInitWindowPosition( 50, 50 );
224 glutInitWindowSize( WindowWidth, WindowHeight );
225 glutCreateWindow( "Dr Mario By PureMilk" );
226 glutDisplayFunc( display );
227 glutKeyboardFunc( key );
228 glutTimerFunc( 1000/speed, timer, 0 );
229 gluOrtho2D( 0, WindowWidth, WindowHeight, 0 );
230 glutMainLoop();
231
232 return 0;
233 }
我就不多说啥了。码代码中。
欢迎飘。