俄罗斯方块C4droid

代码如下~~

/*
俄罗斯方块v1.0bate2
by 千百度 2017-5-1

请调整键盘高度为最低,或设置字号为8
*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
//地图范围
#define H 21
#define W 16
typedef struct
{
	int x[4][4];
	int y[4][4];
	int type;
}TETRIS;
TETRIS t1,t2;//方块结构体

TETRIS getxy();//随机获取一组方块坐标
void loading();
void gyrate();//旋转
void remove();//消除行得分
void initmap();//初始化地图
void drawmap();//绘制地图
void move();//向下移动
void gameover();//游戏结束
void *event(void *);//按键事件,独立线程
int x1[][4][4]=
{//方块横坐标
	{0,2,0,0,1,2,3,0},//T型方块
	{0,0,0,0,1,2,3,4},//I
	{1,2,0,0,1,2,0,0},//O
	{1,2,0,0,0,2,3,0},//Z
	{0,2,3,0,1,2,0,0},//S
	{1,0,0,0,1,2,3,0},//J
	{0,0,3,0,1,2,3,0}//L
	
},y1[][4][4]=
{//方块纵坐标
	{0,1,0,0,2,2,2,0},//T型方块
	{0,0,0,0,2,2,2,2},//I
	{1,1,0,0,2,2,0,0},//O
	{1,1,0,0,0,2,2,0},//Z
	{0,1,1,0,2,2,0,0},//S
	{1,0,0,0,2,2,2,0},//J
	{0,0,1,0,2,2,2,0}//L
};
enum //DIRECTION
{
	UP  = '2',
	DOWN = '5',
	LEFT = '4',
	RIGHT = '6',
	PAUSE = '0'
};
//全局变量
volatile int kkk=0,kk=0,k=0,p=1,hs=0,score=0,w=4,h=4,hh,pt,t_sum[7],map[H+1][W+1],pau=1,star = 0;
//主函数
int main()
{
	loading();
	while (p)
	{
		initmap();
		if (pau>0)
		move();
		drawmap();
		usleep(500000);
		clrscr();
	}
}
//加载界面
void loading()
{
	int k;
	printf("\033[?25l"); //隐藏光标
	pthread_t pid1, pid2;
	do
	{
		k=pthread_create(&pid1, NULL, event, NULL);
	}while (k);
	t2=getxy();
	for (int i=0;i<100;i++)
	{
		printf("\033[47;34m\t   loading......%d%%",i);
		usleep(20000);
		drawmap();
		clrscr();
	}
	t1=getxy();
	star=1;
	initmap();
	drawmap();
	printf("\033[40;32m\t按任意键开始游戏……");
	getch();
	clrscr();
}
//初始化地图
void initmap(void)
{
	for (int i=0;i<=H;++i)//定义地图
	for (int j=0;j<W;++j)
	{
		if ((i==H||i==0)&&j<=11)
		map[i][j]=3;
		else 
		if (j==0||j==11)
		map[i][j]=2;
		else
		if (map[i][j]<4)
		map[i][j]=0;
	}
	for (int i=0;i<w;i++)
	for (int j=0;j<h;j++)
	{
		if (t1.y[i][j]!= 0&&t1.x[i][j]!= 0)
		{
		map[t1.y[i][j]][t1.x[i][j]]=1;
		map[H][t1.x[i][j]]=11;
		}
		if (t2.y[i][j]!= 0&&t2.x[i][j]!= 0)
		map[t2.y[i][j]][t2.x[i][j]+8]=-1;
	}
}
//获取随机方块
TETRIS getxy()
{
	TETRIS a;
	srand(time(NULL));//初始化伪随机数生成器 
	int k = (double)rand()/RAND_MAX*7;
//	k=1;                 //单个调试
	a.type=k;
	for (int i=0;i<h;i++)
	for (int j=0;j<w;j++)
	{
		a.x[i][j]=x1[k][i][j];
		a.y[i][j]=y1[k][i][j];
		if (a.y[i][j]!= 0&&a.x[i][j]!= 0)
		a.x[i][j]+=3;
	}
	return a;
}
/*消除判断和得分*/
void remove()
{
	int k[4]={0},sum=0;
	dd:int kk=0;
	for (int i=hh;i>hh-4;i--)
	{
		k[kk++]=1;
		for (int j=1;j<11;j++)
		if (map[i][j]<4)
		k[kk-1]=0;
	}
	kk=0;
	for (int i=hh;i>hh-4;--i)
	{
		if (k[kk++]==1)
		{
			for (int kkk=i;kkk>1;--kkk)
			{
				for (int j=1;j<11;++j)
	    		map[kkk][j]=map[kkk-1][j];
			}
			sum++;
			hs++;
			goto dd;
		}
	}
	score+=(sum*sum*100/2);
}
//方块右移
void right()
{
	for (int i=0;i<h;i++)
	for (int j=0;j<w;j++)
	{
		if (map[t1.y[i][j]][t1.x[i][j]+1]==2||map[t1.y[i][j]][t1.x[i][j]+1]>3)
		{
			kk=1;
			goto k2;
		}
		else
		kk=0;
	}
	k2:
	if (!kk)
	{
		for (int i=0;i<h;i++)
		for (int j=0;j<w;j++)
		{
			if (t1.y[i][j]!= 0&&t1.x[i][j]!= 0)
			t1.x[i][j]++;
		}
	}
	k=0;		
}
//方块左移
void left()
{	
	for (int i=0;i<h;i++)
	for (int j=0;j<w;j++)
	{
		if (map[t1.y[i][j]][t1.x[i][j]-1]==2||map[t1.y[i][j]][t1.x[i][j]-1]>3)
		{
			kk=1;
			goto k1;
		}
		else
		kk=0;
		}
		k1:
		if (!kk)
		{
		for (int i=0;i<h;i++)
		for (int j=0;j<w;j++)
		{
			if (t1.y[i][j]!= 0&&t1.x[i][j]!= 0)
			t1.x[i][j]--;
		}
	}
	k=0;
}
//按键事件
void *event(void*)
{
	sleep(2);
while(p)
{
	usleep(10000);
	while (kbhit())//判断输入,有则循环
	{
		clrscr();
		int key = getch();//获取按键
		switch(key)
		{
			case RIGHT:
			if (pau>0)
			right();
			break;	
			case LEFT:
			if (pau>0)
			left();
			break;
			case UP:
			if (pau>0)
			gyrate();
			k=0;
			break;
			case DOWN:
			if (pau>0)
			move();
			break;
			case PAUSE:
			pau=-pau;
			break;
		}
		initmap();
		drawmap();

	}
}
}
//方块下落
void move()
{
	if (!k)
	{
	for (int i=0;i<h;i++)
	{
		for (int j=0;j<w;j++)
		{
			if (map[t1.y[i][j]+1][t1.x[i][j]]>=3)
			{
				k=1;
			goto kk;
			}
		}
	}
	for (int i=0;i<h;i++)
	{
		for (int j=0;j<w;j++)
		{
			if (map[t1.y[i][j]][t1.x[i][j]]<2)
			t1.y[i][j]++;
		}
	}
	}
	else
	{
		for (int i=0;i<h;i++)
		for (int j=0;j<w;j++)
		{
			if (t1.y[i][j]!= 0&&t1.x[i][j]!= 0)
			{
				switch (t1.type)
				{
					case 0:
					map[t1.y[i][j]][t1.x[i][j]]=4;
					break;
					case 1:
					map[t1.y[i][j]][t1.x[i][j]]=5;
					break;
					case 2:
					map[t1.y[i][j]][t1.x[i][j]]=6;
					break;
					case 3:
					map[t1.y[i][j]][t1.x[i][j]]=7;
					break;
					case 4:
					map[t1.y[i][j]][t1.x[i][j]]=8;
					break;
					case 5:
					map[t1.y[i][j]][t1.x[i][j]]=9;
					break;
					case 6:
					map[t1.y[i][j]][t1.x[i][j]]=10;
					break;
				}
				hh=t1.y[i][j];
			}
		}
		if (t1.y[1][1]-1==1)
		{
			gameover();
		}
		score+=10;
		remove();
		t_sum[t1.type]++;
		t1=t2;
		t2=getxy();
		k=kk=kkk=0;
	}
	kk:;
}
//游戏结束
void gameover()
{
	clrscr();
	drawmap();	printf("游戏结束,任意键继续…\n您的分数为%d",score);
//	usleep(1000000);
	score=0;
	getch();
	for (int i=1;i<H;i++)
	{
		for (int j=1;j<W;j++)
		map[i][j]=0;
	}
}
//绘制主界面
void drawmap()
{
	for (int i=0;i<=H;i++)//画地图
	{
	for (int j=0;j<W;j++)
	{
		switch(map[i][j])
		{
			case 0:
			printf("\33[40;30m++\33[40;37m");//如果地图异常,请添加一个空格
			break;
			case 2:
			printf("\033[40;37m│ \33[40;37m");
			break;
			case 3:
			printf("\033[40;37m﹌\33[40;37m");
			break;
			case 11:
			printf("\033[47;3%dm==\33[40;37m",t1.type);
			break;
			case 1:
			printf("\033[47;3%dm■ \33[40;37m",t1.type);
			break;
			case -1:
			printf("\033[47;3%dm■ \33[40;37m",t2.type);
			break;
			case 4:
			printf("\033[47;30m■ \33[40;37m");
			break;
			case 5:
			printf("\033[47;31m■ \33[40;37m");
			break;
			case 6:
			printf("\033[47;32m■ \33[40;37m");
			break;
			case 7:
			printf("\033[47;33m■ \33[40;37m");
			break;
			case 8:
			printf("\033[47;34m■ \33[40;37m");
			break;
			case 9:
			printf("\033[47;35m■ \33[40;37m");
			break;
			case 10:
			printf("\033[47;36m■ \33[40;37m");
			break;
		}
	}
	
	if (i==8&&pau<0)
	printf("\033[47;34m暂停中");
	if (i==3)
	printf("\033[43;34m 得分: \033[40;37m  %d",score);	
	if (i==4)
	printf("\033[46;34m 行数: \033[40;37m  %d",hs);
	if (i==5)
	printf("\033[42;30m 级别: \033[40;37m  1");
	if (i==0&&star)
	printf("\033[47;30m 俄罗斯方块v1.2\033[40;37m");
	if (i==2)
	printf("\033[47;30m←下一个");
	if (i==10)
	printf("\033[47;30m■ T :\033[40;36m   %d",t_sum[0]);
	if (i==11)
	printf("\033[47;31m■ I :\033[40;36m   %d",t_sum[1]);
	if (i==12)
	printf("\033[47;32m■ O :\033[40;36m   %d",t_sum[2]);
	if (i==13)
	printf("\033[47;33m■ Z :\033[40;36m   %d",t_sum[3]);
	if (i==14)
	printf("\033[47;34m■ S :\033[40;36m   %d",t_sum[4]);
	if (i==15)
	printf("\033[47;35m■ J :\033[40;36m   %d",t_sum[5]);
	if (i==16)
	printf("\033[47;36m■ L :\033[40;36m   %d",t_sum[6]);
	if (i==17)
	
	printf("\033[47;30msum: \033[40;36m   %d",t_sum[0]+t_sum[1]+t_sum[2]+t_sum[3]+t_sum[4]+t_sum[5]+t_sum[6]);	
	if (i==19)
	printf("\033[43;34m 数字键: '4'左移'6'右移 ");
	if (i==20)
	printf("\033[43;34m '2'旋转;'8'下落;'0'暂停");
	printf("\n");
	}
}
	/*方块的旋转*/
