代码进化到324行。
今天不标记行号了,方便大家copy paste,希望能稍微地给大家点帮助。
瓶口的二值图已经画好了,今天晚上画虫子的二值图,稍后再画医生的彩图,或者,嘿嘿,能不能麻烦糖糖画了咱们一起用,我就偷懒一下。然后今晚的任务就是学怎样读取二值图和彩图,明天把move函数完成并debug,简易的玛丽医生就差不多可以完成了,之后慢慢添加功能,做的完善点儿。
我恨杭电,更恨我的菜。我想彻底放弃ICPC了,我不像猫什么都很强,做做算法还蛮好的。现在我的水平,最好还是老老实实地,一次只做好一件事。
//需要虫子的二值图,瓶口的二值图,医生的彩色图,bmp格式
//需要学会读取bmp格式图片并处理
//move函数的代码,预计50行左右
#include <GL/glut.h>
#include <ctime>
#include <cstdlib>
#include <cstdio>
static const int WindowWidth = 640;
static const int WindowHeight = 480;
int count = 0;
int speed = 8;
bool dead = false;
enum GridType
{
NOTHING, CAPSULE, WORM
};
enum Connect
{
NOT_CONNECTED, LEFT, RIGHT, UP, DOWN
};
enum Color
{
BLACK, YELLOW, BLUE, RED
};
enum DoctorDirect
{
DD_NODIRECT, DD_UP, DD_DOWN, DD_LEFT, DD_RIGHT
};
void setColor( Color color )
{
switch (color)
{
case BLACK:
glColor3f( 0.0f, 0.0f, 0.0f );
break;
case YELLOW:
glColor3f( 1.0f, 1.0f, 0.0f );
break;
case BLUE:
glColor3f( 0.0f, 0.0f, 1.0f );
break;
case RED:
glColor3f( 1.0f, 0.0f, 0.0f );
break;
default:
break;
}
}
class Bottle
{
private:
int b_x,b_y; //bottle可以容纳药丸的最左上角位置
GridType b_grid_type[16][8]; //每个格子20像素长宽
Color b_color[16][8];
Connect b_connect[16][8];
public:
int getx( void )
{
return b_x;
}
int gety( void )
{
return b_y;
}
void drawBottle( void )
{
int i,j;
glColor3f( 0.0f, 1.0f, 1.0f );//青色
//**************先画瓶口再画瓶身******
//画瓶子的内部,根据b_x,b_y和瓶子的内容物
for ( i = 0; i < 16; ++i )
{
for ( j = 0; j < 8; ++j )
{
setColor( b_color[i][j] );
if ( b_grid_type[i][j] == WORM )
drawWorm( b_x+i*20, b_y+j*20 );
else if ( b_grid_type[i][j] == CAPSULE )
{
drawCapsule( b_x+i*20+10, b_y+i*20+10, 10, b_connect[i][j] );
}
}
}
}
void drawCapsule( int x, int y, int r, Connect connect )
{
//将相连的部分画上
switch ( connect )
{
case LEFT:
glRectf( x-10, y-10, x, y+10 );
break;
case RIGHT:
glRectf( x, y-10, x+10, y+10 );
break;
case UP:
glRectf( x-10, y-10, x+10, y );
break;
case DOWN:
glRectf( x-10, y, x+10, y+10 );
break;
default:
break;
}
//***********画圆并填充*********
}
void drawWorm( int x, int y )
{
//*************从坐标x,y开始画虫子,20像素长宽*******************
}
Bottle( int level, int xx, int yy )
{
int i; //vc6这点真恨人
b_x = xx;
b_y = yy;
for ( i = 0; i < 16; ++i )
{
for ( int j = 0; j < 8; ++j )
{
b_grid_type[i][j] = NOTHING;
b_color[i][j] = BLACK;
b_connect[i][j] = NOT_CONNECTED;
}
}
for ( i = 0; i < level*4; ++i )
{
int x = rand()%16;
int y = rand()%8;
Color color = (Color)(rand()%3);
if ( ok( x, y, color, level ) )
{
b_grid_type[x][y] = WORM;
b_color[x][y] = color;
}
else
--i;
}
}
bool ok( int x, int y, Color color, int level )
{
if ( level < 15 && x <= 6 ) return false;
if ( ((level+1)>>1)+3+x < 16 ) return false;
if ( x-2 >= 0 && b_grid_type[x-1][y] == WORM && b_color[x-1][y] == color
&& b_grid_type[x-2][y] == WORM && b_color[x-2][y] == color ) return false;
if ( x+2 < 8 && b_grid_type[x+1][y] == WORM && b_color[x+1][y] == color
&& b_grid_type[x+2][y] == WORM && b_color[x+2][y] == color ) return false;
if ( y+2 < 16 && b_grid_type[x][y+1] == WORM && b_color[x][y+1] == color
&& b_grid_type[x][y+2] == WORM && b_color[x][y+2] == color ) return false;
if ( b_grid_type[x][y-1] == WORM && b_color[x][y-1] == color
&& b_grid_type[x][y-2] == WORM && b_color[x][y-2] == color ) return false;
return true;
}
};
class Doctor
{
private:
int d_x,d_y; //活动药丸(左下角)相对于瓶子的位置,不是像素点
bool d_state; //横的false,竖的true
Color d_color[2]; //俩药丸的颜色
DoctorDirect d_direct;
public:
void drawDoctor( void )
{
//***************画医生的形象*******************
if ( d_state )
{
bottle->drawCapsule( bottle->getx()+20*d_x+10, bottle->gety()+20*d_y+10, UP );
bottle->drawCapsule( bottle->getx()+20*d_x+10, bottle->gety()+20*d_y-10, DOWN );
}
else
{
bottle->drawCapsule( bottle->getx()+20*d_x+10, bottle->gety()+20*d_y+10, RIGHT );
bottle->drawCapsule( bottle->getx()+20*(d_x+1)+10, bottle->gety()+20*d_y+10, LEFT );
}
}
Doctor( void )
{
d_x = 3; d_y = 0;
d_state = false;
d_direct = DD_NODIRECT;
d_color[0] = (Color)(rand()%3);
d_color[1] = (Color)(rand()%3);
}
void move( void )
{
//*****************伪码*********************
/*
if 下移
{
if 落地,也就是不能下移
{
if 可消
{
消除;
与消去相连的部分落地;
}
产生新药;
}
}
else 其它方向
判断是否可移,可移则移,不可移不移
*/
}
void fall( void )
{
d_direct = DD_DOWN;
move();
}
void setDirect( DoctorDirect direct )
{
d_direct = direct;
}
};
static Bottle* bottle = NULL;
static Doctor* doctor = NULL;
void init( void )
{
bottle = new Bottle( 3, 320, 160 );
doctor = new Doctor();
}
void key( unsigned char key_char, int not_use, int not_use_1 )
{
switch ( key_char )
{
case 'w':
case 'W':
doctor->setDirect( DD_UP );
break;
case 's':
case 'S':
doctor->setDirect( DD_DOWN );
break;
case 'a':
case 'A':
doctor->setDirect( DD_LEFT );
break;
case 'd':
case 'D':
doctor->setDirect( DD_RIGHT );
break;
default:
doctor->setDirect( DD_NODIRECT );
break;
}
}
void step( void )
{
if ( dead ) return ;
doctor->move();
++count; if ( count == speed ) { count = 0; doctor->fall(); }
}
void timer( int not_use )
{
step();
glutPostRedisplay();
if ( !dead() )
{
glutTimerFunc( 1000/speed, timer, 0 );
}
}
void drawBG( void )
{
int i,j;
glColor3f( 0.0f, 0.0f, 0.7f );
for ( i = 0; i < 32; ++i )
{
for ( j = 0; j < 30; ++j )
{
if ( ((i+j)&1) == 0 )
glRectf( i*20, j*16, (i+1)*20, (j+1)*16 );
}
}
}
void draw( void )
{
drawBG();
bottle->drawBottle();
doctor->drawDoctor();
}
void display( void )
{
glClear( GL_COLOR_BUFFER_BIT );
draw();
glutSwapBuffers();
}
int main( int argc, char * argv[] )
{
srand( (unsigned int)time(NULL) );
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA|GLUT_DOUBLE );
glutInitWindowPosition( 50, 50 );
glutInitWindowSize( WindowWidth, WindowHeight );
glutCreateWindow( "Dr Mario By PureMilk" );
glutDisplayFunc( display );
glutKeyboardFunc( key );
glutTimerFunc( 1000/speed, timer, 0 );
gluOrtho2D( 0, WindowWidth, WindowHeight, 0 );
glutMainLoop();
return 0;
}