feat(MCU课设): 方块移动

This commit is contained in:
iridiumR 2022-06-06 14:27:04 +08:00
parent f926ffad48
commit 4f0e69ddab
4 changed files with 731 additions and 678 deletions

File diff suppressed because it is too large Load diff

View file

@ -5,6 +5,8 @@
char base[MAX_X][MAX_Y] = {0}; //x*y //0为空 1为下落完成 2为正在下落
enum BLK_TP type = none;
extern enum OPR opr;
void blockInit()
{
@ -23,7 +25,7 @@ void genPiece()
{
char i, j;
//需要一点随机性
//需要一点随机性
int random = TH0 % 4;
int len = 2 + TH0 % 3;
@ -41,13 +43,13 @@ void genPiece()
{
if(random % 2 == 0)
{
base[4][7] = 2; //
base[4][8] = 2; //
base[4][8] = 2; //
}
else
{
base[3][7] = 2; ////
base[4][7] = 2;
base[3][8] = 2; ////
base[4][8] = 2;
}
}
//当三个方块的情况
@ -55,34 +57,34 @@ void genPiece()
{
if(random == 0)
{
base[4][7] = 2;
base[4][10] = 2;
base[4][8] = 2;
base[4][9] = 2;
}
else if(random == 1)
{
base[3][7] = 2;
base[4][7] = 2;
base[5][7] = 2;
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[3][7] = 2;
base[4][7] = 2;
base[4][8] = 2;
}
else if (random == 3)
{
base[4][9] = 2;
base[3][8] = 2;
base[4][8] = 2;
base[3][7] = 2;
base[4][7] = 2;
}
}
//当四个方块的情况
if(len == 3)
{
base[3][7] = 2;
base[4][7] = 2;
base[3][9] = 2;
base[4][9] = 2;
base[3][8] = 2;
base[4][8] = 2;
}
@ -141,13 +143,13 @@ void dropPiece()
{
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;
}
@ -157,26 +159,27 @@ void dropPiece()
}
//方块平移
void movePiece(enum OPR opr)
void moveLeftPiece()
{
char i, j;
for(i = 0; i < MAX_X; i++)
for(j = 0; j < MAX_Y; j++)
if(base[i][j] == 2)
if(base[i][j] == 2 && isIegal(i - 1, j))
{
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;
}
base[i - 1][j] = 2;
base[i][j] = 0;
}
}
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))
{
base[i + 1][j] = 2;
base[i][j] = 0;
}
}

View file

@ -1,7 +1,10 @@
#ifndef _BLOCK_H_
#define _BLOCK_H_
#include <stdlib.h>
# include <string.h>
#include <string.h>
#include "button.h"
#define MAX_X 8
#define MAX_Y 12
@ -22,8 +25,12 @@ struct _group
};
typedef struct _group Group;
void genPiece();
void blockInit();
void blockDestroy();
void genPiece();
void dropPiece();
void drawBlock();
void moveLeftPiece();
void moveRightPiece();
#endif

View file

@ -108,6 +108,7 @@ void m_block(struct _menu* this)
LAST = &M_BLOCK;
blockInit();
}
//ÖØÖÃ
if(opr == confirm)
{
opr = idle;
@ -115,6 +116,20 @@ void m_block(struct _menu* this)
drawBlock();
return;
}
if(opr == left)
{
opr = idle;
moveLeftPiece();
drawBlock();
return;
}
if(opr == right)
{
opr = idle;
moveRightPiece();
drawBlock();
return;
}
genPiece();
dropPiece();