feat(MCU课设): 修复一个bug的菜单及存在一堆bug的俄罗斯方块

This commit is contained in:
iridiumR 2022-06-05 23:35:53 +08:00
parent 5ef12a7ecc
commit 5618c61ce4
10 changed files with 945 additions and 891 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 220 B

Binary file not shown.

File diff suppressed because it is too large Load diff

View file

@ -1,89 +1,109 @@
#include "block.h" #include "block.h"
#include "draw.h"
Group *base; Group piece;
Group *piece; unsigned char regenPieceFlag = 0;
unsigned char base[8][11]={0}; //x*y
void blockInit() void blockInit()
{ {
base=(Group*)calloc(1,sizeof(Group));
groupInit(*base,64); base[0][0]=1;
base[0][1]=1;
base[0][2]=1;
// genPiece();
}
void blockDestroy()
{
piece.type=none;
} }
void genPiece() void genPiece()
{ {
unsigned char i=2+rand()%3; //需要一点随机性
piece=(Group*)calloc(1,sizeof(Group)); int random = rand() % 4;
groupInit(*piece,i); int len = 2 + rand() % 3;
piece.mother.x=4;
piece.mother.y=8;
//当两个方块的情况
if(len == 2)
{
if(random % 2 == 0)
{
piece.type = two_v; //
} //
else
{
piece.type = two_h; ////
}
}
//当三个方块的情况
if(len == 3)
{
if(random == 0)
{
//
piece.type = three_h; //
//
}
else if(random == 1)
{
piece.type = three_v; //////
}
else if(random == 2)
{
//
piece.type = three_l; ////
}
else if (random == 3)
{
piece.type = three_r; //
} ////
}
//当四个方块的情况
if(len == 3)
{
piece.type = four; ////
} ////
} }
void genShape(Group g) void dropPiece()
{ {
//需要一点随机性 piece.mother.y--;
int random=rand()%4; // int i;
// for(i = 0; i < len; i++)
//当两个方块的情况 // {
if(g.len==2) // //触底或下有base方块
{ // if(piece.list[i].x == 0 || base[piece.list[i].x][piece.list[i].y - 1] == 1)
if(random%2==0) // {
{ // for(i = 0; i < len;i++)
g.list[0].x=4; // // {
g.list[0].y=8; // // //全部方块转移到base
g.list[1].x=4; // base[piece.list[i].x][piece.list[i].y] == 1;
g.list[1].y=9; // }
} // //重新生成
else // genPiece();
{ // return;
g.list[0].x=4; //// // }
g.list[0].y=8; // else
g.list[1].x=5; // //正常下落
g.list[1].y=8; // piece.list[i].y--;
} // }
} }
//当三个方块的情况
if(g.len==3)
{ void drawBlock()
if(random%2==0) {
{ int i,j;
g.list[0].x=4; // for(i=0;i<8;i++)
g.list[0].y=8; // for(j=0;j<8;j++)
g.list[1].x=4; // if(base[i][j]==1)
g.list[1].y=9; placeIMG_BLOCK(i,j);
g.list[2].x=4; placeIMG_BLOCK(piece.mother.x,piece.mother.y);
g.list[2].y=10;
}
else
{
g.list[0].x=4; //////
g.list[0].y=8;
g.list[1].x=5;
g.list[1].y=8;
g.list[2].x=4;
g.list[2].y=9;
}
}
//当四个方块的情况
if(g.len==3)
{
if(random==0)
{
g.list[0].x=4; //
g.list[0].y=8; //
g.list[1].x=4; ////
g.list[1].y=9;
g.list[2].x=4;
g.list[2].y=10;
g.list[3].x=5;
g.list[3].y=8;
}
else
{
g.list[0].x=4;
g.list[0].y=8;
g.list[1].x=5;
g.list[1].y=8;
g.list[2].x=4;
g.list[2].y=9;
}
}
} }

View file

