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

223 lines
No EOL
3.8 KiB
C

#include "block.h"
#include "draw.h"
#include "button.h"
#include <reg52.h>
char base[MAX_X][MAX_Y] = {0}; //x*y //0为空 1为下落完成 2为正在下落
extern unsigned char score;
extern enum OPR opr;
void blockInit()
{
}
void blockDestroy()
{
memset(base, 0, sizeof(base));
}
void genPiece()
{
char i, j;
//需要一点随机性
int random = TH0 % 9;
//若存在尚未下落完成的方块则打断
for(i = 0; i < MAX_X; i++)
for(j = 0; j < MAX_Y; j++)
if(base[i][j] == 2)
return;
//当两个方块的情况
if(random == 0)
{
base[4][8] = 2;
}
//当两个方块的情况
if(random == 1 || random == 2)
{
base[4][8] = 2; //
base[5][8] = 2; //
}
else if(random == 3)
{
base[3][8] = 2; ////
base[4][8] = 2;
}
//当三个方块的情况
else if(random == 4 || random == 5)
{
base[4][10] = 2;
base[4][8] = 2;
base[4][9] = 2;
}
else if(random == 6)
{
base[3][8] = 2;
base[4][8] = 2;
base[5][8] = 2;
}
else if(random == 7)
{
base[3][9] = 2;
base[3][8] = 2;
base[4][8] = 2;
}
else if (random == 8)
{
base[4][9] = 2;
base[3][8] = 2;
base[4][8] = 2;
}
//当四个方块的情况
else if(random == 6)
{
base[3][9] = 2;
base[4][9] = 2;
base[3][8] = 2;
base[4][8] = 2;
}
}
//动作是否在屏幕内
unsigned char isIegal(char i, char j)
{
if(i >= 0 && i < MAX_X && j >= 0 && j < MAX_Y && base[i][j] == 0)
return 1;
return 0;
}
//方块绘制
void drawBlock()
{
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
placeVOID_BLOCK(i, j);
}
//方块下落与合并
void dropPiece()
{
char i, j, flag = 0;
//检测是否触底
for(i = 0; i < MAX_X; i++)
{
for(j = 0; j < MAX_Y; 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 < MAX_X; i++)
{
for(j = 0; j < MAX_Y; 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 moveLeftPiece()
{
char i, j;
for(i = 0; i < MAX_X; i++)
for(j = 0; j < MAX_Y; j++)
if(base[i][j] == 2 && !isIegal(i - 1, j))
{
return;
}
for(i = 0; i < MAX_X; i++)
for(j = 0; j < MAX_Y; j++)
if(base[i][j] == 2)
{
base[i - 1][j] = 2;
base[i][j] = 0;
return;
}
}
void moveRightPiece()
{
char i, j;
for(i = MAX_X - 1; i >= 0; i--)
for(j = MAX_Y - 1; j >= 0; j--)
if(base[i][j] == 2 && !isIegal(i + 1, j))
{
return;
}
for(i = MAX_X - 1; i >= 0; i--)
for(j = MAX_Y - 1; j >= 0; j--)
if(base[i][j] == 2 )
{
base[i + 1][j] = 2;
base[i][j] = 0;
}
}
//判断是否需要清除一行
void judgeBlock()
{
char i, j, k, h, count;
//逐行扫描
for(j = 0; j < MAX_Y; j++)
{
count = 0;
for(i = 0; i < MAX_X; i++)
{
//有则计数
if(base[i][j] == 1)
{
count++;
}
}
//计数足则整体下移
if(count == 8)
{
score++;
for(k = 0; k < MAX_X; k++)
for(h = j ; h < MAX_Y - 1; h++)
base[k][h] = base[k][h + 1];
}
}
}