基于STM32的游戏平台,其二TETRIS

本文详细介绍了使用STM32开发TETRIS游戏的12个关键步骤,从画出游戏区域到图案运动、碰撞检测、消除行、分数更新等,涉及图形绘制、数据结构和按键控制等技术。同时,提供了部分源码和视频演示,供读者深入理解和学习。
摘要由CSDN通过智能技术生成

TETRIS

为实现俄罗斯方块,我不禁会问自己以下的问题,这便是软件的思路:
图片中的图形直接对应swtich对应的所有情况
first step: 如何画出游戏图案运动的范围
second step:如何画出一个10乘10的方块以及消除方法
third step: 如何画出或消除对应的19种图案以及消除方法
(有小伙伴会问是哪19种)
fourth step:如何让画出的图案动起来,并消除运动过程的残影
fifth step:如何用按键控制图案的运动,并消除运动残影与重画出新图案
sixth step: 如何用一个按键控制图案在同一位置的图形变换
seventh step:如何用数组存储当前面板上的图案位置信息。
(在这里分为明码与暗码:明码为LCD显示出的图案码,暗码为记录位置信息的数组)
eighth step: 如何判断图案的相撞。
ninth step: 如何消除组合完整一行。
tenth step: 如何将消除后的一行上面的残留块儿,向下运动一行。
eleventh step:如何预测并显示下一个将出现的图案信息。
twelfth step:如何显示分数,并实时更新数据。游戏界面如何设计。

First Step:

/***************************************
//画游戏界面
***************************************/
void Tetris_Boundary(void)
{
   
	LCD_Fill(0,0,240,10,BROWN);
	LCD_Fill(0,0,10,310,BROWN);
	LCD_Fill(240-10,0,240,310,BROWN);
	LCD_Fill(0,310-10,240,310,BROWN);
}

Second Step:

/**************************************
//画10*10的像素方块
*************************************/
void DrawBlock(u16 x,u16 y)
{
   
	u8 i,n;
	for(i=1;i<=8;i++)
	{
   
		for(n=1;n<=8;n++)
		{
   
			GUI_DrawPoint((x+i),(y-n),GRAY);	
		}	
	}
	for(i=0;i<=9;i++)
	{
   
		GUI_DrawPoint((x+i),y,BLACK	);
		GUI_DrawPoint((x+i),(y-9),BLACK	);		
		GUI_DrawPoint(x,(y-i),BLACK	);		
		GUI_DrawPoint((x+9),(y-i),BLACK	);				
	}	
}
/*****************************************
//删除一个10*10的像素块
*****************************************/
void DeleteBlock(u16 x,u16 y)
{
   
	u8 i,n;
	for(i=0;i<=9;i++)
	{
   
		for(n=0;n<=9;n++)
		{
   
			GUI_DrawPoint((x+i),(y-n),WHITE);	
		}	
	}
}

Third Step:

