C++版本俄罗斯方块
这是C++版本的俄罗斯方块,很有特色。#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <ctime>
#include<windows.h>
#include<conio.h>
using namespace std;
const int kzt={
{{{-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;
int dqwz;//x,y,块类别,方向,颜色
int nextzt=6;
int nextcr=4;
int speed=7,yanshi;
int score,line;
HANDLEfontcolor;
//初始化
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,2+kzt);
printf("");
}
for (int i=0;i<20;i++)
{
setpos(2,4+i);
for(int j=0;j<10;j++)
{
if (cr)SetConsoleTextAttribute(fontcolor, cr<<4|BACKGROUND_INTENSITY);
else SetConsoleTextAttribute(fontcolor,0);
printf("");
}
}
return;
}
void xsdqk()
{
SetConsoleTextAttribute(fontcolor, dqwz<<4|BACKGROUND_INTENSITY);
if (2<dqwz)
{
setpos(2+2*dqwz,2+dqwz);
cout<<"";
}
for(int i=0;i<3;i++)
{
if (2<dqwz+kzt]])
{
setpos(2+2*dqwz+2*kzt]],2+dqwz+kzt]]);
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<=2){islose=1;lose();}
dqwz=4;dqwz=0;dqwz=nextzt;dqwz=0;dqwz=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){ok=0;break;}}
if(ok==1)wy++;else break;
}
for(int j=0;j<10;j++)cr=cr;
}
for (;i>=2;i--)
{
for(int j=0;j<10;j++)cr=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!=0)return 0;
return 1;
}
DWORD _stdcall xialuo(LPVOID lpParameter)//控制下落,消去行,加分,下个块
{
while(1)
{
if (islose)return 0;
Sleep(yanshi);
int ok=1;
if(!hefa(dqwz,dqwz+1))ok=0;
for (int i=0;i<3;i++)
if(!hefa(dqwz+kzt]],dqwz+kzt]]+1))ok=0;
if (ok)//如果还能继续下落
{
dqwz++;
}
else
{
cr]]=dqwz;
for (int i=0;i<3;i++)
{
cr+kzt]]]+kzt]]]=dqwz;
}
xiaoqu();
nextkuai();
}
}
}
int main()
{
yanshi=1000-speed*100;
srand(int(time(0)));
nextzt=rand()%7;
nextcr=rand()%15+1;
dqwz=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,dqwz))ok=0;
for (int i=0;i<3;i++)
if(!hefa(dqwz+kzt][(dqwz+1)%4],dqwz+kzt][(dqwz+1)%4]))ok=0;
if (ok)dqwz=(dqwz+1)%4;
break;
case'a':case'A':
if(!hefa(dqwz-1,dqwz))ok=0;
for (int i=0;i<3;i++)
if(!hefa(dqwz+kzt]]-1,dqwz+kzt]]))ok=0;
if (ok)dqwz--;
break;
case'd':case'D':
if(!hefa(dqwz+1,dqwz))ok=0;
for (int i=0;i<3;i++)
if(!hefa(dqwz+kzt]]+1,dqwz+kzt]]))ok=0;
if (ok)dqwz++;
break;
case's':case'S':
while(1)
{
if(!hefa(dqwz,dqwz+1))ok=0;
for (int i=0;i<3;i++)
if(!hefa(dqwz+kzt]],dqwz+kzt]]+1))ok=0;
if (ok)dqwz++;else break;
}
break;
}
}
return 0;
} 可以{:10_256:} 厉害 反正我一个初二的一脸蒙
你这个游戏设计的思路是什么 可以给我说说吗?它的图像是怎么形成的
页:
[1]