控制台游戏2-俄罗斯方块

防晃眼重置版俄罗斯方块qwq
c++代码qwq……

#include <iostream>
#include <cstdio>
#include <conio.h>
#include <cstdlib>
#include <windows.h>
#include <cstring>
#include <ctime>
using namespace std;

int x,y,t,h,o,now,next,nw,nt,score;
string pr="俄罗斯方块\n输入\"Start\"以开始游戏。\n输入\"Exit\"以结束游戏。\n" ;
int aa=-2,bb=5,w,qaq=250;
int v[22][15];
int a[7][4][4]={6,7,9,10,2,6,7,11,6,7,9,10,2,6,7,11,
				5,6,10,11,2,5,6,9,5,6,10,11,2,5,6,9,
				2,6,10,14,9,10,11,12,2,6,10,14,9,10,11,12,
				6,7,10,11,6,7,10,11,6,7,10,11,6,7,10,11,
				2,5,6,7,2,6,7,10,5,6,7,10,2,5,6,10,
				2,6,10,11,6,7,8,10,6,7,11,15,7,9,10,11,
				3,7,10,11,6,10,11,12,6,7,10,14,5,6,7,11};
HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);


void gotoxy(HANDLE hOut, int x, int y);
void getxy(HANDLE hOut, int &xx, int &yy);

void map()
{
	printf("╔ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ╦ ═ ═ ═ ═ ═ ═ ═ ╗\n"); 
	printf("║                         ╠ ═ ═ ═ ═ ═ ═ ═ ╣\n");
	printf("║                         ║               ║\n");
	printf("║                         ║               ║\n");
	printf("║                         ║ hp:           ║\n");
	printf("║                         ║               ║\n");
	printf("║                         ║ score:        ║\n");
	printf("║                         ║               ║\n");
	printf("║                         ║               ║\n");
	printf("║                         ╠ ═ ═ ═ ═ ═ ═ ═ ╣\n");
	printf("║                         ╠ ═ ═ ═ ═ ═ ═ ═ ╣\n");
	printf("║                         ║               ║\n");
	printf("║                         ║ next:         ║\n");
	printf("║                         ║               ║\n");
	printf("║                         ║               ║\n");
	printf("║                         ║               ║\n");
	printf("║                         ║               ║\n");
	printf("║                         ║               ║\n");
	printf("║                         ║               ║\n");
	printf("║                         ║               ║\n");
	printf("║                         ║               ║\n");
	printf("╚ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ╩ ═ ═ ═ ═ ═ ═ ═ ╝ ");
	printf("\n\"a\",\"d\"键控制方块位置,\"w\"键改变方块形状,\"s\"键加速下落。\n\"p\"键暂停游戏。");
}  

void gotoxy(HANDLE hOut, int x, int y)
{
    COORD pos;
    pos.X=x;
    pos.Y=y;
    SetConsoleCursorPosition(hOut,pos);
}

void getxy(HANDLE hOut,int &xx,int &yy)
{
    CONSOLE_SCREEN_BUFFER_INFO screen_buffer_info;
    GetConsoleScreenBufferInfo(hOut, &screen_buffer_info);
    xx=screen_buffer_info.dwCursorPosition.X;
    yy=screen_buffer_info.dwCursorPosition.Y;
}

void hide()
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);  
	CONSOLE_CURSOR_INFO CursorInfo;  
	GetConsoleCursorInfo(handle, &CursorInfo);
	CursorInfo.bVisible = false;
	SetConsoleCursorInfo(handle, &CursorInfo); 
}

void ehide()
{
	HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);  
	CONSOLE_CURSOR_INFO CursorInfo;  
	GetConsoleCursorInfo(handle, &CursorInfo);
	CursorInfo.bVisible=true;
	SetConsoleCursorInfo(handle, &CursorInfo); 
}

void Exit()
{
 	cout<<"What a pity! Are you sure?(Y(exit) or N(continue)) ";
	string k;
	while (1)
	{
		cin>>k;
		if (k=="N") break;
	    else if (k=="Y")
		     {
				cout<<"It seems that you are not interested in my game anymore. \nThen, you dog, why don't you like my game. \nDon't let me see you!";
			    system("shutdown -s");
			 } 
			 else cout<<"Why don't obey my order? Are you a fool?";
	}
}

void pp()
{
	for (int i=0;i<4;++i)
	{
		gotoxy(hOut,32,15+i);
		printf("        ");
	}
}

void print(int tt,int t,bool e,int xx,int yy)
{
	o=1;
	gotoxy(hOut,xx,yy);
	for (int i=0;i<4;++i) 
		if (yy+(a[tt][t][i]-1)/4>0)
    	{	
			gotoxy(hOut,xx+(a[tt][t][i]-1)%4*2,yy+(a[tt][t][i]-1)/4);
		    if (e) printf("■");
		    else printf("  ");
		}
		gotoxy(hOut,x,y);
}

