fix(MCU课设): 修正方块下落显示bug

This commit is contained in:
iridiumR 2022-06-06 13:41:14 +08:00
parent 49a8fdaa76
commit a7eaddb690
5 changed files with 931 additions and 685 deletions

File diff suppressed because it is too large Load diff

View file

@ -1,32 +1,37 @@
#include "block.h"
#include "draw.h"
#include "button.h"
unsigned char base[MAX_X][MAX_Y]={0}; //x*y //0为空 1为下落完成 2为正在下落
char base[MAX_X][MAX_Y] = {0}; //x*y //0为空 1为下落完成 2为正在下落
enum BLK_TP type = none;
void blockInit()
{
base[0][0]=1;
base[0][1]=1;
base[0][2]=1;
// genPiece();
base[0][0] = 1;
base[0][1] = 1;
base[0][2] = 1;
}
void blockDestroy()
{
memset(base, 0, sizeof(base));
memset(base, 0, sizeof(base));
}
void genPiece()
{
char i, j;
//需要一点随机性
int random = rand() % 4;
int len = 2 + rand() % 3;
piece.mother.x=4;
piece.mother.y=8;
//若存在尚未下落完成的方块则打断
for(i = 0; i < MAX_X; i++)
for(j = 0; j < MAX_Y; j++)
if(base[i][j] == 2)
break;
//当两个方块的情况
@ -34,13 +39,13 @@ void genPiece()
{
if(random % 2 == 0)
{
base[4][8]=2; //
base[4][9]=2; //
}
base[4][7] = 2; //
base[4][8] = 2; //
}
else
{
base[3][8]=2; ////
base[4][8]=2;
base[3][7] = 2; ////
base[4][7] = 2;
}
}
//当三个方块的情况
@ -48,65 +53,128 @@ void genPiece()
{
if(random == 0)
{
base[4][8]=2;
base[4][9]=2;
base[4][10]=2;
base[4][7] = 2;
base[4][8] = 2;
base[4][9] = 2;
}
else if(random == 1)
{
base[3][8]=2;
base[4][8]=2;
base[5][8]=2;
base[3][7] = 2;
base[4][7] = 2;
base[5][7] = 2;
}
else if(random == 2)
{
base[3][9]=2;
base[3][8]=2;
base[4][8]=2;
base[3][8] = 2;
base[3][7] = 2;
base[4][7] = 2;
}
else if (random == 3)
{
base[4][9]=2;
base[3][8]=2;
base[4][8]=2;
base[4][8] = 2;
base[3][7] = 2;
base[4][7] = 2;
}
}
//当四个方块的情况
if(len == 3)
{
base[3][9]=2;
base[4][9]=2;
base[3][8]=2;
base[4][8]=2;
base[3][7] = 2;
base[4][7] = 2;
base[3][8] = 2;
base[4][8] = 2;
}
}
unsigned char isIegal(unsigned char i,unsigned char j)
//动作是否在屏幕内
unsigned char isIegal(char i, char j)
{
if(i>=0&&i<MAX_X&&j>=0&&j<MAX_Y)
return 1;
return 0;
}
void dropPiece()
{
int i.j;
for(i=0,i<MAX_X,i++)
for(j=0,j<MAX_Y,j++)
{
if(base[i][j]==2&&isIegal(i,j-1))
{
base[i][j-1]=2;
base[i][j]=0;
}
}
if(i >= 0 && i < MAX_X && j >= 0 && j < MAX_Y && base[i][j] == 0)
return 1;
return 0;
}
//方块绘制
void drawBlock()
{
int i,j;
for(i=0;i<8;i++)
for(j=0;j<8;j++)
if(base[i][j]==1)
placeIMG_BLOCK(i,j);
placeIMG_BLOCK(piece.mother.x,piece.mother.y);
char i, j;
for(i = 0; i < 8; i++)
for(j = 0; j < 8; j++)
if(base[i][j] != 0)
placeIMG_BLOCK(i, j);
else
place_VOID_BLOCK(i, j);
}
//方块下落与合并
void dropPiece()
{
char i, j, flag = 0;
//检测是否触底
for(i = 0; i < 8; i++)
{
for(j = 0; j < 8; j++)
{
if(flag == 1)
break;
if(base[i][j] == 2)
{
//若触底,做标记
if(flag == 0 && (base[i][j - 1] == 1 || j == 0))
{
flag = 1;
break;
}
}
}
if(flag == 1)
break;
}
//转换嵌套
for(i = 0; i < 8; i++)
{
for(j = 0; j < 8; j++)
{
if(base[i][j] == 2)
{
//无标记则下落
if(flag == 0 && isIegal(i, j - 1))
{
base[i][j - 1] = 2;
base[i][j] = 0;
}
//有标记则转换
else if(flag == 1)
base[i][j] = 1;
}
}
}
}
//方块平移
void movePiece(enum OPR opr)
{
char i, j;
for(i = 0; i < 8; i++)
for(j = 0; j < 8; j++)
if(base[i][j] == 2)
{
if(opr == left && isIegal(i - 1, j))
{
base[i - 1][j] = 2;
base[i][j] = 0;
}
else if(opr == left && isIegal(i + 1, j))
{
base[i + 1][j] = 2;
base[i][j] = 0;
}
}
}

View file

@ -244,7 +244,7 @@ void drawMAINMENU(unsigned char i)
}
void placeIMG_BLOCK(unsigned char x,unsigned char y)
{
unsigned int j;
char j;
for(j = 0 ; j < 8; j++)
{
@ -264,3 +264,24 @@ void placeIMG_BLOCK(unsigned char x,unsigned char y)
}
}
void place_VOID_BLOCK(unsigned char x,unsigned char y)
{
char j;
for(j = 0 ; j < 8; j++)
{
CSA = 1;
CSB = 0;
RS = 0;
RW = 0;
P1 = 0xb8 + 7 - y;
write();
P1 = 0X40 + x*8 + j;
write(); //Ò³7-yÁÐx*8+jÆÁA
RS = 1;
RW = 0;
P1 = 0x00;
write();
}
}

View file

@ -9,4 +9,5 @@ void drawMAINMENU(unsigned char);
////=================== 方块 ==================
void placeIMG_BLOCK(unsigned char x,unsigned char y);
void place_VOID_BLOCK(unsigned char x,unsigned char y);
#endif

View file

@ -6,12 +6,12 @@
#include <stdlib.h>
#include "block.h"
Menu M_STARTUP;
Menu M_MAINMENU;//主菜单
Menu M_BLOCK; //俄罗斯方块
Menu M_BALL; //弹球
Menu M_STARTUP;
Menu* NOW; //当前菜单指针
Menu* LAST = NULL; //上一个状态的菜单
@ -102,12 +102,19 @@ void m_block(struct _menu* this)
LAST = &M_BLOCK;
blockInit();
}
while(1)
{
if(opr == confirm)
{
opr = idle;
blockDestroy();
drawBlock();
return;
}
genPiece();
dropPiece();
drawBlock();
delay(200);
}
}
void menuInit()