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

258 lines
4.5 KiB
C
Raw Normal View History

#include "block.h"
#include "draw.h"
#include "button.h"
#include <reg52.h>
char base[MAX_X][MAX_Y] = {0}; //x*y //0Ϊ<30><CEAA><31><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><32><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2022-06-06 09:01:36 +00:00
extern unsigned char score;
2022-06-06 06:27:04 +00:00
extern enum OPR opr;
enum GS blockGameStatus = __idle;
void blockInit()
{
blockGameStatus = start;
}
void blockDestroy()
{
memset(base, 0, sizeof(base));
blockGameStatus = over;
}
void genPiece()
{
char i, j;
2022-06-06 06:27:04 +00:00
//<2F><>Ҫһ<D2AA><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int random = TH0 % 10;
if(blockGameStatus == over)
return;
2022-06-06 06:27:04 +00:00
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>δ<EFBFBD><CEB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɵķ<C9B5><C4B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for(i = 0; i < MAX_X; i++)
for(j = 0; j < MAX_Y; j++)
if(base[i][j] == 2)
return;
2022-06-06 06:27:04 +00:00
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if(random == 0)
{
base[4][9] = 2;
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if(random == 1 || random == 2)
{
base[4][9] = 2; //
base[4][10] = 2; //
}
else if(random == 3)
{
base[3][9] = 2; ////
base[4][9] = 2;
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
else if(random == 4 || random == 5)
{
base[4][11] = 2;
base[4][10] = 2;
base[4][9] = 2;
}
else if(random == 6)
{
base[3][9] = 2;
base[4][9] = 2;
base[5][9] = 2;
}
else if(random == 7)
{
base[3][10] = 2;
base[3][9] = 2;
base[4][9] = 2;
}
else if (random == 8)
{
base[4][9] = 2;
base[3][9] = 2;
base[4][9] = 2;
}
//<2F><><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
else if(random == 9)
{
base[3][10] = 2;
base[4][10] = 2;
2022-06-06 06:27:04 +00:00
base[3][9] = 2;
base[4][9] = 2;
}
}
//<2F><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD><C4BB>
unsigned char isIegal(char i, char j)
{
2022-06-06 09:10:00 +00:00
if(i >= 0 && i < MAX_X && j >= 0 && j < MAX_Y && base[i][j] != 1)
return 1;
return 0;
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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);
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϲ<EFBFBD>
void dropPiece()
{
char i, j, flag = 0;
//<2F><><EFBFBD><EFBFBD><EFBFBD>Ƿ񴥵<C7B7>
for(i = 0; i < MAX_X; i++)
{
for(j = 0; j < MAX_Y; j++)
{
if(flag == 1)
break;
if(base[i][j] == 2)
{
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if(flag == 0 && (base[i][j - 1] == 1 || j == 0))
{
flag = 1;
break;
}
}
}
if(flag == 1)
break;
}
//ת<><D7AA>Ƕ<EFBFBD><C7B6>
for(i = 0; i < MAX_X; i++)
{
for(j = 0; j < MAX_Y; j++)
{
if(base[i][j] == 2)
{
2022-06-06 06:27:04 +00:00
//<2F>ޱ<EFBFBD><DEB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if(flag == 0 && isIegal(i, j - 1))
{
base[i][j - 1] = 2;
base[i][j] = 0;
}
2022-06-06 06:27:04 +00:00
//<2F>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>
else if(flag == 1)
base[i][j] = 1;
}
}
}
}
//<2F><><EFBFBD><EFBFBD>ƽ<EFBFBD><C6BD>
2022-06-06 06:27:04 +00:00
void moveLeftPiece()
{
2022-06-06 06:27:04 +00:00
char i, j;
if(blockGameStatus == over)
return;
for(i = 0; i < MAX_X; i++)
for(j = 0; j < MAX_Y; j++)
2022-06-06 09:01:36 +00:00
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)
{
2022-06-06 06:27:04 +00:00
base[i - 1][j] = 2;
base[i][j] = 0;
}
}
2022-06-06 06:27:04 +00:00
void moveRightPiece()
2022-06-06 06:27:04 +00:00
{
char i, j;
if(blockGameStatus == over)
return;
2022-06-06 06:27:04 +00:00
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 )
2022-06-06 06:27:04 +00:00
{
base[i + 1][j] = 2;
base[i][j] = 0;
}
}
//<2F>ж<EFBFBD><D0B6>Ƿ<EFBFBD><C7B7><EFBFBD>Ϸ<EFBFBD><CFB7><EFBFBD><EFBFBD>/<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void judgeBlock()
{
char i, j, k, h, count;
if(blockGameStatus == over)
return;
//<2F><><EFBFBD><EFBFBD>ɨ<EFBFBD><C9A8>
for(j = 0; j < MAX_Y; j++)
{
count = 0;
for(i = 0; i < MAX_X; i++)
{
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if(base[i][j] == 1)
{
count++;
}
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if(count == 8)
{
score++;
blockGameStatus = good;
for(k = 0; k < MAX_X; k++)
for(h = j ; h < MAX_Y - 1; h++)
base[k][h] = base[k][h + 1];
}
}
}
void blockGameOver()
{
char j,i;
if(blockGameStatus == over)
return;
for(j = 0; j < MAX_X; j++)
{
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϸ<EFBFBD><CFB7><EFBFBD><EFBFBD>
if(base[j][7] == 1)
{
blockGameStatus = over;
break;
}
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if(blockGameStatus == over)
{
for(j = 0; j < MAX_Y; j++)
for(i = 0; i < MAX_X; i++)
base[i][j] = 1;
}
2022-06-06 06:27:04 +00:00
}