我是靠谱客的博主 体贴犀牛,最近开发中收集的这篇文章主要介绍基于stm32的俄罗斯方块游戏机(4、俄罗斯方块基本规则函数)(大三寒假),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

  1. 申明一下,本项目里面的执行函数,算法,都是我个人的理解。算法可能比较垃圾,可能好在32的处理能力还可以,所以最后也成功了。如果你有更好的算法或者认为我的很垃圾,能交流交流更好,但千万不要喷!
  2. 我个人觉得这一块是这个项目的难点了,我在这里只能简单介绍一下我的思路
  3. 首先第一个:下落的函数,这个函数比较简单,但我们需要一个变量来控制速度,所以我们可以这样设计:利用32里面的定时器,大概每10微秒中断一次,在中断函数里面,让一个变量自加。
void TIM4_IRQHandler(void)
{
	if(TIM_GetITStatus(TIM4,TIM_IT_Update))
	{
		TIM_ClearITPendingBit(TIM4,TIM_IT_Update);
		i++;
	}	
}

然后在主函数里面如果i大于speed变量那就执行向下移动函数(这个函数在上一片中介绍过了)

所以这样就可以通过控制speed变量的值来控制下降的速度了。

  1. 下面这个就是判断函数了,判断它有没有碰撞,这个函数是一个难点。首先我们需要存一下我们游戏的状态,就是那个地方有方块 那个地方没有,所以最好的办法就是利用一个二维数组。
u8 zhuangtai[23][16]=
{
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},//**********0**********//
	{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},//**********1**********//
	{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},//**********2**********//
	{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},//**********3**********//
	{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},//**********4**********//
	{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},//**********5**********//
	{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},//**********6**********//
	{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},//**********7**********//
	{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},//**********8**********//
	{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},//**********9**********//
	{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},//**********10**********//
	{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},//**********11**********//
	{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},//**********12**********//
	{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},//**********13**********//
	{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},//**********14**********//
	{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},//**********15**********//
	{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},//**********16**********//
	{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},//**********17**********//
	{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},//**********18**********//
	{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},//**********19**********//
	{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},//**********20**********//
	{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},//**********21**********//
	{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},//**********22**********//
};

看到没,这里面像不像一个容器,1代表有方块,在最外部全部赋1就是当做墙壁用。
这里我并没有用世界规定的俄罗斯方块的长与宽。就是随便设计的,我的是14X22的
在这里我们需要写一些和上一片文章中显示俄罗斯方块函数类似的函数,只是显示函数作用在lcd上,但现在的函数作用这个二维数组中,原理很类似。

这里我就不多做解释了,不懂得可以看一下我的上一篇文章。看看能不能启发一下你,为什么在这里需要这些函数。

/*************************************************
功能:更新一个状态到数组
*************************************************/
void Draw_a_zhuangtai(u16 x,u16 y)
{
	  zhuangtai[y/10][x/10+1]=1;
}


/*************************************************
功能:删除一个状态到数组
*************************************************/
void Deal_a_zhuangtai(u16 x,u16 y)
{
	  zhuangtai[y/10][x/10+1]=0;
}



