C++实现俄罗斯方块

本文详细介绍了使用C++实现俄罗斯方块的代码,包括游戏逻辑、方块生成、移动、旋转、消除行等功能。代码虽然存在一些小问题,但基本实现了游戏的核心玩法。此外,还提供了关键函数的解析,如print_board、make_block、getrand等。
摘要由CSDN通过智能技术生成

 c++实现俄罗斯方块还是比较简单的,我给大家提供一种解决方式

目录

效果:

代码

这是关于latest_log的函数

make_block函数

getrand函数

down,left,right三个函数

rotate函数

两个判断函数

大循环

主函数


代码如下


#include <windows.h>
#include<bits/stdc++.h>
#include<stdlib.h>
#include<conio.h>
#include <cstdlib>
#include <ctime>
using namespace std;
#define b_num 5
bool setting_champ=false;//这个值改了会报错
int board[20][10];
int color[20][10];
#define blue SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_BLUE);
#define green SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_GREEN);
#define red SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_RED);
#define cyan SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_GREEN | FOREGROUND_BLUE);
#define yellow SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_RED | FOREGROUND_GREEN);
#define white SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
#define purple SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_RED | FOREGROUND_BLUE);
string blocks[5][5]={{"00010203","00102030","-","-","-"},{"00011112","10110102","00101121","10110120","-"},{"00011011","-","-","-","-"},{"00102001","01112120","10111202","00010210","-"},{"01121011","00102011","01101121","00010211","-"}};
int mt_x=3,mt_y=0,stage,nowblock;
int highscore=0;
int point=0;
string replay_code="";
unsigned int co=0;
void mainloop(); 
void lat_op(string n){
    ofstream file;
	file.open ("latest_log", ios::out | ios::app );
	file<<n;
	file.close();
} 
void lat_op2(int n){
    ofstream file;
	file.open ("latest_log", ios::out | ios::app );
	file<<n;
	file.close();
} 
void print_board(){
	lat_op("printboard...\n");
	try{
	system("cls");
	printf("    ____________________ \n");
	for(int i=0;i<20;i++){
		printf("   |");
		for(int j=0;j<10;j++){
			if(board[i][j]==0&&i!=2){
				cyan
				printf("  ");
			}
			else if(board[i][j]==0&&i==2){
				cyan
				printf("--");
			}
			else if(board[i][j]==1||board[i][j]==2){
				white
				if(color[i][j]==1){
					blue
				}
				else if(color[i][j]==2){
					green
				}
				else if(color[i][j]==3){
					red
				}
				else if(color[i][j]==4){
					yellow
				}
				else if(color[i][j]==5){
					cyan
				}
				else if(color[i][j]==6){
					purple
				}
				printf("▋▋");
				cyan
			}
		}
		printf("|");
		if(i==1){
			white
			printf("  points: \n");
			cyan
		}
		else if(i==3){
			white
			printf("  %d\n",point);
			cyan
		}
		else if(i==5){
			white
			printf("  highscore: \n");
			cyan
		}
		else if(i==7){
			white
			printf("  %d\n",highscore);
			cyan
		}
		else if(i==9&&highscore<=point) {
			red
			printf("  You broke\n");
			cyan
		}
		else if(i==10&&highscore<=point) {
			red
			printf("  the record!\n");
			cyan
		}
		else if(i==12) {
			white
			printf("  by CSDN\n");
			cyan
		}
        else if(i==13) {
			white
			printf("  Ryan1233\n");
			cyan
		}
		else{
			printf("\n");
		}
	} 
	printf("    ____________________ \n");
	lat_op("printboard_finshed\n");
} 
catch(...){
	lat_op("error_detected\n");
	mainloop();
}
}