@ -1,11 +1,11 @@
#ifndef _BLOCK_H_ #ifndef _BLOCK_H_
#define _BLOCK_H_ #define _BLOCK_H_
#include <stdlib.h> #include <stdlib.h>
# include <string.h>
#define MAX_X 8 #define MAX_X 8
#define MAX_Y 8 #define MAX_Y 8
enum BLK_TP {none,two_h,two_v,three_l,three_r,three_h,three_v,four};
//点 结构体 //点 结构体
struct _dot struct _dot
{ {
@ -17,20 +17,13 @@ typedef struct _dot Dot;
//块 结构体 //块 结构体
struct _group struct _group
{ {
unsigned char used;//拥有的点数量 enum BLK_TP type; //ÀàÐÍ
unsigned char len; //数组申请内存长度 Dot mother;
Dot* list;
}; };
typedef struct _group Group; typedef struct _group Group;
void groupInit(Group g,unsigned char i) void genPiece();
{ void blockInit();
retry: void dropPiece();
g.len=i; void drawBlock();
g.used=0;
g.list=(Dot*)malloc(sizeof(Dot)*g.len);
if(!g.list)
goto retry;
}
#endif #endif

View file

@ -2,6 +2,7 @@
#include "display.h" #include "display.h"
#include "draw.h" #include "draw.h"
#include <reg52.h> #include <reg52.h>
#include "block.h"
sbit RS = P2 ^ 0; sbit RS = P2 ^ 0;
sbit RW = P2 ^ 1; sbit RW = P2 ^ 1;
@ -9,13 +10,15 @@ sbit E = P2 ^ 2;
sbit CSA = P2 ^ 4; sbit CSA = P2 ^ 4;
sbit CSB = P2 ^ 5; sbit CSB = P2 ^ 5;
extern Group piece;
void drawNUMBER() void drawNUMBER()
{ {
unsigned int j; unsigned int j;
for(j = 0 ; j < 64; j++) for(j = 0 ; j < 64; j++)
{ {
CSA = 1; CSA = 1;
CSB = 0; CSB = 0;
RS = 0; RS = 0;
@ -27,7 +30,7 @@ void drawNUMBER()
RS = 1; RS = 1;
RW = 0; RW = 0;
P1 = NUMBER_PAGE0[j - 1]; P1 = NUMBER_PAGE0[j];
write(); write();
RS = 0; RS = 0;
@ -39,10 +42,9 @@ void drawNUMBER()
RS = 1; RS = 1;
RW = 0; RW = 0;
P1 = NUMBER_PAGE1[j - 1]; P1 = NUMBER_PAGE1[j];
write(); write();
} }
for(j = 0 ; j < 16; j++) for(j = 0 ; j < 16; j++)
{ {
@ -59,7 +61,7 @@ void drawNUMBER()
RS = 1; RS = 1;
RW = 0; RW = 0;
P1 = NUMBER_RPAGE0[j - 1]; P1 = NUMBER_RPAGE0[j];
write(); write();
RS = 0; RS = 0;
@ -71,7 +73,7 @@ void drawNUMBER()
RS = 1; RS = 1;
RW = 0; RW = 0;
P1 = NUMBER_RPAGE1[j - 1]; P1 = NUMBER_RPAGE1[j];
write(); write();
@ -95,7 +97,7 @@ void drawNAME()
RS = 1; RS = 1;
RW = 0; RW = 0;
P1 = NAME_PAGE0[j - 1]; P1 = NAME_PAGE0[j];
write(); write();
RS = 0; RS = 0;
@ -107,7 +109,7 @@ void drawNAME()
RS = 1; RS = 1;
RW = 0; RW = 0;
P1 = NAME_PAGE1[j - 1]; P1 = NAME_PAGE1[j];
write(); write();
} }
} }
@ -129,7 +131,7 @@ void drawMAINMENU(unsigned char i)
RS = 1; RS = 1;
RW = 0; RW = 0;
P1 = MAINMANU_PAGE0[j - 1]; P1 = MAINMANU_PAGE0[j];
write(); write();
RS = 0; RS = 0;
@ -141,7 +143,7 @@ void drawMAINMENU(unsigned char i)
RS = 1; RS = 1;
RW = 0; RW = 0;
P1 = MAINMANU_PAGE1[j - 1]; P1 = MAINMANU_PAGE1[j];
write(); write();
CSA = 1; CSA = 1;
@ -158,9 +160,9 @@ void drawMAINMENU(unsigned char i)
RS = 1; RS = 1;
RW = 0; RW = 0;
if(i==1) if(i==1)
P1 = ~MAINMANU_PAGE2[j - 1]; P1 = ~MAINMANU_PAGE2[j];
else else
P1 = MAINMANU_PAGE2[j - 1]; P1 = MAINMANU_PAGE2[j];
write(); write();
RS = 0; RS = 0;
@ -173,9 +175,9 @@ void drawMAINMENU(unsigned char i)
RS = 1; RS = 1;
RW = 0; RW = 0;
if(i==1) if(i==1)
P1 = ~MAINMANU_PAGE3[j - 1]; P1 = ~MAINMANU_PAGE3[j];
else else
P1 = MAINMANU_PAGE3[j - 1]; P1 = MAINMANU_PAGE3[j];
write(); write();
RS = 0; RS = 0;
@ -188,9 +190,9 @@ void drawMAINMENU(unsigned char i)
RS = 1; RS = 1;
RW = 0; RW = 0;
if(i==2) if(i==2)
P1 = ~MAINMANU_PAGE4[j - 1]; P1 = ~MAINMANU_PAGE4[j];
else else
P1 = MAINMANU_PAGE4[j - 1]; P1 = MAINMANU_PAGE4[j];
write(); write();
RS = 0; RS = 0;
@ -203,9 +205,9 @@ void drawMAINMENU(unsigned char i)
RS = 1; RS = 1;
RW = 0; RW = 0;
if(i==2) if(i==2)
P1 = ~MAINMANU_PAGE5[j - 1]; P1 = ~MAINMANU_PAGE5[j];
else else
P1 = MAINMANU_PAGE5[j - 1]; P1 = MAINMANU_PAGE5[j];
write(); write();
RS = 0; RS = 0;
@ -218,9 +220,9 @@ void drawMAINMENU(unsigned char i)
RS = 1; RS = 1;
RW = 0; RW = 0;
if(i==3) if(i==3)
P1 = ~MAINMANU_PAGE6[j - 1]; P1 = ~MAINMANU_PAGE6[j];
else else
P1 = MAINMANU_PAGE6[j - 1]; P1 = MAINMANU_PAGE6[j];
write(); write();
RS = 0; RS = 0;
@ -233,10 +235,32 @@ void drawMAINMENU(unsigned char i)
RS = 1; RS = 1;
RW = 0; RW = 0;
if(i==3) if(i==3)
P1 = ~MAINMANU_PAGE7[j - 1]; P1 = ~MAINMANU_PAGE7[j];
else else
P1 = MAINMANU_PAGE7[j - 1]; P1 = MAINMANU_PAGE7[j];
write(); write();
} }
} }
void placeIMG_BLOCK(unsigned char x,unsigned char y)
{
unsigned int j;
for(j = 0 ; j < 8; j++)
{
CSA = 1;
CSB = 0;
RS = 0;
RW = 0;
P1 = 0xb8 + 7 - y;
write();
P1 = 0X40 + x*8 + j;
write(); //Ò³7-yÁÐx*8+jÆÁA
RS = 1;
RW = 0;
P1 = IMG_BLOCK[j];
write();
}
}