/*************************************************
功能:画对应的状态图形
*************************************************/
void Draw_zhuangtai_tuxing(u16 x,u16 y,u8 what)
{
	switch (what)
	{
		case 1:
		{
		Draw_a_zhuangtai(x,y);
		Draw_a_zhuangtai(x+10,y);
		Draw_a_zhuangtai(x,y+10);
		Draw_a_zhuangtai(x+10,y+10);
		}
		break;
		
		case 2:
		{
		Draw_a_zhuangtai(x,y);
		Draw_a_zhuangtai(x+10,y);
		Draw_a_zhuangtai(x+20,y);
		Draw_a_zhuangtai(x+30,y);
		}
		break;
		
		case 3:
		{
		Draw_a_zhuangtai(x,y);
		Draw_a_zhuangtai(x,y+10);
		Draw_a_zhuangtai(x,y+20);
		Draw_a_zhuangtai(x,y+30);
		}
		break;
		
		case 4:
		{
		Draw_a_zhuangtai(x+10,y);
		Draw_a_zhuangtai(x,y+10);
		Draw_a_zhuangtai(x+10,y+10);
		Draw_a_zhuangtai(x+20,y+10);
		}
		break;
		
		case 5:
		{
		Draw_a_zhuangtai(x+10,y+10);
		Draw_a_zhuangtai(x,y);
		Draw_a_zhuangtai(x,y+10);
		Draw_a_zhuangtai(x,y+20);
		}
		break;

		case 6:
		{
		Draw_a_zhuangtai(x,y+10);
		Draw_a_zhuangtai(x+10,y);
		Draw_a_zhuangtai(x+10,y+10);
		Draw_a_zhuangtai(x+10,y+20);
		}
		break;
		
		case 7:
		{
		Draw_a_zhuangtai(x+10,y+10);
		Draw_a_zhuangtai(x,y);
		Draw_a_zhuangtai(x+10,y);
		Draw_a_zhuangtai(x+20,y);
		}
		break;
	
		case 8:
		{
		Draw_a_zhuangtai(x,y);
		Draw_a_zhuangtai(x,y+10);
		Draw_a_zhuangtai(x,y+20);
		Draw_a_zhuangtai(x+10,y+20);
		}
		break;
		
		case 9:
		{
		Draw_a_zhuangtai(x,y);
		Draw_a_zhuangtai(x,y+10);
		Draw_a_zhuangtai(x+10,y);
		Draw_a_zhuangtai(x+20,y);
		}
		break;
		
		case 10:
		{
		Draw_a_zhuangtai(x,y);
		Draw_a_zhuangtai(x+10,y);
		Draw_a_zhuangtai(x+10,y+10);
		Draw_a_zhuangtai(x+10,y+20);
		}
		break;
		
		case 11:
		{
		Draw_a_zhuangtai(x,y+10);
		Draw_a_zhuangtai(x+10,y+10);
		Draw_a_zhuangtai(x+20,y+10);
		Draw_a_zhuangtai(x+20,y);
		}
		break;
		
		case 12:
		{
		Draw_a_zhuangtai(x+10,y);
		Draw_a_zhuangtai(x+10,y+10);
		Draw_a_zhuangtai(x+10,y+20);
		Draw_a_zhuangtai(x,y+20);
		}
		break;
		
		case 13:
		{
		Draw_a_zhuangtai(x,y);
		Draw_a_zhuangtai(x+10,y);
		Draw_a_zhuangtai(x+20,y);
		Draw_a_zhuangtai(x+20,y+10);
		}
		break;
		
		case 14:
		{
		Draw_a_zhuangtai(x,y);
		Draw_a_zhuangtai(x+10,y);
		Draw_a_zhuangtai(x,y+10);
		Draw_a_zhuangtai(x,y+20);
		}
		break;
		
		case 15:
		{
		Draw_a_zhuangtai(x,y);
		Draw_a_zhuangtai(x,y+10);
		Draw_a_zhuangtai(x+10,y+10);
		Draw_a_zhuangtai(x+20,y+10);
		}
		break;
		
		case 16:
		{
		Draw_a_zhuangtai(x+10,y);
		Draw_a_zhuangtai(x,y+10);
		Draw_a_zhuangtai(x+10,y+10);
		Draw_a_zhuangtai(x,y+20);
		}
		break;
		
		case 17:
		{
		Draw_a_zhuangtai(x,y);
		Draw_a_zhuangtai(x+10,y);
		Draw_a_zhuangtai(x+10,y+10);
		Draw_a_zhuangtai(x+20,y+10);
		}
		break;
		
		case 18:
		{
		Draw_a_zhuangtai(x,y);
		Draw_a_zhuangtai(x,y+10);
		Draw_a_zhuangtai(x+10,y+10);
		Draw_a_zhuangtai(x+10,y+20);
		}
		break;
		
		case 19:
		{
		Draw_a_zhuangtai(x,y+10);
		Draw_a_zhuangtai(x+10,y+10);
		Draw_a_zhuangtai(x+10,y);
		Draw_a_zhuangtai(x+20,y);
		}
		break;
	}

}

