这是小弟写的一个c++控制台俄罗斯方块(有点许BUG),欢迎大家拍砖、指教!
这是小弟写的一个c++控制台俄罗斯方块(有些许BUG),欢迎大家拍砖、指教!!!
编译环境:vs2010
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <conio.h>
#include <ctime>
using namespace std;
int map1[22][12]={0};
int map2[22][7]={0};
int level=1,score=0;
int block[7][4][4][4]=
{
{{0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0},{0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0},{0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0},{0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0}},
{{0,1,0,0,0,1,1,0,0,0,1,0,0,0,0,0},{0,0,0,0,0,1,1,0,1,1,0,0,0,0,0,0},{0,0,0,0,0,1,0,0,0,1,1,0,0,0,1,0},{0,0,0,0,0,0,1,1,0,1,1,0,0,0,0,0}},
{{0,1,0,0,0,1,0,0,0,1,1,0,0,0,0,0},{0,0,0,0,0,0,1,0,1,1,1,0,0,0,0,0},{0,0,0,0,0,1,1,0,0,0,1,0,0,0,1,0},{0,0,0,0,0,1,1,1,0,1,0,0,0,0,0,0}},
{{0,0,0,0,0,1,1,1,0,0,1,0,0,0,0,0},{0,1,0,0,0,1,1,0,0,1,0,0,0,0,0,0},{0,0,0,0,0,1,0,0,1,1,1,0,0,0,0,0},{0,0,0,0,0,0,1,0,0,1,1,0,0,0,1,0}},
{{0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0},{0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0},{0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0},{0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0}},
{{0,1,1,0,0,1,0,0,0,1,0,0,0,0,0,0},{0,0,0,0,1,0,0,0,1,1,1,0,0,0,0,0},{0,0,0,0,0,0,1,0,0,0,1,0,0,1,1,0},{0,0,0,0,0,1,1,1,0,0,0,1,0,0,0,0}},
{{0,0,1,0,0,1,1,0,0,1,0,0,0,0,0,0},{0,0,0,0,1,1,0,0,0,1,1,0,0,0,0,0},{0,0,0,0,0,0,1,0,0,1,1,0,0,1,0,0},{0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0}}
};
void draw_next();
class brick
{
public:
int x,y;
int kind1,kind2;
brick(){}
void brick_erase();
void brick_draw();
void brick_solid_draw();
void brick_change();
void brick_down();
bool check_change();
bool check_side(int a,int b);
bool check_floor(int a,int b);
friend void setcursor(int,int);
friend void hidecursor(int ,int);
};
void brick::brick_erase()
{
int i,j;
int count=0;
for (j=y;j<y+4;j++)
{
setcursor(x,y+count);
for (i=x;i<x+4;i++)
{
if (i>0&&i<11&&j>0&&j<21)
{
if (map1[j][i]!=2)
{
map1[j][i]=0;
cout<<" ";
}
else
cout<<"■";
}
else
cout<<"□";
}
count++;
}
}
void brick::brick_solid_draw()
{
int i,j;
int num=0;
for (j=y;j<y+4;j++)
for (i=x;i<x+4;i++)
{
if (i>0&&i<11&&j>0&&j<21)
{
if (map1[j][i]!=2)
{
map1[j][i]=2*block[kind1][kind2][j-y][i-x];
}
}
}
}
void brick::brick_draw()
{
int i,j;
int num=0;
for (j=y;j<y+4;j++)
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X=2*x;
pos.Y=y+num;
SetConsoleCursorPosition(hOut,pos);
for (i=x;i<x+4;i++)
{
if(i>0&&i<11&&j>0&&j<21)
{
if (map1[j][i]!=2)
{
map1[j][i]=block[kind1][kind2][j-y][i-x];
if (map1[j][i]==1)
{
cout<<"■";
}
else
{cout<<" ";}
}
else if (map1[j][i]==2)
{
cout<<"■";
}
}
else if(map1[j][i]==2)
cout<<"□";
}
num++;
}
}
void brick::brick_change()
{
kind2=(++kind2)%4;
brick_erase();
setcursor(x,y);
brick_draw();
}
bool brick::check_change()
{
int a=(kind2+1)%4;
int i,j;
int sum=1;
for (j=y;j<y+4;j++)
{
for (i=x;i<x+4;i++)
{
if ((block[kind1][a][j-y][i-x]+map1[j][i])>2)
{
return false;
sum=0;
break;
}
}
}
if(sum)
return true;
}
void brick::brick_down()
{
brick_erase();
++y;
编译环境:vs2010
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <conio.h>
#include <ctime>
using namespace std;
int map1[22][12]={0};
int map2[22][7]={0};
int level=1,score=0;
int block[7][4][4][4]=
{
{{0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0},{0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0},{0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0},{0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0}},
{{0,1,0,0,0,1,1,0,0,0,1,0,0,0,0,0},{0,0,0,0,0,1,1,0,1,1,0,0,0,0,0,0},{0,0,0,0,0,1,0,0,0,1,1,0,0,0,1,0},{0,0,0,0,0,0,1,1,0,1,1,0,0,0,0,0}},
{{0,1,0,0,0,1,0,0,0,1,1,0,0,0,0,0},{0,0,0,0,0,0,1,0,1,1,1,0,0,0,0,0},{0,0,0,0,0,1,1,0,0,0,1,0,0,0,1,0},{0,0,0,0,0,1,1,1,0,1,0,0,0,0,0,0}},
{{0,0,0,0,0,1,1,1,0,0,1,0,0,0,0,0},{0,1,0,0,0,1,1,0,0,1,0,0,0,0,0,0},{0,0,0,0,0,1,0,0,1,1,1,0,0,0,0,0},{0,0,0,0,0,0,1,0,0,1,1,0,0,0,1,0}},
{{0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0},{0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0},{0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0},{0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0}},
{{0,1,1,0,0,1,0,0,0,1,0,0,0,0,0,0},{0,0,0,0,1,0,0,0,1,1,1,0,0,0,0,0},{0,0,0,0,0,0,1,0,0,0,1,0,0,1,1,0},{0,0,0,0,0,1,1,1,0,0,0,1,0,0,0,0}},
{{0,0,1,0,0,1,1,0,0,1,0,0,0,0,0,0},{0,0,0,0,1,1,0,0,0,1,1,0,0,0,0,0},{0,0,0,0,0,0,1,0,0,1,1,0,0,1,0,0},{0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0}}
};
void draw_next();
class brick
{
public:
int x,y;
int kind1,kind2;
brick(){}
void brick_erase();
void brick_draw();
void brick_solid_draw();
void brick_change();
void brick_down();
bool check_change();
bool check_side(int a,int b);
bool check_floor(int a,int b);
friend void setcursor(int,int);
friend void hidecursor(int ,int);
};
void brick::brick_erase()
{
int i,j;
int count=0;
for (j=y;j<y+4;j++)
{
setcursor(x,y+count);
for (i=x;i<x+4;i++)
{
if (i>0&&i<11&&j>0&&j<21)
{
if (map1[j][i]!=2)
{
map1[j][i]=0;
cout<<" ";
}
else
cout<<"■";
}
else
cout<<"□";
}
count++;
}
}
void brick::brick_solid_draw()
{
int i,j;
int num=0;
for (j=y;j<y+4;j++)
for (i=x;i<x+4;i++)
{
if (i>0&&i<11&&j>0&&j<21)
{
if (map1[j][i]!=2)
{
map1[j][i]=2*block[kind1][kind2][j-y][i-x];
}
}
}
}
void brick::brick_draw()
{
int i,j;
int num=0;
for (j=y;j<y+4;j++)
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X=2*x;
pos.Y=y+num;
SetConsoleCursorPosition(hOut,pos);
for (i=x;i<x+4;i++)
{
if(i>0&&i<11&&j>0&&j<21)
{
if (map1[j][i]!=2)
{
map1[j][i]=block[kind1][kind2][j-y][i-x];
if (map1[j][i]==1)
{
cout<<"■";
}
else
{cout<<" ";}
}
else if (map1[j][i]==2)
{
cout<<"■";
}
}
else if(map1[j][i]==2)
cout<<"□";
}
num++;
}
}
void brick::brick_change()
{
kind2=(++kind2)%4;
brick_erase();
setcursor(x,y);
brick_draw();
}
bool brick::check_change()
{
int a=(kind2+1)%4;
int i,j;
int sum=1;
for (j=y;j<y+4;j++)
{
for (i=x;i<x+4;i++)
{
if ((block[kind1][a][j-y][i-x]+map1[j][i])>2)
{
return false;
sum=0;
break;
}
}
}
if(sum)
return true;
}
void brick::brick_down()
{
brick_erase();
++y;