int getrand();
void make_block(int thing,int stage){
	lat_op("makingblock...\n");
	try{
 
		string blocknow="";
  		thing=thing%b_num;
  		//thing=3;
  		nowblock=thing;
  		
  		lat_op("gettingblock...\n");
  		
  		blocknow=blocks[thing][stage];
  		
  		lat_op("gotblock:");
  		lat_op(blocknow);
  		lat_op("\n");
  		
  		
		srand((unsigned)time(NULL));
  		co=(0+rand()%(6)+1);
  		
  		for(int i=0;i<blocknow.length();i+=2){
  			
  		lat_op("read:");
  		lat_op2(blocknow[i]-'0');
  		lat_op2(blocknow[i+1]-'0');
  		lat_op("\n");
  		lat_op("block making at (");
  		lat_op2(mt_y+(blocknow[i]-'0'));
  		lat_op(",");
  		lat_op2(mt_x+(blocknow[i+1]-'0'));
  		lat_op(")\n");
  		
  		board[mt_y+(blocknow[i]-'0')][mt_x+(blocknow[i+1]-'0')]=2;
  		color[mt_y+(blocknow[i]-'0')][mt_x+(blocknow[i+1]-'0')]=co;
  		
  		lat_op("block_made\n");
		}
		lat_op("makeblock_finished\n");
	}
	catch(...){
		lat_op("error_detected\n");
		mainloop();
	}	
}
void clear(){
	lat_op("clearing_board...\n");
	try{
	for(int i=0;i<20;i++){
		for(int j=0;j<10;j++){
			board[i][j]=0;
		}
	}
	lat_op("board_cleared\n");
}
catch(...){
		lat_op("error_detected\n");
		mainloop();
	}
}
int getrand(){
	try{
		lat_op("getting_random number...\n");
		unsigned int thing=0;
		srand((unsigned)time(NULL));
  		thing=(0+rand()%(b_num-0+1));
  		lat_op("number=");
  		lat_op2(thing);
  		lat_op("\n");
  		return thing;
	} 
	catch(...){
		lat_op("error_detected\n");
		mainloop();
	}
}
int down(){
	try{ 
	lat_op("moving_block_(down)...\n");
	for(int i=19;i>=0;i--){
		for(int j=9;j>=0;j--){
			if(board[i][j]==2){
				if(board[i+1][j]==1||i==19){
					return 1;
				}
				else if(i<19&&board[i+1][j]==0){
					continue;
				}
			}
		}		
	} 
	for(int i=19;i>=0;i--){
		for(int j=9;j>=0;j--){
			if(board[i][j]==2){
				board[i+1][j]=2;
				board[i][j]=0;
				color[i+1][j]=color[i][j];
				color[i][j]=0;
			}
		}
	}
	mt_y++;
	lat_op("block_moved\n");
	return 0;
    }
    catch(...){
    	lat_op("error_detected\n");
        mainloop();
	} 
}
int left(){
	try{
	lat_op("moving_block_(left)...\n");
	for(int i=0;i<20;i++){
		for(int j=0;j<10;j++){
			if(board[i][j]==2){
				if(board[i][j-1]==1||j==0){
					return 1;
				}
				else if(j>0&&board[i][j-1]==0){
					continue;
				}
			}
		}		
	} 
	for(int i=0;i<20;i++){
		for(int j=0;j<10;j++){
			if(board[i][j]==2){
				board[i][j-1]=2;
				board[i][j]=0;
				color[i][j-1]=color[i][j];
				color[i][j]=0;
			}
		}
	}
	if(mt_x>0)mt_x--;
	lat_op("block_moved\n");
	return 0;
	}
	catch(...){
		lat_op("error_detected\n");
		mainloop();
	}
}
int right(){
	try{
	lat_op("moving_block_(right)...\n");
	for(int i=19;i>=0;i--){
		for(int j=9;j>=0;j--){
			if(board[i][j]==2){
				if(board[i][j+1]==1||j==9){
					return 1;
				}
				else if(j<9&&board[i][j+1]==0){
					continue;
				}
			}
		}		
	} 
	for(int i=19;i>=0;i--){
		for(int j=9;j>=0;j--){
			if(board[i][j]==2){
				board[i][j+1]=2;
				board[i][j]=0;
				color[i][j+1]=color[i][j];
				color[i][j]=0;
			}
		}
	}
	if(mt_x<19)mt_x++;
	lat_op("block_moved\n");
	return 0;
	}
	catch(...){
		lat_op("error_detected\n");
		mainloop();
	}
}
void cf_lines(){
	try{
	lat_op("checking_for_cleaned_lines...\n");
	int cnt=0,k; 
	for(int i=0;i<20;i++){
		for(int j=0;j<10;j++){
			if(board[i][j]==1){
				cnt++;
			}
			k=i;
		}
		if(cnt==10){
			lat_op("found,moving...\n");
			point+=1000;
			for(int j=0;j<10;j++){
				board[k][j]=0;
			}
			for(i=k;i>=0;i--){
				for(int j=9;j>=0;j--){
					if(board[i][j]==1){
						if(i<19){
							board[i+1][j]=1;
							color[i+1][j]=color[i][j];
						}
						board[i][j]=0;
						color[i][j]=0;
					}
				}
			}	
			lat_op("moved\n");
			return;
		}
		cnt=0;
	}
	lat_op("not_found\n");
}
catch(...){
	lat_op("error_detected\n");
		mainloop();
	}
}
void rotate(){
	try{
	lat_op("rotating_as_block:");
	lat_op2(nowblock);
	lat_op("\nstage:");
	lat_op2(stage);
	lat_op("\n");
	for(int i=0;i<20;i++){
		for(int j=0;j<10;j++){
			if(board[i][j]==2){
				board[i][j]=0;
			}
		}
	}
	if(blocks[nowblock][stage+1]=="-"){
		stage=0;
		make_block(nowblock,stage);
	}
	else{
		make_block(nowblock,++stage);
	}
	print_board();
	return;
	}
	catch(...){
		lat_op("error_detected\n");
		mainloop();
	}
}
int cf_high(){
	try{
	lat_op("checking_for_hight_exceedment...\n");
	for(int i=0;i<10;i++){
		if(board[2][i]==1){
			lat_op("found\n");
			return 1;
		}
	}
	lat_op("not_found\n");
	return 0;
}
catch(...){
	lat_op("error_detected\n");
	mainloop();
	}

}
void mainloop(){
	lat_op("rebooting\n");
	int ch;
	try{
	while(1){
		print_board();
        lat_op("score: ");
        lat_op2(score);
        lat_op("\n");
		if(_kbhit()){
			ch=_getch();
			if(ch==72){
				rotate();
			}
			else if(ch==75){
				left();
			}
			else if(ch==77){
				right();
			}
			else if(ch==80){
				int task=down();
				if(task==1){
					point+=10;
					print_board();
					_sleep(1000);
					for(int l=19;l>=0;l--){
						for(int m=9;m>=0;m--){
							if(board[l][m]==2){
								board[l][m]=1;
								continue;
							}
						}
					}
					mt_x=3;
					mt_y=0;
					stage=0;
					make_block(getrand(),stage);
				}
			}
			
			else if(ch==27){
				lat_op("killing_sequence_initiated\n");
				system("cls");
				print_board();
				printf("finalscore:%d\ncontinue?Y/N\n",point);
				char con;
				while(1){
					if(_kbhit()){
						con=_getch();
						if(con==121){
							lat_op("rescued\n");
							printf("Yes");
							clear();
							_sleep(500);
							mt_x=3;
							mt_y=0;
							stage=0;
							nowblock=0;
							point=0;
							make_block(getrand(),stage);
							mainloop();
							return;
						}
						else{
							lat_op("crush\n");
							printf("No");
							return;
						}
					}
				}	
			}
			else if(ch==32){
				lat_op("paused\n");
				print_board();
				printf("press_any_key_to_countinue...");
				while(1){
					if(_kbhit()){
						lat_op("rescued\n");
						mainloop();
						return;
					}
				}
			}
		}
		_sleep(100);
		int task=down();
		if(task==1){
			point+=10;
			print_board();
			_sleep(1000);
			for(int l=19;l>=0;l--){
				for(int m=9;m>=0;m--){
					if(board[l][m]==2){
						board[l][m]=1;
						continue;
					}
				}
			}
			mt_x=3;
			mt_y=0;
			stage=0;
			make_block(getrand(),stage);
			
		}
		cf_lines();
		if(cf_high()==1){
			//down();
			system("cls");
			print_board();
			printf("finalscore:%d\ncontinue?Y/N\n",point);
			char con;
			while(1){
				if(_kbhit()){
					con=_getch();
					if(con==121){
						printf("Yes");
						clear();
						_sleep(500);
						mt_x=3;
						mt_y=0;
						stage=0;
						nowblock=0;
						point=0;
						make_block(getrand(),stage);
						mainloop();
						return;
					}
					else{
						printf("No");
						_sleep(500);
						return;
					}
				}
			}	
		}
		if(highscore<=point){
			if(setting_champ==true) WinExec("copy_champ_log.exe", SW_SHOWMINIMIZED);
			highscore=point;
		}
		ofstream file;
		file.open ("score", ios::out);
		file<<highscore;
		file.close();
		
	}	
	
}
catch(...){
	mainloop();
}
}
int main(){
	system("color 0B"); 
	ofstream filedel;
	filedel.open ("latest_log", ios::out | ios::trunc);
	filedel.close();
	lat_op("starting_log:\n\n") ;
	ifstream copy;
	copy.open ("copy", ios::in);
	copy>>setting_champ;
	copy.close();
	try{
	system("mode con cols=40 lines=25");
	SetConsoleTitle("Tetris");
	fstream filein ("score", ios::in);
	filein>>highscore;
	filein.close();
	mt_x=3;
	mt_y=0;
	stage=0;
	make_block(getrand(),stage);
	mainloop();
	ofstream file;
	file.open ("score", ios::out);
	file<<highscore;
	file.close();
	//if(highscore==point){
	//	WinExec("copy_champ_log.exe", SW_SHOWMINIMIZED);
	//}
	lat_op("log_end\n\n") ;
	return 0;
}
catch(...){
	printf("UNE DETECTED");
	return 0;
}
}

