diff --git a/MCU/MCU_REAL _FINAL/block.c b/MCU/MCU_REAL _FINAL/block.c index cea089c..a0a45f0 100644 --- a/MCU/MCU_REAL _FINAL/block.c +++ b/MCU/MCU_REAL _FINAL/block.c @@ -5,6 +5,7 @@ #include "delay.h" #include "eeprom.h" +extern unsigned int bestScore[]; char base[MAX_X][MAX_Y] = {0}; //x*y //0为空 1为下落完成 2为正在下落 extern unsigned int score; extern enum OPR opr; @@ -12,7 +13,7 @@ enum GS blockGameStatus = __idle; void blockInit() { - score=0; + score = 0; blockGameStatus = start; } @@ -157,10 +158,10 @@ void dropPiece() } //有标记则转换 else if(flag == 1) - { - score++; - base[i][j] = 1; - } + { + score++; + base[i][j] = 1; + } } } } @@ -230,7 +231,7 @@ void judgeBlock() //计数足则整体下移 if(count == 8) { - score=score+10; + score = score + 10; blockGameStatus = good; for(k = 0; k < MAX_X; k++) for(h = j ; h < MAX_Y - 1; h++) @@ -243,8 +244,8 @@ void judgeBlock() //判断是否游戏结束 void blockGameOver() { - char j,i; - if(blockGameStatus == over) + char j, i; + if(blockGameStatus == over) return; for(j = 0; j < MAX_X; j++) { @@ -258,14 +259,74 @@ void blockGameOver() //结束则填满 if(blockGameStatus == over) { - addBest(score); + //写入排行榜 + if(addBest(score)) + { + IapEraseSector(IAP_ADDRESS); + IapProgramByte(IAP_ADDRESS + 0, bestScore[0] >> 8); + IapProgramByte(IAP_ADDRESS + 1, bestScore[0] % 256); + IapProgramByte(IAP_ADDRESS + 2, bestScore[1] >> 8); + IapProgramByte(IAP_ADDRESS + 3, bestScore[1] % 256); + IapProgramByte(IAP_ADDRESS + 4, bestScore[2] >> 8); + IapProgramByte(IAP_ADDRESS + 5, bestScore[2] % 256); + IapProgramByte(IAP_ADDRESS + 6, bestScore[3] >> 8); + IapProgramByte(IAP_ADDRESS + 7, bestScore[3] % 256); + IapProgramByte(IAP_ADDRESS + 8, bestScore[4] >> 8); + IapProgramByte(IAP_ADDRESS + 9, bestScore[4] % 256); + IapProgramByte(IAP_ADDRESS + 10, bestScore[5] >> 8); + IapProgramByte(IAP_ADDRESS + 11, bestScore[5] % 256); + IapProgramByte(IAP_ADDRESS + 12, bestScore[6] >> 8); + IapProgramByte(IAP_ADDRESS + 13, bestScore[6] % 256); + IapProgramByte(IAP_ADDRESS + 14, bestScore[7] >> 8); + IapProgramByte(IAP_ADDRESS + 15, bestScore[7] % 256); + IapProgramByte(IAP_ADDRESS + 16, bestScore[8] >> 8); + IapProgramByte(IAP_ADDRESS + 17, bestScore[8] % 256); + IapProgramByte(IAP_ADDRESS + 18, bestScore[9] >> 8); + IapProgramByte(IAP_ADDRESS + 19, bestScore[9] % 256); + } for(j = 0; j < MAX_Y; j++) for(i = 0; i < MAX_X; i++) - { - //此处看似降低效率,但其实是为了增加喜剧效果 - delayms(10); - base[i][j]=1; - placeIMG_BLOCK(i, j); - } + { + //此处看似降低效率,但其实是为了增加喜剧效果 + delayms(10); + base[i][j] = 1; + placeIMG_BLOCK(i, j); + } + } +} + +unsigned int addBest(unsigned int s) +{ + int i, j; + for(i = 0; i < 10; i++) + if(bestScore[i] < s) + { + for(j = 9; j > i; j--) + bestScore[j] = bestScore[j - 1]; + + bestScore[i] = s; + return 1; + } + return 0; +} + +void saveBest() +{ + unsigned int i; + IapEraseSector(IAP_ADDRESS); + delayms(20); + for(i = 0; i++; i < 10) + { + IapProgramByte(IAP_ADDRESS + 2 * i, bestScore[i] >> 8); + IapProgramByte(IAP_ADDRESS + 2 * i + 1, bestScore[i] - (bestScore[i] >> 4) << 8); + } +} + +void readBest() +{ + unsigned int i; + for(i = 0; i++; i < 10) + { + bestScore[i] = (unsigned int)IapReadByte(IAP_ADDRESS + 2 * i) << 8 + (unsigned int)IapReadByte(IAP_ADDRESS + 2 * i + 1); } } \ No newline at end of file diff --git a/MCU/MCU_REAL _FINAL/block.h b/MCU/MCU_REAL _FINAL/block.h index 9331e68..7b3da10 100644 --- a/MCU/MCU_REAL _FINAL/block.h +++ b/MCU/MCU_REAL _FINAL/block.h @@ -19,4 +19,8 @@ void moveLeftPiece(); void moveRightPiece(); void judgeBlock(); void blockGameOver(); + +void saveBest(); +void readBest(); +unsigned int addBest(unsigned int s); #endif \ No newline at end of file diff --git a/MCU/MCU_REAL _FINAL/eeprom.c b/MCU/MCU_REAL _FINAL/eeprom.c index 76a6344..b1885f4 100644 --- a/MCU/MCU_REAL _FINAL/eeprom.c +++ b/MCU/MCU_REAL _FINAL/eeprom.c @@ -15,7 +15,6 @@ #include "eeprom.h" #include "delay.h" -unsigned int bestScore[10]={0}; /*---------------------------- Disable ISP/IAP/EEPROM function @@ -48,7 +47,6 @@ BYTE IapReadByte(WORD addr) _nop_(); //MCU will hold here until ISP/IAP/EEPROM operation complete dat = IAP_DATA; //Read ISP/IAP/EEPROM data IapIdle(); //Close ISP/IAP/EEPROM function - return dat; //Return Flash data } @@ -69,6 +67,7 @@ void IapProgramByte(WORD addr, BYTE dat) IAP_TRIG = 0xb9; //Send trigger command2 (0xb9) _nop_(); //MCU will hold here until ISP/IAP/EEPROM operation complete IapIdle(); + delayms(10); } /*---------------------------- @@ -86,43 +85,5 @@ void IapEraseSector(WORD addr) IAP_TRIG = 0xb9; //Send trigger command2 (0xb9) _nop_(); //MCU will hold here until ISP/IAP/EEPROM operation complete IapIdle(); + delayms(10); } - -void addBest(unsigned int s) -{ - int i,j; - for(i=0;i<10;i++) - if(bestScore[i]i;j--) - bestScore[j]=bestScore[j-1]; - - bestScore[i]=s; - saveBest(); - return; - } - -} - -void saveBest() -{ - char i; - IapEraseSector(IAP_ADDRESS); - delayms(20); - for(i=0;i++;i<10) - { - IapProgramByte(IAP_ADDRESS+2*i, bestScore[i]/256); - delayms(15); - IapProgramByte(IAP_ADDRESS+2*i+1, bestScore[i]%256); - delayms(15); - } -} - -void readBest() -{ - char i; - for(i=0;i++;i<10) - { - bestScore[i]=(unsigned int)IapReadByte(IAP_ADDRESS+2*i)*256 +(unsigned int)IapReadByte(IAP_ADDRESS+2*i+1); - } -} \ No newline at end of file diff --git a/MCU/MCU_REAL _FINAL/eeprom.h b/MCU/MCU_REAL _FINAL/eeprom.h index d2a7a1a..0545249 100644 --- a/MCU/MCU_REAL _FINAL/eeprom.h +++ b/MCU/MCU_REAL _FINAL/eeprom.h @@ -33,8 +33,4 @@ BYTE IapReadByte(WORD addr); void IapProgramByte(WORD addr, BYTE dat); void IapEraseSector(WORD addr); -void saveBest(); -void readBest(); -void addBest(unsigned int s); - #endif \ No newline at end of file diff --git a/MCU/MCU_REAL _FINAL/img.h b/MCU/MCU_REAL _FINAL/img.h index 53f5e92..e6ecb95 100644 --- a/MCU/MCU_REAL _FINAL/img.h +++ b/MCU/MCU_REAL _FINAL/img.h @@ -164,12 +164,13 @@ code unsigned char FONT_OVER1[]={ 0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20, }; //=================== 记录字库 ================== +//排行 code unsigned char FONT_BEST0[]={ -0x40,0x40,0xC0,0x5F,0x55,0x55,0xD5,0x55,0x55,0x55,0x55,0x5F,0x40,0x40,0x40,0x00, -0x04,0x04,0x04,0x04,0xF4,0x94,0x95,0x96,0x94,0x94,0xF4,0x04,0x04,0x04,0x04,0x00, +0x10,0x10,0x10,0xFF,0x90,0x08,0x88,0x88,0xFF,0x00,0x00,0xFF,0x88,0x88,0x08,0x00, +0x00,0x10,0x88,0xC4,0x33,0x00,0x40,0x42,0x42,0x42,0xC2,0x42,0x42,0x42,0x40,0x00, }; code unsigned char FONT_BEST1[]={ -0x20,0x60,0x3F,0x25,0x15,0x15,0xFF,0x90,0x47,0x29,0x11,0x2D,0x43,0x80,0x80,0x00, -0x00,0xFE,0x02,0x02,0x7A,0x4A,0x4A,0x4A,0x4A,0x4A,0x7A,0x02,0x82,0xFE,0x00,0x00, +0x02,0x42,0x81,0x7F,0x00,0x08,0x08,0x08,0xFF,0x00,0x00,0xFF,0x08,0x08,0x08,0x00, +0x02,0x01,0x00,0xFF,0x00,0x00,0x00,0x00,0x40,0x80,0x7F,0x00,0x00,0x00,0x00,0x00, }; #endif \ No newline at end of file diff --git a/MCU/MCU_REAL _FINAL/main.c b/MCU/MCU_REAL _FINAL/main.c index cc1e29f..7456cba 100644 --- a/MCU/MCU_REAL _FINAL/main.c +++ b/MCU/MCU_REAL _FINAL/main.c @@ -6,7 +6,7 @@ unsigned char TH, TL; extern Menu *NOW; //菜单指针 extern enum OPR opr; //按键状态 - +extern unsigned int bestScore[]; void int0() interrupt 0 //外部中断:检测左右旋钮 { @@ -46,6 +46,7 @@ void time() interrupt 1 // int main() { + TH = RELOAD / 256; //填充值预计算 TL = RELOAD - (RELOAD / 256) * 256; TH0 = TH; @@ -66,7 +67,29 @@ int main() displayInit(); //屏幕初始化 clear(); menuInit(); //菜单初始化 - readBest(); + + //读取排行榜 + bestScore[0] = ((unsigned int)IapReadByte(IAP_ADDRESS + 0)) * 256 + + (unsigned int)IapReadByte(IAP_ADDRESS + 1); + bestScore[1] = ((unsigned int)IapReadByte(IAP_ADDRESS + 2)) * 256 + + (unsigned int)IapReadByte(IAP_ADDRESS + 3); + bestScore[2] = ((unsigned int)IapReadByte(IAP_ADDRESS + 4)) * 256 + + (unsigned int)IapReadByte(IAP_ADDRESS + 5); + bestScore[3] = ((unsigned int)IapReadByte(IAP_ADDRESS + 6)) * 256 + + (unsigned int)IapReadByte(IAP_ADDRESS + 7); + bestScore[4] = ((unsigned int)IapReadByte(IAP_ADDRESS + 8)) * 256 + + (unsigned int)IapReadByte(IAP_ADDRESS + 9); + bestScore[5] = ((unsigned int)IapReadByte(IAP_ADDRESS + 10)) * 256 + + (unsigned int)IapReadByte(IAP_ADDRESS + 11); + bestScore[6] = ((unsigned int)IapReadByte(IAP_ADDRESS + 12)) * 256 + + (unsigned int)IapReadByte(IAP_ADDRESS + 13); + bestScore[7] = ((unsigned int)IapReadByte(IAP_ADDRESS + 14)) * 256 + + (unsigned int)IapReadByte(IAP_ADDRESS + 15); + bestScore[8] = ((unsigned int)IapReadByte(IAP_ADDRESS + 16)) * 256 + + (unsigned int)IapReadByte(IAP_ADDRESS + 17); + bestScore[9] = ((unsigned int)IapReadByte(IAP_ADDRESS + 18)) * 256 + + (unsigned int)IapReadByte(IAP_ADDRESS + 19); + while(1) { diff --git a/MCU/MCU_REAL _FINAL/menu.c b/MCU/MCU_REAL _FINAL/menu.c index 38cde5e..fc53a52 100644 --- a/MCU/MCU_REAL _FINAL/menu.c +++ b/MCU/MCU_REAL _FINAL/menu.c @@ -24,7 +24,7 @@ Menu* LAST = NULL; // enum OPR opr = idle; static unsigned char local[2] = {0}; unsigned int score; -extern unsigned int bestScore[10]; +unsigned int bestScore[10]; extern enum GS blockGameStatus; @@ -170,7 +170,7 @@ void m_best(struct _menu* this) if(LAST != &M_BEST) { LAST = &M_BEST; - drawBEST(2, 0); + drawBEST(2, 0); //绘制字 drawSUPNUM(2, 2, bestScore[0] / 10000); //绘制分数 drawSUPNUM(11, 2, (bestScore[0] % 10000) / 1000); drawSUPNUM(20, 2, (bestScore[0] % 1000) / 100);