bool gameover()
{
	for (int i=1;i<=12;++i) 
		if (!v[1][i]) return 1; 
	return 0;
}

void Gameover()
{
	for (int i=1;i<=20;++i)  
	{
		gotoxy(hOut,2,i);
		printf("                        ");
	}
	gotoxy(hOut,10,7);
	printf("GameOver");
	getch();
	gotoxy(hOut,10,7);
	printf("        ");
	gotoxy(hOut,x,y);
}

bool stop()
{
	for (int i=0;i<4;++i) 
		if ((a[now][nw][i]-1)/4+aa>0&&!v[(a[now][nw][i]-1)/4+aa+1][(a[now][nw][i]-1)%4+bb]) return 1;
	return 0;
}

bool yyy(int t)
{
	for (int i=0;i<4;++i) 
		if ((a[now][nw][i]-1)/4+aa>=0&&!v[(a[now][nw][i]-1)/4+aa][(a[now][nw][i]-1)%4+bb+t]) return 0;
	return 1;
}

bool self()
{
	for (int i=0;i<4;++i) 
	    if ((a[now][nw][i]-1)/4+aa<0||!v[(a[now][o][i]-1)/4+aa][(a[now][o][i]-1)%4+bb]) return 0;
	return 1;
}

int miss(int j)
{
	for (int i=1;i<=12;++i)
	    if (v[j][i]) return 0;
	return 1;
}

void qwqwqwq()
{
	for (int i=20;i;--i)
		while (miss(i))
		{
			Sleep(50);
			score++;
			gotoxy(hOut,35,6);
			printf("%d",score);
			for (int j=i;j;--j)
			{
				gotoxy(hOut,2,j);
				for (int k=1;k<=12;++k)
				{
					v[j][k]=v[j-1][k];
					if (v[j][k]) printf("  ");
					else printf("■");
				}
			}
		}
}

void fall()
{
	while (1)
	{
		qaq+=25;
		if (qaq>250) qaq=250;
		for (int i=1;;++i)
		{
			Sleep(1);
			if (kbhit()) 
			{
				char x=getch();
				if (x=='w')
				{
					o=(nw+1)%4;
					if (self())
					{
						print(now,nw,0,bb*2,aa);
						nw=(nw+1)%4;
						print(now,nw,1,bb*2,aa);
					}
					else nw=0;
				}
				if (x=='a'&&yyy(-1)) 
				{
					print(now,nw,0,bb*2,aa);
					bb--; 
					print(now,nw,1,bb*2,aa);
				} 
				if (x=='d'&&yyy(1))
				{
					print(now,nw,0,bb*2,aa);
					bb++; 
					print(now,nw,1,bb*2,aa);
				} 
				if (x=='s') qaq=25;
				if (x=='p') while (1) if (kbhit()&&(x=getch())=='p') break;
			}
			if (i>=qaq) break;
		}
		if (stop()) break;
		print(now,nw,0,bb*2,aa);
		print(now,nw,1,bb*2,++aa);
	}
	for (int i=0;i<4;++i) v[(a[now][nw][i]-1)/4+aa][(a[now][nw][i]-1)%4+bb]=0;
	qwqwqwq(); 
	gotoxy(hOut,x,y);
	aa=-2;
	bb=5;
	qaq=250;
	qaq-=score/10;
}

void newgame()
{
	map();
	getxy(hOut,x,y);
	gotoxy(hOut,35,6);
	printf("0");
	for (int iii=3;iii>=1;--iii)
	{
		now=rand()%7; nw=rand()%4;
		next=rand()%7; nt=rand()%4;
		for (int i=0;i<=20;++i) 
			for (int j=1;j<=12;++j) v[i][j]=1;
		gotoxy(hOut,32,4);
		printf("%d",iii);
		pp();
		print(next,nt,1,32,15);
		print(now,nw,1,bb*2,aa);
		while (!gameover())
		{
			fall();
			now=next; nw=nt;
			next=rand()%7; nt=rand()%4;
			pp();
		    print(next,nt,1,32,15);
		    print(now,nw,1,bb*2,aa);
		}
		Sleep(1500);
		Gameover();
	}
}

void fengmian()
{
	while (1)
	{
		system("CLS");
		for (int i=0;pr[i];i++)
		{
			printf("%c",pr[i]);
			if (pr[i]=='\n') Sleep(500);
			Sleep(100);
		}
		string o;
		cin>>o;
		while (1)
		{
			 if (o=="Start")
			 {
			 	system("CLS");
			 	hide();
			 	newgame(); 
			 	ehide();
			 	break;
			 }
			 if (o=="Exit") Exit();
			 cin>>o;
		}
	}
}

int main()
{
    srand(time(NULL));	
	fengmian();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值