效果:

目前这个代码还有很多问题,如旋转时颜色改变,但勉强能用

以下函数请忽略try-catch部分

那是我修bug瞎写的

lat_op的两个函数

我起名的方式真的不怎么样

代码

这是关于latest_log的函数

void lat_op(string n){
    ofstream file;
	file.open ("latest_log", ios::out | ios::app );
	file<<n;
	file.close();
} 
void lat_op2(int n){
    ofstream file;
	file.open ("latest_log", ios::out | ios::app );
	file<<n;
	file.close();
} 

就是打开文件,后面插一段。

#define blue SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_BLUE);
#define green SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_GREEN);
#define red SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_RED);
#define cyan SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_GREEN | FOREGROUND_BLUE);
#define yellow SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_RED | FOREGROUND_GREEN);
#define white SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
#define purple SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_RED | FOREGROUND_BLUE);
void print_board(){
	lat_op("printboard...\n");
	try{
	system("cls");
	printf("    ____________________ \n");
	for(int i=0;i<20;i++){
		printf("   |");
		for(int j=0;j<10;j++){
			if(board[i][j]==0&&i!=2){
				cyan
				printf("  ");
			}
			else if(board[i][j]==0&&i==2){
				cyan
				printf("--");
			}
			else if(board[i][j]==1||board[i][j]==2){
				white
				if(color[i][j]==1){
					blue
				}
				else if(color[i][j]==2){
					green
				}
				else if(color[i][j]==3){
					red
				}
				else if(color[i][j]==4){
					yellow
				}
				else if(color[i][j]==5){
					cyan
				}
				else if(color[i][j]==6){
					purple
				}
				printf("▋▋");
				cyan
			}
		}
		printf("|");
		if(i==1){
			white
			printf("  points: \n");
			cyan
		}
		else if(i==3){
			white
			printf("  %d\n",point);
			cyan
		}
		else if(i==5){
			white
			printf("  highscore: \n");
			cyan
		}
		else if(i==7){
			white
			printf("  %d\n",highscore);
			cyan
		}
		else if(i==9&&highscore<=point) {
			red
			printf("  You broke\n");
			cyan
		}
		else if(i==10&&highscore<=point) {
			red
			printf("  the record!\n");
			cyan
		}
		else if(i==12) {
			white
			printf("  by CSDN\n");
			cyan
		}
        else if(i==13) {
			white
			printf("  Ryan1233\n");
			cyan
		}
		else{
			printf("\n");
		}
	} 
	printf("    ____________________ \n");
	lat_op("printboard_finshed\n");
} 
catch(...){
	lat_op("error_detected\n");
	mainloop();
}
}