/*************************************************
//画出方块组合图形
*************************************************/
void DrawShape(u16 x,u16 y,u8 what)
{
   
	switch (what)
	{
   
		case 1:{
   DrawBlock(x,y);DrawBlock(x+10,y);DrawBlock(x,y-10);DrawBlock(x+10,y-10);}break;
		case 2:{
   DrawBlock(x,y);DrawBlock(x+10,y);DrawBlock(x+20,y);DrawBlock(x+30,y);}break;
		case 3:{
   DrawBlock(x,y);DrawBlock(x,y-10);DrawBlock(x,y-20);DrawBlock(x,y-30);}break;
		case 4:{
   DrawBlock(x,y);DrawBlock(x+10,y-10);DrawBlock(x+10,y);DrawBlock(x+20,y);}break;
		case 5:{
   DrawBlock(x,y);DrawBlock(x+10,y-10);DrawBlock(x,y-10);DrawBlock(x,y-20);}break;
		case 6:{
   DrawBlock(x,y);DrawBlock(x-10,y-10);DrawBlock(x,y-10);DrawBlock(x,y-20);}break;
		case 7:{
   DrawBlock(x-10,y-10);DrawBlock(x,y);DrawBlock(x,y-10);DrawBlock(x+10,y-10);}break;
		case 8:{
   DrawBlock(x,y);DrawBlock(x,y-10);DrawBlock(x,y-20);DrawBlock(x+10,y);}break;
		case 9:{
   DrawBlock(x,y);DrawBlock(x,y-10);DrawBlock(x+10,y-10);DrawBlock(x+20,y-10);}break;
		case 10:{
   DrawBlock(x,y);DrawBlock(x,y-10);DrawBlock(x,y-20);DrawBlock(x-10,y-20);}break;
		case 11:{
   DrawBlock(x,y);DrawBlock(x+10,y);DrawBlock(x+20,y);DrawBlock(x+20,y-10);}break;
		case 12:{
   DrawBlock(x,y);DrawBlock(x+10,y);DrawBlock(x+10,y-10);DrawBlock(x+10,y-20);}break;
		case 13:{
   DrawBlock(x,y);DrawBlock(x+10,y);DrawBlock(x,y-10);DrawBlock(x,y-20);}break;
		case 14:{
   DrawBlock(x,y);DrawBlock(x,y-10);DrawBlock(x,y-20);DrawBlock(x+10,y-20);}break;
		case 15:{
   DrawBlock(x,y);DrawBlock(x,y-10);DrawBlock(x+10,y);DrawBlock(x+20,y);}break;
		case 16:{
   DrawBlock(x,y);DrawBlock(x,y-10);DrawBlock(x+10,y-10);DrawBlock(x+10,y-20);}break;
		case 17:{
   DrawBlock(x,y);DrawBlock(x+10,y);DrawBlock(x,y-10);DrawBlock(x-10,y-10);}break;
		case 18:{
   DrawBlock(x,y);DrawBlock(x,y-10);DrawBlock(x-10,y-10);DrawBlock(x-10,y-20);}break;
		case 19:{
   DrawBlock(x,y);DrawBlock(x+10,y);DrawBlock(x+10,y-10);DrawBlock(x+20,y-10);}break;
	}
}
/*************************************************
//消除方块组合图形
*************************************************/
void DeleteShape(u16 x,u16 y,u8 what)
{
   
	switch (what)
	{
   
		case 1: {
   DeleteBlock(x,y);DeleteBlock(x+10,y)   ;DeleteBlock(x,y-10);DeleteBlock(x+10,y-10);}break;
		case 2: {
   DeleteBlock(x,y);DeleteBlock(x+10,y)   ;DeleteBlock(x+20,y);DeleteBlock(x+30,y);}break;
		case 3: {
   DeleteBlock(x,y);DeleteBlock(x,y-10)   ;DeleteBlock(x,y-20);DeleteBlock(x,y-30);}break;
		case 4: {
   DeleteBlock(x,y);DeleteBlock(x+10,y-10);DeleteBlock(x+10,y);DeleteBlock(x+20,y);}break;
		case 5: {
   DeleteBlock(x,y);DeleteBlock(x+10,y-10);DeleteBlock(x,y-10);DeleteBlock(x,y-20);}break;
		case 6: {
   DeleteBlock(x,y);DeleteBlock(x-10,y-10);DeleteBlock(x,y-10);DeleteBlock(x,y-20);}break;
		case 7: {
   DeleteBlock(x-10,y-10);DeleteBlock(x,y);DeleteBlock(x,y-10);DeleteBlock(x+10,y-10);}break;
		case 8: {
   DeleteBlock(x,y);DeleteBlock(x,y-10);DeleteBlock(x,y-20)   ;DeleteBlock(x+10,y);}break;
		case 9: {
   DeleteBlock(x,y);DeleteBlock(x,y-10);DeleteBlock(x+10,y-10);DeleteBlock(x+20,y-10);}break;
		case 10:{
   DeleteBlock(x,y);DeleteBlock(x,y-10);DeleteBlock(x,y-20)   ;DeleteBlock(x-10,y-20);}break;
		case 11:{
   DeleteBlock(x,y);DeleteBlock(x+10,y);DeleteBlock(x+20,y)   ;DeleteBlock(x+20,y-10);}break;
		case 12:{
   DeleteBlock(x,y);DeleteBlock(x+10,y);DeleteBlock(x+10,y-10);DeleteBlock(x+10,y-20);}break;
		case 13:{
   DeleteBlock(x,y);DeleteBlock(x+10,y);DeleteBlock(x,y-10)   ;DeleteBlock(x,y-20);}break;
		case 14:{
   DeleteBlock(x,y);DeleteBlock(x,y-10);DeleteBlock(x,y-20)   ;DeleteBlock(x+10,y-20);}break;
		case 15:{
   DeleteBlock(x,y);DeleteBlock(x,y-10);DeleteBlock(x+10,y)   ;DeleteBlock(x+20,y);}break;
		case 16:{
   DeleteBlock(x,y);DeleteBlock(x,y-10);DeleteBlock(x+10,y-10);DeleteBlock(x+10,y-20);}break;
		case 17:{
   DeleteBlock(x,y);DeleteBlock(x+10,y);DeleteBlock(x,y-10)   ;DeleteBlock(x-10,y-10);}break;
		case 18:{
   DeleteBlock(x,y);DeleteBlock(x,y-10);DeleteBlock(x-10,y-10);DeleteBlock(x-10,y-20);}break;
		case 19:{
   DeleteBlock(x,y);DeleteBlock(x+10,y);DeleteBlock(x+10,y-10);DeleteBlock(x+20,y-10);}break;
	}
}