/*************************************************
功能:删除对应的状态图形
*************************************************/
void Deal_zhuangtai_tuxing(u16 x,u16 y,u8 what)
{
	switch (what)
	{
		case 1:
		{
		Deal_a_zhuangtai(x,y);
		Deal_a_zhuangtai(x+10,y);
		Deal_a_zhuangtai(x,y+10);
		Deal_a_zhuangtai(x+10,y+10);
		}
		break;
		
		case 2:
		{
		Deal_a_zhuangtai(x,y);
		Deal_a_zhuangtai(x+10,y);
		Deal_a_zhuangtai(x+20,y);
		Deal_a_zhuangtai(x+30,y);
		}
		break;
		
		case 3:
		{
		Deal_a_zhuangtai(x,y);
		Deal_a_zhuangtai(x,y+10);
		Deal_a_zhuangtai(x,y+20);
		Deal_a_zhuangtai(x,y+30);
		}
		break;
		
		case 4:
		{
		Deal_a_zhuangtai(x+10,y);
		Deal_a_zhuangtai(x,y+10);
		Deal_a_zhuangtai(x+10,y+10);
		Deal_a_zhuangtai(x+20,y+10);
		}
		break;
		
		case 5:
		{
		Deal_a_zhuangtai(x+10,y+10);
		Deal_a_zhuangtai(x,y);
		Deal_a_zhuangtai(x,y+10);
		Deal_a_zhuangtai(x,y+20);
		}
		break;

		case 6:
		{
		Deal_a_zhuangtai(x,y+10);
		Deal_a_zhuangtai(x+10,y);
		Deal_a_zhuangtai(x+10,y+10);
		Deal_a_zhuangtai(x+10,y+20);
		}
		break;
		
		case 7:
		{
		Deal_a_zhuangtai(x+10,y+10);
		Deal_a_zhuangtai(x,y);
		Deal_a_zhuangtai(x+10,y);
		Deal_a_zhuangtai(x+20,y);
		}
		break;
	
		case 8:
		{
		Deal_a_zhuangtai(x,y);
		Deal_a_zhuangtai(x,y+10);
		Deal_a_zhuangtai(x,y+20);
		Deal_a_zhuangtai(x+10,y+20);
		}
		break;
		
		case 9:
		{
		Deal_a_zhuangtai(x,y);
		Deal_a_zhuangtai(x,y+10);
		Deal_a_zhuangtai(x+10,y);
		Deal_a_zhuangtai(x+20,y);
		}
		break;
		
		case 10:
		{
		Deal_a_zhuangtai(x,y);
		Deal_a_zhuangtai(x+10,y);
		Deal_a_zhuangtai(x+10,y+10);
		Deal_a_zhuangtai(x+10,y+20);
		}
		break;
		
		case 11:
		{
		Deal_a_zhuangtai(x,y+10);
		Deal_a_zhuangtai(x+10,y+10);
		Deal_a_zhuangtai(x+20,y+10);
		Deal_a_zhuangtai(x+20,y);
		}
		break;
		
		case 12:
		{
		Deal_a_zhuangtai(x+10,y);
		Deal_a_zhuangtai(x+10,y+10);
		Deal_a_zhuangtai(x+10,y+20);
		Deal_a_zhuangtai(x,y+20);
		}
		break;
		
		case 13:
		{
		Deal_a_zhuangtai(x,y);
		Deal_a_zhuangtai(x+10,y);
		Deal_a_zhuangtai(x+20,y);
		Deal_a_zhuangtai(x+20,y+10);
		}
		break;
		
		case 14:
		{
		Deal_a_zhuangtai(x,y);
		Deal_a_zhuangtai(x+10,y);
		Deal_a_zhuangtai(x,y+10);
		Deal_a_zhuangtai(x,y+20);
		}
		break;
		
		case 15:
		{
		Deal_a_zhuangtai(x,y);
		Deal_a_zhuangtai(x,y+10);
		Deal_a_zhuangtai(x+10,y+10);
		Deal_a_zhuangtai(x+20,y+10);
		}
		break;
		
		case 16:
		{
		Deal_a_zhuangtai(x+10,y);
		Deal_a_zhuangtai(x,y+10);
		Deal_a_zhuangtai(x+10,y+10);
		Deal_a_zhuangtai(x,y+20);
		}
		break;
		
		case 17:
		{
		Deal_a_zhuangtai(x,y);
		Deal_a_zhuangtai(x+10,y);
		Deal_a_zhuangtai(x+10,y+10);
		Deal_a_zhuangtai(x+20,y+10);
		}
		break;
		
		case 18:
		{
		Deal_a_zhuangtai(x,y);
		Deal_a_zhuangtai(x,y+10);
		Deal_a_zhuangtai(x+10,y+10);
		Deal_a_zhuangtai(x+10,y+20);
		}
		break;
		
		case 19:
		{
		Deal_a_zhuangtai(x,y+10);
		Deal_a_zhuangtai(x+10,y+10);
		Deal_a_zhuangtai(x+10,y);
		Deal_a_zhuangtai(x+20,y);
		}
		break;
	}
}
/*************************************************
功能:向下移动状态
*************************************************/
void Down_zhuangtai_move(u16 x,u16 y,u16 what)
{
	Deal_zhuangtai_tuxing(x,y,what);
	Draw_zhuangtai_tuxing(x,y+10,what);
}