遍历数组判断这个位置上应该显示什么

0——没有方块

1——已固定的方块

2——下落中的方块

这种方式并没有区分各个块,因此在后面会产生问题,如果有能修改的大佬可以改一下

make_block函数

string blocks[5][5]={{"00010203","00102030","-","-","-"},{"00011112","10110102","00101121","10110120","-"},{"00011011","-","-","-","-"},{"00102001","01112120","10111202","00010210","-"},{"01121011","00102011","01101121","00010211","-"}};

 设置↑

void make_block(int thing,int stage){
	lat_op("makingblock...\n");
	try{
 
		string blocknow="";
  		thing=thing%b_num;
  		//thing=3;
  		nowblock=thing;
  		
  		lat_op("gettingblock...\n");
  		
  		blocknow=blocks[thing][stage];
  		
  		lat_op("gotblock:");
  		lat_op(blocknow);
  		lat_op("\n");
  		
  		
		srand((unsigned)time(NULL));
  		co=(0+rand()%(6)+1);
  		
  		for(int i=0;i<blocknow.length();i+=2){
  			
  		lat_op("read:");
  		lat_op2(blocknow[i]-'0');
  		lat_op2(blocknow[i+1]-'0');
  		lat_op("\n");
  		lat_op("block making at (");
  		lat_op2(mt_y+(blocknow[i]-'0'));
  		lat_op(",");
  		lat_op2(mt_x+(blocknow[i+1]-'0'));
  		lat_op(")\n");
  		
  		board[mt_y+(blocknow[i]-'0')][mt_x+(blocknow[i+1]-'0')]=2;
  		color[mt_y+(blocknow[i]-'0')][mt_x+(blocknow[i+1]-'0')]=co;
  		
  		lat_op("block_made\n");
		}
		lat_op("makeblock_finished\n");
	}
	catch(...){
		lat_op("error_detected\n");
		mainloop();
	}	
}