void gyrate()
{
	kkk++;
	if (kkk>3)
	kkk=0;
	switch (t1.type)
	{
		case 0:
		switch (kkk)
		{
			case 1:
			if(!map[t1.y[1][1]+1][t1.x[1][1]])
			{
				t1.x[2][1]=t1.x[1][0]+1;
				t1.y[2][1]=t1.y[1][0]+1;
				t1.x[1][0]=t1.y[1][0]=0;
			}
			else kkk--;
			break;
			case 2:
			if(!map[t1.y[1][1]][t1.x[1][1]-1])
			{
				t1.x[1][0]=t1.x[0][1]-1;
				t1.y[1][0]=t1.y[0][1]+1;
				t1.x[0][1]=t1.y[0][1]=0;
			}
			else kkk--;
			break;
			case 3:
			if(!map[t1.y[1][1]][t1.x[1][1]+1])
			{
				t1.x[0][1]=t1.x[1][2]-1;
				t1.y[0][1]=t1.y[1][2]-1;
				t1.x[1][2]=t1.y[1][2]=0;
			}
			else kkk--;
			break;
			case 0:
			if(!map[t1.y[1][1]][t1.x[1][1]+1])
			{
				t1.x[1][2]=t1.x[2][1]+1;
				t1.y[1][2]=t1.y[2][1]-1;
				t1.x[2][1]=t1.y[2][1]=0;
			}
			else kkk--;
			break;
		}
		break;
		case 1:
		if (kkk>1)
		kkk-=2;
		switch (kkk)
		{
			case 1:
			if (!(map[t1.y[1][1]+2][t1.x[1][1]]||map[t1.y[1][1]+1][t1.x[1][1]]))
			{
			    t1.x[0][1]=t1.x[1][0]+1;
			    t1.y[0][1]=t1.y[1][0]-1;
			    t1.x[2][1]=t1.x[1][2]-1;
			    t1.y[2][1]=t1.y[1][2]+1;
			    t1.x[3][1]=t1.x[1][3]-2;
			    t1.y[3][1]=t1.y[1][3]+2;
			    t1.x[1][0]=t1.y[1][0]=
			    t1.x[1][2]=t1.y[1][2]=
			    t1.x[1][3]=t1.y[1][3]=0;}
			    else kkk--;
			break;
			case 0:
			if (!(map[t1.y[1][1]][t1.x[1][1]-1]||map[t1.y[1][1]][t1.x[1][1]+1]||map[t1.y[1][1]][t1.x[1][1]+2]))
			{
			    t1.x[1][0]=t1.x[0][1]-1;
			    t1.y[1][0]=t1.y[0][1]+1;
			    t1.x[1][2]=t1.x[2][1]+1;
			    t1.y[1][2]=t1.y[2][1]-1;
			    t1.x[1][3]=t1.x[3][1]+2;
			    t1.y[1][3]=t1.y[3][1]-2;
			    t1.x[0][1]=t1.y[0][1]=
			    t1.x[2][1]=t1.y[2][1]=
			    t1.x[3][1]=t1.y[3][1]=0;}
			    else kkk--;
			break;
		}
		break;
		case 3:
		if (kkk>1)
		kkk-=2;
		switch (kkk)
		{
			case 1:
			if (!map[t1.y[1][1]+1][t1.x[1][1]])
			{
			    t1.x[0][2]=t1.x[0][0]+2;
			    t1.y[0][2]=t1.y[0][0];
			    t1.x[2][1]=t1.x[0][1];
			    t1.y[2][1]=t1.y[0][1]+2;
			    t1.x[0][0]=t1.x[0][1]=
			    t1.y[0][0]=t1.y[0][1]=0;
			}
			else kkk--;
			break;
			case 0:
			if (!map[t1.y[1][1]-1][t1.x[1][1]-1])
			{
			    t1.x[0][0]=t1.x[0][2]-2;
			    t1.y[0][0]=t1.y[0][2];
			    t1.x[0][1]=t1.x[2][1];
			    t1.y[0][1]=t1.y[2][1]-2;
			    t1.x[0][2]=t1.x[2][1]=
			    t1.y[0][2]=t1.y[0][2]=0;
			}
			else kkk--;
			break;
		}
		break;
		case 4:
		if (kkk>1)
		kkk-=2;
		switch (kkk)
		{
			case 1:
			if (!map[t1.y[1][1]+1][t1.x[1][1]])
			{
			    t1.x[1][2]=t1.x[1][0]+2;
			    t1.y[1][2]=t1.y[1][0];
			    t1.x[2][2]=t1.x[0][2];
			    t1.y[2][2]=t1.y[0][2]+2;
			    t1.x[1][0]=t1.x[0][2]=
			    t1.y[1][0]=t1.y[0][2]=0;
			}
			else kkk--;
			break;
			case 0:
			if (!map[t1.y[1][1]][t1.x[1][1]-1])
			{
			    t1.x[1][0]=t1.x[1][2]-2;
			    t1.y[1][0]=t1.y[1][2];
			    t1.x[0][2]=t1.x[2][2];
			    t1.y[0][2]=t1.y[2][2]-2;
			    t1.x[1][2]=t1.x[2][2]=
			    t1.y[1][2]=t1.y[2][2]=0;
			}
			else kkk--;
			break;
		}
		break;
		case 5:
		switch (kkk)
		{
			case 1:
			if (!(map[t1.y[1][1]+1][t1.x[1][1]]||map[t1.y[1][1]+1][t1.x[1][1]+1]))
			{
			    t1.x[0][1]=t1.x[1][0]+1;
			    t1.y[0][1]=t1.y[1][0]-1;
			    t1.x[2][1]=t1.x[1][2]-1;
			    t1.y[2][1]=t1.y[1][2]+1;
			    t1.x[0][2]=t1.x[0][0]+2;
			    t1.y[0][2]=t1.y[0][0];
			    t1.x[1][0]=t1.y[1][0]=
			    t1.x[1][2]=t1.y[1][2]=
			    t1.x[0][0]=t1.y[0][0]=0;
			}
			else kkk--;
			break;
			case 2:
			if (!map[t1.y[1][1]][t1.x[1][1]-1])
			{
		    	t1.x[1][2]=t1.x[0][1]+1;
			    t1.y[1][2]=t1.y[0][1]+1;
			    t1.x[1][0]=t1.x[2][1]-1;
			    t1.y[1][0]=t1.y[2][1]-1;
			    t1.x[2][2]=t1.x[0][2];
			    t1.y[2][2]=t1.y[0][2]+2;
			    t1.x[0][1]=t1.y[0][1]=
			    t1.x[2][1]=t1.y[2][1]=
			    t1.x[0][2]=t1.y[0][2]=0;
			}
			else kkk--;
			break;
			case 3:
			if (!map[t1.y[1][1]][t1.x[1][1]-1])
			{
			    t1.x[0][1]=t1.x[1][0]+1;
			    t1.y[0][1]=t1.y[1][0]-1;
			    t1.x[2][1]=t1.x[1][2]-1;
			    t1.y[2][1]=t1.y[1][2]+1;
			    t1.x[2][0]=t1.x[2][2]-2;
			    t1.y[2][0]=t1.y[2][2];
			    t1.x[1][0]=t1.y[1][0]=
			    t1.x[1][2]=t1.y[1][2]=
			    t1.x[2][2]=t1.y[2][2]=0;
			}
			else kkk--;
			break;
			case 0:
			if (!(map[t1.y[1][1]][t1.x[1][1]+1]))
			{
			    t1.x[1][2]=t1.x[0][1]+1;
			    t1.y[1][2]=t1.y[0][1]+1;
			    t1.x[1][0]=t1.x[2][1]-1;
			    t1.y[1][0]=t1.y[2][1]-1;
			    t1.x[0][0]=t1.x[2][0];
			    t1.y[0][0]=t1.y[2][0]-2;
			    t1.x[0][1]=t1.y[0][1]=
			    t1.x[2][1]=t1.y[2][1]=
			    t1.x[2][0]=t1.y[2][0]=0;
			}
			else kkk--;
			break;
		}
		break;
		case 6:
		switch (kkk)
		{
			case 1:
			if (!(map[t1.y[1][1]+1][t1.x[1][1]]||map[t1.y[1][1]+1][t1.x[1][1]+1]))
			{
			    t1.x[0][1]=t1.x[1][0]+1;
			    t1.y[0][1]=t1.y[1][0]-1;
			    t1.x[2][1]=t1.x[1][2]-1;
			    t1.y[2][1]=t1.y[1][2]+1;
			    t1.x[2][2]=t1.x[0][2];
			    t1.y[2][2]=t1.y[0][2]+2;
			    t1.x[1][0]=t1.y[1][0]=
			    t1.x[1][2]=t1.y[1][2]=
			    t1.x[0][2]=t1.y[0][2]=0;
			}
			else kkk--;
			break;
			case 2:
			if (!map[t1.y[1][1]][t1.x[1][1]-1])
			{
			    t1.x[1][2]=t1.x[0][1]+1;
			    t1.y[1][2]=t1.y[0][1]+1;
			    t1.x[1][0]=t1.x[2][1]-1;
			    t1.y[1][0]=t1.y[2][1]-1;
			    t1.x[2][0]=t1.x[2][2]-2;
			    t1.y[2][0]=t1.y[2][2];
			    t1.x[0][1]=t1.y[0][1]=
			    t1.x[2][1]=t1.y[2][1]=
			    t1.x[2][2]=t1.y[2][2]=0;
			}
			else kkk--;
			break;
			case 3:
			if (!map[t1.y[1][1]+1][t1.x[1][1]])
			{
			    t1.x[0][1]=t1.x[1][0]+1;
			    t1.y[0][1]=t1.y[1][0]-1;
			    t1.x[2][1]=t1.x[1][2]-1;
			    t1.y[2][1]=t1.y[1][2]+1;
			    t1.x[0][0]=t1.x[2][0];
			    t1.y[0][0]=t1.y[2][0]-2;
			    t1.x[1][0]=t1.y[1][0]=
			    t1.x[1][2]=t1.y[1][2]=
			    t1.x[2][0]=t1.y[2][0]=0;
			}
			else kkk--;
			break;
			case 0:
			if (!(map[t1.y[1][1]][t1.x[1][1]+1]||map[t1.y[1][1]][t1.x[1][1]-1]))
			{
			    t1.x[1][2]=t1.x[0][1]+1;
			    t1.y[1][2]=t1.y[0][1]+1;
			    t1.x[1][0]=t1.x[2][1]-1;
			    t1.y[1][0]=t1.y[2][1]-1;
			    t1.x[0][2]=t1.x[0][0]+2;
			    t1.y[0][2]=t1.y[0][0];
			    t1.x[0][1]=t1.y[0][1]=
			    t1.x[2][1]=t1.y[2][1]=
			    t1.x[0][0]=t1.y[0][0]=0;
			}
			else kkk--;
			break;
		}
		break;
	}
}

 

  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值