俄罗斯方块c源代码费下载
第一个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 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(); //清除键盘缓冲区 void revolve(int a[][4],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 m[][MAPW],int row,int w,int h); //消除一行
int dx=30,dy=5; //屏幕上的偏移量
int score=0,level=0;
int map[MAPH][MAPW];
int a1[4][4]={{1},{1,1,1}}; int a2[4][4]={{0,1},{1,1,1}}; int a3[4][4]={{1,1},{0,1,1}}; int a4[4][4]={{0,0,1},{1,1,1}}; int a5[4][4]={{0,1,1},{1,1}}; int a6[4][4]={{1,1,1,1}}; int a7[4][4]={{1,1},{1,1}};
int a[4][4];
int main()
{
HANDLE handle;
handle=initiate();
WORD wColors[1]={FOREGROUND_BLUE| FOREGROUND_GREEN|FOREGROUND_INTENSITY };
while(1)
{
sndPlaySound("Resource\\Just Dance.wav",SND_LOOP|SND_ASYNC);//用异步方式播放音乐,PlaySound函数在开始播放后立即返回
system("CLS");
int n=0;
printf("目录\n1.开始游戏\n2.退出游戏\n\n\n");
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')
{
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;
}
else if(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;
}
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,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;i<4;i++) //复制方块
for(int j=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();
HANDLE handle;
handle=initiate();
WORD wColors[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();
char string[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)
{
int delay=0,max_delay=100-10*level; //延迟量
while(delay0)
{
if(temp==-3)
{
textout(handle,2*(x+j)+dx,y+i+dy,wColors,nColors,"◎");
_sleep(30);
}
else if(temp==-2)
{
textout(handle,2*(x+j)+dx,y+i+dy,wColors,nColors,"?");
_sleep(30);
}
else if(temp==1)
textout(handle,2*(x+j)+dx,y+i+dy,wColors,nColors,"◎");
else if(temp==-1)
{
textout(handle,2*(x+j)+dx,y+i+dy,wColors,nColors,"?");
_sleep(30);
}
}
}
void delete_cache() //清除缓冲区
{
while(_kbhit())
{
_getch();
}
}
void delete_blocks(int *a,int w,int h,int x,int y) //覆盖方块 {
HANDLE handle;
handle=initiate();
WORD wColors[1]={SQUARE_COLOR};
for(int i=0;i0)
textout(handle,2*(x+j)+dx,y+i+dy,wColors,1," ");
}
void revolve(int a[][4],int w,int h,int *x,int y) //转动方块
{
int b[4][4]={{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}};
int sign=0,line=0;
for(int i=h-1;i>=0;i--)
{
for(int j=0;j1;i--)
{
delete_blocks(&m[i][1],MAPW-2,1,1,i);
for(int j=1;j
#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 'w'
#define down 's'
#define left 'a'
#define right 'd'
#define esc 'z'
#define MAPW 15 //地图的宽度
#define MAPH 22 //地图的高度
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(); //清除键盘缓冲区 void revolve(int a[][4],int w,int h,int *x,int y); //转动方块 void game();
void end();
void delete_blocks(int *a,int w,int h,int x,int y);
void gameover();
void deletefull_line(int m[][MAPW],int row,int w,int h); //消除一行
int dx=30,dy=0; //屏幕上的偏移量
int score=0,level=0;
int map[MAPH][MAPW];
int a1[4][4]={{1},{1,1,1}}; int a2[4][4]={{0,1},{1,1,1}}; int a3[4][4]={{1,1},{0,1,1}}; int a4[4][4]={{0,0,1},{1,1,1}}; int a5[4][4]={{0,1,1},{1,1}}; int a6[4][4]={{1,1,1,1}};
int a7[4][4]={{1,1},{1,1}}; int a[4][4];
int main() //主菜单选择
{
HANDLE handle;
handle=initiate();
WORD wColors[1];
wColors[0]=FOREGROUND_BLUE| FOREGROUND_GREEN|FOREGROUND_INTENSITY ;
while(1)
{
// sndPlaySound("Resource\\Just Dance.wav",SND_LOOP|SND_ASYNC);//用异
步方式播放音乐,PlaySound函数在开始播放后立即返回
// system("CLS");
// int n=0;
textout(handle,32,11,wColors,1,"开始游戏,请按b");
textout(handle,32,13,wColors,1,"退出游戏,请按q");
int n=_getch();
switch(n)
{
case 'b':
system("CLS");
game();
break;
case 'q':
return 0;
default:
printf("错误,按键继续");
while(!_kbhit());
}
}
}
void game() //游戏主题 {
initiate1(); //初始化界面
int *b=NULL;
b=build(); //创建方块
int m,n,x,y;
while(1)
{
for(int i=0;i<4;i++) //复制方块
for(int j=0;j<4;j++)
if(a[i][j]=*(b+i*4+j)) n=i;
y=1-n;x=4;
delete_blocks(&a[0][0],4,4,16,10);
b=build();
HANDLE handle;
handle=initiate();
WORD wColors[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();
char string[5];
wColors[0]=FOREGROUND_RED|
FOREGROUND_GREEN|FOREGROUND_INTENSITY;
textout(handle,dx-15,4+dy,wColors,1,itoa(score,string,10));
textout(handle,dx-15,14+dy,wColors,1,itoa(level,string,10));
m=1;
while(m)
{
int delay=0,max_delay=50-10*level; //延迟量
while(delay方法:");
textout(handle,dx-15,7+dy,wColors,1,"right d");
textout(handle,dx-15,8+dy,wColors,1,"left a");
textout(handle,dx-15,9+dy,wColors,1,"down s");
textout(handle,dx-15,10+dy,wColors,1,"change shape w");
textout(handle,dx-15,11+dy,wColors,1,"escape z");
textout(handle,dx-15,13+dy,wColors,1,"level");
wColors[0]=FOREGROUND_RED|FOREGROUND_INTENSITY;
drawblocks(&map[0][0],15,20,0,0,wColors,1);
wColors[0]=FOREGROUND_BLUE|
FOREGROUND_GREEN|FOREGROUND_INTENSITY;
textout(handle,dx+8,dy+5,wColors,1,"按任意键开始");
int x=_getch();
srand(time(NULL));
textout(handle,dx+8,dy+5,wColors,1," "); }
int * build() //创建方块
{
int * a=NULL;
int c=rand()%7;
switch(c)
{
case 0:
a=&a1[0][0];break;
case 1:
a=&a2[0][0];break;
case 2:
a=&a3[0][0];break;
case 3:
a=&a4[0][0];break;
case 4:
a=&a5[0][0];break;
case 5:
a=&a6[0][0];break;
case 6:
a=&a7[0][0];break;
}
return a;
}
void drawblocks(int a[],int w,int h,int x,int y,WORD wColors[],int nColors) //画出方块
{
HANDLE handle;
handle = initiate();
int temp;
for(int i=0;i
0)
{
if(temp==-3)
{
textout(handle,2*(x+j)+dx,y+i+dy,wColors,nColors,"?");
_sleep(30);
}
else if(temp==-2)
{
textout(handle,2*(x+j)+dx,y+i+dy,wColors,nColors,"|");
_sleep(30);
}
else if(temp==1)
textout(handle,2*(x+j)+dx,y+i+dy,wColors,nColors,"?");
}
}
void delete_cache() //清除缓冲区
{
while(_kbhit())
{
_getch();
}
}
void delete_blocks(int *a,int w,int h,int x,int y) //覆盖方块 {
HANDLE handle;
handle=initiate();
WORD wColors[1]={SQUARE_COLOR};
for(int i=0;i0)
textout(handle,2*(x+j)+dx,y+i+dy,wColors,1," ");
}
void revolve(int a[][4],int w,int h,int *x,int y) //转动方块
{
int b[4][4]={{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}};
int sign=0,line=0;
for(int i=h-1;i>=0;i--)
{
for(int j=0;j1;i--)
{
delete_blocks(&m[i][1],MAPW-2,1,1,i);
for(int j=1;j