View file

@ -1,8 +1,12 @@
#ifndef _DRAW_H_ #ifndef _DRAW_H_
#define _DRAW_H_ #define _DRAW_H_
////=================== 菜单 ==================
void drawNAME(); void drawNAME();
void drawNUMBER(); void drawNUMBER();
void drawMAINMENU(unsigned char); void drawMAINMENU(unsigned char);
void drawBLOCK();
////=================== 方块 ==================
void placeIMG_BLOCK(unsigned char x,unsigned char y);
#endif #endif

View file

@ -87,4 +87,12 @@ code unsigned char MAINMANU_PAGE7[]={
0x81,0x81,0x41,0x41,0x21,0x11,0x0D,0x03,0x0D,0x11,0x21,0x41,0x41,0x81,0x81,0x00, 0x81,0x81,0x41,0x41,0x21,0x11,0x0D,0x03,0x0D,0x11,0x21,0x41,0x41,0x81,0x81,0x00,
0x00,0x00,0x00,0x00,0x00,0x40,0x80,0x7F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x40,0x80,0x7F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
}; };
//=================== 方块 ==================
code unsigned char IMG_BLOCK[]={
/*-- 宽度x高度=8x8 --*/
0x81,0x7E,0x7E,0x7E,0x7A,0x72,0x7E,0x81,
};
#endif #endif

View file

@ -86,5 +86,6 @@ int main()
{ {
NOW->f(NOW); NOW->f(NOW);
} }
} }

View file

@ -4,6 +4,7 @@
#include "draw.h" #include "draw.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "block.h"
Menu M_STARTUP; Menu M_STARTUP;
Menu M_MAINMENU;//主菜单 Menu M_MAINMENU;//主菜单
@ -74,7 +75,7 @@ void m_mainmenu(struct _menu* this)
switch(local[0]) switch(local[0])
{ {
case 0: case 0:
break; return;
case 1: case 1:
NOW = &M_BLOCK; //转向下一菜单 NOW = &M_BLOCK; //转向下一菜单
break; break;
@ -96,11 +97,18 @@ void m_mainmenu(struct _menu* this)
//方块游戏 //方块游戏
void m_block(struct _menu* this) void m_block(struct _menu* this)
{ {
if(LAST != &M_MAINMENU) if(LAST != &M_BLOCK)
{ {
LAST = &M_MAINMENU; LAST = &M_BLOCK;
drawBLOCK(); blockInit();
} }
while(1)
{
dropPiece();
drawBlock();
delay(200);
}
} }
void menuInit() void menuInit()
{ {
@ -108,5 +116,8 @@ void menuInit()
M_STARTUP.f = m_startup; M_STARTUP.f = m_startup;
M_MAINMENU.f = m_mainmenu; M_MAINMENU.f = m_mainmenu;
M_BLOCK.f = m_block;
NOW = &M_STARTUP; NOW = &M_STARTUP;
} }