|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这是C++版本的俄罗斯方块,很有特色。
#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <ctime>
#include<windows.h>
#include<conio.h>
using namespace std;
const int kzt[7][4][3][2]={
{{{-1,0},{0,-1},{1,-1}},{{-1,0},{-1,-1},{0,1}},{{-1,0},{0,-1},{1,-1}},{{-1,0},{-1,-1},{0,1}}},
{{{1,0},{0,-1},{-1,-1}},{{-1,0},{0,-1},{-1,1}},{{1,0},{0,-1},{-1,-1}},{{-1,0},{0,-1},{-1,1}}},
{{{-1,0},{1,0},{-1,-1}},{{0,1},{0,-1},{1,-1}},{{-1,0},{1,0},{1,1}},{{0,-1},{0,1},{-1,1}}},
{{{-1,0},{1,0},{1,-1}},{{0,-1},{0,1},{-1,-1}},{{-1,0},{1,0},{-1,1}},{{0,-1},{0,1},{1,1}}},
{{{-1,0},{1,0},{2,0}},{{0,-1},{0,1},{0,2}},{{-1,0},{1,0},{2,0}},{{0,-1},{0,1},{0,2}}},
{{{1,0},{0,-1},{1,-1}},{{1,0},{0,-1},{1,-1}},{{1,0},{0,-1},{1,-1}},{{1,0},{0,-1},{1,-1}}},
{{{-1,0},{1,0},{0,-1}},{{-1,0},{0,1},{0,-1}},{{-1,0},{1,0},{0,1}},{{0,-1},{0,1},{1,0}}}
};//S、Z、L、J、I、O、T
int islose=0;
int cr[10][22];
int dqwz[5];//x,y,块类别,方向,颜色
int nextzt=6;
int nextcr=4;
int speed=7,yanshi;
int score,line;
HANDLE fontcolor;
//初始化
void setpos(int x,int y)
{
COORD pos;
HANDLE handle;
pos.X=x;
pos.Y=y;
handle=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(handle,pos);
}
void print()
{
SetConsoleTextAttribute(fontcolor, FOREGROUND_BLUE|FOREGROUND_INTENSITY);
setpos(2,1);
printf("Score:%4d",score);
setpos(2,2);
printf("Line:%5d",line);
SetConsoleTextAttribute(fontcolor,0);
setpos(14,1);cout<<" ";
setpos(14,2);cout<<" ";
SetConsoleTextAttribute(fontcolor, nextcr<<4|BACKGROUND_INTENSITY);
setpos(16,2);cout<<" "<<endl;
for (int i=0;i<3;i++)
{
setpos(16+2*kzt[nextzt][0][i][0],2+kzt[nextzt][0][i][1]);
printf(" ");
}
for (int i=0;i<20;i++)
{
setpos(2,4+i);
for(int j=0;j<10;j++)
{
if (cr[j][i+2])SetConsoleTextAttribute(fontcolor, cr[j][i+2]<<4|BACKGROUND_INTENSITY);
else SetConsoleTextAttribute(fontcolor,0);
printf(" ");
}
}
return;
}
void xsdqk()
{
SetConsoleTextAttribute(fontcolor, dqwz[4]<<4|BACKGROUND_INTENSITY);
if (2<dqwz[1])
{
setpos(2+2*dqwz[0],2+dqwz[1]);
cout<<" ";
}
for(int i=0;i<3;i++)
{
if (2<dqwz[1]+kzt[dqwz[2]][dqwz[3]][i][1])
{
setpos(2+2*dqwz[0]+2*kzt[dqwz[2]][dqwz[3]][i][0],2+dqwz[1]+kzt[dqwz[2]][dqwz[3]][i][1]);
cout<<" ";
}
}
}
DWORD _stdcall jiemian(LPVOID lpParameter)//线程显示函数
{
if(islose)return 0;
setpos(0,0);
SetConsoleTextAttribute(fontcolor,FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
printf("//----------//--------//\n");
printf("// // //\n");
printf("// // //\n");
printf("//----------//--------//\n");
for (int i=0;i<20;i++)printf(" / / \n");
printf("//--------------------//\n");//初始化大窗体
print();
while(1)
{
print();
xsdqk();
Sleep(100);
}
return 0;
}
void lose()
{
system("title 输");
system("pause>>nul");
exit(0);
}
void nextkuai()
{
if(dqwz[1]<=2){islose=1;lose();}
dqwz[0]=4;dqwz[1]=0;dqwz[2]=nextzt;dqwz[3]=0;dqwz[4]=nextcr;
nextcr=rand()%15+1;
nextzt=rand()%7;
return;
}
void xiaoqu()
{
int ok,wy=0;
int i;
for(i=21;i-wy>=2;i--)
{
ok=1;
while(i-wy>=2)
{
for(int j=0;j<10;j++){if(!cr[j][i-wy]){ok=0;break;}}
if(ok==1)wy++;else break;
}
for(int j=0;j<10;j++)cr[j][i]=cr[j][i-wy];
}
for (;i>=2;i--)
{
for(int j=0;j<10;j++)cr[j][i]=0;
}
if (wy==1)score+=10;
if (wy==2)score+=25;
if (wy==3)score+=40;
if (wy==4)score+=80;
line+=wy;
return;
}
int hefa(int x,int y)
{
if (x<0||x>9||y<0||y>21||cr[x][y]!=0)return 0;
return 1;
}
DWORD _stdcall xialuo(LPVOID lpParameter)//控制下落,消去行,加分,下个块
{
while(1)
{
if (islose)return 0;
Sleep(yanshi);
int ok=1;
if(!hefa(dqwz[0],dqwz[1]+1))ok=0;
for (int i=0;i<3;i++)
if(!hefa(dqwz[0]+kzt[dqwz[2]][dqwz[3]][i][0],dqwz[1]+kzt[dqwz[2]][dqwz[3]][i][1]+1))ok=0;
if (ok)//如果还能继续下落
{
dqwz[1]++;
}
else
{
cr[dqwz[0]][dqwz[1]]=dqwz[4];
for (int i=0;i<3;i++)
{
cr[dqwz[0]+kzt[dqwz[2]][dqwz[3]][i][0]][dqwz[1]+kzt[dqwz[2]][dqwz[3]][i][1]]=dqwz[4];
}
xiaoqu();
nextkuai();
}
}
}
int main()
{
yanshi=1000-speed*100;
srand(int(time(0)));
nextzt=rand()%7;
nextcr=rand()%15+1;
dqwz[1]=20;
nextkuai();
system("title 俄罗斯方块");
system("mode con cols=25 lines=26");
fontcolor = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cursor_info={1,0};
SetConsoleCursorInfo(fontcolor, &cursor_info);//隐藏光标
SetConsoleTextAttribute(fontcolor, FOREGROUND_RED|FOREGROUND_INTENSITY);
printf("按任意键以开始游戏\n");
system("pause>>nul");
CreateThread(NULL,0,jiemian,NULL,0,NULL);//创建一个线程,去执行界面显示函数
CreateThread(NULL,0,xialuo,NULL,0,NULL);//创建一个线程,去执行方块下落函数
while(1)
{
char ch;
ch=_getch();
int ok=1;
switch(ch)
{
case'w':case'W':
if(!hefa(dqwz[0],dqwz[1]))ok=0;
for (int i=0;i<3;i++)
if(!hefa(dqwz[0]+kzt[dqwz[2]][(dqwz[3]+1)%4][i][0],dqwz[1]+kzt[dqwz[2]][(dqwz[3]+1)%4][i][1]))ok=0;
if (ok)dqwz[3]=(dqwz[3]+1)%4;
break;
case'a':case'A':
if(!hefa(dqwz[0]-1,dqwz[1]))ok=0;
for (int i=0;i<3;i++)
if(!hefa(dqwz[0]+kzt[dqwz[2]][dqwz[3]][i][0]-1,dqwz[1]+kzt[dqwz[2]][dqwz[3]][i][1]))ok=0;
if (ok)dqwz[0]--;
break;
case'd':case'D':
if(!hefa(dqwz[0]+1,dqwz[1]))ok=0;
for (int i=0;i<3;i++)
if(!hefa(dqwz[0]+kzt[dqwz[2]][dqwz[3]][i][0]+1,dqwz[1]+kzt[dqwz[2]][dqwz[3]][i][1]))ok=0;
if (ok)dqwz[0]++;
break;
case's':case'S':
while(1)
{
if(!hefa(dqwz[0],dqwz[1]+1))ok=0;
for (int i=0;i<3;i++)
if(!hefa(dqwz[0]+kzt[dqwz[2]][dqwz[3]][i][0],dqwz[1]+kzt[dqwz[2]][dqwz[3]][i][1]+1))ok=0;
if (ok)dqwz[1]++;else break;
}
break;
}
}
return 0;
} |
-
|