/*************************************************
功能:向左移动状态
*************************************************/
void Left_zhuangtai_move(u16 x,u16 y,u8 what)
{
	Deal_zhuangtai_tuxing(x,y,what);
	Draw_zhuangtai_tuxing(x-10,y,what);
}

/*************************************************
功能:向右移动状态
*************************************************/
void Right_zhuangtai_move(u16 x,u16 y,u8 what)
{
	Deal_zhuangtai_tuxing(x,y,what);
	Draw_zhuangtai_tuxing(x+10,y,what);
}

至于判断它有没有碰撞,我提供两种思路,我写了两个函数也是都可以的,一个原理容易想到,但是写起来很麻烦,还有一种是最后才想到的,但语句表较短。、

思路一(垃圾一点的思路):假设某个方块需要向上下左右某个方向移动,那么我们就先判断它朝向的那个方向,是否有和它贴近的方块,也就是数组中的1。如果有那就说明前面有障碍,不可以移动了。

功能:判断是否碰撞(重点)!!
//x,y表示坐标,what表示是哪一种方块(前面的文章中给出了不同的what值对应的图形),fangxiang代表要移动的方向其中 1:向左2:向下3:向右4:表示如果方块变化(这里可以仔细想想,比如那个最长的方块可以有两种变化,竖着的和横着的,如果它竖着进入一个很小的空间,此时想想可不可以把他变横起来,显然不可以应该横起来就会膨胀,所以这个也需要判断
//在下面的这个函数中我没有加fangxiang=4的情况,应该太麻烦了,第二个思路中有
//碰撞了返回值1,没有碰撞就返回0。
int panduan(u16 x,u16 y,u8 what,u8 fangxiang)
{
	x=x/10+1;
	y=y/10;
	switch(what)
	{
		case 1: 
		{
		switch(fangxiang)
			{
			  case 1:if(zhuangtai[y][x-1]||zhuangtai[y+1][x-1])return 1; else return 0;
				case 2:if(zhuangtai[y+2][x]||zhuangtai[y+2][x+1])return 1; else return 0;
				case 3:if(zhuangtai[y][x+2]||zhuangtai[y+1][x+2])return 1; else return 0;
		  }	
		}			

		
		case 2: 
		{
		switch(fangxiang)
			{
			  case 1:if(zhuangtai[y][x-1])return 1; else return 0;
				case 2:if(zhuangtai[y+1][x]||zhuangtai[y+1][x+1]||zhuangtai[y+1][x+2]==1||zhuangtai[y+1][x+3])return 1; else return 0;
				case 3:if(zhuangtai[y][x+4])return 1; else return 0;
		  }	
		}			

	  
		case 3: 
		{
		switch(fangxiang)
			{
			  case 1:if(zhuangtai[y][x-1]||zhuangtai[y+1][x-1]||zhuangtai[y+2][x-1]||zhuangtai[y+3][x-1])return 1; else return 0;
				case 2:if(zhuangtai[y+4][x])return 1; else return 0;
				case 3:if(zhuangtai[y][x+1]||zhuangtai[y+1][x+1]||zhuangtai[y+2][x+1]||zhuangtai[y+3][x+1])return 1; else return 0;
		  }	
		}			

		
		case 4: 
		{
		switch(fangxiang)
			{
			  case 1:if(zhuangtai[y+1][x-1]||zhuangtai[y][x])return 1; else return 0;
				case 2:if(zhuangtai[y+2][x]||zhuangtai[y+2][x+1]||zhuangtai[y+2][x+2])return 1; else return 0;
				case 3:if(zhuangtai[y][x+2]||zhuangtai[y+1][x+3])return 1; else return 0;
		  }	
		}			

		
		case 5: 
		{
		switch(fangxiang)
			{
			  case 1:if(zhuangtai[y][x-1]||zhuangtai[y+1][x-1]||zhuangtai[y+2][x-1]==1)return 1; else return 0;
				case 2:if(zhuangtai[y+3][x]||zhuangtai[y+2][x+1])return 1; else return 0;
				case 3:if(zhuangtai[y][x+1]||zhuangtai[y+1][x+2]||zhuangtai[y+2][x+1]==1)return 1; else return 0;
		  }	
		}			

		
		case 6: 
		{
		switch(fangxiang)
			{
			  case 1:if(zhuangtai[y][x]||zhuangtai[y+1][x-1]||zhuangtai[y+2][x]==1)return 1; else return 0;
				case 2:if(zhuangtai[y+2][x]||zhuangtai[y+3][x+1])return 1; else return 0;
				case 3:if(zhuangtai[y][x+2]||zhuangtai[y+1][x+2]||zhuangtai[y+2][x+3])return 1; else return 0;
		  }	
		}			

		
		case 7: 
		{
		switch(fangxiang)
			{
			  case 1:if(zhuangtai[y][x-1]||zhuangtai[y+1][x])return 1; else return 0;
				case 2:if(zhuangtai[y+1][x]||zhuangtai[y+2][x+1]||zhuangtai[y+1][x+2])return 1; else return 0;
				case 3:if(zhuangtai[y][x+3]||zhuangtai[y+1][x+2])return 1; else return 0;
		  }	
		}			

		
		case 8: 
		{
		switch(fangxiang)
			{
			  case 1:if(zhuangtai[y][x-1]||zhuangtai[y+1][x-1]||zhuangtai[y+2][x-1])return 1; else return 0;
				case 2:if(zhuangtai[y+3][x]||zhuangtai[y+3][x+1])return 1; else return 0;
				case 3:if(zhuangtai[y][x+1]||zhuangtai[y+1][x+1]||zhuangtai[y+2][x+2])return 1; else return 0;
		  }	
		}			
	
		
		case 9: 
		{
		switch(fangxiang)
			{
			  case 1:if(zhuangtai[y][x-1]||zhuangtai[y+1][x-1])return 1; else return 0;
				case 2:if(zhuangtai[y+2][x]||zhuangtai[y+1][x+1]||zhuangtai[y+1][x+2])return 1; else return 0;
				case 3:if(zhuangtai[y][x+3]||zhuangtai[y+1][x+1])return 1; else return 0;
		  }	
		}			
	
		
		case 10: 
		{
		switch(fangxiang)
			{
			  case 1:if(zhuangtai[y][x-1]||zhuangtai[y+1][x]||zhuangtai[y+2][x])return 1; else return 0;
				case 2:if(zhuangtai[y+1][x]||zhuangtai[y+3][x+1])return 1; else return 0;
				case 3:if(zhuangtai[y][x+2]||zhuangtai[y+1][x+2]||zhuangtai[y+2][x+2])return 1; else return 0;
		  }	
		}			
		
		case 11: 
		{
		switch(fangxiang)
			{
			  case 1:if(zhuangtai[y+1][x-1]||zhuangtai[y][x+1])return 1; else return 0;
				case 2:if(zhuangtai[y+2][x]||zhuangtai[y+2][x+1]||zhuangtai[y+2][x+2])return 1; else return 0;
				case 3:if(zhuangtai[y][x+3]||zhuangtai[y+1][x+3])return 1; else return 0;
		  }	
		}
		
		case 12: 
		{
		switch(fangxiang)
			{
			  case 1:if(zhuangtai[y][x]||zhuangtai[y+1][x]||zhuangtai[y+2][x-1])return 1; else return 0;
				case 2:if(zhuangtai[y+3][x]||zhuangtai[y+3][x+1])return 1; else return 0;
				case 3:if(zhuangtai[y][x+2]||zhuangtai[y+1][x+2]||zhuangtai[y+2][x+2])return 1; else return 0;
		  }	
		}
		
		case 13: 
		{
		switch(fangxiang)
			{
			  case 1:if(zhuangtai[y][x-1]||zhuangtai[y+1][x+1])return 1; else return 0;
				case 2:if(zhuangtai[y+1][x]||zhuangtai[y+1][x+1]||zhuangtai[y+2][x+2])return 1; else return 0;
				case 3:if(zhuangtai[y][x+3]||zhuangtai[y+1][x+3])return 1; else return 0;
		  }	
		}
		
		case 14: 
		{
		switch(fangxiang)
			{
			  case 1:if(zhuangtai[y][x-1]||zhuangtai[y+1][x-1]||zhuangtai[y+2][x-1])return 1; else return 0;
				case 2:if(zhuangtai[y+3][x]||zhuangtai[y+1][x+1])return 1; else return 0;
				case 3:if(zhuangtai[y][x+2]||zhuangtai[y+1][x+1]||zhuangtai[y+2][x+2])return 1; else return 0;
		  }	
		}
		
		case 15: 
		{
		switch(fangxiang)
			{
			  case 1:if(zhuangtai[y][x-1]||zhuangtai[y+1][x-1])return 1; else return 0;
				case 2:if(zhuangtai[y+2][x]||zhuangtai[y+2][x+1]||zhuangtai[y+2][x+2])return 1; else return 0;
				case 3:if(zhuangtai[y][x+1]||zhuangtai[y+1][x+3])return 1; else return 0;
		  }	
		}
		
		case 16: 
		{
		switch(fangxiang)
			{
			  case 1:if(zhuangtai[y][x]||zhuangtai[y+1][x-1]||zhuangtai[y+2][x-1])return 1; else return 0;
				case 2:if(zhuangtai[y+3][x]||zhuangtai[y+2][x+1])return 1; else return 0;
				case 3:if(zhuangtai[y][x+2]||zhuangtai[y+1][x+2]||zhuangtai[y+2][x+1])return 1; else return 0;
		  }	
		}
		
		case 17: 
		{
		switch(fangxiang)
			{
			  case 1:if(zhuangtai[y][x-1]||zhuangtai[y+1][x])return 1; else return 0;
				case 2:if(zhuangtai[y+1][x]||zhuangtai[y+2][x+1]||zhuangtai[y+2][x+2])return 1; else return 0;
				case 3:if(zhuangtai[y][x+2]||zhuangtai[y+1][x+3])return 1; else return 0;
		  }	
		}
		
		case 18: 
		{
		switch(fangxiang)
			{
			  case 1:if(zhuangtai[y][x-1]||zhuangtai[y+1][x-1]||zhuangtai[y+2][x])return 1; else return 0;
				case 2:if(zhuangtai[y+2][x]||zhuangtai[y+3][x+1])return 1; else return 0;
				case 3:if(zhuangtai[y][x+1]||zhuangtai[y+1][x+2]||zhuangtai[y+2][x+2])return 1; else return 0;
		  }	
		}
		
		case 19: 
		{
		switch(fangxiang)
			{
			  case 1:if(zhuangtai[y][x]||zhuangtai[y+1][x-1])return 1; else return 0;
				case 2:if(zhuangtai[y+2][x]||zhuangtai[y+2][x+1]||zhuangtai[y+1][x+2])return 1; else return 0;
				case 3:if(zhuangtai[y][x+3]||zhuangtai[y+1][x+2])return 1; else return 0;
		  }	
		}		
	}
	
}

