共分为2个头文件和3个.cpp文件,分别为Draw.h,Draw.cpp,keylistener.h,keylistener.cpp和Main.cpp
1、Draw.h
class draw {
public : draw(); ~draw(); static void draw_rect(intleftX, intleftY, int color); static void draw_line(intstartX, intstartY, intendX, intendY,int color[3]); static void display();
};
static void rand_stone_and_placedir(int& stone, int&pd); static void get_brick_mode(int stone, int mode[4][4][2]);
static void display_next_brick(int stone, intpd,int mode[4][4][2]); static void drawString(char *str); static void draw_char(char* str); static void draw_Text();
voidget_map(int *map[12]);
int* get_stone(); int* get_score(); int* get_placedir(); int* get_drop_flag();
voidget_cur_mode(int *mode[4][4]);
2、Draw.cpp
#include
#include \
#define width 24
int white[3] = {255 , 255 , 255}; int yellow[3] = {255 , 255 , 0}; int red[3] = {255 , 0 , 0}; int black[3] = {0 , 0 , 0}; int blue[3] = {0 , 162 , 232}; int green[3] = {80 , 202 , 53}; int orange[3] = {255, 128, 0}; int purple[3] = {128, 0, 128}; int pink[3] = {255, 0, 255};
int *clr[8] = {black, green, yellow, red, blue, purple, pink, orange};
//第1个
int LL[4][4][2] = { {{1, 0}, {1, -1}, {1, -2}, {0, -2}}, {{0, -1}, {1, -1}, {2, -1}, {2, -2}}, {{0, 0}, {1, 0}, {0, -1}, {0, -2}}, {{0, -2}, {1, -2}, {2, -2}, {0, -1}} };
//第2个
int LR[4][4][2] = { {{0, 0}, {0, -1}, {0, -2}, {1, -2}}, {{0, -2}, {1, -2}, {2, -2}, {2, -1}}, {{0 ,0}, {1, 0}, {1, -1}, {1, -2}}, {{0, -1}, {1, -1}, {2, -1}, {0, -2}} };
//第3个
int T[4][4][2] = { {{0, -1}, {1, -1}, {2, -1}, {1, 0}}, {{1, 0}, {0, -1}, {1, -1}, {1, -2}}, {{0 ,0}, {1, 0}, {2, 0}, {1, -1}}, {{0 ,0}, {0, -1}, {0, -2}, {1, -1}} };
//第4个
int O[4][4][2] = { {{0 ,0}, {1, 0}, {0, -1}, {1, -1}}, {{0 ,0}, {1, 0}, {0, -1}, {1, -1}}, {{0 ,0}, {1, 0}, {0, -1}, {1, -1}}, {{0 ,0}, {1, 0}, {0, -1}, {1, -1}} };
//第5个
int ZL[4][4][2] = { {{1, 0}, {2, 0}, {0, -1}, {1, -1}}, {{0 ,1}, {0, 0}, {1, 0}, {1, -1}}, {{1, 0}, {2, 0}, {0, -1}, {1, -1}}, {{0 ,1}, {0, 0}, {1, 0}, {1, -1}} };
//inta[3][3][2] = {
// {{0 ,0}, {1, 0}, {2, 0}}, // {{0, -1}, {1, -1}, {2, -1}}, // {{0, -2}, {1, -2}, {2, -2}} //};
//第6个
int ZR[4][4][2] = { {{0 ,0}, {1, 0}, {1, -1}, {2, -1}}, {{1 ,1}, {0 ,0}, {1, 0}, {0, -1}}, {{0 ,0}, {1, 0}, {1, -1}, {2, -1}}, {{1 ,1}, {0 ,0}, {1, 0}, {0, -1}}, };
//第7个
int I[4][4][2] = { {{0 ,0}, {1, 0}, {2, 0}, {3, 0}}, {{1, 0}, {1, 1}, {1, 2}, {1, 3}}, {{0 ,0}, {1, 0}, {2, 0}, {3, 0}}, {{1, 0}, {1, 1}, {1, 2}, {1, 3}} };
intcur_mode[4][4][2]; //当前方块模板 intnext_mode[4][4][2];
intmp[18][32] = {0};
intdrop_flag = 0; //是否落地标志 intcur_stone; //方块种类 intcur_place_dir; //方块放置的朝向 int score = 0;
charpre_score[10] = \intnext_stone; intnext_place_dir; intnext_x = 12; intnext_y = 17;
draw::draw() { srand((unsigned)time(NULL)); rand_stone_and_placedir(cur_stone, cur_place_dir); rand_stone_and_placedir(next_stone, next_place_dir); get_brick_mode(cur_stone,cur_mode); get_brick_mode(next_stone,next_mode); }
draw::~draw() {
}
//画直线
void draw::draw_line(intstartX, intstartY, intendX, intendY,int color[3]) { glBegin(GL_LINES); glColor3ub(color[0], color[1], color[2]); glVertex2d(startX, startY); //起点 glVertex2d(endX, endY); //终点 glEnd(); //结束画线 }
//画长方形
void draw::draw_rect(intleftX, intleftY, int color) { glBegin(GL_POLYGON);
glColor3ub(clr[color][0], clr[color][1], clr[color][2]); glVertex2d(leftX + 1, leftY + 1); //左下角 glVertex2d(leftX + width, leftY + 1); //右下角 glVertex2d(leftX + width, leftY + width); //右上角 glVertex2d(leftX + 1, leftY + width); //左上角 glEnd();//结束画线 }
//展示下一个形状方块
void draw::display_next_brick(int stone, intpd,int mode[4][4][2]) { for(inti = 0; i< 4; i++) { int x = next_x + mode[pd][i][0]; int y = next_y + mode[pd][i][1]; mp[x][y] = stone; } }
void draw::draw_Text() { if(drop_flag == 2) { glColor3ub(red[0], red[1], red[2]); glRasterPos2i(10, 240); drawString(\ } glColor3ub(black[0], black[1], black[2]);