《俄罗斯方块c++源代码.doc》由会员分享,可在线阅读,更多相关《俄罗斯方块c++源代码.doc(25页珍藏版)》请在金锄头文库上搜索。
1、第一个Cpp#include #include #include#include #include #pragma comment(lib,winmm.lib) /播放背景音乐的头文件#include colorConsole.h#include#define SQUARE_COLOR BACKGROUD_BLUE|BACKGROUD_RED| BACKGROUD_INTENSITY /背景颜色#define SQUARE_COLOR FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_INTENSITY /方块的颜色#define up 72#define
2、down 80#define left 75#define right 77#define esc 27#define MAPW 15 /地图的宽度#define MAPH 25 /地图的高度void initiate1();int * build(); /创建方块 /初始化工作BOOL isavailable(int a,int x,int y,int w,int h); /判定是否能放下void drawblocks(int a,int w,int h,int x,int y,WORD wColors,int nColors);void delete_cache(); /清除键盘缓冲区vo
3、id revolve(int a4,int w,int h,int *x,int y); /转动方块void pro();void end();void delete_blocks(int *a,int w,int h,int x,int y);void gameover();void deletefull_line(int mMAPW,int row,int w,int h); /消除一行int dx=30,dy=5; /屏幕上的偏移量int score=0,level=0;int mapMAPHMAPW;int a144=1,1,1,1;int a244=0,1,1,1,1;int a34
4、4=1,1,0,1,1;int a444=0,0,1,1,1,1;int a544=0,1,1,1,1;int a644=1,1,1,1;int a744=1,1,1,1;int a44;int main()HANDLE handle;handle=initiate();WORD wColors1=FOREGROUND_BLUE| FOREGROUND_GREEN|FOREGROUND_INTENSITY ;while(1) sndPlaySound(ResourceJust Dance.wav,SND_LOOP|SND_ASYNC);/用异步方式播放音乐,PlaySound函数在开始播放后立
5、即返回 system(CLS);int n=0;printf(目录n1.开始游戏n2.退出游戏nnn);scanf(%d,&n);switch(n)case 1:system(CLS);textout(handle,22,6,wColors+2,1,请选择游戏等级:);textout(handle,32,8,wColors+2,1,1.初级);textout(handle,32,10,wColors+2,1,2.中级);textout(handle,32,12,wColors+2,1,3.高级);while(1)char choice;choice=_getch();if(choice=1)t
6、extout(handle,22,6,wColors+2,1,开始游戏,初级);textout(handle,32,8,wColors+2,1, );textout(handle,32,10,wColors+2,1, );textout(handle,32,12,wColors+2,1, );level=0,score=0;Sleep(2000);textout(handle,22,6,wColors+2,1, );break;else if(choice=2)textout(handle,22,6,wColors+2,1,开始游戏,中级);textout(handle,32,8,wColor
7、s+2,1, );textout(handle,32,10,wColors+2,1, );textout(handle,32,12,wColors+2,1, );level=2,score=20;Sleep(2000);textout(handle,22,6,wColors+2,1, );break;else if(choice=3)textout(handle,22,6,wColors+2,1,开始游戏,高级);textout(handle,32,8,wColors+2,1, );textout(handle,32,10,wColors+2,1, );textout(handle,32,12
8、,wColors+2,1, );level=4,score=40;Sleep(2000);textout(handle,22,6,wColors+2,1, );break;else if(choice!=1&choice!=2&choice!=3)continue;pro();break;case 2:return 0;default:printf(错误,按键继续);while(!_kbhit();void pro() /游戏主题initiate1();int *b=NULL;b=build(); /创建方块int sign,blank,x,y;while(1)for(int i=0;i4;i
9、+) /复制方块for(int j=0;j4;j+)if(aij=*(b+i*4+j) blank=i; y=1-blank;x=4; delete_blocks(&a00,4,4,16,10);b=build();HANDLE handle;handle=initiate();WORD wColors1=FOREGROUND_BLUE| FOREGROUND_GREEN|FOREGROUND_INTENSITY ;drawblocks(b,4,4,16,10,wColors,1);wColors0=SQUARE_COLOR;drawblocks(&a00,4,4,x,y,wColors,1)
10、;delete_cache();char string5;wColors0=FOREGROUND_RED| FOREGROUND_GREEN|FOREGROUND_INTENSITY;textout(handle,dx-10,8+dy,wColors,1,itoa(score,string,10);textout(handle,dx-10,14+dy,wColors,1,itoa(level,string,10);sign=1;while(sign)int delay=0,max_delay=100-10*level; /延迟量while(delaymax_delay)if(_kbhit()
11、/用if避免按住键使方块卡住int draw=0;int key=_getch(); switch (key)case up:delete_blocks(&a00,4,4,x,y);revolve(a,4,4,&x,y);draw=1;break;case down:delay=max_delay;break;case left:if(isavailable(&a00,x-1,y,4,4)delete_blocks(&a00,4,4,x,y);x-;draw=1;break;case right:if(isavailable(&a00,x+1,y,4,4)delete_blocks(&a00,4,4,x,y);x+;draw=1;break;case 32:/32 是空格键的ASCII码,按空格键暂停 while(1)