俄罗斯方块源代码c++.docx
- 文档编号:5919286
- 上传时间:2023-01-02
- 格式:DOCX
- 页数:16
- 大小:18.35KB
俄罗斯方块源代码c++.docx
《俄罗斯方块源代码c++.docx》由会员分享,可在线阅读,更多相关《俄罗斯方块源代码c++.docx(16页珍藏版)》请在冰豆网上搜索。
俄罗斯方块源代码c++
第一个Cpp
#include
#include
#include
#include
#include
#pragmacomment(lib,"winmm.lib")//播放背景音乐的头文件
#include"colorConsole.h"
#include
#defineSQUARE_COLORBACKGROUD_BLUE|BACKGROUD_RED|BACKGROUD_INTENSITY//背景颜色
#defineSQUARE_COLORFOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_INTENSITY//方块的颜色
#defineup72
#definedown80
#defineleft75
#defineright77
#defineesc27
#defineMAPW15//地图的宽度
#defineMAPH25//地图的高度
voidinitiate1();
int*build();//创建方块//初始化工作
BOOLisavailable(inta[],intx,inty,intw,inth);//判定是否能放下
voiddrawblocks(inta[],intw,inth,intx,inty,WORDwColors[],intnColors);
voiddelete_cache();//清除键盘缓冲区
voidrevolve(inta[][4],intw,inth,int*x,inty);//转动方块
voidpro();
voidend();
voiddelete_blocks(int*a,intw,inth,intx,inty);
voidgameover();
voiddeletefull_line(intm[][MAPW],introw,intw,inth);//消除一行
intdx=30,dy=5;//屏幕上的偏移量
intscore=0,level=0;
intmap[MAPH][MAPW];
inta1[4][4]={{1},{1,1,1}};
inta2[4][4]={{0,1},{1,1,1}};
inta3[4][4]={{1,1},{0,1,1}};
inta4[4][4]={{0,0,1},{1,1,1}};
inta5[4][4]={{0,1,1},{1,1}};
inta6[4][4]={{1,1,1,1}};
inta7[4][4]={{1,1},{1,1}};
inta[4][4];
intmain()
{
HANDLEhandle;
handle=initiate();
WORDwColors[1]={FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_INTENSITY};
while
(1)
{
sndPlaySound("Resource\\JustDance.wav",SND_LOOP|SND_ASYNC);//用异步方式播放音乐,PlaySound函数在开始播放后立即返回
system("CLS");
intn=0;
printf("目录\n1.开始游戏\n2.退出游戏\n\n\n");
scanf("%d",&n);
switch(n)
{
case1:
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)
{
charchoice;
choice=_getch();
if(choice=='1')
{
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,wColors+2,1,"");
level=0,score=0;
Sleep(2000);
textout(handle,22,6,wColors+2,1,"");
break;
}
elseif(choice=='2')
{
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,wColors+2,1,"");
level=2,score=20;
Sleep(2000);
textout(handle,22,6,wColors+2,1,"");
break;
}
elseif(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,wColors+2,1,"");
level=4,score=40;
Sleep(2000);
textout(handle,22,6,wColors+2,1,"");
break;
}
elseif(choice!
='1'&&choice!
='2'&&choice!
='3')
continue;
}
pro();
break;
case2:
return0;
default:
printf("错误,按键继续");
while(!
_kbhit());
}
}
}
voidpro()//游戏主题
{
initiate1();
int*b=NULL;
b=build();//创建方块
intsign,blank,x,y;
while
(1)
{
for(inti=0;i<4;i++)//复制方块
for(intj=0;j<4;j++)
if(a[i][j]=*(b+i*4+j))blank=i;
y=1-blank;x=4;
delete_blocks(&a[0][0],4,4,16,10);
b=build();
HANDLEhandle;
handle=initiate();
WORDwColors[1]={FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_INTENSITY};
drawblocks(b,4,4,16,10,wColors,1);
wColors[0]=SQUARE_COLOR;
drawblocks(&a[0][0],4,4,x,y,wColors,1);
delete_cache();
charstring[5];
wColors[0]=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)
{
intdelay=0,max_delay=100-10*level;//延迟量
while(delay { if(_kbhit())//用if避免按住键使方块卡住 { intdraw=0; intkey=_getch(); switch(key) { caseup: delete_blocks(&a[0][0],4,4,x,y); revolve(a,4,4,&x,y); draw=1; break; casedown: delay=max_delay; break; caseleft: if(isavailable(&a[0][0],x-1,y,4,4)) { delete_blocks(&a[0][0],4,4,x,y); x--; draw=1; } break; caseright: if(isavailable(&a[0][0],x+1,y,4,4)) { delete_blocks(&a[0][0],4,4,x,y); x++; draw=1; } break; case32: //32是空格键的ASCII码,按空格键暂停 while (1) { textout(handle,dx,-2+dy,wColors,1,"Pressanykeytocontinue"); Sleep(200); textout(handle,dx,-2+dy,wColors,1,""); Sleep(200); if(_kbhit()) { draw=1; break; } } break; caseesc: //按键退出游戏 exit(EXIT_SUCCESS); } if(draw) { HANDLEhandle; handle=initiate(); WORDwColors[1]={SQUARE_COLOR}; drawblocks(&a[0][0],4,4,x,y,wColors,1); draw=0; } } _sleep(5);delay++; } if(isavailable(&a[0][0],x,y+1,4,4))//是否能下移 { delete_blocks(&a[0][0],4,4,x,y); y++; HANDLEhandle; handle=initiate(); WORDwColors[1]={SQUARE_COLOR}; drawblocks(&a[0][0],4,4,x,y,wColors,1); } else { sign=0;//标记,使跳出while(sign)循环,产生新方块 if(y<=1) { system("CLS"); HANDLEhandle; handle=initiate(); WORDwColors[1]={FOREGROUND_RED|FOREGROUND_GREEN}; textout(handle,4+dx,6+dy,wColors,1,"GAMEOVER! ! ! "); textout(handle,4+dx,8+dy,wColors,1,"分数: "); textout(handle,10+dx,8+dy,wColors,1,itoa(score,string,10)); textout(handle,4+dx,10+dy,wColors,1,"制作者: ***"); delete_cache(); exit(EXIT_SUCCESS); }//是否结束 for(inti=0;i<4;i++)//放下方块 for(intj=0;j<4;j++) if(a[i][j]&&((i+y) map[i+y][j+x]=a[i][j]; intfull,k=0; for(i=y;i { full=1; for(intj=1;j<14;j++) if(! map[i][j])full=0; if(full)//消掉一行 { deletefull_line(map,i,MAPW,MAPH); k++; score=score+k; level=min(score/10,9); } } } } } } voidinitiate1()//初始化 { inti; for(i=0;i<25;i++) { map[i][0]=-2; map[i][14]=-2; } for(i=0;i<15;i++) { map[0][i]=-1; map[24][i]=-1; } map[0][0]=-3; map[0][14]=-3; map[24][0]=-3; map[24][14]=-3; HANDLEhandle; handle=initiate(); WORDwColors[1]={FOREGROUND_GREEN|FOREGROUND_BLUE|FOREGROUND_INTENSITY}; textout(handle,dx-10,6+dy,wColors,1,"SCORE"); textout(handle,dx-10,12+dy,wColors,1,"LEVEL"); textout(handle,32+dx,8+dy,wColors,1,"NEXT"); wColors[0]=FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_INTENSITY; drawblocks(&map[0][0],15,25,0,0,wColors,1); textout(handle,dx,dy,wColors,1,"◎═════════════◎"); wColors[0]=FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_INTENSITY; textout(handle,dx+8,dy+5,wColors,1,"按任意键开始"); wColors[0]=FOREGROUND_BLUE|FOREGROUND_RED|FOREGROUND_INTENSITY; textout(handle,dx+7,dy-3,wColors,1,"制作者: ***"); intx=_getch(); srand(time(NULL)); textout(handle,dx+8,dy+5,wColors,1,""); } int*build()//创建方块 { int*a=NULL; intc=rand()%7; switch(c) { case0: a=&a1[0][0];break; case1: a=&a2[0][0];break; case2: a=&a3[0][0];break; case3: a=&a4[0][0];break; case4: a=&a5[0][0];break; case5: a=&a6[0][0];break; case6: a=&a7[0][0];break; } returna; } voiddrawblocks(inta[],intw,inth,intx,inty,WORDwColors[],intnColors)//画出方块 { HANDLEhandle; handle=initiate(); inttemp; for(inti=0;i for(intj=0;j if((temp=a[i*w+j])&&y+i>0) { if(temp==-3) { textout(handle,2*(x+j)+dx,y+i+dy,wColors,nColors,"◎"); _sleep(30); } elseif(temp==-2) { textout(handle,2*(x+j)+dx,y+i+dy,wColors,nColors,"║"); _sleep(30); } elseif(temp==1) textout(handle,2*(x+j)+dx,y+i+dy,wColors,nColors,"◎"); elseif(temp==-1) { textout(handle,2*(x+j)+dx,y+i+dy,wColors,nColors,"═"); _sleep(30); } } } voiddelete_cache()//清除缓冲区 { while(_kbhit()) { _getch(); } } voiddelete_blocks(int*a,intw,inth,intx,inty)//覆盖方块 { HANDLEhandle; handle=initiate(); WORDwColors[1]={SQUARE_COLOR}; for(inti=0;i for(intj=0;j if(a[i*w+j]&&i+y>0) textout(handle,2*(x+j)+dx,y+i+dy,wColors,1,""); } voidrevolve(inta[][4],intw,inth,int*x,inty)//转动方块 { intb[4][4]={{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}}; intsign=0,line=0; for(inti=h-1;i>=0;i--) { for(intj=0;j if(a[i][j]) { b[j][line]=a[i][j]; sign=1; } if(sign) { line++; sign=0; } } for(i=0;i<4;i++) if(isavailable(&b[0][0],*x-i,y,w,h)) { *x-=i; for(intk=0;k for(intj=0;j a[k][j]=b[k][j]; break; } } voiddeletefull_line(intm[][MAPW],introw,intw,inth)//消除满行的方块 { HANDLEhandle; handle=initiate(); WORDwColors[1]={SQUARE_COLOR}; textout(handle,2+dx,row+dy,wColors,1,"﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌"); _sleep(100); inti; for(i=row;i>1;i--) { delete_blocks(&m[i][1],MAPW-2,1,1,i); for(intj=1;j m[i][j]=m[i-1][j]; drawblocks(&m[i][1],MAPW-2,1,1,i,wColors,1); } for(i=1;i m[1][i]=0; } BOOLisavailable(inta[],intx,inty,intw,inth) { for(inti=max(y,1);i for(intj=x;j if(map[i][j]&&a[w*(i-y)+j-x]) return0; return1; } 第二个cpp: #include"colorConsole.h" HANDLEinitiate() { HANDLEhOutput; hOutput=GetStdHandle(STD_OUTPUT_HANDLE); returnhOutput; } BOOLtextout(HANDLEhOutput,intx,inty,WORDwColors[],intnColors,LPTSTRlpszString) { DWORDcWritten; BOOLfSuccess; COORDcoord; coord.X=x;//startatf
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 俄罗斯方块源代码 c+ 俄罗斯方块 源代码