fourth step:

/*************************************
按键控制图形运动
*************************************/
void Tetris_Game_Control(void)
{
   
    TETRIS_KEY_RECEIVER = Menu_Control_Read2(); 
	switch(TETRIS_KEY_RECEIVER)		   
	{
   
		case 2:
		{
   
			delay_ms(200);
			TransFormer(TETRIS_X_POSITION,TETRIS_Y_POSITION);
			Usart1_Send(BeCode[0],768);
		}break;
		case 10:
		{
   
			delay_ms(200);	
			DeleteShape(TETRIS_X_POSITION,TETRIS_Y_POSITION,RandomNumber);
			DeleteShapeState(TETRIS_X_POSITION,TETRIS_Y_POSITION,RandomNumber);
			TETRIS_Y_POSITION += 10;
			DrawShape(TETRIS_X_POSITION,TETRIS_Y_POSITION,RandomNumber);
			DrawShapeState(TETRIS_X_POSITION,TETRIS_Y_POSITION,RandomNumber);
			Usart1_Send(BeCode[0],768);
		}break;
		case 5:
		{
   
			delay_ms(200);	
			DeleteShape(TETRIS_X_POSITION,TETRIS_Y_POSITION,RandomNumber);
			DeleteShapeState(TETRIS_X_POSITION,TETRIS_Y_POSITION,RandomNumber);
			switch(RandomNumber)
			{
   
				case 1:if((BeCode[TETRIS_Y_POSITION/10][(TETRIS_X_POSITION-10)/10]!=1)||(BeCode[(TETRIS_Y_POSITION-10)/10][(TETRIS_X_POSITION-10)/10]!=1))TETRIS_X_POSITION -= 10;break;
				case 2:if(BeCode[TETRIS_Y_POSITION/10][(TETRIS_X_POSITION-10)/10]!=1)TETRIS_X_POSITION -= 10;break;
				case 3:if((BeCode[TETRIS_Y_POSITION/10][(TETRIS_X_POSITION-10)/10]!=1)||(BeCode[(TETRIS_Y_POSITION-10)/10][(TETRIS_X_POSITION-10)/10]!=1)||(BeCode[(TETRIS_Y_POSITION-20)/10][(TETRIS_X_POSITION-10)/10]!=1)||(BeCode[(TETRIS_Y_POSITION-30)/10][(TETRIS_X_POSITION-10)/10]!=1))TETRIS_X_POSITION -= 10;break;
				case 4:if(BeCode[TETRIS_Y_POSITION/10][(TETRIS_X_POSITION-10)/10]!=1)TETRIS_X_POSITION -= 10;break;
				case 5:if((BeCode[TETRIS_Y_POSITION/10][(TETRIS_X_POSITION-10)/10]!=1)||(BeCode[(TETRIS_Y_POSITION-10)/10][(TETRIS_X_POSITION-10)/10]!=1)||(BeCode[(TETRIS_Y_POSITION-20)/10][(TETRIS_X_POSITION-10)/10]!=1))TETRIS_X_POSITION -= 10;break;
				case 6:if((BeCode[TETRIS_Y_POSITION/10][(TETRIS_X_POSITION-10)/10]!=1)||(BeCode[(TETRIS_Y_POSITION-10)/10][(TETRIS_X_POSITION-20)/10]!=1))TETRIS_X_POSITION 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值