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

View file

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

View file

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