俄罗斯方块

来源:互联网 发布:万方数据库的检索途径 编辑:程序博客网 时间:2024/06/05 23:52

俄罗斯方块源码

 (2014-11-13 19:47:44)
转载
标签: 

it

 
//最近诗兴大发,写了个俄罗斯方块c++版,拿出来分享,废话不说上源码
//欢迎同我一起交流
#include
#include
#include
#include
#include
#include
int IsOver();//判断是否结束
class Game{//游戏类的定义
private:
int Speed;//速度
int Score;//分数
int Next;//下一个方块的形状
int Random;//是否可以产生下一个方块
public:
Game();
~Game();
void ModifySpeed(int speed);
void ModifyRandom(int random);
void ModifyScore(int score);
void ModifyNext();//自动修改下一个方块
void SetNext(int next);//手动修改下一个方块
int GetRandom();
int GetSpeed();
int GetScore();
int GetNext();
void inforefresh(int method);//信息刷新
};
class Tetris{//方块类的定义
private:
int x_move;//当前方块是否能够自由下落
int y_move;//当前方块是否出现在视野中
//int transform;//当前方块是否能变形
//int random;//是否可以产生方块
int locationx;//方块的4*4矩形的左上角横坐标
int locationy;//方块的4*4矩形的左上角纵坐标
int method;//这个参数无用
int block;//方块
public:
Tetris();
~Tetris();
void setmethod(int me);
int getlocationx();//获取当前坐标的x
int getlocationy();//获取当前坐标的y
void Refresh();//刷新界面函数
void up();//变形函数
int down();//加速函数
int Remove(int &score,int speed,int next);//消除
void left();//左移函数
void right();//右移函数
void display(int score,int speed,int next);//显示函数
    int * FreeFall(int score,int speed,int next);//自由下落函数
void created(int next);//产生方块函数
void Transform(int x,int y);//变形子函数
};
int map[24][12]=//地图
{
//0 1 2 3 4 5 6 7 8 9 a b 
{0,0,0,0,0,0,0,0,0,0,0,0},//0
{0,0,0,0,0,0,0,0,0,0,0,0},//1
{0,0,0,0,0,0,0,0,0,0,0,0},//2
{0,0,0,0,0,0,0,0,0,0,0,0},//3
{1,4,4,4,4,4,4,4,4,4,4,1},//4
{1,0,0,0,0,0,0,0,0,0,0,1},//5
{1,0,0,0,0,0,0,0,0,0,0,1},//6 
{1,0,0,0,0,0,0,0,0,0,0,1},//7
{1,0,0,0,0,0,0,0,0,0,0,1},//8
{1,0,0,0,0,0,0,0,0,0,0,1},//9
{1,0,0,0,0,0,0,0,0,0,0,1},//10
{1,0,0,0,0,0,0,0,0,0,0,1},//11
{1,0,0,0,0,0,0,0,0,0,0,1},//12
{1,0,0,0,0,0,0,0,0,0,0,1},//13
{1,0,0,0,0,0,0,0,0,0,0,1},//14
{1,0,0,0,0,0,0,0,0,0,0,1},//15
{1,0,0,0,0,0,0,0,0,0,0,1},//16
{1,0,0,0,0,0,0,0,0,0,0,1},//17
{1,0,0,0,0,0,0,0,0,0,0,1},//18
{1,0,0,0,0,0,0,0,0,0,0,1},//19
{1,0,0,0,0,0,0,0,0,0,0,1},//20
{1,0,0,0,0,0,0,0,0,0,0,1},//21
{1,0,0,0,0,0,0,0,0,0,0,1},//22
{1,1,1,1,1,1,1,1,1,1,1,1},//23
};
int Block[19][8]={//所有的方块的类型
{2,1,2,2,3,1,3,2},//0
{2,1,2,2,2,3,3,3},//1
{1,2,2,2,3,1,3,2},//2
{2,1,3,1,3,2,3,3},//3
{1,1,1,2,2,1,3,1},//4
{0,2,1,2,2,2,3,2},//5
{3,0,3,1,3,2,3,3},//6
{2,2,3,1,3,2,3,3},//7
{1,1,2,1,2,2,3,1},//8
{2,1,2,2,2,3,3,2},//9
{1,2,2,1,2,2,3,2},//10
{1,1,2,1,2,2,3,2},//11
{2,2,2,3,3,1,3,2},//12
{1,2,2,1,2,2,3,1},//13
{2,1,2,2,3,2,3,3},//14
{2,3,3,1,3,2,3,3},//15
{1,1,2,1,3,1,3,2},//16
{2,1,2,2,2,3,3,1},//17
{1,1,1,2,2,2,3,2},//18
};
Game::Game(){//初始化游戏类
Speed=500;
Score=0;
Random=1;
srand((unsigned)time(NULL));//随机产生方块
Next=rand();
}
Game::~Game(){
}
void Game::ModifySpeed(int speed){
Speed=speed;
}
void Game::ModifyRandom(int random){
Random=random;
}
void Game::ModifyScore(int score){
Score=score;
}
void Game::ModifyNext(){
srand((unsigned)time(NULL));//随机产生方块
Next=rand();
}
void Game::SetNext(int next){
Next=next;
}
int Game::GetRandom(){
return Random;
}
int Game::GetSpeed(){
return Speed;
}
int Game::GetScore(){
return Score;
}
int Game::GetNext(){
return Next;
}
void Game::inforefresh(int method){//信息栏的刷新
int i,j;
HANDLE hout;
COORD coord;
hout=GetStdHandle(STD_OUTPUT_HANDLE);
if(method==0){//刷新分数
coord.X=34;
coord.Y=10;
SetConsoleCursorPosition(hout,coord);
printf("分数: %d",Score);
}
else if(1==method){//刷新下一个方块
for(i=6;i<10;i++){
for(j=34;j<42;j++){
coord.X=j;
coord.Y=i;
SetConsoleCursorPosition(hout,coord);
printf(" ");
}
}
for(i=0;i<8;i=i+2){
coord.X=34+Block[Next][i+1]*2;
coord.Y=6+Block[Next][i];
SetConsoleCursorPosition(hout,coord);
printf("■");
}
}
else if(2==method){//刷新速度
coord.X=34;
coord.Y=11;
SetConsoleCursorPosition(hout,coord);
printf("速度:%dms/格",Speed);
}
coord.X=0;
coord.Y=0;
SetConsoleCursorPosition(hout,coord);
}
Tetris::Tetris(){//方块的初始化
method=0;
x_move=1;
y_move=0;
locationx=0;
locationy=3;
block=100;
}
Tetris::~Tetris(){
}
void Tetris::setmethod(int me){
method=me;
}
void Tetris::created(int next){
int i,j;
j=0;
block=next;
for(i=0;i<4;i++){
map[Block[block][j]+locationx][Block[block][j+1]+locationy]=3;
j=j+2;
}
}
int Tetris::getlocationx(){
return locationx;
}
int Tetris::getlocationy(){
return locationy;
}
void Tetris::Refresh(){
int i,j;
HANDLE hout;
COORD coord;
hout=GetStdHandle(STD_OUTPUT_HANDLE);
for(i=locationx-1;i
for(j=locationy-1;j
coord.X=j*2;
coord.Y=i;
SetConsoleCursorPosition(hout,coord);
if(j>=0&&j<=11&&i>3){
if(map[i][j]==0)printf("  ");
if(map[i][j]==1)printf("□");      //不可穿过的墙
if(map[i][j]==2)printf("■");       //方块的不可动状态 
if(map[i][j]==3)printf("■");       //方块的可动状态
if(map[i][j]==4)printf("□");       //可穿过的墙,主要就是地图的上面的墙
if(map[i][j]==5)printf("□");       //墙和块重合
}
}
}
coord.X=0;
coord.Y=0;
SetConsoleCursorPosition(hout,coord);
}
void Tetris::display(int score,int speed,int next){
int i,j,flag=0,k,l;
HANDLE hout;
COORD coord;
hout=GetStdHandle(STD_OUTPUT_HANDLE);
coord.X=0;
coord.Y=4;
SetConsoleCursorPosition(hout,coord);
for(i=4;i<24;i++){
for(j=0;j<12;j++){
if(map[i][j]==0)printf("  ");
if(map[i][j]==1)printf("□");      //不可穿过的墙
if(map[i][j]==2)printf("■");       //方块的不可动状态 
if(map[i][j]==3)printf("■");       //方块的可动状态
if(map[i][j]==4)printf("□");       //可穿过的墙,主要就是地图的上面的墙
if(map[i][j]==5)printf("□");       //墙和块重合
}
if(i==5){//打印说明文字
printf("          下一个方块:");
}
if(i==6){
printf("          ");
for(k=0;k<8;k=k+2){
if(Block[next][k]==0){
if(flag==0){
flag=1;
for(l=0;l
printf("  ");
}
}
printf("■");
}
}
flag=0;
}
if(i==7){
printf("          ");
for(k=0;k<8;k=k+2){
if(Block[next][k]==1){
if(flag==0){
flag=1;
for(l=0;l
printf("  ");
}
}
printf("■");
}
}
flag=0;
}
if(i==8){
printf("          ");
for(k=0;k<8;k=k+2){
if(Block[next][k]==2){
if(flag==0){
flag=1;
for(l=0;l
printf("  ");
}
}
printf("■");
}
}
flag=0;
}
if(i==9){
printf("          ");
for(k=0;k<8;k=k+2){
if(Block[next][k]==3){
if(flag==0){
flag=1;
for(l=0;l
printf("  ");
}
}
printf("■");
}
}
flag=0;
}
if(i==10){
printf("          分数: %d",score);
}
if(i==11){
printf("          速度:%dms/格",speed);
}
if(i==13){
printf("          说明:↑键变形、↓键加速、←键右移、→左移");
}
if(i==15){
printf("               空格键暂停");
}
if(i==17){
printf("               ESC键退出");
}
printf("\n");
}
}
int * Tetris::FreeFall(int score,int speed,int next){
int i,j,k;
static int a[2];
j=0;
a[0]=0;
a[1]=score;
for(i=0;i<4;i++){//判断是否可以自由下落
if(map[Block[block][j]+locationx+1][Block[block][j+1]+locationy]==1||map[Block[block][j]+locationx+1][Block[block][j+1]+locationy]==2){
x_move=0;
break;
}
j=j+2;
}
if(x_move==1){
for(i=locationx+3;i>=locationx;i--){
for(j=locationy+3;j>=locationy;j--){
if(map[i][j]==3){//分类讨论:要移动的是方块
if(map[i+1][j]==0){//下一个是空格
map[i][j]=0;
map[i+1][j]=3;
}
else if(map[i+1][j]==4){//下一个是可穿过的墙
map[i][j]=0;
map[i+1][j]=5;
}
}
else if(map[i][j]==5){//要移动的是墙和方块重合的快
map[i][j]=4;//地图中只有一面可穿过的墙故只有这以一种情况,下一个是空格
map[i+1][j]=3;
}
}
}
locationx++;
}
else{//方块不可移动
j=0;
for(i=0;i<4;i++){
map[Block[block][j]+locationx][Block[block][j+1]+locationy]=2;//将方块变为不可移动的方块
j=j+2;
}
a[1]=Remove(score,speed,next);//检查是否可以消除,a【1】接收分数
a[0]=1;//是否可以产生方块
}
return a;
}
int Tetris::Remove(int &score,int speed,int next){
int i,j,count=0,k,l;
for(i=22;i>=5;i--){
for(j=1,count=0;j<=10;j++){
if(map[i][j]==0){
count++;
}
}
if(count==10){
}
else if(count==0){
for(k=i;k>5;k--){
for(l=1;l<=10;l++){
map[k][l]=map[k-1][l];
}
}
score=score+100;
display(score,speed,next);
i++;
}
}
return score;
}
void Tetris::right(){
int i,j=0,flag=1;
if(locationx>4&&y_move==0){//方块没完全出来前不允许左右移动
y_move=1;
}
for(i=0;i<4;i++){//检测是否可以进行右移操作
if(map[Block[block][j]+locationx][Block[block][j+1]+locationy+1]==1||map[Block[block][j]+locationx][Block[block][j+1]+locationy+1]==2){
flag=0;
break;
}
j=j+2;
}
if(flag==1&&y_move==1){//可以右移且方块已经完全出现在视野中//是否可以简化里面
for(i=locationx+3;i>=locationx;i--){//3
for(j=locationy+3;j>=locationy;j--){//2
if(map[i][j]==3){//1
map[i][j]=0;
map[i][j+1]=3;
}//1
if(map[i][j]==5){//1
map[i][j]=4;
map[i][j+1]=5;
}//1
}//2
}//3
locationy++;//方块的纵坐标加一
}
}
void Tetris::left(){//跟右移操作类似
int i,j=0,flag=1;
if(locationx>4&&y_move==0){
y_move=1;
}
for(i=0;i<4;i++){//检测是否可以进行右移操作
if(map[Block[block][j]+locationx][Block[block][j+1]+locationy-1]==1||map[Block[block][j]+locationx][Block[block][j+1]+locationy-1]==2){
flag=0;
break;
}
j=j+2;
}
if(flag==1&&y_move==1){
for(i=locationx;i<=locationx+3;i++){//3
for(j=locationy;j<=locationy+3;j++){//2
if(map[i][j]==3){//1
map[i][j]=0;
map[i][j-1]=3;
}//1
if(map[i][j]==5){//1
map[i][j]=4;
map[i][j-1]=5;
}//1
}//2
}//3
locationy--;
}
}
void Tetris::up(){//变形函数,共19中快分7类,每类循环变换
int i,j,flag=0;
if(block==0){
}
else if(block>=1&&block<=4){
Transform(1,4);//这段代码要重复利用,故写成一个函数
}
else if(block>=5&&block<=6){
Transform(5,6);
}
else if(block>=7&&block<=10){
Transform(7,10);
}
else if(block>=11&&block<=12){
Transform(11,12);
}
else if(block>=13&&block<=14){
Transform(13,14);
}
else if(block>=15&&block<=18){
Transform(15,18);
}
}
int Tetris::down(){
return 30;//更改速度
}
void Tetris::Transform(int x,int y){
int i,j,flag=0;
if(block==y){//如果达到循环的边界
for(i=0,j=0;i<4;i++){//检测是否可以变形
if(map[Block[x][j]+locationx][Block[x][j+1]+locationy]==2||map[Block[x][j]+locationx][Block[x][j+1]+locationy]==1){
flag=1;
break;
}
j=j+2;
}
if(flag==0){
for(i=0,j=0;i<4;i++){
if(map[Block[block][j]+locationx][Block[block][j+1]+locationy]==3){//先将原有的块擦除,分两种情况:仅为方块
map[Block[block][j]+locationx][Block[block][j+1]+locationy]=0;
}
else{//块和墙重合
map[Block[block][j]+locationx][Block[block][j+1]+locationy]=4;
}
j=j+2;
}
block=x;//更改方块的类型
for(i=0,j=0;i<4;i++){
if(map[Block[block][j]+locationx][Block[block][j+1]+locationy]==0){//写入新的方块,也分两种情况
map[Block[block][j]+locationx][Block[block][j+1]+locationy]=3;
}
else{
map[Block[block][j]+locationx][Block[block][j+1]+locationy]=5;
}
j=j+2;
}
}
}
else{//没有到边界
for(i=0,j=0;i<4;i++){
if(map[Block[block+1][j]+locationx][Block[block+1][j+1]+locationy]==2||map[Block[block+1][j]+locationx][Block[block+1][j+1]+locationy]==1){
flag=1;
break;
}
j=j+2;
}
if(flag==0){
for(i=0,j=0;i<4;i++){
if(map[Block[block][j]+locationx][Block[block][j+1]+locationy]==3){
map[Block[block][j]+locationx][Block[block][j+1]+locationy]=0;
}
else{
map[Block[block][j]+locationx][Block[block][j+1]+locationy]=4;
}
j=j+2;
}
block++;
for(i=0,j=0;i<4;i++){
if(map[Block[block][j]+locationx][Block[block][j+1]+locationy]==0){
map[Block[block][j]+locationx][Block[block][j+1]+locationy]=3;
}
else{
map[Block[block][j]+locationx][Block[block][j+1]+locationy]=5;
}
j=j+2;
}
}
}
}
int main(){
HANDLE hout;
COORD coord;
Game game;
Tetris *tetris;
int * returnvalue,x,y,count=0,tempnext;
system("color 4e");
tetris=new Tetris();
tetris->created(game.GetNext());
game.ModifyRandom(0);
//srand((unsigned)time(NULL));//随机产生方块
tempnext=rand();//改变下一个方块
game.SetNext(tempnext);//通过手动改变
game.inforefresh(1);
system("cls");
tetris->display(game.GetScore(),game.GetSpeed(),game.GetNext());//显示(改进后只显示一次,后面只刷新局部)
while(!IsOver()){
while(!kbhit()&&!IsOver()){
if(game.GetRandom()==1){
delete tetris;//将原来申请的方块空间返还给操作系统
tetris=NULL;//置为空指针
tetris=new Tetris();//重新创建方块类
tetris->created(game.GetNext());
game.ModifyNext();
game.ModifyRandom(0);
game.inforefresh(1);
}
returnvalue=tetris->FreeFall(game.GetScore(),game.GetSpeed(),game.GetNext());
tetris->Refresh();//刷新
x=returnvalue[0];//x为是否可以产生方块
y=returnvalue[1];//y为分数
game.ModifyRandom(x);
game.ModifyScore(y);
game.inforefresh(0);//信息刷新
Sleep(game.GetSpeed());
if(game.GetScore()<1000){
game.ModifySpeed(500);
game.inforefresh(2);
}
else if(game.GetScore()<3000){
game.ModifySpeed(300);
game.inforefresh(2);
}
else if(game.GetScore()<10000){
game.ModifySpeed(200);
game.inforefresh(2);
}
else if(game.GetScore()<50000){
game.ModifySpeed(100);
game.inforefresh(2);
}
tetris->setmethod(0);
}
switch(getch())
{
case 72:
tetris->up();
tetris->Refresh();
break;
case 80:
game.ModifySpeed(tetris->down());
game.inforefresh(2);
break;
case 75:
tetris->left();
tetris->Refresh();
break;
case 77:
tetris->right();
tetris->Refresh();
break;
case 32:
coord.X=0;
coord.Y=0;
SetConsoleCursorPosition(hout,coord);
system("pause");
break;
case 27:
exit(0);
break;
}
}
return 0;
}
int IsOver(){
int i;
for(i=1;i<11;i++){
if(map[5][i]==2){
HANDLE hout;
COORD coord;
coord.X=0;
coord.Y=0;
hout=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hout,coord);
printf("对不起!您输啦?\n");
return 1;
}
}
return 0;
}

0 0
原创粉丝点击