注意:本源代码包含<graphics.h>头文件,VC6.0请自行下载库文件包,解决没有库文件的问题环境:WINDOWS7 VC6.0程序清单:库文件MYFILE.H/****************************************************************************** *********************File Name : MYFILE.HCopyright :Module Name :CPU : Intel i7RTOS :Creat Date : 2017/1/13Author : YangAbstract Description: C++、C实用函数******************************************************************************* *********************/#ifndef _MYFILE_#define _MYFILE_#include<stdio.h>#include<iostream.h>void introduce(){printf("欢迎使用!MYFILE.H\n");}/*********************************C++常用类******************************/template<class T_STACK> //栈class STACK{private:int top;T_STACK stackspace[100];public:STACK(){top =-1;}void PUSH(T_STACK x){if(top>=100-1)cout<<"STACK OVER FLOW"<<endl;else{ top++;stackspace[top]=x;}}T_STACK POP(){T_STACK x;if(top<-1){ top = -1; cout<<"STACK EMPTY"<<endl; return stackspace[0];}x = stackspace[top];top--;return x;}};template<class T_List> //队列class LIST{private:int point_in;int point_out;T_List listspace[100];public:LIST(){point_in=-1;point_out=-1;}void INList(T_List x){point_in=(point_in+1)%100;listspace[point_in]=x;}T_List OUTList(){T_List x;point_out=(point_out+1)%100;x = listspace[point_out];return x;}void showList(){int i=0;for(i=point_out+1;i<=point_in;i++)cout<<listspace[i]<<"\t";}};/**************************C语言通用数据结构********************************//***************************C语言控制台显示*********************************/ #include <graphics.h>#define xystep (15)#define IMAGE_X (450)#define IMAGE_Y (450)//初始化UI界面void UI_init(){initgraph(IMAGE_X,IMAGE_Y);settextcolor(LIGHTGREEN);setbkcolor(BLACK);}//display_rectange(坐标轴x,坐标轴y,要显示的字符串,颜色);void display_str(int x,int y,char* s,int color){ x = (x)*xystep;y = (y)*xystep;switch(color){case 1:settextcolor(BLACK);break;case 2:settextcolor(LIGHTBLUE);break;case 3:settextcolor(LIGHTGREEN);break;case 4:settextcolor(LIGHTCYAN);break;case 5:settextcolor(LIGHTRED);break;case 6:settextcolor(LIGHTMAGENTA);break;case 7:settextcolor(YELLOW);break;case 8:settextcolor(WHITE);break;case 9:settextcolor(BROWN);break;case 10:settextcolor(LIGHTGRAY);break;case 11:settextcolor(RED);break;default:settextcolor(WHITE);}outtextxy(x,y,s);}//使用printf在指定位置输出字符串(WIN32API)void WIN32display(int x,int y,char* c) //在x,y显示一个字符串{//RECT rect;COORD pos;HANDLE hOutput;pos.X = x;pos.Y = y;hOutput = GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleCursorPosition(hOutput , pos);printf("%s",c);}#endif//源程序/****************************************************************************** *******************File Name : 俄罗斯方块.cppCopyright :Module Name :CPU : Intel i7RTOS :Creat Date : 2017/1/13Author : YangAbstract Description: 俄罗斯方块游戏编译环境: Visual C++ 6.0绿色版************************************************************************************************/#include <conio.h>#include<iostream.h>#include <myfile.h>#include<math.h>#include<time.h>#include<windows.h>/******************************类与函数的声明******************************/ class shape;void create_new(shape* x);void key_deal();void UI_NEXT(shape* x);void UI();void delay();/******************************自己定义的一些宏******************************/ #define LOCATIONX 9 //砖块初始出现位置#define LOCATIONY 6#define TIME_STEP 30000 //砖块下落的速度/******************************全局变量的声明******************************/static char key_vaule=0;/******************************类的定义******************************/class shape //基类:形状{public:virtual void trans() = 0;int x1,y1,x2,y2,x3,y3,x4,y4; //实时坐标int x12,y12,x22,y22,x32,y32,x42,y42,status2; //坐标,状态备份副本int angle;int status;int color;shape(){x1=0;x2=0;x3=0;x4=0;y1=0;y2=0;y3=0;y4=0;angle=0;color=3;}shape(shape &x) //拷贝构造函数{x1=x.x1;x2=x.x2;x3=x.x3;x4=x.x4;y1=x.y1;y2=x.y2;y3=x.y3;y4=x.y4;color=x.color;}void up(){saved();y1--;y2--;y3--;y4--;}void down(){saved();y1++;y2++;y3++;y4++;}void left(){saved();x1--;x2--;x3--;x4--;}void right(){saved();x1++;x2++;x3++;x4++;}void saved() //坐标备份{ status2=status;x12=x1;y12=y1;x22=x2;y22=y2;x32=x3;y32=y3;x42=x4;y42=y4;}void show(){display_str(x1,y1,"■",color);display_str(x2,y2,"■",color);display_str(x3,y3,"■",color);display_str(x4,y4,"■",color);}void clear(){display_str(x1,y1,"",color);display_str(x2,y2,"",color);display_str(x3,y3,"",color);display_str(x4,y4,"",color);}};class bulid //建筑物类{public:int score;char MAP[30][30]; //BUILDMAP坐标体统一:X: 0 - 29 Y:0 - 29bulid() //建筑体初始化{ int i=0;int j=0;for(i=0;i<30;i++)for(j=0;j<30;j++)MAP[i][j]=0;for(i=5;i<30;i++) //画竖着的杆子MAP[列][行]{MAP[0][i]=1;MAP[1][i]=1;MAP[15][i]=1;MAP[16][i]=1;}for(i=0;i<15;i++) //画底{MAP[i][29]=1;}score=0;}void receive(shape *s) //接纳一个砖头作为建筑物的一部分{MAP[s->x1][s->y1]=1;MAP[s->x2][s->y2]=1;MAP[s->x3][s->y3]=1;MAP[s->x4][s->y4]=1;}void display_mode() //把模板显示出来,方便调试{ int i=0;int j=0;for(i=0;i<30;i++)for(j=0;j<30;j++)display_str(i,j,"□",2);}void display_bulid(int color) //把建筑物显示出来一次{ int i = 0,j=0;for(i=0;i<19;i++) //19:半屏显示for(j=3;j<30;j++)if(MAP[i][j]==1){ if(i==0||i==1||i==15||i==16||j==29)display_str(i,j,"■",8); //容器的颜色elsedisplay_str(i,j,"■",color); //color:已经成了建筑物的砖块的颜色}else if(MAP[i][j]==0)display_str(i,j,"",color);elsedisplay_str(i,j,"☆",color);}int place(shape *s) //是否着陆?{int i=0,j=0;for(i=0;i<30;i++)for(j=0;j<30;j++){if( ((j==(s->y1))&&(i==s->x1)) //坐标与建筑物重合|| ((j==(s->y2))&&(i==s->x2))|| ((j==(s->y3))&&(i==s->x3))|| ((j==(s->y4))&&(i==s->x4)))if(MAP[i][j]==1){if(key_vaule == 'S'||key_vaule == 's') //是向下键{ //可以着陆,那么收录坐标,遴选y最小值s->y1--;s->y2--;s->y3--;s->y4--;receive(s);Score_count();display_bulid(7);delete s; //删除已经变成了建筑物的活体对象的空间,节约内存return 1; //返回1,表示已经着陆}else{ s->status=s->status2;s->x1=s->x12;s->y1=s->y12;s->x2=s->x22;s->y2=s->y22;s->x3=s->x32;s->y3=s->y32;s->x4=s->x42;s->y4=s->y42;}}}return 0;}int END() //是否终止游戏{ int i=0;for(i=2;i<15;i++)if(MAP[i][6]==1){ display_str(20,12,"END THE GAME!!",5);return 1;}return 0;}void Score_count() //计分板{ char SCORE[20];int i,j,count=0;for(i=6;i<29;i++) //从第6行检查到28行{for(j=2;j<15;j++){if(MAP[j][i]==1) //从第2列检查到第14列count++;if(count>=13){ delete_line(i); //删除第i行score++;}}count=0;}//计分显示display_str(22,10,"SCORE",5);itoa(score,SCORE,10);display_str(26,10,SCORE,5);}void delete_line(int Line){int i=0,j=0;for(j=Line;j>0;j--)for(i=2;i<15;i++)if(j-1==5) //第5行实际上是最上方的墙壁了MAP[i][j]=0;elseMAP[i][j]=MAP[i][j-1];}};//-----------------------定义具体的砖块类----------------class BAR:public shape //长条{private:void trans_0() //姿态0{x1-=1;y1+=1;x3+=1;y3-=1;x4+=2;y4-=2;}void trans_1() //姿态1{x1+=1;y1-=1;x3-=1;y3+=1;x4-=2;y4+=2;}public:BAR(){ x2=LOCATIONX;y2=LOCATIONY;x1=x2; y1=y2-1;x3=x2; y3=y2+1;x4=x2; y4=y2+2;status=0;color=2;}void trans(){ saved();if(status==0)trans_0();if(status==1)trans_1();status = (++status)%2;}};class SQUARE:public shape //正方形{public:SQUARE(){ x2=LOCATIONX;y2=LOCATIONY;x1=x2-1;y1=y2;x3=x2-1;y3=y2+1;x4=x2;y4=y2+1;color = 3;status=0;}void trans(){saved();}};class JBAR:public shape //J形{private:void trans_0(){x1-=1;y1+=1;x3+=1;y3-=1;x4+=2;}void trans_1(){x1+=1;y1+=1;x3-=1;y3-=1;y4-=2;}void trans_2(){x1+=1;y1-=1;x3-=1;y3+=1;x4-=2;}void trans_3(){x1-=1;y1-=1;x3+=1;y3+=1;y4+=2;}public:JBAR(){x2=LOCATIONX;y2=LOCATIONY;x1=x2; y1=y2-1;x3=x2; y3=y2+1;x4=x2-1; y4=y2+1;status=0;color=4;}void trans(){ saved();if(status==0)trans_0();if(status==1)trans_1();if(status==2)trans_2();if(status==3)trans_3();status = (++status)%4;}};class LBAR:public shape //L形{private:void trans_0(){x1-=1;y1+=1;x3+=1;y3-=1;y4-=2;}void trans_1(){x1+=1;y1+=1;x3-=1;y3-=1;x4-=2;}void trans_2(){x1+=1;y1-=1;x3-=1;y3+=1;y4+=2;}void trans_3(){x1-=1;y1-=1;x3+=1;y3+=1;x4+=2;}public:LBAR(){x2=LOCATIONX;y2=LOCATIONY;x1=x2; y1=y2-1;x3=x2; y3=y2+1;x4=x2+1; y4=y2+1;status=0;color=5;}void trans(){ saved();if(status==0)trans_0();if(status==1)trans_1();if(status==2)trans_2();if(status==3)trans_3();status = (++status)%4;};class ZBAR:public shape{private:void trans_0(){x1+=1;x4+=1;y4-=2;}void trans_1(){x1-=1;x4-=1;y4+=2;}public:ZBAR(){x2=LOCATIONX;y2=LOCATIONY;x1=x2; y1=y2-1;x3=x2+1; y3=y2;x4=x2+1; y4=y2+1;status=0;color=6;}void trans(){ saved();if(status==0)trans_0();if(status==1)trans_1();status = (++status)%2;};class SBAR:public shape{private:void trans_0(){x1-=1;y1+=1;x3+=1;y3+=1;x4+=2;}void trans_1(){x1+=1;y1-=1;x3-=1;y3-=1;x4-=2;}public:SBAR(){x2=LOCATIONX;y2=LOCATIONY;x1=x2; y1=y2-1;x3=x2-1; y3=y2;x4=x2-1; y4=y2+1;status=0;color=11;}void trans(){ saved();if(status==0)trans_0();if(status==1)trans_1();status = (++status)%2;}};class TBAR:public shape{private:void trans_0(){x1+=1;y1+=1;x3-=1;y3+=1;x4-=1;y4-=1;}void trans_1(){x1+=1;y1-=1;x3+=1;y3+=1;x4-=1;y4+=1;}void trans_2(){x1-=1;y1-=1;x3+=1;y3-=1;x4+=1;y4+=1;}void trans_3(){x1-=1;y1+=1;x3-=1;y3-=1;x4+=1;y4-=1;}public:TBAR(){x2=LOCATIONX;y2=LOCATIONY;x1=x2-1; y1=y2;x3=x2; y3=y2-1;x4=x2+1; y4=y2;status=0;color=10;//}void trans(){ saved();if(status==0)trans_0();if(status==1)trans_1();if(status==2)trans_2();if(status==3)trans_3();status = (++status)%4;}};/******************************自定义的类外的函数******************************/void create_new(shape* x){ int random1,random2;//srand((unsigned)time(0));这个srand()函数只需要执行一次就够了;执行多次会导致生成的数字不那么随机random1=rand()%7+1;//delete x;switch(random1) //随机生成活动对象{case 1: x = new LBAR; break;case 2: x = new SQUARE; break;case 3: x = new BAR; break;case 4: x = new ZBAR; break;case 5: x = new TBAR; break;case 6: x = new SBAR; break;case 7: x = new JBAR; break;default :x =new TBAR; break;}random2=rand()%4+1;switch(random2) //随机生成活动对象的状态{case 1: x->trans();break;case 2: x->trans();x->trans();break;case 3: x->trans();x->trans();x->trans();break;case 4: x->trans();x->trans();x->trans();x->trans();break;default :break;}}void UI() //用户问好界面{ UI_init();display_str(22,10,"SCORE",5);display_str(26,10,"0",5);display_str(1,0,"YWQ:2017/1/18 平台WINDOWS_7 VC6.0 ",3);display_str(1,2,"欢迎各路大神讨论C/C++的学习",4);display_str(20,20,"A:向左移动",8);display_str(20,21,"D:向右移动",8);display_str(20,22,"S:快速落下",8);display_str(20,23,"W:方块变形",8);}void UI_NEXT(shape* x) //把下一个要出现的砖块展示出来{display_str(17,5,"",1);display_str(17,6,"",1);display_str(17,7,"",1);display_str(17,8,"",1);display_str(x->x1+10,x->y1,"■",x->color);display_str(x->x2+10,x->y2,"■",x->color);display_str(x->x3+10,x->y3,"■",x->color);display_str(x->x4+10,x->y4,"■",x->color);}void key_deal(){ int i=0;int time=0;int flag=0;shape* p,*p2;bulid B;p = new LBAR; //当前的方块p2=new LBAR; //下次的方块B.display_bulid(7);delete p;create_new(p); //当前的delete p2;create_new(p2); //下次的UI_NEXT(p2);p->show();while(!B.END()){if(kbhit()) //kbhit():检测当前是否有按键按下非阻塞函数{key_vaule = getch();}else{ time++;key_vaule='U';if(time>TIME_STEP) //自动下降{key_vaule='S';time=0;}}switch(key_vaule){case 'A':case 'a':p->clear();p->left();B.place(p); {p->show();}break;case 'D':case 'd':p->clear();p->right();B.place(p); {p->show();}break;case 'S':case 's':p->clear();p->down(); if(B.place(p)){ p=p2;p2=new LBAR;delete p2;create_new(p2);UI_NEXT(p2);} p->show();break;//不知道为什么,p2一定要先删除一次,再分配一次,数据才是新的case 'w':case 'W':p->clear();p->trans();B.place(p); {p->show();}break;case 'U':break;//U:不做反应default:;}}}void delay(){ int i=0,j=0;for(i=0;i<50000;i++)for(j=0;j<50000;j++);}/******************************主函数****************************************/ void main(){ srand((unsigned)time(0));UI();key_deal();getch();getch();getch();getch();}/******************************此源文件结束******************************/。