feat(MCU课设): 方块:优化得分判定,增加游戏结束判定

This commit is contained in:
iridiumR 2022-06-06 19:05:34 +08:00
parent 2bb187635c
commit 81d8da0747
7 changed files with 247 additions and 40 deletions

View file

@ -6,16 +6,17 @@
char base[MAX_X][MAX_Y] = {0}; //x*y //0为空 1为下落完成 2为正在下落 char base[MAX_X][MAX_Y] = {0}; //x*y //0为空 1为下落完成 2为正在下落
extern unsigned char score; extern unsigned char score;
extern enum OPR opr; extern enum OPR opr;
enum GS blockGameStatus = __idle;
void blockInit() void blockInit()
{ {
blockGameStatus = start;
} }
void blockDestroy() void blockDestroy()
{ {
memset(base, 0, sizeof(base)); memset(base, 0, sizeof(base));
blockGameStatus = over;
} }
void genPiece() void genPiece()
@ -23,9 +24,12 @@ void genPiece()
char i, j; char i, j;
//需要一点随机性 //需要一点随机性
int random = TH0 % 9; int random = TH0 % 10;
if(blockGameStatus == over)
return;
//若存在尚未下落完成的方块则打断 //若存在尚未下落完成的方块则打断
for(i = 0; i < MAX_X; i++) for(i = 0; i < MAX_X; i++)
@ -36,55 +40,55 @@ void genPiece()
//当两个方块的情况 //当两个方块的情况
if(random == 0) if(random == 0)
{ {
base[4][8] = 2; base[4][9] = 2;
} }
//当两个方块的情况 //当两个方块的情况
if(random == 1 || random == 2) if(random == 1 || random == 2)
{ {
base[4][8] = 2; // base[4][9] = 2; //
base[5][8] = 2; // base[4][10] = 2; //
} }
else if(random == 3) else if(random == 3)
{ {
base[3][8] = 2; //// base[3][9] = 2; ////
base[4][8] = 2; base[4][9] = 2;
} }
//当三个方块的情况 //当三个方块的情况
else if(random == 4 || random == 5) else if(random == 4 || random == 5)
{ {
base[4][11] = 2;
base[4][10] = 2; base[4][10] = 2;
base[4][8] = 2;
base[4][9] = 2; base[4][9] = 2;
} }
else if(random == 6) else if(random == 6)
{ {
base[3][8] = 2; base[3][9] = 2;
base[4][8] = 2; base[4][9] = 2;
base[5][8] = 2; base[5][9] = 2;
} }
else if(random == 7) else if(random == 7)
{ {
base[3][10] = 2;
base[3][9] = 2; base[3][9] = 2;
base[3][8] = 2; base[4][9] = 2;
base[4][8] = 2;
} }
else if (random == 8) else if (random == 8)
{ {
base[4][9] = 2; base[4][9] = 2;
base[3][8] = 2; base[3][9] = 2;
base[4][8] = 2; base[4][9] = 2;
} }
//当四个方块的情况 //当四个方块的情况
else if(random == 6) else if(random == 9)
{ {
base[3][10] = 2;
base[4][10] = 2;
base[3][9] = 2; base[3][9] = 2;
base[4][9] = 2; base[4][9] = 2;
base[3][8] = 2;
base[4][8] = 2;
} }
} }
@ -100,6 +104,7 @@ unsigned char isIegal(char i, char j)
void drawBlock() void drawBlock()
{ {
char i, j; char i, j;
for(i = 0; i < 8; i++) for(i = 0; i < 8; i++)
for(j = 0; j < 8; j++) for(j = 0; j < 8; j++)
if(base[i][j] != 0) if(base[i][j] != 0)
@ -161,6 +166,8 @@ void moveLeftPiece()
{ {
char i, j; char i, j;
if(blockGameStatus == over)
return;
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 && !isIegal(i - 1, j)) if(base[i][j] == 2 && !isIegal(i - 1, j))
@ -179,6 +186,8 @@ void moveRightPiece()
{ {
char i, j; char i, j;
if(blockGameStatus == over)
return;
for(i = MAX_X - 1; i >= 0; i--) for(i = MAX_X - 1; i >= 0; i--)
for(j = MAX_Y - 1; j >= 0; j--) for(j = MAX_Y - 1; j >= 0; j--)
if(base[i][j] == 2 && !isIegal(i + 1, j)) if(base[i][j] == 2 && !isIegal(i + 1, j))
@ -194,10 +203,12 @@ void moveRightPiece()
} }
} }
//判断是否需要清除一行 //判断是否游戏结束/分数增加
void judgeBlock() void judgeBlock()
{ {
char i, j, k, h, count; char i, j, k, h, count;
if(blockGameStatus == over)
return;
//逐行扫描 //逐行扫描
for(j = 0; j < MAX_Y; j++) for(j = 0; j < MAX_Y; j++)
{ {
@ -214,6 +225,7 @@ void judgeBlock()
if(count == 8) if(count == 8)
{ {
score++; score++;
blockGameStatus = good;
for(k = 0; k < MAX_X; k++) for(k = 0; k < MAX_X; k++)
for(h = j ; h < MAX_Y - 1; h++) for(h = j ; h < MAX_Y - 1; h++)
base[k][h] = base[k][h + 1]; base[k][h] = base[k][h + 1];
@ -221,3 +233,26 @@ void judgeBlock()
} }
} }
void blockGameOver()
{
char j,i;
if(blockGameStatus == over)
return;
for(j = 0; j < MAX_X; j++)
{
//满足此条件则游戏结束
if(base[j][7] == 1)
{
blockGameStatus = over;
break;
}
}
//结束则填满
if(blockGameStatus == over)
{
for(j = 0; j < MAX_Y; j++)
for(i = 0; i < MAX_X; i++)
base[i][j] = 1;
}
}

View file

@ -8,7 +8,6 @@
#define MAX_X 8 #define MAX_X 8
#define MAX_Y 12 #define MAX_Y 12
enum GS {start,good,over};
void blockInit(); void blockInit();
void blockDestroy(); void blockDestroy();
@ -19,4 +18,5 @@ void drawBlock();
void moveLeftPiece(); void moveLeftPiece();
void moveRightPiece(); void moveRightPiece();
void judgeBlock(); void judgeBlock();
void blockGameOver();
#endif #endif

View file

@ -2,5 +2,6 @@
#define _BUTTON_H_ #define _BUTTON_H_
enum OPR {left,right,confirm,idle}; enum OPR {left,right,confirm,idle};
enum GS {__idle,start,good,over};
#endif #endif

View file

@ -397,6 +397,94 @@ void drawSUPNUM(unsigned char x, unsigned char y, unsigned char num)
} }
} }
void drawGAME(unsigned char x, unsigned char y)
{
unsigned int j;
if(x < 0 || x > 127)
return;
else if(x < 64)
{
CSA = 1;
CSB = 0;
}
else
{
x = x - 63;
CSA = 0;
CSB = 1;
}
for(j = 0; j < 32; j++)
{
RS = 0;
RW = 0;
P1 = 0xb8 + y;
write();
P1 = 0X40 + x + j;
write(); //Ò³yÁÐx+j
RS = 1;
RW = 0;
P1 = FONT_GAME0[j];
write();
RS = 0;
RW = 0;
P1 = 0xb8 + y + 1;
write();
P1 = 0X40 + x + j;
write(); //Ò³y+1ÁÐx+j
RS = 1;
RW = 0;
P1 = FONT_GAME1[j];
write();
}
}
void drawOVER(unsigned char x, unsigned char y)
{
unsigned int j;
if(x < 0 || x > 127)
return;
else if(x < 64)
{
CSA = 1;
CSB = 0;
}
else
{
x = x - 63;
CSA = 0;
CSB = 1;
}
for(j = 0; j < 32; j++)
{
RS = 0;
RW = 0;
P1 = 0xb8 + y;
write();
P1 = 0X40 + x + j;
write(); //Ò³yÁÐx+j
RS = 1;
RW = 0;
P1 = FONT_OVER0[j];
write();
RS = 0;
RW = 0;
P1 = 0xb8 + y + 1;
write();
P1 = 0X40 + x + j;
write(); //Ò³y+1ÁÐx+j
RS = 1;
RW = 0;
P1 = FONT_OVER1[j];
write();
}
}
void drawGOOD(unsigned char x, unsigned char y) void drawGOOD(unsigned char x, unsigned char y)
{ {
unsigned int j; unsigned int j;
@ -413,7 +501,7 @@ void drawGOOD(unsigned char x, unsigned char y)
CSA = 0; CSA = 0;
CSB = 1; CSB = 1;
} }
for(j = 0; j < 8; j++) for(j = 0; j < 40; j++)
{ {
RS = 0; RS = 0;
RW = 0; RW = 0;
@ -440,3 +528,46 @@ void drawGOOD(unsigned char x, unsigned char y)
write(); write();
} }
} }
void drawVOID8(unsigned char x,unsigned char y,unsigned char len)
{
unsigned int j;
if(x < 0 || x > 127)
return;
else if(x < 64)
{
CSA = 1;
CSB = 0;
}
else
{
x = x - 63;
CSA = 0;
CSB = 1;
}
for(j = 0; j < len; j++)
{
RS = 0;
RW = 0;
P1 = 0xb8 + y;
write();
P1 = 0X40 + x + j;
write(); //Ò³yÁÐx+j
RS = 1;
RW = 0;
P1 = 0x00;
write();
RS = 0;
RW = 0;
P1 = 0xb8 + y + 1;
write();
P1 = 0X40 + x + j;
write(); //Ò³y+1ÁÐx+j
RS = 1;
RW = 0;
P1 = 0x00;
write();
}
}

