This repository has been archived on 2024-01-06. You can view files and clone it, but cannot push or open issues or pull requests.
justhomework/MCU/MCU_REAL _FINAL/block.c
2022-06-06 09:40:49 +08:00

112 lines
No EOL
1.7 KiB
C

#include "block.h"
#include "draw.h"
unsigned char base[MAX_X][MAX_Y]={0}; //x*y //0为空 1为下落完成 2为正在下落
void blockInit()
{
base[0][0]=1;
base[0][1]=1;
base[0][2]=1;
// genPiece();
}
void blockDestroy()
{
memset(base, 0, sizeof(base));
}
void genPiece()
{
//需要一点随机性
int random = rand() % 4;
int len = 2 + rand() % 3;
piece.mother.x=4;
piece.mother.y=8;
//当两个方块的情况
if(len == 2)
{
if(random % 2 == 0)
{
base[4][8]=2; //
base[4][9]=2; //
}
else
{
base[3][8]=2; ////
base[4][8]=2;
}
}
//当三个方块的情况
if(len == 3)
{
if(random == 0)
{
base[4][8]=2;
base[4][9]=2;
base[4][10]=2;
}
else if(random == 1)
{
base[3][8]=2;
base[4][8]=2;
base[5][8]=2;
}
else if(random == 2)
{
base[3][9]=2;
base[3][8]=2;
base[4][8]=2;
}
else if (random == 3)
{
base[4][9]=2;
base[3][8]=2;
base[4][8]=2;
}
}
//当四个方块的情况
if(len == 3)
{
base[3][9]=2;
base[4][9]=2;
base[3][8]=2;
base[4][8]=2;
}
}
unsigned char isIegal(unsigned char i,unsigned 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;
}
}
}
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);
}