负责将我写的方块设置转换到数组中去,同时赋予随机的颜色(之后就知道为什么旋转时颜色改变了

起始点是(3,0)

int mt_x=3,mt_y=0,stage,nowblock;

从起始点加上一定的坐标,在该处将数组值改为2(下落方块)

getrand函数

int getrand(){
	try{
		lat_op("getting_random number...\n");
		unsigned int thing=0;
		srand((unsigned)time(NULL));
  		thing=(0+rand()%(b_num-0+1));
  		lat_op("number=");
  		lat_op2(thing);
  		lat_op("\n");
  		return thing;
	} 
	catch(...){
		lat_op("error_detected\n");
		mainloop();
	}
}

非常正常的随机数函数

利用当前毫秒数实现伪随机

down,left,right三个函数

int down(){
	try{ 
	lat_op("moving_block_(down)...\n");
	for(int i=19;i>=0;i--){
		for(int j=9;j>=0;j--){
			if(board[i][j]==2){
				if(board[i+1][j]==1||i==19){
					return 1;
				}
				else if(i<19&&board[i+1][j]==0){
					continue;
				}
			}
		}		
	} 
	for(int i=19;i>=0;i--){
		for(int j=9;j>=0;j--){
			if(board[i][j]==2){
				board[i+1][j]=2;
				board[i][j]=0;
				color[i+1][j]=color[i][j];
				color[i][j]=0;
			}
		}
	}
	mt_y++;
	lat_op("block_moved\n");
	return 0;
    }
    catch(...){
    	lat_op("error_detected\n");
        mainloop();
	} 
}
int left(){
	try{
	lat_op("moving_block_(left)...\n");
	for(int i=0;i<20;i++){
		for(int j=0;j<10;j++){
			if(board[i][j]==2){
				if(board[i][j-1]==1||j==0){
					return 1;
				}
				else if(j>0&&board[i][j-1]==0){
					continue;
				}
			}
		}		
	} 
	for(int i=0;i<20;i++){
		for(int j=0;j<10;j++){
			if(board[i][j]==2){
				board[i][j-1]=2;
				board[i][j]=0;
				color[i][j-1]=color[i][j];
				color[i][j]=0;
			}
		}
	}
	if(mt_x>0)mt_x--;
	lat_op("block_moved\n");
	return 0;
	}
	catch(...){
		lat_op("error_detected\n");
		mainloop();
	}
}
int right(){
	try{
	lat_op("moving_block_(right)...\n");
	for(int i=19;i>=0;i--){
		for(int j=9;j>=0;j--){
			if(board[i][j]==2){
				if(board[i][j+1]==1||j==9){
					return 1;
				}
				else if(j<9&&board[i][j+1]==0){
					continue;
				}
			}
		}		
	} 
	for(int i=19;i>=0;i--){
		for(int j=9;j>=0;j--){
			if(board[i][j]==2){
				board[i][j+1]=2;
				board[i][j]=0;
				color[i][j+1]=color[i][j];
				color[i][j]=0;
			}
		}
	}
	if(mt_x<19)mt_x++;
	lat_op("block_moved\n");
	return 0;
	}
	catch(...){
		lat_op("error_detected\n");
		mainloop();
	}
}

通过遍历实现将所有处于“2”状态的值移动

第一个循环先判断是否被阻挡

然后第二个循环移动

遍历方向与移动方向相反很重要,不然会覆盖其他值

  ▋▋▋

▋  ▋▋

▋▋  ▋

▋▋▋  

这是正常实现(遍历方向从左到右,移动方向从右到左)

  ▋▋▋

  ▋▋

  ▋

▋     

方向反了


rotate函数

旋转方块,也是我写的最粗糙的函数

通过清空并重新放入旋转后的方块实现

void rotate(){
	try{
	lat_op("rotating_as_block:");
	lat_op2(nowblock);
	lat_op("\nstage:");
	lat_op2(stage);
	lat_op("\n");
	for(int i=0;i<20;i++){
		for(int j=0;j<10;j++){
			if(board[i][j]==2){
				board[i][j]=0;
			}
		}
	}
	if(blocks[nowblock][stage+1]=="-"){
		stage=0;
		make_block(nowblock,stage);
	}
	else{
		make_block(nowblock,++stage);
	}
	print_board();
	return;
	}
	catch(...){
		lat_op("error_detected\n");
		mainloop();
	}
}

这里用到了make_block函数,这也是旋转改变颜色bug的原因

两个判断函数

void cf_lines(){
	try{
	lat_op("checking_for_cleaned_lines...\n");
	int cnt=0,k; 
	for(int i=0;i<20;i++){
		for(int j=0;j<10;j++){
			if(board[i][j]==1){
				cnt++;
			}
			k=i;
		}
		if(cnt==10){
			lat_op("found,moving...\n");
			point+=1000;
			for(int j=0;j<10;j++){
				board[k][j]=0;
			}
			for(i=k;i>=0;i--){
				for(int j=9;j>=0;j--){
					if(board[i][j]==1){
						if(i<19){
							board[i+1][j]=1;
							color[i+1][j]=color[i][j];
						}
						board[i][j]=0;
						color[i][j]=0;
					}
				}
			}	
			lat_op("moved\n");
			return;
		}
		cnt=0;
	}
	lat_op("not_found\n");
}
catch(...){
	lat_op("error_detected\n");
		mainloop();
	}
}



int cf_high(){
	try{
	lat_op("checking_for_hight_exceedment...\n");
	for(int i=0;i<10;i++){
		if(board[2][i]==1){
			lat_op("found\n");
			return 1;
		}
	}
	lat_op("not_found\n");
	return 0;
}
catch(...){
	lat_op("error_detected\n");
	mainloop();
	}

}

第一个寻找满的行并将所有上面的行向下移动一格

注意事项同移动的三个函数

第二个在第三行遍历是否有超过高线的方块(此处有值为1)

大循环

void mainloop(){
	lat_op("rebooting\n");
	int ch;
	try{
	while(1){
		print_board();
		lat_op("score: ");
        lat_op2(score);
        lat_op("\n");
		if(_kbhit()){
			ch=_getch();
			if(ch==72){
				rotate();
			}
			else if(ch==75){
				left();
			}
			else if(ch==77){
				right();
			}
			else if(ch==80){
				int task=down();
				if(task==1){
					point+=10;
					print_board();
					_sleep(1000);
					for(int l=19;l>=0;l--){
						for(int m=9;m>=0;m--){
							if(board[l][m]==2){
								board[l][m]=1;
								continue;
							}
						}
					}
					mt_x=3;
					mt_y=0;
					stage=0;
					make_block(getrand(),stage);
				}
			}
			
			else if(ch==27){
				lat_op("killing_sequence_initiated\n");
				system("cls");
				print_board();
				printf("finalscore:%d\ncontinue?Y/N\n",point);
				char con;
				while(1){
					if(_kbhit()){
						con=_getch();
						if(con==121){
							lat_op("rescued\n");
							printf("Yes");
							clear();
							_sleep(500);
							mt_x=3;
							mt_y=0;
							stage=0;
							nowblock=0;
							point=0;
							make_block(getrand(),stage);
							mainloop();
							return;
						}
						else{
							lat_op("crush\n");
							printf("No");
							return;
						}
					}
				}	
			}
			else if(ch==32){
				lat_op("paused\n");
				print_board();
				printf("press_any_key_to_countinue...");
				while(1){
					if(_kbhit()){
						lat_op("rescued\n");
						mainloop();
						return;
					}
				}
			}
		}
		_sleep(100);
		int task=down();
		if(task==1){
			point+=10;
			print_board();
			_sleep(1000);
			for(int l=19;l>=0;l--){
				for(int m=9;m>=0;m--){
					if(board[l][m]==2){
						board[l][m]=1;
						continue;
					}
				}
			}
			mt_x=3;
			mt_y=0;
			stage=0;
			make_block(getrand(),stage);
			
		}
		cf_lines();
		if(cf_high()==1){
			//down();
			system("cls");
			print_board();
			printf("finalscore:%d\ncontinue?Y/N\n",point);
			char con;
			while(1){
				if(_kbhit()){
					con=_getch();
					if(con==121){
						printf("Yes");
						clear();
						_sleep(500);
						mt_x=3;
						mt_y=0;
						stage=0;
						nowblock=0;
						point=0;
						make_block(getrand(),stage);
						mainloop();
						return;
					}
					else{
						printf("No");
						_sleep(500);
						return;
					}
				}
			}	
		}
		if(highscore<=point){
			if(setting_champ==true) WinExec("copy_champ_log.exe", SW_SHOWMINIMIZED);
			highscore=point;
		}
		ofstream file;
		file.open ("score", ios::out);
		file<<highscore;
		file.close();
		
	}	
	
}
catch(...){
	mainloop();
}
}

负责键盘控制以及暂停和退出

键盘事件

↑:72

↓:80

←:75

→:77

Esc:121

空格:32

向下的时候如果被阻挡则锁定所有方块并再生成一个

向下的代码有两处

主函数

int main(){
	system("color 0B"); 
	ofstream filedel;
	filedel.open ("latest_log", ios::out | ios::trunc);
	filedel.close();
	lat_op("starting_log:\n\n") ;
	ifstream copy;
	copy.open ("copy", ios::in);
	copy>>setting_champ;
	copy.close();
	try{
	system("mode con cols=40 lines=25");
	SetConsoleTitle("Tetris");
	fstream filein ("score", ios::in);
	filein>>highscore;
	filein.close();
	mt_x=3;
	mt_y=0;
	stage=0;
	make_block(getrand(),stage);
	mainloop();
	ofstream file;
	file.open ("score", ios::out);
	file<<highscore;
	file.close();
	//if(highscore==point){
	//	WinExec("copy_champ_log.exe", SW_SHOWMINIMIZED);
	//}
	lat_op("log_end\n\n") ;
	return 0;
}
catch(...){
	printf("UNE DETECTED");
	return 0;
}
}

主要是一堆的窗口格式设置

还有最高分的记录

system("mod con cols=x lines=y")

前面的是列数,后面的是行数

代码共587行,但无意义的很多,有意义的大约在400行左右。

如果有大佬可以帮忙改一下bug吗

第一次写,求支持

  • 20
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值