View file

@ -5,12 +5,14 @@ void drawNAME();
void drawNUMBER(); void drawNUMBER();
void drawMAINMENU(unsigned char); void drawMAINMENU(unsigned char);
void drawSUPNUM(unsigned char x,unsigned char y,unsigned char num); void drawSUPNUM(unsigned char x,unsigned char y,unsigned char num);
void drawVOID8(unsigned char x,unsigned char y,unsigned char len);
////=================== ·½¿é ================== ////=================== ·½¿é ==================
void placeIMG_BLOCK(unsigned char x,unsigned char y); void placeIMG_BLOCK(unsigned char x,unsigned char y);
void placeVOID_BLOCK(unsigned char x,unsigned char y); void placeVOID_BLOCK(unsigned char x,unsigned char y);
void drawGOOD(unsigned char x, unsigned char y); void drawGOOD(unsigned char x, unsigned char y);
void drawGAME(unsigned char x, unsigned char y);
void drawOVER(unsigned char x, unsigned char y);
void drawBLOCKSCORE(); void drawBLOCKSCORE();
////=================== ¼¸ºÎ ================== ////=================== ¼¸ºÎ ==================
void drawVerticalDottedLine(char x); void drawVerticalDottedLine(char x);

View file

@ -14,8 +14,6 @@ code unsigned char NAME_PAGE1[] ={
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x10, 0x08, 0x06, 0x01, 0xFF, 0x01, 0x06, 0x00, 0x00, 0x3F, 0x10, 0x10, 0x10, 0x3F, 0x00, 0x00 0x10, 0x08, 0x06, 0x01, 0xFF, 0x01, 0x06, 0x00, 0x00, 0x3F, 0x10, 0x10, 0x10, 0x3F, 0x00, 0x00
}; };
//ѧºÅ ×ó //ѧºÅ ×ó
code unsigned char NUMBER_PAGE0[]={ code unsigned char NUMBER_PAGE0[]={
@ -112,12 +110,11 @@ code unsigned char SUPER_NUM1[]={
0x00,0x01,0x12,0x22,0x22,0x11,0x0F,0x00, 0x00,0x01,0x12,0x22,0x22,0x11,0x0F,0x00,
}; };
//=================== ·½¿é×Ö¿â ================== //=================== ·½¿é×Ö¿â ==================
//Ò»¸ö8*8µÄ·½¿é
code unsigned char IMG_BLOCK[]={ code unsigned char IMG_BLOCK[]={
/*-- 宽度x高度=8x8 --*/
0x81,0x7E,0x7E,0x7E,0x7A,0x72,0x7E,0x81, 0x81,0x7E,0x7E,0x7E,0x7A,0x72,0x7E,0x81,
}; };
//µÃ·Ö
code unsigned char FONT_SCORE0[]={ code unsigned char FONT_SCORE0[]={
0x00,0x10,0x88,0xC4,0x33,0x00,0xBE,0xAA,0xAA,0xAA,0xAA,0xAA,0xBE,0x80,0x00,0x00, 0x00,0x10,0x88,0xC4,0x33,0x00,0xBE,0xAA,0xAA,0xAA,0xAA,0xAA,0xBE,0x80,0x00,0x00,
0x80,0x40,0x20,0x90,0x88,0x86,0x80,0x80,0x80,0x83,0x8C,0x10,0x20,0x40,0x80,0x00, 0x80,0x40,0x20,0x90,0x88,0x86,0x80,0x80,0x80,0x83,0x8C,0x10,0x20,0x40,0x80,0x00,
@ -126,7 +123,7 @@ code unsigned char FONT_SCORE1[]={
0x02,0x01,0x00,0xFF,0x00,0x02,0x0A,0x12,0x02,0x42,0x82,0x7F,0x02,0x02,0x02,0x00, 0x02,0x01,0x00,0xFF,0x00,0x02,0x0A,0x12,0x02,0x42,0x82,0x7F,0x02,0x02,0x02,0x00,
0x00,0x80,0x40,0x20,0x18,0x07,0x00,0x40,0x80,0x40,0x3F,0x00,0x00,0x00,0x00,0x00, 0x00,0x80,0x40,0x20,0x18,0x07,0x00,0x40,0x80,0x40,0x3F,0x00,0x00,0x00,0x00,0x00,
}; };
//Good£¡
code unsigned char FONT_GOOD0[]={ code unsigned char FONT_GOOD0[]={
0xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00, 0xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00, 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
@ -141,4 +138,29 @@ code unsigned char FONT_GOOD1[]={
0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20, 0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20,
0x00,0x00,0x00,0x33,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x33,0x00,0x00,0x00,0x00,
}; };
//GAME OVER
code unsigned char FONT_GAME0[]={
0xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,
0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,
0x08,0xF8,0xF8,0x00,0xF8,0xF8,0x08,0x00,
0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,
};
code unsigned char FONT_GAME1[]={
0x07,0x18,0x20,0x20,0x22,0x1E,0x02,0x00,
0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20,
0x20,0x3F,0x01,0x3E,0x01,0x3F,0x20,0x00,
0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00,
};
code unsigned char FONT_OVER0[]={
0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,
0x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08,
0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,
0x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,
};
code unsigned char FONT_OVER1[]={
0x0F,0x10,0x20,0x20,0x20,0x10,0x0F,0x00,
0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00,
0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00,
0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20,
};
#endif #endif

View file

@ -7,7 +7,7 @@
#include "block.h" #include "block.h"
//便于调试的预编译命令 //便于调试的预编译命令
#define DEBUG_MODE //#define DEBUG_MODE
#ifdef DEBUG_MODE #ifdef DEBUG_MODE
#define M_DEBUG M_BLOCK #define M_DEBUG M_BLOCK
@ -24,8 +24,8 @@ Menu* LAST = NULL; //
enum OPR opr = idle; enum OPR opr = idle;
static unsigned char local[10] = {0}; static unsigned char local[10] = {0};
extern unsigned char score=0; unsigned char score = 0;
extern enum GS blockGameStatus;
//初始化显示 //初始化显示
void m_startup(struct _menu* this) void m_startup(struct _menu* this)
{ {
@ -115,7 +115,8 @@ void m_block(struct _menu* this)
{ {
opr = idle; opr = idle;
blockDestroy(); blockDestroy();
drawBlock(); NOW=&M_MAINMENU;
clear();
return; return;
} }
if(opr == left) if(opr == left)
@ -134,7 +135,22 @@ void m_block(struct _menu* this)
drawBlock(); drawBlock();
return; return;
} }
drawSUPNUM(110, 0, score); switch(blockGameStatus)
{
case good:
drawGOOD(70, 2);
blockGameStatus=start;
break;
case over:
drawGAME(70, 2);
drawOVER(70, 4);
break;
default:
drawVOID8(70, 2, 40);
drawVOID8(70, 4, 32);
}
drawSUPNUM(110, 0, score); //绘制分数
blockGameOver(); //检查是否游戏结束
genPiece(); //若有需要,生成新块 genPiece(); //若有需要,生成新块
dropPiece(); //若有需要,块下落 dropPiece(); //若有需要,块下落
judgeBlock(); //若有需要,清除一行 judgeBlock(); //若有需要,清除一行