第二个思路:这个思路我觉得挺完美的,先求和这个二维数组中的每一项,然后假设一个俄罗斯方块移动了,之后再求和这个数组,把两个和在一起比较一下,如果一样说明没有碰撞,如果不一样那就说明碰撞了。至于为什么这样,我表达能力有限,你自己好好想想理解理解吧!
下面给出函数

int panduan1(u16 x,u16 y,u8 what,u8 fangxiang)

{
	u16 sum1=0,sum2=0;
	u8 i,n;
	u8 sbuff[23][16];
	x=x/10+1;
	y=y/10;
	for(i=0;i<23;i++)
	{
		for(n=0;n<16;n++)
		{
			sbuff[i][n]=zhuangtai[i][n];
			sum1=sum1+zhuangtai[i][n];	
		}	  
	}
	switch(fangxiang)
	{
		case 1:Left_zhuangtai_move((x-1)*10,10*y,what);break;
		case 2:Down_zhuangtai_move((x-1)*10,10*y,what);break;
		case 3:Right_zhuangtai_move((x-1)*10,10*y,what);break;
		case 4:change1();break;		
	}
		for(i=0;i<23;i++)
	{		
		for(n=0;n<16;n++)
		{
			sum2=sum2+zhuangtai[i][n];	
			zhuangtai[i][n]=sbuff[i][n];			
    }
	}
	return !(sum1==sum2);
}

有没有觉得短了很多,而且囊括了四个方向的判断,上面的只有三个。
上面的change1()这个函数就俄罗斯方块变化函数我也给出

void change1()
{
	switch(what)
	{
		case 1:break;
		
		case 2:Deal_zhuangtai_tuxing(x,y,2);Draw_zhuangtai_tuxing(x,y,3);break;
		case 3:Deal_zhuangtai_tuxing(x,y,3);Draw_zhuangtai_tuxing(x,y,2);break;
		
		case 4:Deal_zhuangtai_tuxing(x,y,4);Draw_zhuangtai_tuxing(x,y,5);break;
		case 5:Deal_zhuangtai_tuxing(x,y,5);Draw_zhuangtai_tuxing(x,y,7);break;
		case 6:Deal_zhuangtai_tuxing(x,y,6);Draw_zhuangtai_tuxing(x,y,4);break;
		case 7:Deal_zhuangtai_tuxing(x,y,7);Draw_zhuangtai_tuxing(x,y,6);break;
		
		case 8:Deal_zhuangtai_tuxing(x,y,8);Draw_zhuangtai_tuxing(x,y,9);break;
		case 9:Deal_zhuangtai_tuxing(x,y,9);Draw_zhuangtai_tuxing(x,y,10);break;
		case 10:Deal_zhuangtai_tuxing(x,y,10);Draw_zhuangtai_tuxing(x,y,11);break;
		case 11:Deal_zhuangtai_tuxing(x,y,11);Draw_zhuangtai_tuxing(x,y,8);break;
			
		case 12:Deal_zhuangtai_tuxing(x,y,12);Draw_zhuangtai_tuxing(x,y,15);break;
		case 13:Deal_zhuangtai_tuxing(x,y,13);Draw_zhuangtai_tuxing(x,y,12);break;
		case 14:Deal_zhuangtai_tuxing(x,y,14);Draw_zhuangtai_tuxing(x,y,13);break;
		case 15:Deal_zhuangtai_tuxing(x,y,15);Draw_zhuangtai_tuxing(x,y,14);break;
		
		case 16:Deal_zhuangtai_tuxing(x,y,16);Draw_zhuangtai_tuxing(x,y,17);break;
		case 17:Deal_zhuangtai_tuxing(x,y,17);Draw_zhuangtai_tuxing(x,y,16);break;
		
		case 18:Deal_zhuangtai_tuxing(x,y,18);Draw_zhuangtai_tuxing(x,y,19);break;
		case 19:Deal_zhuangtai_tuxing(x,y,19);Draw_zhuangtai_tuxing(x,y,18);break;
	}

  1. 最难的函数,我觉得就是消除函数了,一行满了那就消除这一行,单纯这样很简单,但后面还需要把消除行上面的方块移动下面,好像听起来还是很简单,但是你想想如果很多行同时消除呢。函数只能调用一次,这样的话就真的不简单了。

首先 我们需要写一个可以交换相邻两行的函数和删除一行的函数

void lie_move(u16 y)
{
	u8 i;
	y=y/10;
	for(i=1;i<15;i++)
	{
		if(zhuangtai[y][i]==1)
		{
			zhuangtai[y][i]=zhuangtai[y+1][i];
			zhuangtai[y+1][i]=1;
			
				Deal_realbox((i-1)*10,y*10);
			Draw_realbox((i-1)*10,(y+1)*10);
		}
		else if(zhuangtai[y][i]==0)
		{
			zhuangtai[y][i]=zhuangtai[y+1][i];
			zhuangtai[y+1][i]=0;
			
				Deal_realbox((i-1)*10,y*10);
			Deal_realbox((i-1)*10,(y+1)*10);
		}
	}
}

//请大家忽略我“删除这个单词写错了啊”
void Deal_lie(u16 y)
{
	u8 i;
	y=y/10;
	for(i=1;i<15;i++)
	{
		zhuangtai[y][i]=0;
		Deal_realbox((i-1)*10,y*10);
	}
}

然后就是消除函数了

void xiaochu()
{
	u8 n;
	for(n=20;n>0;n--)
	{
		if(n>=20)
		{
			n=20;		
		}
		if(					
 				(
					zhuangtai[n+1][1]&&zhuangtai[n+1][2]&&zhuangtai[n+1][3]&&zhuangtai[n+1][4]&&zhuangtai[n+1][5]&&zhuangtai[n+1][6]&&zhuangtai[n+1][7]&&zhuangtai[n+1][8]&&zhuangtai[n+1][9]&&zhuangtai[n+1][10]&&zhuangtai[n+1][11]&&zhuangtai[n+1][12]&&zhuangtai[n+1][13]&&zhuangtai[n+1][14]
					) 
				 &&
					!(
					zhuangtai[n][1]&&zhuangtai[n][2]&&zhuangtai[n][3]&&zhuangtai[n][4]&&zhuangtai[n][5]&&zhuangtai[n][6]&&zhuangtai[n][7]&&zhuangtai[n][8]&&zhuangtai[n][9]&&zhuangtai[n][10]&&zhuangtai[n][11]&&zhuangtai[n][12]&&zhuangtai[n][13]&&zhuangtai[n][14]
					)
		   )
		{
			lie_move(10*n);
			n=n+2;
		}		
		if(
					(
					!zhuangtai[n][1]&&!zhuangtai[n][2]&&!zhuangtai[n][3]&&!zhuangtai[n][4]&&!zhuangtai[n][5]&&!zhuangtai[n][6]&&!zhuangtai[n][7]&&!zhuangtai[n][8]&&!zhuangtai[n][9]&&!zhuangtai[n][10]&&!zhuangtai[n][11]&&!zhuangtai[n][12]&&!zhuangtai[n][13]&&!zhuangtai[n][14]
					)
		  )
		{
			for(n=0;n<22;n++)
	{
		if(
				(
				zhuangtai[n][1]&&zhuangtai[n][2]&&zhuangtai[n][3]&&zhuangtai[n][4]&&zhuangtai[n][5]&&zhuangtai[n][6]&&zhuangtai[n][7]&&zhuangtai[n][8]&&zhuangtai[n][9]&&zhuangtai[n][10]&&zhuangtai[n][11]&&zhuangtai[n][12]&&zhuangtai[n][13]&&zhuangtai[n][14]
				)
		  )
		{
			Deal_lie(n*10);fengshu++;display_fengshu();
				if(fengshu%10==0)
				{
					speed=speed-10;
					leave++;
					display_leave();				
				}
		}	
	}
	break;		
		}
	}
}

不是不想做解释,是压根解释不清楚,建议你可以拿一个笔比划比划,凭空想真的很难!

差不多就这些函数了,不用担心这些函数零零散散的,后面我会给出工程文件夹的压缩包
下一篇说一下每一个方块产生的随机函数!

总工程文件:点击下载

最后

以上就是体贴犀牛为你收集整理的基于stm32的俄罗斯方块游戏机(4、俄罗斯方块基本规则函数)(大三寒假)的全部内容,希望文章能够帮你解决基于stm32的俄罗斯方块游戏机(4、俄罗斯方块基本规则函数)(大三寒假)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(33)

评论列表共有 0 条评论

立即
投稿
返回
顶部